taskrambler  0.1.8
Web server and task management solution.
daemonize.c File Reference
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
+ Include dependency graph for daemonize.c:

Go to the source code of this file.

Macros

#define WORKDIR   "/"
 
#define UMASK   0
 

Functions

void daemonize (void)
 

Detailed Description

Disconnect from parent process and let all standard file handles point to /dev/null.

Author
Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file daemonize.c.

Macro Definition Documentation

#define UMASK   0

Definition at line 36 of file daemonize.c.

Referenced by daemonize().

#define WORKDIR   "/"

Definition at line 35 of file daemonize.c.

Function Documentation

void daemonize ( void  )

Definition at line 38 of file daemonize.c.

References UMASK.

Referenced by main().

38  {
39  pid_t pid;
40 
41  if (0 > ((pid = fork()))) {
42  perror("deamoinze[fork]");
43  exit(EXIT_FAILURE);
44  } else if (0 != pid) {
45  exit(EXIT_SUCCESS);
46  }
47 
48  // make new child session leader
49  setsid();
50 
51  if (0 > ((pid = fork()))) {
52  perror("deamoinze[fork]");
53  exit(EXIT_FAILURE);
54  } else if (0 != pid) {
55  exit(EXIT_SUCCESS);
56  }
57 
58  // set umask and change to working directory to /
59  umask(UMASK);
60  if (-1 == chdir("/")) {
61  perror("daemonize");
62  exit(EXIT_FAILURE);
63  }
64 
65  // we should close all open filedescriptors now.
66  // But I assume that this function is called at the very start of the
67  // program and no more filedescriptors are open than the standard
68  // ones.
69 
70  // connect all standard streams to /dev/null
71  stderr = freopen("/dev/null", "w", stderr);
72  stdin = freopen("/dev/null", "r", stdin);
73  stdout = freopen("/dev/null", "w", stdout);
74 }
#define UMASK
Definition: daemonize.c:36

+ Here is the caller graph for this function: