Skip to content

Instantly share code, notes, and snippets.

@taeguk
Created June 22, 2017 19:30
Show Gist options
  • Save taeguk/26c98d7e6588e16a0101474b02f7ec2b to your computer and use it in GitHub Desktop.
Save taeguk/26c98d7e6588e16a0101474b02f7ec2b to your computer and use it in GitHub Desktop.
haha4.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
static void start_skeleton_daemon()
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/");
/* Close all open file descriptors */
int x;
for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
{
close (x);
}
/* Open the log file */
openlog ("puzzle_daemon", LOG_PID, LOG_DAEMON);
}
// TODO: write real codes. :)
int daemon_main()
{
while (1)
{
syslog (LOG_NOTICE, "Puzzle daemon started.");
usleep(2*1000*1000);
}
return EXIT_SUCCESS;
}
int main()
{
int ret;
start_skeleton_daemon();
syslog (LOG_NOTICE, "Puzzle daemon started.");
ret = daemon_main();
syslog (LOG_NOTICE, "Puzzle daemon terminated.");
closelog();
return ret;
}
///////////////////////////////
CC = arm-none-linux-gnueabi-gcc
OUT = puzzle_daemon
SRC = $(OUT).c
all: $(OUT)
$(OUT): $(SRC)
$(CC) -static $(SRC) -o $(OUT)
clear:
rm -rf $(OUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment