2 ** Copyright 2000-2006 Double Precision, Inc. See COPYING for
3 ** distribution information.
18 #include <sys/types.h>
19 #include "../numlib/numlib.h"
24 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
27 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
30 #include <sys/ioctl.h>
33 #define exit(_a_) _exit(_a_)
35 static const char rcsid
[]="$Id: lockdaemon.c,v 1.10 2006/05/28 15:29:52 mrsam Exp $";
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 ** succesfully 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);
155 if (fcntl(lockfd
, F_SETFD
, FD_CLOEXEC
) < 0)
163 if (ll_lock_ex_test(lockfd
))
165 if (write(fd
, "", 1) != 1)
166 exit(1); /* Shouldn't happen */
169 exit (0); /* Already running, pretend success */
173 ** Return >0 to main, so it can continue main's setup.
179 int ll_daemon_resetio()
184 if (open("/dev/null", O_RDONLY
) != 0)
188 i
=open(CONSOLE
, O_WRONLY
);
189 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
190 if (i
!= 1) return (-1);
193 i
=open(CONSOLE
, O_WRONLY
);
194 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
195 if (i
!= 2) return (-1);
199 void ll_daemon_started(const char *pidfile
, int fd
)
201 char buf
[NUMBUFSIZE
+1];
202 char *p
=strcat(libmail_str_pid_t(getpid(), buf
), "\n");
206 if ((fp
=fopen(pidfile
, "w")) == NULL
||
207 fprintf(fp
, "%s", p
) < 0 || fflush(fp
) < 0 || fclose(fp
))
213 if (write(fd
, "", 1) != 1) /* Signal waiting parent */
214 exit(1); /* Shouldn't happen */
218 static void stop1(const char *, const char *);
220 int ll_daemon_stop(const char *lockfile
, const char *pidfile
)
226 ** We fork, and the child process attempts to stop the daemon,
227 ** then communicates the success to us, via its exit code.
230 signal(SIGCHLD
, SIG_DFL
);
231 if ((p
=fork()) == -1)
236 if (p
== 0) stop1(lockfile
, pidfile
);
238 while ((p2
=wait(&waitstat
)) != p
)
241 if (WIFEXITED(waitstat
))
242 return (WEXITSTATUS(waitstat
));
247 ** The child process forks too. The parent process goes in a loop,
248 ** trying to kill the daemon process.
250 ** The child process attempts to lock the lock file. When it
251 ** succeeds, it exits. When the child process exits, the parent
252 ** process kills itself.
255 static RETSIGTYPE
sigexit(int signum
)
257 kill(getpid(), SIGKILL
);
258 #if RETSIGTYPE != void
263 static void stop1(const char *lockfile
, const char *pidfile
)
268 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
274 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
277 exit (0); /* That was easy! */
280 signal(SIGCHLD
, sigexit
);
282 if ((p
=fork()) == -1)
288 if (p
) /* Parent - first sends a SIGTERM, then a SIGKILL */
298 if ((fp
=fopen(pidfile
, "r")) == NULL
)
303 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')
316 if (ll_lock_ex(lockfd
))
322 int ll_daemon_restart(const char *lockfile
, const char *pidfile
)
329 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
335 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
338 return (0); /* That was easy! */
342 if ((fp
=fopen(pidfile
, "r")) == NULL
)
347 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')