From owner-freebsd-hackers Wed Nov 22 15:20:23 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from karon.dynas.se (karon.dynas.se [192.71.43.4]) by hub.freebsd.org (Postfix) with SMTP id 623BB37B4C5 for ; Wed, 22 Nov 2000 15:20:16 -0800 (PST) Received: (qmail 89833 invoked from network); 22 Nov 2000 23:20:15 -0000 Received: from spirit.sto.dynas.se (HELO spirit.dynas.se) (172.16.1.10) by karon.sto.dynas.se with SMTP; 22 Nov 2000 23:20:15 -0000 Received: (qmail 2884 invoked from network); 22 Nov 2000 23:20:42 -0000 Received: from explorer.rsa.com (10.81.217.59) by spirit.dynas.se with SMTP; 22 Nov 2000 23:20:42 -0000 Received: (from mikko@localhost) by explorer.rsa.com (8.11.1/8.11.1) id eAMNKDx45514; Wed, 22 Nov 2000 15:20:13 -0800 (PST) (envelope-from mikko) Date: Wed, 22 Nov 2000 15:20:13 -0800 (PST) From: Mikko Tyolajarvi Message-Id: <200011222320.eAMNKDx45514@explorer.rsa.com> To: wollman@khavrinen.lcs.mit.edu Cc: freebsd-hackers@freebsd.org Subject: Re: RFC: /dev/console -> /var/log/messages idea/patch Newsgroups: local.freebsd-current References: <1470.974928159@critter> <200011222239.RAA48717@khavrinen.lcs.mit.edu> X-Newsreader: NN version 6.5.6 (NOV) Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In local.freebsd-current you write: >< said: >> Another particular thing I remember was that some syslog-challenged >> daemons whine on /dev/console long after /etc/rc has finished. >They can try, but by the time they do the console has already been >revoke()d, so they no longer have access to the real console. >I've thought about writing daemon(8) which will put these turkeys in >their place. Just a Small Matter of Programming.... Do you mean something like this? $.02, /Mikko ---8<----------------------------------------------------- /* * Description: * Utility to start a program as a daemon. * Closes stdin/out/err and execs the program. * Program is located using PATH. * * Usage: * daemon [options] program [ args to program ] * * Options: * -c Do not close stdin/out/err * -d Do not chdir("/") * -o file Append stdout/err to file * -p file Write pid to file * -u user User to run program as */ #include #include #include #include #include #include #include #include #include #define PROGNAME "daemon" #define USAGE "Usage: " PROGNAME \ " [-c] [-o output] [-p pidfile] [-u user] program [args ...]" int main(int argc, char **argv) { int fd, c, noclose, nochdir; char *outfile, *prog, *s, *pidfile; FILE *pf; struct passwd *pw; char *user; noclose = 0; nochdir = 0; outfile = NULL; pidfile = NULL; user = NULL; fd = -1; pf = NULL; pw = NULL; while ((c = getopt(argc, argv, "cdo:p:u:")) != -1) { switch (c) { case 'c': noclose++; break; case 'd': nochdir++; break; case 'o': outfile = optarg; noclose++; break; case 'p': pidfile = optarg; break; case 'u': user = optarg; break; default: errx(2, USAGE); } } if (optind == argc) errx(2, USAGE); if (user != NULL && (pw = getpwnam(user)) == NULL) errx(EXIT_FAILURE, "user unknown: \"%s\"", user); if (pidfile != NULL && ((pf = fopen(pidfile, "w")) == NULL)) err(EXIT_FAILURE, "cannot create \"%s\"", pidfile); if (outfile != NULL) { if ((fd = open(outfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) < 0) { err(EXIT_FAILURE, "cannot create \"%s\"", outfile); } } if (pw != NULL) { initgroups(pw->pw_name, pw->pw_gid); if (setuid(pw->pw_uid) < 0) err(EXIT_FAILURE, "setuid(%d)", pw->pw_uid); } if (daemon(nochdir, noclose) < 0) err(EXIT_FAILURE, "daemon"); if (pidfile != NULL) { fprintf(pf, "%u\n", (unsigned)getpid()); fclose(pf); } if (fd != -1) { dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); close(fd); } prog = argv[optind]; if ((s = strrchr(argv[optind], '/')) != 0) { argv[optind] = s+1; } execvp(prog, argv + optind); err(EXIT_FAILURE, "cannot exec %s", prog); if (pidfile != NULL) { unlink(pidfile); } exit(EXIT_FAILURE); } -- Mikko Työläjärvi_______________________________________mikko@rsasecurity.com RSA Security To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message