minor update to clean up code. tested on dell xps 9570, seems to be working correctly.

This commit is contained in:
Josh Ashton
2023-11-13 14:35:17 -07:00
parent 6c3048ce49
commit 591e147f44
+29 -9
View File
@@ -62,26 +62,46 @@ void recordData() {
fclose(fp); fclose(fp);
} }
int main(int argc, char * argv[]) { void daemonize() {
// Check if the argument is --daemonize
if (argc > 1) {
if ((strcmp(argv[1], "--daemonize") == 0) || (strcmp(argv[1], "-d") == 0)) {
printf("Daemonizing...\n"); printf("Daemonizing...\n");
// Daemonize the process // Daemonize the process
pid_t pid, sid; pid_t pid, sid;
pid = fork(); pid = fork();
if (pid < 0) exit(EXIT_FAILURE);
if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) {
printf("daemonizing failed! pid: %d\n", pid);
exit(EXIT_FAILURE);
}
// This is not needed.
if (pid > 0) {
printf("daemonizing succeeded! pid: %d\n", pid);
exit(EXIT_SUCCESS);
}
sid = setsid(); sid = setsid();
if (sid < 0) exit(EXIT_FAILURE); if (sid < 0) {
if ((chdir("/")) < 0) exit(EXIT_FAILURE); printf("daemonizing failed! sid: %d\n", sid);
exit(EXIT_FAILURE);
} }
if ((chdir("/")) < 0) {
printf("daemonizing failed! dir not changed\n");
exit(EXIT_FAILURE);
}
}
int main(int argc, char * argv[]) {
// Check if the argument is --daemonize
if (argc > 1) {
if ((strcmp(argv[1], "--daemonize") == 0) || (strcmp(argv[1], "-d") == 0))
daemonize();
} else { } else {
printf("Usage: %s [--daemonize|-d]\n", argv[0]); printf("Usage: %s [--daemonize|-d]\n", argv[0]);
printf("recording data once.\n");
recordData();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }