X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=daemon.git;a=blobdiff_plain;f=daemon.c;h=abc076fdc1526cae3b204c860a90e2065b65a63d;hp=7fccad04b5e53594f766ef5d0252c0413275339c;hb=HEAD;hpb=0aa9138f10ceb0258291ce0945e207393c2fe4cc diff --git a/daemon.c b/daemon.c index 7fccad0..abc076f 100644 --- a/daemon.c +++ b/daemon.c @@ -28,17 +28,16 @@ * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp */ -#include -__FBSDID("$FreeBSD: src/usr.sbin/daemon/daemon.c,v 1.8 2007/04/19 16:43:30 peter Exp $"); +#include "config.h" +#include "pidfile.h" +#include "getprogname.h" #include -#include #include #include -#include -#include #include +#include #include #include @@ -53,9 +52,11 @@ main(int argc, char *argv[]) const char *pidfile, *user; pid_t otherpid; + setprogname(argv[0]); + nochdir = noclose = 1; pidfile = user = NULL; - while ((ch = getopt(argc, argv, "-cfp:u:")) != -1) { + while ((ch = getopt(argc, argv, "cfp:u:")) != -1) { switch (ch) { case 'c': nochdir = 0; @@ -90,15 +91,20 @@ main(int argc, char *argv[]) pfh = pidfile_open(pidfile, 0600, &otherpid); if (pfh == NULL) { if (errno == EEXIST) { - errx(3, "process already running, pid: %d", - otherpid); + fprintf(stderr, "process already running, pid: %d\n", otherpid); + exit(3); } - err(2, "pidfile ``%s''", pidfile); + else { + fprintf(stderr, "pidfile ``%s'': %s\n", pidfile, strerror(errno)); + exit(2); + } } } - if (daemon(nochdir, noclose) == -1) - err(1, NULL); + if (daemon(nochdir, noclose) == -1) { + fprintf(stderr, "%s\n", strerror(errno)); + exit(1); + } /* Now that we are the child, write out the pid */ if (pidfile) @@ -115,7 +121,8 @@ main(int argc, char *argv[]) pidfile_remove(pfh); /* The child is now running, so the exit status doesn't matter. */ - errc(1, errcode, "%s", argv[0]); + fprintf(stderr, "%s: %s\n", argv[0], strerror(errcode)); + exit(1); } static void @@ -124,18 +131,22 @@ restrict_process(const char *user) struct passwd *pw = NULL; pw = getpwnam(user); - if (pw == NULL) - errx(1, "unknown user: %s", user); - - if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) - errx(1, "failed to set user environment"); + if (pw == NULL) { + fprintf(stderr, "unknown user: %s\n", user); + exit(1); + } + + if (setuid(pw->pw_uid) != 0) { + fprintf(stderr, "failed to setuid to %s\n", user); + exit(1); + } } static void usage(void) { - (void)fprintf(stderr, - "usage: daemon [-cf] [-p pidfile] [-u user] command " - "arguments ...\n"); + fprintf(stderr, + "usage: daemon [-cf] [-p pidfile] [-u user] command " + "arguments ...\n"); exit(1); }