Skip to content

Instantly share code, notes, and snippets.

@myzhan
Created December 8, 2018 12:47
Show Gist options
  • Save myzhan/ab4f6b54af01b659fbbe346361bbf4e0 to your computer and use it in GitHub Desktop.
Save myzhan/ab4f6b54af01b659fbbe346361bbf4e0 to your computer and use it in GitHub Desktop.
Linux Daemon Writing HOWTO
#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