home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume23 / mlpd / daemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  3.4 KB  |  120 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Riverside. 
  11.  *
  12.  * NOTE : That's Riverside.  Not Berkeley, not Santa Cruz, not even
  13.  *        Irvine.  Riverside.  Ri - ver - side.
  14.  *
  15.  * The name of the University may not be used to endorse or promote 
  16.  * products derived from this software without specific prior written
  17.  * permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  * 
  23.  * MLPD -- Multiple Line Printer Daemon (Version 1.3)
  24.  * SCCS keywords: @(#)daemon.c    1.3 12/1/90
  25.  */
  26.  
  27. #include "config.h"
  28.  
  29. /*
  30. // (int) init_daemon() -- Initialize a process as a daemon process.
  31. //
  32. // This function will start off by forking a process off from the
  33. // parent, and give control over to the child by opening up the
  34. // root directory, duplicating stdout and stderr for the child,
  35. // and release the terminal control for the main terminal.
  36. // The program also calls lock_printer() after the fork(), so that
  37. // the child process is saved into a file when the status of the 
  38. // program needs to be checked.
  39. //
  40. // Arguments : None
  41. */
  42. int init_daemon(base)
  43. char *base;
  44. {
  45.     int pid;
  46.  
  47.     /*
  48.     // Fork off a process.
  49.     */
  50.     if ((pid = fork()) < 0)
  51.     {
  52.         bomb("fork");
  53.     }
  54.     /*
  55.     // First we will check in on the parent, and get rid of it.
  56.     */
  57.     if (pid)
  58.     {
  59.         exit(0);
  60.     }
  61.     /*
  62.     // Now we specifically want all of the code in the child.
  63.     // We try to lock down the printer daemon by creating a lock
  64.     // file.
  65.     */
  66.     if (lock_printer(base) < 0)
  67.     {
  68.         bomb("lock_printer");
  69.     }
  70.     /*
  71.     // Well, if this returns, then we must start closing off the
  72.     // terminal control.  We call open(), then dup2() for stdout
  73.     // and stderr, then we try to call setpgrp on the process to
  74.     // put itself into the background.  Only the ioctl call will
  75.     // release control from the terminal.  Then we close the de-
  76.     // scriptor created by the second open() call.
  77.     */
  78.     if (open("/", 0) < 0) { bomb("open"); }
  79.     if (dup2(0, 1) < 0) { bomb("dup2:stdout"); }
  80.     if (dup2(0, 2) < 0) { bomb("dup2:stderr"); }
  81.     if (setpgrp(0, getpid()) < 0) { bomb("setpgrp"); }
  82.     /*
  83.     // Well, everything must have worked correctly, so we will default
  84.     // back to the parent.
  85.     */
  86. }
  87.  
  88. /*
  89. // (int) bomb() -- Display an error message, then exit.
  90. //
  91. // bomb() simply will call perror() and send the function call a string
  92. // declaring the error to be displayed to the user.  Once this returns
  93. // we can then call exit().
  94. //
  95. // Arguments : char *str (The string message to be displayed)
  96. */
  97. int bomb(str)
  98. char *str;
  99. {
  100.     char buf[1024];
  101.     time_t now;
  102.     struct tm *nowtime;
  103.  
  104.  
  105.     buf[0] = '\0';
  106.     (void)perror(str);
  107.     now = (time_t)time();
  108.     nowtime = localtime(&now);
  109.     (void)sprintf(buf, "Date: (%d/%d/%d)\n\
  110.         Time: (%d:%d:%d)\n\
  111.         Message: Process died from error message (%s)\n\n", 
  112.         nowtime->tm_mon, nowtime->tm_mday, nowtime->tm_year, 
  113.         nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec, 
  114.         str);
  115.     (void)openlog("mlpd", LOG_PID, LOG_LPR);
  116.     (void)syslog(LOG_ERR, buf);
  117.     (void)closelog();
  118.     (void)exit(1);
  119. }
  120.