don't fork off an output filter if it won't ever be used
[unix-history] / usr / src / usr.sbin / lpr / lpd / printjob.c
index 0d1dc74..347c2eb 100644 (file)
@@ -1,6 +1,23 @@
+/*
+ * Copyright (c) 1983 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, Berkeley.  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.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)printjob.c 4.23 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)printjob.c 5.11 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * printjob -- print jobs in the queue.
 
 /*
  * printjob -- print jobs in the queue.
@@ -10,6 +27,7 @@ static char sccsid[] = "@(#)printjob.c        4.23 (Berkeley) %G%";
  */
 
 #include "lp.h"
  */
 
 #include "lp.h"
+#include "pathnames.h"
 
 #define DORETURN       0       /* absorb fork error */
 #define DOABORT                1       /* abort if dofork fails */
 
 #define DORETURN       0       /* absorb fork error */
 #define DOABORT                1       /* abort if dofork fails */
@@ -48,7 +66,7 @@ char  length[10] = "-l";      /* page length in lines */
 char   pxwidth[10] = "-x";     /* page width in pixels */
 char   pxlength[10] = "-y";    /* page length in pixels */
 char   indent[10] = "-i0";     /* indentation size in characters */
 char   pxwidth[10] = "-x";     /* page width in pixels */
 char   pxlength[10] = "-y";    /* page length in pixels */
 char   indent[10] = "-i0";     /* indentation size in characters */
-char   tmpfile[] = "errsXXXXXX"; /* file name for filter output */
+char   tempfile[] = "errsXXXXXX"; /* file name for filter output */
 
 printjob()
 {
 
 printjob()
 {
@@ -62,6 +80,11 @@ printjob()
 
        init();                                 /* set up capabilities */
        (void) write(1, "", 1);                 /* ack that daemon is started */
 
        init();                                 /* set up capabilities */
        (void) write(1, "", 1);                 /* ack that daemon is started */
+       (void) close(2);                        /* set up log file */
+       if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) {
+               syslog(LOG_ERR, "%s: %m", LF);
+               (void) open(_PATH_DEVNULL, O_WRONLY);
+       }
        setgid(getegid());
        pid = getpid();                         /* for use with lprm */
        setpgrp(0, pid);
        setgid(getegid());
        pid = getpid();                         /* for use with lprm */
        setpgrp(0, pid);
@@ -70,7 +93,7 @@ printjob()
        signal(SIGQUIT, abortpr);
        signal(SIGTERM, abortpr);
 
        signal(SIGQUIT, abortpr);
        signal(SIGTERM, abortpr);
 
-       (void) mktemp(tmpfile);
+       (void) mktemp(tempfile);
 
        /*
         * uses short form file names
 
        /*
         * uses short form file names
@@ -188,7 +211,7 @@ again:
                        if (TR != NULL)         /* output trailer */
                                (void) write(ofd, TR, strlen(TR));
                }
                        if (TR != NULL)         /* output trailer */
                                (void) write(ofd, TR, strlen(TR));
                }
-               (void) unlink(tmpfile);
+               (void) unlink(tempfile);
                exit(0);
        }
        goto again;
                exit(0);
        }
        goto again;
@@ -196,11 +219,11 @@ again:
 
 char   fonts[4][50];   /* fonts for troff */
 
 
 char   fonts[4][50];   /* fonts for troff */
 
-char ifonts[4][18] = {
-       "/usr/lib/vfont/R",
-       "/usr/lib/vfont/I",
-       "/usr/lib/vfont/B",
-       "/usr/lib/vfont/S"
+char ifonts[4][40] = {
+       _PATH_VFONTR,
+       _PATH_VFONTI,
+       _PATH_VFONTB,
+       _PATH_VFONTS,
 };
 
 /*
 };
 
 /*
@@ -318,7 +341,7 @@ printit(file)
                        continue;
 
                case 'L':       /* identification line */
                        continue;
 
                case 'L':       /* identification line */
-                       if (!SH)
+                       if (!SH && !HL)
                                banner(line+1, jobname);
                        continue;
 
                                banner(line+1, jobname);
                        continue;
 
@@ -367,6 +390,11 @@ pass2:
        fseek(cfp, 0L, 0);
        while (getline(cfp))
                switch (line[0]) {
        fseek(cfp, 0L, 0);
        while (getline(cfp))
                switch (line[0]) {
+               case 'L':       /* identification line */
+                       if (!SH && HL)
+                               banner(line+1, jobname);
+                       continue;
+
                case 'M':
                        if (bombed < NOACCT)    /* already sent if >= NOACCT */
                                sendmail(line+1, bombed);
                case 'M':
                        if (bombed < NOACCT)    /* already sent if >= NOACCT */
                                sendmail(line+1, bombed);
@@ -400,6 +428,7 @@ print(format, file)
        register int n;
        register char *prog;
        int fi, fo;
        register int n;
        register char *prog;
        int fi, fo;
+       FILE *fp;
        char *av[15], buf[BUFSIZ];
        int pid, p[2], stopped = 0;
        union wait status;
        char *av[15], buf[BUFSIZ];
        int pid, p[2], stopped = 0;
        union wait status;
@@ -432,7 +461,7 @@ print(format, file)
        switch (format) {
        case 'p':       /* print file using 'pr' */
                if (IF == NULL) {       /* use output filter */
        switch (format) {
        case 'p':       /* print file using 'pr' */
                if (IF == NULL) {       /* use output filter */
-                       prog = PR;
+                       prog = _PATH_PR;
                        av[0] = "pr";
                        av[1] = width;
                        av[2] = length;
                        av[0] = "pr";
                        av[1] = width;
                        av[2] = length;
@@ -448,8 +477,9 @@ print(format, file)
                        dup2(p[1], 1);          /* pipe is stdout */
                        for (n = 3; n < NOFILE; n++)
                                (void) close(n);
                        dup2(p[1], 1);          /* pipe is stdout */
                        for (n = 3; n < NOFILE; n++)
                                (void) close(n);
-                       execl(PR, "pr", width, length, "-h", *title ? title : " ", 0);
-                       syslog(LOG_ERR, "cannot execl %s", PR);
+                       execl(_PATH_PR, "pr", width, length,
+                           "-h", *title ? title : " ", 0);
+                       syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
                        exit(2);
                }
                (void) close(p[1]);             /* close output side */
                        exit(2);
                }
                (void) close(p[1]);             /* close output side */
@@ -491,7 +521,7 @@ print(format, file)
                } else {
                        for (n = 0; n < 4; n++) {
                                if (fonts[n][0] != '/')
                } else {
                        for (n = 0; n < 4; n++) {
                                if (fonts[n][0] != '/')
-                                       (void) write(fo, "/usr/lib/vfont/", 15);
+                                       (void) write(fo, _PATH_VFONT, 15);
                                (void) write(fo, fonts[n], strlen(fonts[n]));
                                (void) write(fo, "\n", 1);
                        }
                                (void) write(fo, fonts[n], strlen(fonts[n]));
                                (void) write(fo, "\n", 1);
                        }
@@ -553,7 +583,7 @@ start:
        if ((child = dofork(DORETURN)) == 0) {  /* child */
                dup2(fi, 0);
                dup2(fo, 1);
        if ((child = dofork(DORETURN)) == 0) {  /* child */
                dup2(fi, 0);
                dup2(fo, 1);
-               n = open(tmpfile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
+               n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
                if (n >= 0)
                        dup2(n, 2);
                for (n = 3; n < NOFILE; n++)
                if (n >= 0)
                        dup2(n, 2);
                for (n = 3; n < NOFILE; n++)
@@ -577,6 +607,16 @@ start:
                }
        }
        tof = 0;
                }
        }
        tof = 0;
+
+       /* Copy filter output to "lf" logfile */
+       if (fp = fopen(tempfile, "r")) {
+               char tbuf[512];
+
+               while (fgets(buf, sizeof(buf), fp))
+                       fputs(buf, stderr);
+               close(fp);
+       }
+
        if (!WIFEXITED(status)) {
                syslog(LOG_WARNING, "%s: Daemon filter '%c' terminated (%d)",
                        printer, format, status.w_termsig);
        if (!WIFEXITED(status)) {
                syslog(LOG_WARNING, "%s: Daemon filter '%c' terminated (%d)",
                        printer, format, status.w_termsig);
@@ -716,7 +756,7 @@ sendfile(type, file)
                if (i == 0)
                        status("no space on remote; waiting for queue to drain");
                if (i == 10)
                if (i == 0)
                        status("no space on remote; waiting for queue to drain");
                if (i == 10)
-                       syslog(LOG_SALERT, "%s: can't send to %s; queue full",
+                       syslog(LOG_ALERT, "%s: can't send to %s; queue full",
                                printer, RM);
                sleep(5 * 60);
        }
                                printer, RM);
                sleep(5 * 60);
        }
@@ -894,12 +934,12 @@ sendmail(user, bombed)
                dup2(p[0], 0);
                for (i = 3; i < NOFILE; i++)
                        (void) close(i);
                dup2(p[0], 0);
                for (i = 3; i < NOFILE; i++)
                        (void) close(i);
-               if ((cp = rindex(MAIL, '/')) != NULL)
+               if ((cp = rindex(_PATH_SENDMAIL, '/')) != NULL)
                        cp++;
                else
                        cp++;
                else
-                       cp = MAIL;
+                       cp = _PATH_SENDMAIL;
                sprintf(buf, "%s@%s", user, fromhost);
                sprintf(buf, "%s@%s", user, fromhost);
-               execl(MAIL, cp, buf, 0);
+               execl(_PATH_SENDMAIL, cp, buf, 0);
                exit(0);
        } else if (s > 0) {                             /* parent */
                dup2(p[1], 1);
                exit(0);
        } else if (s > 0) {                             /* parent */
                dup2(p[1], 1);
@@ -920,8 +960,8 @@ sendmail(user, bombed)
                        printf("\ncould not be printed without an account on %s\n", host);
                        break;
                case FILTERERR:
                        printf("\ncould not be printed without an account on %s\n", host);
                        break;
                case FILTERERR:
-                       if (stat(tmpfile, &stb) < 0 || stb.st_size == 0 ||
-                           (fp = fopen(tmpfile, "r")) == NULL) {
+                       if (stat(tempfile, &stb) < 0 || stb.st_size == 0 ||
+                           (fp = fopen(tempfile, "r")) == NULL) {
                                printf("\nwas printed but had some errors\n");
                                break;
                        }
                                printf("\nwas printed but had some errors\n");
                                break;
                        }
@@ -980,7 +1020,7 @@ dofork(action)
  */
 abortpr()
 {
  */
 abortpr()
 {
-       (void) unlink(tmpfile);
+       (void) unlink(tempfile);
        kill(0, SIGINT);
        if (ofilter > 0)
                kill(ofilter, SIGCONT);
        kill(0, SIGINT);
        if (ofilter > 0)
                kill(ofilter, SIGCONT);
@@ -992,13 +1032,17 @@ abortpr()
 init()
 {
        int status;
 init()
 {
        int status;
+       char *s;
 
 
-       if ((status = pgetent(line, printer)) < 0)
-               fatal("can't open printer description file");
-       else if (status == 0)
-               fatal("unknown printer");
+       if ((status = pgetent(line, printer)) < 0) {
+               syslog(LOG_ERR, "can't open printer description file");
+               exit(1);
+       } else if (status == 0) {
+               syslog(LOG_ERR, "unknown printer: %s", printer);
+               exit(1);
+       }
        if ((LP = pgetstr("lp", &bp)) == NULL)
        if ((LP = pgetstr("lp", &bp)) == NULL)
-               LP = DEFDEVLP;
+               LP = _PATH_DEFDEVLP;
        if ((RP = pgetstr("rp", &bp)) == NULL)
                RP = DEFLP;
        if ((LO = pgetstr("lo", &bp)) == NULL)
        if ((RP = pgetstr("rp", &bp)) == NULL)
                RP = DEFLP;
        if ((LO = pgetstr("lo", &bp)) == NULL)
@@ -1006,9 +1050,9 @@ init()
        if ((ST = pgetstr("st", &bp)) == NULL)
                ST = DEFSTAT;
        if ((LF = pgetstr("lf", &bp)) == NULL)
        if ((ST = pgetstr("st", &bp)) == NULL)
                ST = DEFSTAT;
        if ((LF = pgetstr("lf", &bp)) == NULL)
-               LF = DEFLOGF;
+               LF = _PATH_CONSOLE;
        if ((SD = pgetstr("sd", &bp)) == NULL)
        if ((SD = pgetstr("sd", &bp)) == NULL)
-               SD = DEFSPOOL;
+               SD = _PATH_DEFSPOOL;
        if ((DU = pgetnum("du")) < 0)
                DU = DEFUID;
        if ((FF = pgetstr("ff", &bp)) == NULL)
        if ((DU = pgetnum("du")) < 0)
                DU = DEFUID;
        if ((FF = pgetstr("ff", &bp)) == NULL)
@@ -1026,6 +1070,9 @@ init()
                PY = 0;
        sprintf(&pxlength[2], "%d", PY);
        RM = pgetstr("rm", &bp);
                PY = 0;
        sprintf(&pxlength[2], "%d", PY);
        RM = pgetstr("rm", &bp);
+       if (s = checkremote())
+               syslog(LOG_WARNING, s);
+
        AF = pgetstr("af", &bp);
        OF = pgetstr("of", &bp);
        IF = pgetstr("if", &bp);
        AF = pgetstr("af", &bp);
        OF = pgetstr("of", &bp);
        IF = pgetstr("if", &bp);
@@ -1041,6 +1088,7 @@ init()
        SF = pgetflag("sf");
        SH = pgetflag("sh");
        SB = pgetflag("sb");
        SF = pgetflag("sf");
        SH = pgetflag("sh");
        SB = pgetflag("sb");
+       HL = pgetflag("hl");
        RW = pgetflag("rw");
        BR = pgetnum("br");
        if ((FC = pgetnum("fc")) < 0)
        RW = pgetflag("rw");
        BR = pgetnum("br");
        if ((FC = pgetnum("fc")) < 0)
@@ -1062,7 +1110,7 @@ openpr()
        register int i, n;
        int resp;
 
        register int i, n;
        int resp;
 
-       if (*LP) {
+       if (!sendtorem && *LP) {
                for (i = 1; ; i = i < 32 ? i << 1 : i) {
                        pfd = open(LP, RW ? O_RDWR : O_WRONLY);
                        if (pfd >= 0)
                for (i = 1; ; i = i < 32 ? i << 1 : i) {
                        pfd = open(LP, RW ? O_RDWR : O_WRONLY);
                        if (pfd >= 0)
@@ -1110,7 +1158,7 @@ openpr()
        /*
         * Start up an output filter, if needed.
         */
        /*
         * Start up an output filter, if needed.
         */
-       if (OF) {
+       if (!remote && OF) {
                int p[2];
                char *cp;
 
                int p[2];
                char *cp;
 
@@ -1190,14 +1238,6 @@ setty()
                syslog(LOG_ERR, "%s: ioctl(TIOCSETP): %m", printer);
                exit(1);
        }
                syslog(LOG_ERR, "%s: ioctl(TIOCSETP): %m", printer);
                exit(1);
        }
-       if (XC || XS) {
-               int ldisc = NTTYDISC;
-
-               if (ioctl(pfd, TIOCSETD, &ldisc) < 0) {
-                       syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer);
-                       exit(1);
-               }
-       }
        if (XC) {
                if (ioctl(pfd, TIOCLBIC, &XC) < 0) {
                        syslog(LOG_ERR, "%s: ioctl(TIOCLBIC): %m", printer);
        if (XC) {
                if (ioctl(pfd, TIOCLBIC, &XC) < 0) {
                        syslog(LOG_ERR, "%s: ioctl(TIOCLBIC): %m", printer);