"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/daemon.c" (13 Jun 2004, 1832 Bytes) of package /linux/misc/old/pidentd-3.0.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "daemon.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** daemon.c - Become a Unix daemon, and support functions.
    3 **
    4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 
   15 #include "config.h"
   16 
   17 #include <stdio.h>
   18 #include <stdlib.h>
   19 
   20 #ifdef HAVE_UNISTD_H
   21 #include <unistd.h>
   22 #endif
   23 
   24 #include <syslog.h>
   25 #include <string.h>
   26 #include <fcntl.h>
   27 #include <sys/types.h>
   28 #include <sys/stat.h>
   29 #include <signal.h>
   30 
   31 #include "pidentd.h"
   32 
   33 
   34 /*
   35 ** Fork and disassociate ourself from any controlling tty.
   36 */
   37 void
   38 become_daemon(void)
   39 {
   40     pid_t pid;
   41     int i, fd;
   42 
   43 
   44     pid = fork();
   45     if (pid < 0)
   46     {
   47     syslog(LOG_ERR, "fork() failed: %m");
   48     exit(EXIT_FAILURE);
   49     }
   50     else if (pid > 0)
   51     exit(EXIT_SUCCESS);
   52 
   53     signal(SIGTTOU, SIG_IGN);
   54     signal(SIGTTIN, SIG_IGN);
   55 #ifdef SIGTTSP
   56     signal(SIGTTSP, SIG_IGN);
   57 #endif
   58 
   59 #ifdef HAVE_SETSID
   60     setsid();
   61 #endif
   62 
   63     chdir("/");
   64     umask(0);
   65     
   66     for (i = 0; i < 3; i++)
   67     {
   68     close(i);
   69 
   70     fd = s_open("/dev/null", O_RDWR);
   71     if (fd != i)
   72     {
   73         dup2(fd, i);
   74         close(fd);
   75     }
   76     }
   77 }
   78 
   79 
   80 /*
   81 ** Create a (or truncate an existing) file containing
   82 ** our process id number.
   83 */
   84 void
   85 pidfile_create(const char *path)
   86 {
   87     int fd;
   88     char buf[64];
   89 
   90     
   91     fd = s_open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
   92     if (fd < 0)
   93     {
   94     syslog(LOG_ERR, "s_open(\"%s\", O_WRONLY): %m", path);
   95     return;
   96     }
   97 
   98     s_snprintf(buf, sizeof(buf), "%ld\n", (long) getpid());
   99     if (s_write(fd, buf, strlen(buf)) < 0)
  100     syslog(LOG_ERR, "s_write(fd, ..., %d): %m", fd, strlen(buf));
  101     
  102     s_close(fd);
  103 }
  104