12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /*
- * mycodaemon.c
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <syslog.h>
- #include "../src/myco-daemon.c"
- static void daemonize()
- {
- 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 ("firstdaemon", LOG_PID, LOG_DAEMON);
- }
- int main() {
- daemonize();
- syslog(LOG_NOTICE, "mycodaemon started.");
- if (myco_daemon_start(getpid(), "127.0.0.1", "127.0.0.1") == -1) {
- return EXIT_FAILURE;
- }
- syslog (LOG_NOTICE, "mycodaemon terminated.");
- closelog();
- return EXIT_SUCCESS;
- }
|