Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/csdaemon.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
/*
23
* Glenn Fowler
24
* AT&T Research
25
*
26
* slip into the background
27
* (1<<fd) bits not in fds will be modified:
28
* 0-2 dup'd to /dev/null
29
* 3-sysconf(_SC_OPEN_MAX) closed
30
*
31
* daemon details thanks to
32
* "UNIX Network Programming, W. Richard Stevens, Prentice Hall, 1990"
33
*/
34
35
#include "cslib.h"
36
37
#include <sig.h>
38
39
int
40
csdaemon(register Cs_t* state, int fds)
41
{
42
register int fd;
43
register int i;
44
int oerrno;
45
46
messagef((state->id, NiL, -8, "daemon(%o) call", fds));
47
oerrno = errno;
48
49
/*
50
* unconditionally ignore some signals
51
*/
52
53
signal(SIGHUP, SIG_IGN);
54
#ifdef SIGTTOU
55
signal(SIGTTOU, SIG_IGN);
56
#endif
57
#ifdef SIGTTIN
58
signal(SIGTTIN, SIG_IGN);
59
#endif
60
#ifdef SIGTSTP
61
signal(SIGTSTP, SIG_IGN);
62
#endif
63
64
/*
65
* generate some children if not scheduled from init
66
*/
67
68
if (getppid() > 1)
69
{
70
#if _lib_fork
71
if (!(state->flags & CS_DAEMON_SLAVE)) switch (fork())
72
{
73
case -1:
74
return -1;
75
case 0:
76
systrace(NiL);
77
break;
78
default:
79
/*
80
* allow some time for service setup
81
*/
82
83
sleep(2);
84
exit(0);
85
}
86
#endif
87
88
/*
89
* become process group leader and drop control tty
90
*/
91
92
setsid();
93
}
94
95
/*
96
* redirect {0,1,2} to /dev/null if not in fds
97
*/
98
99
if ((fds & ((1<<0)|(1<<1)|(1<<2))) != ((1<<0)|(1<<1)|(1<<2)) && (fd = open("/dev/null", O_RDWR)) >= 0)
100
{
101
fds |= (1<<fd);
102
for (i = 0; i <= 2; i++)
103
if (!(fds & (1<<i)))
104
{
105
close(i);
106
dup(fd);
107
}
108
close(fd);
109
}
110
111
/*
112
* close any other garbage fds
113
*/
114
115
for (i = 3; i < 20; i++)
116
if (!(fds & (1<<i)) && !fcntl(i, F_GETFD, 0))
117
close(i);
118
119
/*
120
* avoid unexpected permission problems
121
*/
122
123
umask(0);
124
125
/*
126
* no command-relative relative root searching
127
*/
128
129
pathpath(NiL, "", 0, NiL, 0);
130
131
/*
132
* we ignored a lot of errors to get here
133
*/
134
135
errno = oerrno;
136
messagef((state->id, NiL, -8, "daemon(%o) = 0", fds));
137
return 0;
138
}
139
140
int
141
_cs_daemon(int fds)
142
{
143
return csdaemon(&cs, fds);
144
}
145
146