Improved readability of daemon.c

This commit is contained in:
Patrick Siegl
2019-03-21 22:14:15 +01:00
parent ad6c6b3dcb
commit a19456d221

View File

@@ -8,22 +8,20 @@
#include "infnoise.h" #include "infnoise.h"
// Write af PID in a file // Write PID in a file
static bool writePid(int32_t pid, char *fileName) { static bool writePid(int32_t pid, char *fileName) {
FILE *pidFile; FILE *pidFile = fopen(fileName, "w");
int ret;
pidFile = fopen(fileName,"w");
if(pidFile == NULL) { if(pidFile == NULL) {
return false; return false;
} }
ret = fprintf(pidFile, "%d\n", pid); int ret = fprintf(pidFile, "%d\n", pid);
fclose(pidFile); fclose(pidFile);
return (ret >= 0); return (ret >= 0);
} }
void startDaemon(struct opt_struct* opts) { void startDaemon(struct opt_struct* opts) {
if(!opts->daemon) { if(!opts->daemon) {
// No backgrounding, optionslly write current PID // No backgrounding, optionally write current PID
if(opts->pidFileName != NULL) { if(opts->pidFileName != NULL) {
writePid(getpid(), opts->pidFileName); writePid(getpid(), opts->pidFileName);
} }
@@ -33,14 +31,11 @@ void startDaemon(struct opt_struct* opts) {
if(pid < 0) { if(pid < 0) {
fputs("fork() failed\n", stderr); fputs("fork() failed\n", stderr);
exit(1); exit(1);
} else if(pid > 0) { }
if(pid > 0) {
// Parent // Parent
if(opts->pidFileName != NULL) { exit(opts->pidFileName != NULL
if(!writePid(pid, opts->pidFileName)) { && !writePid(pid, opts->pidFileName));
exit(1);
}
}
exit(0);
} }
// Child // Child
} }