Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 22 Nov 2000 15:20:13 -0800 (PST)
From:      Mikko Tyolajarvi <mikko@dynas.se>
To:        wollman@khavrinen.lcs.mit.edu
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: RFC: /dev/console -> /var/log/messages idea/patch 
Message-ID:  <200011222320.eAMNKDx45514@explorer.rsa.com>
References:  <1470.974928159@critter> <200011222239.RAA48717@khavrinen.lcs.mit.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
In local.freebsd-current you write:

><<On Wed, 22 Nov 2000 22:22:39 +0100, Poul-Henning Kamp <phk@critter.freebsd.dk> 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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pwd.h>
#include <err.h>
#include <sys/stat.h>

#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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200011222320.eAMNKDx45514>