home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / cmd / faxspooler / faxspooler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  4.0 KB  |  175 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel. 
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <pwd.h>
  24. #include <syslog.h>
  25. #include <sys/ioctl.h>
  26.  
  27. #include "../../lib/libfax/libfax.h"
  28.  
  29. /*
  30.  * Make a default for this:
  31.  */
  32. #ifndef INCOMING_EMAIL_ADDR
  33. #define INCOMING_EMAIL_ADDR    NULL
  34. #endif
  35.  
  36. static void
  37. daemonize()
  38. {
  39.     int s;
  40.     int pid;
  41.  
  42.     if ((pid = fork()) != 0) {
  43.       if (pid < 0)
  44.       perror("start_daemon: fork");
  45.       exit(0);
  46.     }
  47.  
  48.     for (s = getdtablesize()-1; s >= 0; --s)
  49.       close(s);
  50.  
  51.     open("/dev/null", O_RDONLY);
  52.     dup2(0, 1);
  53.     dup2(0, 2);
  54.     
  55.     /* disassociate server from controlling terminal */
  56.     if ((s = open("/dev/tty", O_RDWR)) >= 0) {
  57.     ioctl(s, TIOCNOTTY, 0);
  58.     close(s);
  59.     }
  60. }
  61.  
  62. main(argc,argv)
  63.      int argc;
  64.      char *argv[];
  65. {
  66.     int c;
  67.     int errflg = 0;
  68.     extern char *optarg;
  69.     extern int optind;
  70.  
  71.     char *fax_device = FAX_DEVICE;
  72.     int be_daemon = FALSE;
  73.     int max_delivery_time = MAX_DELIVERY_TIME;
  74.     int min_retry_wait = MIN_RETRY_WAIT;
  75.     char *out_qdir = OUTGOING_QUEUE;
  76.     char *in_qdir = INCOMING_QUEUE;
  77.     char *in_email = INCOMING_EMAIL_ADDR;
  78.     int setuid_fax = TRUE;
  79.     FaxModem fm[1];
  80.  
  81.     /*
  82.      * This is a friendly umask.
  83.      */
  84.     umask(0002);
  85.  
  86.     /*
  87.      * Start with a reasonable log level.
  88.      */
  89.     log_set_level(LOG_WARNING);
  90.  
  91.     while ((c = getopt(argc, argv, "l:f:d:r:DI:O:E:U")) != -1)
  92.       switch (c) {
  93.     case 'l':
  94.       log_set_level(atoi(optarg));
  95.       break;
  96.     case 'f':
  97.       fax_device = optarg;
  98.       break;
  99.     case 'd':
  100.       max_delivery_time = atoi(optarg);
  101.       break;
  102.     case 'r':
  103.       min_retry_wait = atoi(optarg);
  104.       break;
  105.     case 'D':
  106.       be_daemon = TRUE;
  107.       break;
  108.     case 'I':
  109.       in_qdir = optarg;
  110.       break;
  111.     case 'O':
  112.       out_qdir = optarg;
  113.       break;
  114.     case 'E':
  115.       in_email = optarg;
  116.       break;
  117.     case 'U':
  118.       setuid_fax = FALSE;
  119.       break;
  120.     case '?':
  121.       errflg++;;
  122.       break;
  123.       }
  124.  
  125.     if (errflg) {
  126.     fprintf(stderr, "usage: %s\n", argv[0]);
  127.     fprintf(stderr, "optional args:\n");
  128.     fprintf(stderr, "\t-l <level> sets the log level\n");
  129.     fprintf(stderr, "\t-f <device> sets the fax device\n");
  130.     fprintf(stderr, "\t-d <time> sets the max delivery time\n");
  131.     fprintf(stderr, "\t-r <time> sets the max retry period\n");
  132.     fprintf(stderr, "\t-D runs the spooler as a daemon\n");    
  133.     fprintf(stderr, "\t-I <dir> sets the incoming queue directory\n");
  134.     fprintf(stderr, "\t-O <dir> sets the outgoing queue directory\n");
  135.     fprintf(stderr, "\t-E <addr> sets the incoming email notice addr\n");
  136.     fprintf(stderr, "\t-U don't setuid to user fax\n");
  137.     exit(1);
  138.     }
  139.  
  140.     if (be_daemon) {
  141.     daemonize();
  142.     log_enable_syslog(LOG_LOCAL0);
  143.     }
  144.  
  145.     if (setuid_fax) {
  146.     struct passwd *pwd;
  147.     if ((pwd = getpwnam("fax")) != NULL) {
  148.         if (setgid(pwd->pw_gid) < 0)
  149.           log(L_ALERT, "can't setgid to fax: %m");
  150.         if (setuid(pwd->pw_uid) < 0)
  151.           log(L_ALERT, "can't setuid to fax: %m");
  152.     } else
  153.       log(L_ALERT, "fax user unknown, running anyway");
  154.     }
  155.         
  156.     if (faxmodem_open(&fm[0], fax_device) < 0) {
  157.     log(L_ALERT, "open of fax failed: %m");
  158.     exit(2);
  159.     }
  160.  
  161.     if (faxmodem_sync(&fm[0], FAXMODEM_SYNC_TRIES) < 0) {
  162.     log(L_ALERT, "can't sync to the modem");
  163.     exit(2);
  164.     }
  165.     
  166.     /*
  167.      * Start running the queue.  This never should return under normal
  168.      * operations.
  169.      */
  170.     process_queue(out_qdir, in_qdir, in_email, fm, 1, 
  171.           max_delivery_time, min_retry_wait);
  172.  
  173.     exit(3);
  174. }
  175.