Created
December 8, 2018 12:47
-
-
Save myzhan/ab4f6b54af01b659fbbe346361bbf4e0 to your computer and use it in GitHub Desktop.
Linux Daemon Writing HOWTO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <syslog.h> | |
#include <string.h> | |
int main(void) { | |
pid_t pid, sid; | |
pid = fork(); | |
if (pid < 0) { | |
exit(EXIT_FAILURE); | |
} | |
if (pid > 0) { | |
exit(EXIT_SUCCESS); | |
} | |
umask(0); | |
sid = setsid(); | |
if (sid < 0) { | |
exit(EXIT_FAILURE); | |
} | |
if ((chdir("/")) < 0) { | |
exit(EXIT_FAILURE); | |
} | |
close(STDIN_FILENO); | |
close(STDOUT_FILENO); | |
close(STDERR_FILENO); | |
while (1) { | |
sleep(30); | |
} | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment