/* Truncate, close and unlink an existent pidfile, * if and only if it was created by this process. * The pidfile is truncated because we may have dropped permissions * or entered a chroot and thus unable to unlink it. * * Returns 0 on truncation success, otherwise -1. */ int pidfile_clean(void) { int error; if (pidfile_fd == -1) { errno = EBADF; return -1; } if (pidfile_pid != getpid()) error = EPERM; else if (ftruncate(pidfile_fd, 0) == -1) error = errno; else { (void) unlink(pidfile_path); error = 0; } (void) pidfile_close(); if (error != 0) { errno = error; return -1; } return 0; }
void pidfile_delete(struct pidfile *pidfile) { if(!pidfile) return; pidfile_close(pidfile); free(pidfile->path); free(pidfile); }
static void common_shutdown(void) { log_state = "stopping "; if (daemon_process && log_syslog) { notice(""); } pidfile_close(); for (int i = array_len(__exit) - 1 ; i >= 0 ; --i) { array_elt(__exit, i)(); } array_wipe(__exit); }
/* * Test that pidfile_open() can create a pidfile and that pidfile_write() * can write to it. */ static const char * test_pidfile_uncontested(void) { const char *fn = "test_pidfile_uncontested"; struct pidfh *pf; pid_t other = 0; unlink(fn); pf = pidfile_open(fn, 0600, &other); if (pf == NULL && other != 0) return ("pidfile exists and is locked"); if (pf == NULL) return (strerror(errno)); if (pidfile_write(pf) != 0) { pidfile_close(pf); unlink(fn); return ("failed to write PID"); } pidfile_close(pf); unlink(fn); return (NULL); }
/* * Test that pidfile_open() locks against self. */ static const char * test_pidfile_self(void) { const char *fn = "test_pidfile_self"; struct pidfh *pf1, *pf2; pid_t other = 0; int serrno; unlink(fn); pf1 = pidfile_open(fn, 0600, &other); if (pf1 == NULL && other != 0) return ("pidfile exists and is locked"); if (pf1 == NULL) return (strerror(errno)); if (pidfile_write(pf1) != 0) { serrno = errno; pidfile_close(pf1); unlink(fn); return (strerror(serrno)); } // second open should fail pf2 = pidfile_open(fn, 0600, &other); if (pf2 != NULL) { pidfile_close(pf1); pidfile_close(pf2); unlink(fn); return ("managed to opened pidfile twice"); } if (other != getpid()) { pidfile_close(pf1); unlink(fn); return ("pidfile contained wrong PID"); } pidfile_close(pf1); unlink(fn); return (NULL); }
int main(int argc, char **argv) { int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, timeout = 60; bool dont_daemonize = false; struct pidfh *pidfh; pid_t pid, otherpid; const char *pidfile_path = DEFAULT_PIDFILE; struct iscsi_daemon_request request; while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { switch (ch) { case 'P': pidfile_path = optarg; break; case 'd': dont_daemonize = true; debug++; break; case 'l': debug = atoi(optarg); break; case 'm': maxproc = atoi(optarg); break; case 't': timeout = atoi(optarg); break; case '?': default: usage(); } } argc -= optind; if (argc != 0) usage(); log_init(debug); pidfh = pidfile_open(pidfile_path, 0600, &otherpid); if (pidfh == NULL) { if (errno == EEXIST) log_errx(1, "daemon already running, pid: %jd.", (intmax_t)otherpid); log_err(1, "cannot open or create pidfile \"%s\"", pidfile_path); } iscsi_fd = open(ISCSI_PATH, O_RDWR); if (iscsi_fd < 0 && errno == ENOENT) { saved_errno = errno; retval = kldload("iscsi"); if (retval != -1) iscsi_fd = open(ISCSI_PATH, O_RDWR); else errno = saved_errno; } if (iscsi_fd < 0) log_err(1, "failed to open %s", ISCSI_PATH); if (dont_daemonize == false) { if (daemon(0, 0) == -1) { log_warn("cannot daemonize"); pidfile_remove(pidfh); exit(1); } } pidfile_write(pidfh); register_sigchld(); for (;;) { log_debugx("waiting for request from the kernel"); memset(&request, 0, sizeof(request)); error = ioctl(iscsi_fd, ISCSIDWAIT, &request); if (error != 0) { if (errno == EINTR) { nchildren -= wait_for_children(false); assert(nchildren >= 0); continue; } log_err(1, "ISCSIDWAIT"); } if (dont_daemonize) { log_debugx("not forking due to -d flag; " "will exit after servicing a single request"); } else { nchildren -= wait_for_children(false); assert(nchildren >= 0); while (maxproc > 0 && nchildren >= maxproc) { log_debugx("maxproc limit of %d child processes hit; " "waiting for child process to exit", maxproc); nchildren -= wait_for_children(true); assert(nchildren >= 0); } log_debugx("incoming connection; forking child process #%d", nchildren); nchildren++; pid = fork(); if (pid < 0) log_err(1, "fork"); if (pid > 0) continue; } pidfile_close(pidfh); handle_request(iscsi_fd, &request, timeout); } return (0); }
/* * Common code for test_pidfile_{contested,inherited}. */ static const char * common_test_pidfile_child(const char *fn, int parent_open) { struct pidfh *pf = NULL; pid_t other = 0, pid = 0; int fd[2], serrno, status; char ch; unlink(fn); if (pipe(fd) != 0) return (strerror(errno)); if (parent_open) { pf = pidfile_open(fn, 0600, &other); if (pf == NULL && other != 0) return ("pidfile exists and is locked"); if (pf == NULL) return (strerror(errno)); } pid = fork(); if (pid == -1) return (strerror(errno)); if (pid == 0) { // child close(fd[0]); signal(SIGINT, signal_handler); if (!parent_open) { pf = pidfile_open(fn, 0600, &other); if (pf == NULL && other != 0) return ("pidfile exists and is locked"); if (pf == NULL) return (strerror(errno)); } if (pidfile_write(pf) != 0) { serrno = errno; pidfile_close(pf); unlink(fn); return (strerror(serrno)); } if (pf == NULL) _exit(1); if (pidfile_write(pf) != 0) _exit(1); if (write(fd[1], "*", 1) != 1) _exit(1); select(0, 0, 0, 0, 0); _exit(0); } // parent close(fd[1]); if (pf) pidfile_close(pf); // wait for the child to signal us if (read(fd[0], &ch, 1) != 1) { serrno = errno; unlink(fn); kill(pid, SIGTERM); errno = serrno; return (strerror(errno)); } // We shouldn't be able to lock the same pidfile as our child pf = pidfile_open(fn, 0600, &other); if (pf != NULL) { pidfile_close(pf); unlink(fn); return ("managed to lock contested pidfile"); } // Failed to lock, but not because it was contested if (other == 0) { unlink(fn); return (strerror(errno)); } // Locked by the wrong process if (other != pid) { unlink(fn); return ("pidfile contained wrong PID"); } // check our child's fate if (pf) pidfile_close(pf); unlink(fn); if (kill(pid, SIGINT) != 0) return (strerror(errno)); if (waitpid(pid, &status, 0) == -1) return (strerror(errno)); if (WIFSIGNALED(status)) return ("child caught signal"); if (WEXITSTATUS(status) != 0) return ("child returned non-zero status"); // success return (NULL); }