taskrambler
v0.1.8
Web server and task management solution.
Main Page
Related Pages
Data Structures
Files
File List
Globals
daemonize.c
Go to the documentation of this file.
1
/**
2
* \file
3
* Disconnect from parent process and let all standard file handles
4
* point to /dev/null.
5
*
6
* \author Georg Hopp
7
*
8
* \copyright
9
* Copyright © 2012 Georg Hopp
10
*
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
20
*
21
* You should have received a copy of the GNU General Public License
22
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
*/
24
25
#include <stdio.h>
// for printf() and fprintf()
26
#include <unistd.h>
// for getopt
27
#include <stdlib.h>
28
#include <unistd.h>
29
#include <errno.h>
30
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
34
35
#define WORKDIR "/"
36
#define UMASK 0
37
38
void
daemonize
(
void
) {
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
}
75
76
// vim: set ts=4 sw=4:
daemonize
void daemonize(void)
Definition:
daemonize.c:38
UMASK
#define UMASK
Definition:
daemonize.c:36
src
utils
daemonize.c
Generated on Wed Apr 13 2016 12:25:16 for taskrambler by
1.8.10