39 if (! pidfile.empty ())
42 std::ifstream pidrifs;
43 pidrifs.open(pidfile, std::fstream::in);
47 if (pidrifs >> oldpid && oldpid > 1 && kill(oldpid, 0) == 0)
49 quit(
"PID file " + pidfile +
" already exists and the PID therein is valid");
54 pidofs.open(pidfile, std::fstream::out | std::fstream::trunc);
57 quit(
"Failed to open specified PID file for writing");
63 if (pid_t pid = ::
fork())
76 quit(
"First fork failed");
85 if (pid_t pid = ::
fork())
94 quit(
"Second fork failed");
100 int pid = ::getpid();
101 pidofs << pid << std::endl;
112 if (open(
"/dev/null", O_RDONLY) < 0)
114 quit(
"Unable to open /dev/null");
117 #ifdef DEBUG_TMPDIR_LOG 119 const char *tmpdir = getenv(
"TMPDIR");
123 output +=
"/electroneum.daemon.stdout.stderr";
124 const int flags = O_WRONLY | O_CREAT | O_APPEND;
125 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
126 if (open(output.c_str(), flags, mode) < 0)
128 quit(
"Unable to open output file: " + output);
134 quit(
"Unable to dup output descriptor");
void fork(const std::string &pidfile)