diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index 6447a0d9606d6884f4494472b8c57bf8e681d4b9..5037e6d0cc64a3e7fc6d9d1236372a471e88c2cc 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -65,7 +65,6 @@ static void daemonize(const char *cwd, const char *lock_file) { pid_t pid, sid; FILE* lock_file_f; char process_pid[20]; - /* already a daemon */ if ( getppid() == 1 ) return; @@ -76,11 +75,23 @@ static void daemonize(const char *cwd, const char *lock_file) { } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { + if (lock_file) { + /* write our pid into it & close the file. */ + lock_file_f = fopen(lock_file, "w+"); + if (lock_file_f == NULL) { + std::cerr << "Cannot create lock file " << lock_file << ". Exiting\n"; + exit(1); + } + sprintf(process_pid,"%d\n",pid); + if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) { + std::cerr << "Cannot write to lock file " << lock_file << ". Exiting\n"; + exit(1); + } + fclose(lock_file_f); + } exit(0); } - /* At this point we are executing as the child process */ - /* Change the file mode mask */ umask(0); @@ -95,21 +106,6 @@ static void daemonize(const char *cwd, const char *lock_file) { if ((chdir(cwd)) < 0) { exit(1); } - - if (lock_file) { - /* write our pid into it & close the file. */ - lock_file_f = fopen(lock_file, "w+"); - if (lock_file_f == NULL) { - std::cout << "EE cannot create lock file " << lock_file << ". Exiting\n"; - exit(1); - } - sprintf(process_pid,"%d\n",getpid()); - if (fwrite(process_pid,1,strlen(process_pid),lock_file_f) < strlen(process_pid)) { - std::cout << "EE cannot write to lock file " << lock_file << ". Exiting\n"; - exit(1); - } - fclose(lock_file_f); - } if (freopen( "/dev/null", "r", stdin) == NULL) { std::cout << "EE cannot open /dev/null. Exiting\n";