home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1990 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Riverside.
- *
- * NOTE : That's Riverside. Not Berkeley, not Santa Cruz, not even
- * Irvine. Riverside. Ri - ver - side.
- *
- * The name of the University may not be used to endorse or promote
- * products derived from this software without specific prior written
- * permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- * MLPD -- Multiple Line Printer Daemon (Version 1.3)
- * SCCS keywords: @(#)daemon.c 1.3 12/1/90
- */
-
- #include "config.h"
-
- /*
- // (int) init_daemon() -- Initialize a process as a daemon process.
- //
- // This function will start off by forking a process off from the
- // parent, and give control over to the child by opening up the
- // root directory, duplicating stdout and stderr for the child,
- // and release the terminal control for the main terminal.
- // The program also calls lock_printer() after the fork(), so that
- // the child process is saved into a file when the status of the
- // program needs to be checked.
- //
- // Arguments : None
- */
- int init_daemon(base)
- char *base;
- {
- int pid;
-
- /*
- // Fork off a process.
- */
- if ((pid = fork()) < 0)
- {
- bomb("fork");
- }
- /*
- // First we will check in on the parent, and get rid of it.
- */
- if (pid)
- {
- exit(0);
- }
- /*
- // Now we specifically want all of the code in the child.
- // We try to lock down the printer daemon by creating a lock
- // file.
- */
- if (lock_printer(base) < 0)
- {
- bomb("lock_printer");
- }
- /*
- // Well, if this returns, then we must start closing off the
- // terminal control. We call open(), then dup2() for stdout
- // and stderr, then we try to call setpgrp on the process to
- // put itself into the background. Only the ioctl call will
- // release control from the terminal. Then we close the de-
- // scriptor created by the second open() call.
- */
- if (open("/", 0) < 0) { bomb("open"); }
- if (dup2(0, 1) < 0) { bomb("dup2:stdout"); }
- if (dup2(0, 2) < 0) { bomb("dup2:stderr"); }
- if (setpgrp(0, getpid()) < 0) { bomb("setpgrp"); }
- /*
- // Well, everything must have worked correctly, so we will default
- // back to the parent.
- */
- }
-
- /*
- // (int) bomb() -- Display an error message, then exit.
- //
- // bomb() simply will call perror() and send the function call a string
- // declaring the error to be displayed to the user. Once this returns
- // we can then call exit().
- //
- // Arguments : char *str (The string message to be displayed)
- */
- int bomb(str)
- char *str;
- {
- char buf[1024];
- time_t now;
- struct tm *nowtime;
-
-
- buf[0] = '\0';
- (void)perror(str);
- now = (time_t)time();
- nowtime = localtime(&now);
- (void)sprintf(buf, "Date: (%d/%d/%d)\n\
- Time: (%d:%d:%d)\n\
- Message: Process died from error message (%s)\n\n",
- nowtime->tm_mon, nowtime->tm_mday, nowtime->tm_year,
- nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec,
- str);
- (void)openlog("mlpd", LOG_PID, LOG_LPR);
- (void)syslog(LOG_ERR, buf);
- (void)closelog();
- (void)exit(1);
- }
-