From owner-svn-src-all@FreeBSD.ORG Thu Sep 19 18:00:06 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 3732B56C; Thu, 19 Sep 2013 18:00:06 +0000 (UTC) (envelope-from trociny@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 24AB32DF9; Thu, 19 Sep 2013 18:00:06 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r8JI056p026572; Thu, 19 Sep 2013 18:00:05 GMT (envelope-from trociny@svn.freebsd.org) Received: (from trociny@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r8JI05Wt026571; Thu, 19 Sep 2013 18:00:05 GMT (envelope-from trociny@svn.freebsd.org) Message-Id: <201309191800.r8JI05Wt026571@svn.freebsd.org> From: Mikolaj Golub Date: Thu, 19 Sep 2013 18:00:05 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r255707 - head/usr.sbin/daemon X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Sep 2013 18:00:06 -0000 Author: trociny Date: Thu Sep 19 18:00:05 2013 New Revision: 255707 URL: http://svnweb.freebsd.org/changeset/base/255707 Log: 1. Properly clean pid files in the case of the error. 2. Write the supervisor pid before the restart loop, so we don't uselessly rewrite it after every child restart. 3. Remove duplicate ppfh and pfh initialization. Approved by: re (glebius) MFC after: 2 weeks Modified: head/usr.sbin/daemon/daemon.c Modified: head/usr.sbin/daemon/daemon.c ============================================================================== --- head/usr.sbin/daemon/daemon.c Thu Sep 19 16:22:05 2013 (r255706) +++ head/usr.sbin/daemon/daemon.c Thu Sep 19 18:00:05 2013 (r255707) @@ -55,13 +55,12 @@ main(int argc, char *argv[]) { struct pidfh *ppfh, *pfh; sigset_t mask, oldmask; - int ch, nochdir, noclose, restart; + int ch, nochdir, noclose, restart, serrno; const char *pidfile, *ppidfile, *user; pid_t otherpid, pid; nochdir = noclose = 1; restart = 0; - ppfh = pfh = NULL; ppidfile = pidfile = user = NULL; while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) { switch (ch) { @@ -108,11 +107,13 @@ main(int argc, char *argv[]) err(2, "pidfile ``%s''", pidfile); } } - - /* do same for actual daemon process */ + /* Do the same for actual daemon process. */ if (ppidfile != NULL) { ppfh = pidfile_open(ppidfile, 0600, &otherpid); if (ppfh == NULL) { + serrno = errno; + pidfile_remove(pfh); + errno = serrno; if (errno == EEXIST) { errx(3, "process already running, pid: %d", otherpid); @@ -121,8 +122,12 @@ main(int argc, char *argv[]) } } - if (daemon(nochdir, noclose) == -1) - err(1, NULL); + if (daemon(nochdir, noclose) == -1) { + warn("daemon"); + goto exit; + } + /* Write out parent pidfile if needed. */ + pidfile_write(ppfh); /* * If the pidfile or restart option is specified the daemon @@ -139,22 +144,28 @@ main(int argc, char *argv[]) * Restore default action for SIGTERM in case the * parent process decided to ignore it. */ - if (signal(SIGTERM, SIG_DFL) == SIG_ERR) - err(1, "signal"); + if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { + warn("signal"); + goto exit; + } /* * Because SIGCHLD is ignored by default, setup dummy handler * for it, so we can mask it. */ - if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) - err(1, "signal"); + if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) { + warn("signal"); + goto exit; + } /* * Block interesting signals. */ sigemptyset(&mask); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGCHLD); - if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) - err(1, "sigprocmask"); + if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) { + warn("sigprocmask"); + goto exit; + } /* * Try to protect against pageout kill. Ignore the * error, madvise(2) will fail only if a process does @@ -168,8 +179,8 @@ restart: */ pid = fork(); if (pid == -1) { - pidfile_remove(pfh); - err(1, "fork"); + warn("fork"); + goto exit; } } if (pid <= 0) { @@ -192,18 +203,16 @@ restart: */ err(1, "%s", argv[0]); } - /* write out parent pidfile if needed */ - if (ppidfile != NULL) - pidfile_write(ppfh); setproctitle("%s[%d]", argv[0], pid); if (wait_child(pid, &mask) == 0 && restart) { sleep(1); goto restart; } +exit: pidfile_remove(pfh); pidfile_remove(ppfh); - exit(0); /* Exit status does not matter. */ + exit(1); /* If daemon(3) succeeded exit status does not matter. */ } static void