2 ** Copyright 2000-2007 Double Precision, Inc. See COPYING for
3 ** distribution information.
19 #include <sys/types.h>
20 #include "../numlib/numlib.h"
25 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
28 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
31 #include <sys/ioctl.h>
34 #define exit(_a_) _exit(_a_)
37 static int start1(const char *, int);
39 #define CONSOLE "/dev/null"
41 int ll_daemon_start(const char *lockfile
)
49 ** Close any open file descriptors.
52 for (i
=3; i
< 256; i
++)
56 ** We fork, and set up a pipe from the child process. If we read
57 ** a single 0 byte from the pipe, it means that the child has
58 ** successfully initialized, and will return to main, so we exit(0).
59 ** If we do not read a single 0 byte from the pipe, it means that
60 ** there was an initialization error, so we return -1 to main.
82 ** We fork once more, so that the daemon process will not
83 ** be the child process of anyone.
96 ** Continue initialization in start1()
98 return (start1(lockfile
, pipefd
[1]));
102 if (read(pipefd
[0], &c
, 1) <= 0)
106 exit (0); /* Successful start of daemon */
111 static int start1(const char *lockfile
, int fd
)
125 int fd
=open("/dev/tty", O_RDWR
);
129 ioctl(fd
, TIOCNOTTY
, 0);
136 /* Attempt to obtain a lock */
138 lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600);
142 /* Perhaps an upgraded daemon runs under new uid? */
145 lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600);
148 #if HAVE_GETDTABLESIZE
149 maxfd
=getdtablesize()-1;
150 #elif defined(OPEN_MAX)
152 #elif HAVE_SYSCONF && defined(_SC_OPEN_MAX)
153 if ((maxfd
=sysconf(_SC_OPEN_MAX
)) < 0)
161 if (lockfd
< 0 || dup2(lockfd
, maxfd
) != maxfd
)
171 if (fcntl(lockfd
, F_SETFD
, FD_CLOEXEC
) < 0)
179 if (ll_lock_ex_test(lockfd
))
181 if (write(fd
, "", 1) != 1)
182 exit(1); /* Shouldn't happen */
185 exit (0); /* Already running, pretend success */
189 ** Return >0 to main, so it can continue main's setup.
195 int ll_daemon_resetio()
200 if (open("/dev/null", O_RDONLY
) != 0)
204 i
=open(CONSOLE
, O_WRONLY
);
205 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
206 if (i
!= 1) return (-1);
209 i
=open(CONSOLE
, O_WRONLY
);
210 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
211 if (i
!= 2) return (-1);
215 void ll_daemon_started(const char *pidfile
, int fd
)
217 char buf
[NUMBUFSIZE
+1];
218 char *p
=strcat(libmail_str_pid_t(getpid(), buf
), "\n");
222 if ((fp
=fopen(pidfile
, "w")) == NULL
||
223 fprintf(fp
, "%s", p
) < 0 || fflush(fp
) < 0 || fclose(fp
))
229 if (write(fd
, "", 1) != 1) /* Signal waiting parent */
230 exit(1); /* Shouldn't happen */
234 static void stop1(const char *, const char *);
236 int ll_daemon_stop(const char *lockfile
, const char *pidfile
)
242 ** We fork, and the child process attempts to stop the daemon,
243 ** then communicates the success to us, via its exit code.
246 signal(SIGCHLD
, SIG_DFL
);
247 if ((p
=fork()) == -1)
252 if (p
== 0) stop1(lockfile
, pidfile
);
254 while ((p2
=wait(&waitstat
)) != p
)
257 if (WIFEXITED(waitstat
))
258 return (WEXITSTATUS(waitstat
));
263 ** The child process forks too. The parent process goes in a loop,
264 ** trying to kill the daemon process.
266 ** The child process attempts to lock the lock file. When it
267 ** succeeds, it exits. When the child process exits, the parent
268 ** process kills itself.
271 static RETSIGTYPE
sigexit(int signum
)
273 kill(getpid(), SIGKILL
);
274 #if RETSIGTYPE != void
279 static void stop1(const char *lockfile
, const char *pidfile
)
284 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
290 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
293 exit (0); /* That was easy! */
296 signal(SIGCHLD
, sigexit
);
298 if ((p
=fork()) == -1)
304 if (p
) /* Parent - first sends a SIGTERM, then a SIGKILL */
314 if ((fp
=fopen(pidfile
, "r")) == NULL
)
319 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')
332 if (ll_lock_ex(lockfd
))
338 int ll_daemon_restart(const char *lockfile
, const char *pidfile
)
345 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
351 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
354 return (0); /* That was easy! */
358 if ((fp
=fopen(pidfile
, "r")) == NULL
)
363 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')