5ff83a1000c74940bc16ba1b342a9db4c6fa91b4
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>
37 #define OPEN_MAX (my_open_max())
39 static int my_open_max()
41 long n
=sysconf(_SC_OPEN_MAX
);
56 #define exit(_a_) _exit(_a_)
58 static const char rcsid
[]="$Id: lockdaemon.c,v 1.13 2007/09/26 02:36:59 mrsam Exp $";
60 static int start1(const char *, int);
62 #define CONSOLE "/dev/null"
64 int ll_daemon_start(const char *lockfile
)
72 ** Close any open file descriptors.
75 for (i
=3; i
< 256; i
++)
79 ** We fork, and set up a pipe from the child process. If we read
80 ** a single 0 byte from the pipe, it means that the child has
81 ** succesfully initialized, and will return to main, so we exit(0).
82 ** If we do not read a single 0 byte from the pipe, it means that
83 ** there was an initialization error, so we return -1 to main.
105 ** We fork once more, so that the daemon process will not
106 ** be the child process of anyone.
119 ** Continue initialization in start1()
121 return (start1(lockfile
, pipefd
[1]));
125 if (read(pipefd
[0], &c
, 1) <= 0)
129 exit (0); /* Successful start of daemon */
134 static int start1(const char *lockfile
, int fd
)
148 int fd
=open("/dev/tty", O_RDWR
);
152 ioctl(fd
, TIOCNOTTY
, 0);
159 /* Attempt to obtain a lock */
161 lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600);
165 /* Perhaps an upgraded daemon runs under new uid? */
168 lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600);
171 if (lockfd
< 0 || dup2(lockfd
, OPEN_MAX
-1) != OPEN_MAX
-1)
181 if (fcntl(lockfd
, F_SETFD
, FD_CLOEXEC
) < 0)
189 if (ll_lock_ex_test(lockfd
))
191 if (write(fd
, "", 1) != 1)
192 exit(1); /* Shouldn't happen */
195 exit (0); /* Already running, pretend success */
199 ** Return >0 to main, so it can continue main's setup.
205 int ll_daemon_resetio()
210 if (open("/dev/null", O_RDONLY
) != 0)
214 i
=open(CONSOLE
, O_WRONLY
);
215 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
216 if (i
!= 1) return (-1);
219 i
=open(CONSOLE
, O_WRONLY
);
220 if (i
< 0) i
=open("/dev/null", O_WRONLY
);
221 if (i
!= 2) return (-1);
225 void ll_daemon_started(const char *pidfile
, int fd
)
227 char buf
[NUMBUFSIZE
+1];
228 char *p
=strcat(libmail_str_pid_t(getpid(), buf
), "\n");
232 if ((fp
=fopen(pidfile
, "w")) == NULL
||
233 fprintf(fp
, "%s", p
) < 0 || fflush(fp
) < 0 || fclose(fp
))
239 if (write(fd
, "", 1) != 1) /* Signal waiting parent */
240 exit(1); /* Shouldn't happen */
244 static void stop1(const char *, const char *);
246 int ll_daemon_stop(const char *lockfile
, const char *pidfile
)
252 ** We fork, and the child process attempts to stop the daemon,
253 ** then communicates the success to us, via its exit code.
256 signal(SIGCHLD
, SIG_DFL
);
257 if ((p
=fork()) == -1)
262 if (p
== 0) stop1(lockfile
, pidfile
);
264 while ((p2
=wait(&waitstat
)) != p
)
267 if (WIFEXITED(waitstat
))
268 return (WEXITSTATUS(waitstat
));
273 ** The child process forks too. The parent process goes in a loop,
274 ** trying to kill the daemon process.
276 ** The child process attempts to lock the lock file. When it
277 ** succeeds, it exits. When the child process exits, the parent
278 ** process kills itself.
281 static RETSIGTYPE
sigexit(int signum
)
283 kill(getpid(), SIGKILL
);
284 #if RETSIGTYPE != void
289 static void stop1(const char *lockfile
, const char *pidfile
)
294 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
300 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
303 exit (0); /* That was easy! */
306 signal(SIGCHLD
, sigexit
);
308 if ((p
=fork()) == -1)
314 if (p
) /* Parent - first sends a SIGTERM, then a SIGKILL */
324 if ((fp
=fopen(pidfile
, "r")) == NULL
)
329 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')
342 if (ll_lock_ex(lockfd
))
348 int ll_daemon_restart(const char *lockfile
, const char *pidfile
)
355 if ((lockfd
=open(lockfile
, O_RDWR
|O_CREAT
, 0600)) < 0)
361 if ( ll_lock_ex_test(lockfd
) == 0) /* No daemon process running */
364 return (0); /* That was easy! */
368 if ((fp
=fopen(pidfile
, "r")) == NULL
)
373 while ((c
=getc(fp
)) != EOF
&& c
!= '\n')