static void stop1(const char *lockfile, const char *pidfile) { int lockfd; pid_t p; if ((lockfd=open(lockfile, O_RDWR|O_CREAT, 0600)) < 0) { perror(lockfile); exit(1); } if ( ll_lock_ex_test(lockfd) == 0) /* No daemon process running */ { close(lockfd); exit (0); /* That was easy! */ } signal(SIGCHLD, sigexit); if ((p=fork()) == -1) { perror("fork"); exit(1); } if (p) /* Parent - first sends a SIGTERM, then a SIGKILL */ { int signum=SIGTERM; close(lockfd); for (;; sleep(10)) { FILE *fp; int c; if ((fp=fopen(pidfile, "r")) == NULL) continue; p=0; while ((c=getc(fp)) != EOF && c != '\n') { if (isdigit(c)) p=p*10+(c-'0'); } fclose(fp); if (p) kill(p, signum); signum=SIGKILL; } } if (ll_lock_ex(lockfd)) perror("lock"); close(lockfd); exit(0); }
static int start1(const char *lockfile, int fd) { int lockfd; #if HAVE_SETPGRP #if SETPGRP_VOID setpgrp(); #else setpgrp(0, 0); #endif #endif #ifdef TIOCNOTTY { int fd=open("/dev/tty", O_RDWR); if (fd >= 0) { ioctl(fd, TIOCNOTTY, 0); close(fd); } } #endif /* Attempt to obtain a lock */ if ((lockfd=open(lockfile, O_RDWR|O_CREAT, 0600)) < 0) { perror(lockfile); exit(1); } #ifdef FD_CLOEXEC if (fcntl(lockfd, F_SETFD, FD_CLOEXEC) < 0) { perror("fcntl"); close(lockfd); exit(1); } #endif if (ll_lock_ex_test(lockfd)) { write(fd, "", 1); close(fd); exit (0); /* Already running, pretend success */ } /* ** Return >0 to main, so it can continue main's setup. */ return (fd); }
int ll_daemon_restart(const char *lockfile, const char *pidfile) { int lockfd; pid_t p; FILE *fp; int c; if ((lockfd=open(lockfile, O_RDWR|O_CREAT, 0600)) < 0) { perror(lockfile); return (1); } if ( ll_lock_ex_test(lockfd) == 0) /* No daemon process running */ { close(lockfd); return (0); /* That was easy! */ } close(lockfd); if ((fp=fopen(pidfile, "r")) == NULL) return (0); p=0; while ((c=getc(fp)) != EOF && c != '\n') { if (isdigit(c)) p=p*10+(c-'0'); } fclose(fp); if (p) kill(p, SIGHUP); return (0); }
static int start1(const char *lockfile, int fd) { int lockfd, maxfd; #if HAVE_SETPGRP #if SETPGRP_VOID setpgrp(); #else setpgrp(0, 0); #endif #endif #ifdef TIOCNOTTY { int fd=open("/dev/tty", O_RDWR); if (fd >= 0) { ioctl(fd, TIOCNOTTY, 0); close(fd); } } #endif /* Attempt to obtain a lock */ lockfd=open(lockfile, O_RDWR|O_CREAT, 0600); if (lockfd < 0) { /* Perhaps an upgraded daemon runs under new uid? */ unlink(lockfile); lockfd=open(lockfile, O_RDWR|O_CREAT, 0600); } #if HAVE_GETDTABLESIZE maxfd=getdtablesize()-1; #elif defined(OPEN_MAX) maxfd=OPEN_MAX-1; #elif HAVE_SYSCONF && defined(_SC_OPEN_MAX) if ((maxfd=sysconf(_SC_OPEN_MAX)) < 0) maxfd=63; else if (maxfd > 0) maxfd--; #else maxfd=63; #endif if (lockfd < 0 || dup2(lockfd, maxfd) != maxfd) { perror(lockfile); exit(1); } close(lockfd); lockfd=maxfd; #ifdef FD_CLOEXEC if (fcntl(lockfd, F_SETFD, FD_CLOEXEC) < 0) { perror("fcntl"); close(lockfd); exit(1); } #endif if (ll_lock_ex_test(lockfd)) { if (write(fd, "", 1) != 1) exit(1); /* Shouldn't happen */ close(fd); exit (0); /* Already running, pretend success */ } /* ** Return >0 to main, so it can continue main's setup. */ return (fd); }