2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95";
45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
46 __FBSDID("$FreeBSD$");
49 * printjob -- print jobs in the queue.
51 * NOTE: the lock file is used to pass information to lpq and lprm.
52 * it does not need to be removed because file locks are dynamic.
55 #include <sys/param.h>
58 #include <sys/types.h>
72 #include <sys/ioctl.h>
77 #include "pathnames.h"
80 #define DORETURN 0 /* dofork should return "can't fork" error */
81 #define DOABORT 1 /* dofork should just die if fork() fails */
84 * The buffer size to use when reading/writing spool files.
86 #define SPL_BUFSIZ BUFSIZ
99 static dev_t fdev; /* device of file pointed to by symlink */
100 static ino_t fino; /* inode of file pointed to by symlink */
101 static FILE *cfp; /* control file */
102 static pid_t of_pid; /* process id of output filter, if any */
103 static int child; /* id of any filters */
104 static int job_dfcnt; /* count of datafiles in current user job */
105 static int lfd; /* lock file descriptor */
106 static int ofd; /* output filter file descriptor */
107 static int tfd = -1; /* output filter temp file output */
108 static int pfd; /* prstatic inter file descriptor */
109 static int prchild; /* id of pr process */
110 static char title[80]; /* ``pr'' title */
111 static char locale[80]; /* ``pr'' locale */
113 /* these two are set from pp->daemon_user, but only if they are needed */
114 static char *daemon_uname; /* set from pwd->pw_name */
115 static int daemon_defgid;
117 static char class[32]; /* classification field */
118 static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */
119 /* indentation size in static characters */
120 static char indent[10] = "-i0";
121 static char jobname[100]; /* job or file name */
122 static char length[10] = "-l"; /* page length in lines */
123 static char logname[32]; /* user's login name */
124 static char pxlength[10] = "-y"; /* page length in pixels */
125 static char pxwidth[10] = "-x"; /* page width in pixels */
126 /* tempstderr is the filename used to catch stderr from exec-ing filters */
127 static char tempstderr[] = "errs.XXXXXXX";
128 static char width[10] = "-w"; /* page width in static characters */
129 #define TFILENAME "fltXXXXXX"
130 static char tfile[] = TFILENAME; /* file name for filter output */
132 static void abortpr(int _signo);
133 static void alarmhandler(int _signo);
134 static void banner(struct printer *_pp, char *_name1, char *_name2);
135 static int dofork(const struct printer *_pp, int _action);
136 static int dropit(int _c);
137 static int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
138 int _infd, int _outfd);
139 static void init(struct printer *_pp);
140 static void openpr(const struct printer *_pp);
141 static void opennet(const struct printer *_pp);
142 static void opentty(const struct printer *_pp);
143 static void openrem(const struct printer *pp);
144 static int print(struct printer *_pp, int _format, char *_file);
145 static int printit(struct printer *_pp, char *_file);
146 static void pstatus(const struct printer *_pp, const char *_msg, ...)
148 static char response(const struct printer *_pp);
149 static void scan_out(struct printer *_pp, int _scfd, char *_scsp,
151 static char *scnline(int _key, char *_p, int _c);
152 static int sendfile(struct printer *_pp, int _type, char *_file,
153 char _format, int _copyreq);
154 static int sendit(struct printer *_pp, char *_file);
155 static void sendmail(struct printer *_pp, char *_userid, int _bombed);
156 static void setty(const struct printer *_pp);
157 static void wait4data(struct printer *_pp, const char *_dfile);
160 printjob(struct printer *pp)
163 register struct jobqueue *q, **qp;
164 struct jobqueue **queue;
165 register int i, nitems;
168 int errcnt, jobcount, statok, tempfd;
171 init(pp); /* set up capabilities */
172 (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */
173 (void) close(STDERR_FILENO); /* set up log file */
174 if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
175 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
177 (void) open(_PATH_DEVNULL, O_WRONLY);
179 if(setgid(getegid()) != 0) err(1, "setgid() failed");
180 printpid = getpid(); /* for use with lprm */
181 setpgid((pid_t)0, printpid);
184 * At initial lpd startup, printjob may be called with various
185 * signal handlers in effect. After that initial startup, any
186 * calls to printjob will have a *different* set of signal-handlers
187 * in effect. Make sure all handlers are the ones we want.
189 signal(SIGCHLD, SIG_DFL);
190 signal(SIGHUP, abortpr);
191 signal(SIGINT, abortpr);
192 signal(SIGQUIT, abortpr);
193 signal(SIGTERM, abortpr);
196 * uses short form file names
198 if (chdir(pp->spool_dir) < 0) {
199 syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer,
203 statok = stat(pp->lock_file, &stb);
204 if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS))
205 exit(0); /* printing disabled */
207 lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
210 if (errno == EWOULDBLOCK) /* active daemon present */
212 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
217 * If the initial call to stat() failed, then lock_file will have
218 * been created by open(). Update &stb to match that new file.
221 statok = stat(pp->lock_file, &stb);
222 /* turn off non-blocking mode (was turned on for lock effects only) */
223 if (fcntl(lfd, F_SETFL, 0) < 0) {
224 syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer,
230 * write process id for others to know
232 sprintf(line, "%u\n", printpid);
233 pidoff = i = strlen(line);
234 if (write(lfd, line, i) != i) {
235 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
240 * search the spool directory for work and sort by queue order.
242 if ((nitems = getq(pp, &queue)) < 0) {
243 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
247 if (nitems == 0) /* no work to do */
249 if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
250 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
251 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
255 /* create a file which will be used to hold stderr from filters */
256 if ((tempfd = mkstemp(tempstderr)) == -1) {
257 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
261 if ((i = fchmod(tempfd, 0664)) == -1) {
262 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
266 /* lpd doesn't need it to be open, it just needs it to exist */
269 openpr(pp); /* open printer or remote */
272 * we found something to do now do it --
273 * write the name of the current control file into the lock file
274 * so the spool queue program can tell what we're working on
276 for (qp = queue; nitems--; free((char *) q)) {
278 if (stat(q->job_cfname, &stb) < 0)
282 (void) lseek(lfd, pidoff, 0);
283 (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
285 if (write(lfd, line, i) != i)
286 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
289 i = printit(pp, q->job_cfname);
291 i = sendit(pp, q->job_cfname);
293 * Check to see if we are supposed to stop printing or
294 * if we are to rebuild the queue.
296 if (fstat(lfd, &stb) == 0) {
297 /* stop printing before starting next job? */
298 if (stb.st_mode & LFM_PRINT_DIS)
300 /* rebuild queue (after lpc topq) */
301 if (stb.st_mode & LFM_RESET_QUE) {
302 for (free(q); nitems--; free(q))
304 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
307 "%s: fchmod(%s): %m",
308 pp->printer, pp->lock_file);
312 if (i == OK) /* all files of this job printed */
314 else if (i == REPRINT && ++errcnt < 5) {
315 /* try reprinting the job */
316 syslog(LOG_INFO, "restarting %s", pp->printer);
318 kill(of_pid, SIGCONT); /* to be sure */
320 while ((i = wait(NULL)) > 0 && i != of_pid)
323 syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
324 pp->printer, of_pid);
327 (void) close(pfd); /* close printer */
328 if (ftruncate(lfd, pidoff) < 0)
329 syslog(LOG_WARNING, "%s: ftruncate(%s): %m",
330 pp->printer, pp->lock_file);
331 openpr(pp); /* try to reopen printer */
334 syslog(LOG_WARNING, "%s: job could not be %s (%s)",
336 pp->remote ? "sent to remote host" : "printed",
339 /* ensure we don't attempt this job again */
340 (void) unlink(q->job_cfname);
341 q->job_cfname[0] = 'd';
342 (void) unlink(q->job_cfname);
344 sendmail(pp, logname, FATALERR);
350 * search the spool directory for more work.
352 if ((nitems = getq(pp, &queue)) < 0) {
353 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
357 if (nitems == 0) { /* no more work to do */
359 if (jobcount > 0) { /* jobs actually printed */
360 if (!pp->no_formfeed && !pp->tof)
361 (void) write(ofd, pp->form_feed,
362 strlen(pp->form_feed));
363 if (pp->trailer != NULL) /* output trailer */
364 (void) write(ofd, pp->trailer,
365 strlen(pp->trailer));
369 (void) unlink(tempstderr);
375 char fonts[4][50]; /* fonts for troff */
377 char ifonts[4][40] = {
385 * The remaining part is the reading of the control file (cf)
386 * and performing the various actions.
389 printit(struct printer *pp, char *file)
393 int bombed, didignorehdr;
398 * open control file; ignore if no longer there.
400 if ((cfp = fopen(file, "r")) == NULL) {
401 syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file);
407 for (i = 0; i < 4; i++)
408 strcpy(fonts[i], ifonts[i]);
409 sprintf(&width[2], "%ld", pp->page_width);
410 strcpy(indent+2, "0");
412 /* initialize job-specific count of datafiles processed */
416 * read the control file for work to do
418 * file format -- first character in the line is a command
419 * rest of the line is the argument.
420 * valid commands are:
422 * S -- "stat info" for symbolic link protection
423 * J -- "job name" on banner page
424 * C -- "class name" on banner page
425 * L -- "literal" user's name to print on banner
426 * T -- "title" for pr
427 * H -- "host name" of machine where lpr was done
428 * P -- "person" user's login name
429 * I -- "indent" amount to indent output
430 * R -- laser dpi "resolution"
431 * f -- "file name" name of text file to print
432 * l -- "file name" text file with control chars
433 * o -- "file name" postscript file, according to
434 * the RFC. Here it is treated like an 'f'.
435 * p -- "file name" text file to print with pr(1)
436 * t -- "file name" troff(1) file to print
437 * n -- "file name" ditroff(1) file to print
438 * d -- "file name" dvi file to print
439 * g -- "file name" plot(1G) file to print
440 * v -- "file name" plain raster file to print
441 * c -- "file name" cifplot file to print
442 * 1 -- "R font file" for troff
443 * 2 -- "I font file" for troff
444 * 3 -- "B font file" for troff
445 * 4 -- "S font file" for troff
446 * N -- "name" of file (used by lpq)
447 * U -- "unlink" name of file to remove
448 * (after we print it. (Pass 2 only)).
449 * M -- "mail" to user when done printing
450 * Z -- "locale" for pr
452 * get_line reads a line and expands tabs to blanks
457 while (get_line(cfp))
460 strlcpy(origin_host, line + 1, sizeof(origin_host));
461 if (class[0] == '\0') {
462 strlcpy(class, line+1, sizeof(class));
467 strlcpy(logname, line + 1, sizeof(logname));
468 if (pp->restricted) { /* restricted */
469 if (getpwnam(logname) == NULL) {
471 sendmail(pp, line+1, bombed);
480 while (*cp >= '0' && *cp <= '9')
481 i = i * 10 + (*cp++ - '0');
485 while (*cp >= '0' && *cp <= '9')
486 i = i * 10 + (*cp++ - '0');
491 if (line[1] != '\0') {
492 strlcpy(jobname, line + 1, sizeof(jobname));
494 strcpy(jobname, " ");
499 strlcpy(class, line + 1, sizeof(class));
500 else if (class[0] == '\0') {
501 /* XXX - why call gethostname instead of
502 * just strlcpy'ing local_host? */
503 gethostname(class, sizeof(class));
504 class[sizeof(class) - 1] = '\0';
508 case 'T': /* header title for pr */
509 strlcpy(title, line + 1, sizeof(title));
512 case 'L': /* identification line */
513 if (!pp->no_header && !pp->header_last)
514 banner(pp, line+1, jobname);
517 case '1': /* troff fonts */
521 if (line[1] != '\0') {
522 strlcpy(fonts[line[0]-'1'], line + 1,
527 case 'W': /* page width */
528 strlcpy(width+2, line + 1, sizeof(width) - 2);
531 case 'I': /* indent amount */
532 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
535 case 'Z': /* locale for pr */
536 strlcpy(locale, line + 1, sizeof(locale));
539 default: /* some file to print */
540 /* only lowercase cmd-codes include a file-to-print */
541 if ((line[0] < 'a') || (line[0] > 'z')) {
542 /* ignore any other lines */
546 syslog(LOG_INFO, "%s: in %s :",
550 syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
551 pp->printer, line[0], &line[1]);
554 i = print(pp, line[0], line+1);
566 sendmail(pp, logname, bombed);
582 while (get_line(cfp))
584 case 'L': /* identification line */
585 if (!pp->no_header && pp->header_last)
586 banner(pp, line+1, jobname);
590 if (bombed < NOACCT) /* already sent if >= NOACCT */
591 sendmail(pp, line+1, bombed);
595 if (strchr(line+1, '/'))
597 (void) unlink(line+1);
600 * clean-up in case another control file exists
604 return (bombed == OK ? OK : ERROR);
609 * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
610 * Return -1 if a non-recoverable error occurred,
611 * 2 if the filter detected some errors (but printed the job anyway),
612 * 1 if we should try to reprint this job and
614 * Note: all filters take stdin as the file, stdout as the printer,
615 * stderr as the log file, and must not ignore SIGINT.
618 print(struct printer *pp, int format, char *file)
624 char *av[15], buf[SPL_BUFSIZ];
626 int p[2], retcode, stopped, wstatus, wstatus_set;
629 /* Make sure the entire data file has arrived. */
632 if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
633 syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
634 pp->printer, file, format);
638 * Check to see if data file is a symbolic link. If so, it should
639 * still point to the same file or someone is trying to print
640 * something he shouldn't.
642 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
643 (stb.st_dev != fdev || stb.st_ino != fino))
646 job_dfcnt++; /* increment datafile counter for this job */
647 stopped = 0; /* output filter is not stopped */
649 /* everything seems OK, start it up */
650 if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
651 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
654 if (pp->filters[LPF_INPUT] == NULL
655 && (format == 'f' || format == 'l' || format == 'o')) {
657 while ((n = read(fi, buf, SPL_BUFSIZ)) > 0)
658 if (write(ofd, buf, n) != n) {
666 case 'p': /* print file using 'pr' */
667 if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */
674 av[i++] = *title ? title : " ";
676 av[i++] = *locale ? locale : "C";
683 if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */
684 dup2(fi, STDIN_FILENO); /* file is stdin */
685 dup2(p[1], STDOUT_FILENO); /* pipe is stdout */
688 execl(_PATH_PR, "pr", width, length,
689 "-h", *title ? title : " ",
690 "-L", *locale ? locale : "C",
692 syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
695 (void) close(p[1]); /* close output side */
702 fi = p[0]; /* use pipe for input */
703 case 'f': /* print plain text file */
704 prog = pp->filters[LPF_INPUT];
710 case 'o': /* print postscript file */
712 * Treat this as a "plain file with control characters", and
713 * assume the standard LPF_INPUT filter will recognize that
714 * the data is postscript and know what to do with it. These
715 * 'o'-file requests could come from MacOS 10.1 systems.
716 * (later versions of MacOS 10 will explicitly use 'l')
717 * A postscript file can contain binary data, which is why 'l'
718 * is somewhat more appropriate than 'f'.
721 case 'l': /* like 'f' but pass control characters */
722 prog = pp->filters[LPF_INPUT];
729 case 'r': /* print a fortran text file */
730 prog = pp->filters[LPF_FORTRAN];
735 case 't': /* print troff output */
736 case 'n': /* print ditroff output */
737 case 'd': /* print tex output */
738 (void) unlink(".railmag");
739 if ((fo = creat(".railmag", FILMOD)) < 0) {
740 syslog(LOG_ERR, "%s: cannot create .railmag",
742 (void) unlink(".railmag");
744 for (n = 0; n < 4; n++) {
745 if (fonts[n][0] != '/')
746 (void) write(fo, _PATH_VFONT,
747 sizeof(_PATH_VFONT) - 1);
748 (void) write(fo, fonts[n], strlen(fonts[n]));
749 (void) write(fo, "\n", 1);
753 prog = (format == 't') ? pp->filters[LPF_TROFF]
754 : ((format == 'n') ? pp->filters[LPF_DITROFF]
755 : pp->filters[LPF_DVI]);
760 case 'c': /* print cifplot output */
761 prog = pp->filters[LPF_CIFPLOT];
766 case 'g': /* print plot(1G) output */
767 prog = pp->filters[LPF_GRAPH];
772 case 'v': /* print raster output */
773 prog = pp->filters[LPF_RASTER];
780 syslog(LOG_ERR, "%s: illegal format character '%c'",
781 pp->printer, format);
787 "%s: no filter found in printcap for format character '%c'",
788 pp->printer, format);
791 if ((av[0] = strrchr(prog, '/')) != NULL)
798 av[n++] = origin_host;
799 av[n++] = pp->acct_file;
802 if (of_pid > 0) { /* stop output filter */
803 write(ofd, "\031\1", 2);
805 wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid)
809 "%s: after stopping 'of', wait3() returned: %m",
811 else if (!WIFSTOPPED(wstatus)) {
813 syslog(LOG_WARNING, "%s: output filter died "
814 "(pid=%d retcode=%d termsig=%d)",
815 pp->printer, of_pid, WEXITSTATUS(wstatus),
822 if ((child = dofork(pp, DORETURN)) == 0) { /* child */
823 dup2(fi, STDIN_FILENO);
824 dup2(fo, STDOUT_FILENO);
825 /* setup stderr for the filter (child process)
826 * so it goes to our temporary errors file */
827 n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
829 dup2(n, STDERR_FILENO);
833 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
842 while ((wpid = wait(&wstatus)) > 0 && wpid != child)
847 "%s: after execv(%s), wait() returned: %m",
851 retcode = WEXITSTATUS(wstatus);
856 if (stopped) { /* restart output filter */
857 if (kill(of_pid, SIGCONT) < 0) {
858 syslog(LOG_ERR, "cannot restart output filter");
864 /* Copy the filter's output to "lf" logfile */
865 if ((fp = fopen(tempstderr, "r"))) {
866 while (fgets(buf, sizeof(buf), fp))
871 if (wstatus_set && !WIFEXITED(wstatus)) {
872 syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
873 pp->printer, format, WTERMSIG(wstatus));
885 syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
886 pp->printer, format, retcode);
892 * Send the daemon control file (cf) and any data files.
893 * Return -1 if a non-recoverable error occurred, 1 if a recoverable error and
897 sendit(struct printer *pp, char *file)
899 int dfcopies, err, i;
900 char *cp, last[sizeof(line)];
905 if ((cfp = fopen(file, "r")) == NULL)
908 /* initialize job-specific count of datafiles processed */
912 * read the control file for work to do
914 * file format -- first character in the line is a command
915 * rest of the line is the argument.
916 * commands of interest are:
918 * a-z -- "file name" name of file to print
919 * U -- "unlink" name of file to remove
920 * (after we print it. (Pass 2 only)).
927 while (get_line(cfp)) {
929 if (line[0] == 'S') {
932 while (*cp >= '0' && *cp <= '9')
933 i = i * 10 + (*cp++ - '0');
937 while (*cp >= '0' && *cp <= '9')
938 i = i * 10 + (*cp++ - '0');
940 } else if (line[0] == 'H') {
941 strlcpy(origin_host, line + 1, sizeof(origin_host));
942 if (class[0] == '\0') {
943 strlcpy(class, line + 1, sizeof(class));
945 } else if (line[0] == 'P') {
946 strlcpy(logname, line + 1, sizeof(logname));
947 if (pp->restricted) { /* restricted */
948 if (getpwnam(logname) == NULL) {
949 sendmail(pp, line+1, NOACCT);
954 } else if (line[0] == 'I') {
955 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
956 } else if (line[0] >= 'a' && line[0] <= 'z') {
959 while ((i = get_line(cfp)) != 0) {
960 if (strcmp(last, line) != 0)
964 switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
973 sendmail(pp, logname, ACCESS);
980 if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
988 while (get_line(cfp))
989 if (line[0] == 'U' && !strchr(line+1, '/'))
990 (void) unlink(line+1);
992 * clean-up in case another control file exists
1000 * Send a data file to the remote machine and spool it.
1001 * Return positive if we should try resending.
1004 sendfile(struct printer *pp, int type, char *file, char format, int copyreq)
1008 char *av[15], *filtcmd;
1009 char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
1010 int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;
1012 /* Make sure the entire data file has arrived. */
1013 wait4data(pp, file);
1015 statrc = lstat(file, &stb);
1017 syslog(LOG_ERR, "%s: error from lstat(%s): %m",
1021 sfd = open(file, O_RDONLY);
1023 syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
1028 * Check to see if data file is a symbolic link. If so, it should
1029 * still point to the same file or someone is trying to print something
1032 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
1033 (stb.st_dev != fdev || stb.st_ino != fino)) {
1038 /* Everything seems OK for reading the file, now to send it */
1044 * Type == 3 means this is a datafile, not a control file.
1045 * Increment the counter of data-files in this job, and
1046 * then check for input or output filters (which are only
1047 * applied to datafiles, not control files).
1052 * Note that here we are filtering datafiles, one at a time,
1053 * as they are sent to the remote machine. Here, the *only*
1054 * difference between an input filter (`if=') and an output
1055 * filter (`of=') is the argument list that the filter is
1056 * started up with. Here, the output filter is executed
1057 * for each individual file as it is sent. This is not the
1058 * same as local print queues, where the output filter is
1059 * started up once, and then all jobs are passed thru that
1060 * single invocation of the output filter.
1062 * Also note that a queue for a remote-machine can have an
1063 * input filter or an output filter, but not both.
1065 if (pp->filters[LPF_INPUT]) {
1066 filtcmd = pp->filters[LPF_INPUT];
1069 strcpy(opt_c, "-c");
1070 strcpy(opt_h, "-h");
1071 strcpy(opt_n, "-n");
1075 av[++narg] = length;
1076 av[++narg] = indent;
1078 av[++narg] = logname;
1080 av[++narg] = origin_host;
1081 av[++narg] = pp->acct_file;
1083 } else if (pp->filters[LPF_OUTPUT]) {
1084 filtcmd = pp->filters[LPF_OUTPUT];
1088 av[++narg] = length;
1094 * If there is an input or output filter, we have to run
1095 * the datafile thru that filter and store the result as
1096 * a temporary spool file, because the protocol requires
1097 * that we send the remote host the file-size before we
1098 * start to send any of the data.
1100 strcpy(tfile, TFILENAME);
1101 tfd = mkstemp(tfile);
1103 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
1108 filtstat = execfilter(pp, filtcmd, av, sfd, tfd);
1110 /* process the return-code from the filter */
1122 "%s: filter '%c' exited (retcode=%d)",
1123 pp->printer, format, filtstat);
1127 statrc = fstat(tfd, &stb); /* to find size of tfile */
1130 "%s: error processing 'if', fstat(%s): %m",
1131 pp->printer, tfile);
1137 lseek(sfd, 0, SEEK_SET);
1145 (void) sprintf(buf, "%c%" PRId64 " %s\n", type, stb.st_size,
1148 (void) sprintf(buf, "%c%" PRId64 " %s_c%d\n", type, stb.st_size,
1151 for (i = 0; ; i++) {
1152 if (write(pfd, buf, amt) != amt ||
1153 (resp = response(pp)) < 0 || resp == '\1') {
1156 } else if (resp == '\0')
1160 "no space on remote; waiting for queue to drain");
1162 syslog(LOG_ALERT, "%s: can't send to %s; queue full",
1163 pp->printer, pp->remote_host);
1167 pstatus(pp, "sending to %s", pp->remote_host);
1169 * XXX - we should change trstat_init()/trstat_write() to include
1170 * the copycnt in the statistics record it may write.
1173 trstat_init(pp, file, job_dfcnt);
1174 for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) {
1176 if (i + amt > stb.st_size)
1177 amt = stb.st_size - i;
1178 if (sizerr == 0 && read(sfd, buf, amt) != amt)
1180 if (write(pfd, buf, amt) != amt) {
1187 syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
1188 /* tell recvjob to ignore this file */
1189 (void) write(pfd, "\1", 1);
1193 if (write(pfd, "", 1) != 1 || response(pp)) {
1198 trstat_write(pp, TR_SENDING, stb.st_size, logname,
1199 pp->remote_host, origin_host);
1201 * Usually we only need to send one copy of a datafile,
1202 * because the control-file will simply print the same
1203 * file multiple times. However, some printers ignore
1204 * the control file, and simply print each data file as
1205 * it arrives. For such "remote hosts", we need to
1206 * transfer the same data file multiple times. Such a
1207 * a host is indicated by adding 'rc' to the printcap
1209 * XXX - Right now this ONLY works for remote hosts which
1210 * do ignore the name of the data file, because
1211 * this sends the file multiple times with slight
1212 * changes to the filename. To do this right would
1213 * require that we also rewrite the control file
1214 * to match those filenames.
1216 if (pp->resend_copies && (copycnt < copyreq)) {
1217 lseek(sfd, 0, SEEK_SET);
1227 * If tfd is set, then it is the same value as sfd, and
1228 * therefore it is already closed at this point. All
1229 * we need to do is remove the temporary file.
1238 * Some print servers send the control-file first, and then start sending the
1239 * matching data file(s). That is not the correct order. If some queue is
1240 * already printing an active job, then when that job is finished the queue
1241 * may proceed to the control file of any incoming print job. This turns
1242 * into a race between the process which is receiving the data file, and the
1243 * process which is actively printing the very same file. When the remote
1244 * server sends files in the wrong order, it is even possible that a queue
1245 * will start to print a data file before the file has been created!
1247 * So before we start to print() or send() a data file, we call this routine
1248 * to make sure the data file is not still changing in size. Note that this
1249 * problem will only happen for jobs arriving from a remote host, and that
1250 * the process which has decided to print this job (and is thus making this
1251 * check) is *not* the process which is receiving the job.
1253 * A second benefit of this is that any incoming job is guaranteed to appear
1254 * in a queue listing for at least a few seconds after it has arrived. Some
1255 * lpr implementations get confused if they send a job and it disappears
1256 * from the queue before they can check on it.
1258 #define MAXWAIT_ARRIVE 16 /* max to wait for the file to *exist* */
1259 #define MAXWAIT_4DATA (20*60) /* max to wait for it to stop changing */
1260 #define MINWAIT_4DATA 4 /* This value must be >= 1 */
1261 #define DEBUG_MINWAIT 1
1263 wait4data(struct printer *pp, const char *dfile)
1269 time_t amtslept, cur_time, prev_mtime;
1272 /* Skip these checks if the print job is from the local host. */
1273 dlen = strlen(dfile);
1274 hlen = strlen(local_host);
1276 cp = dfile + dlen - hlen;
1277 if (strcmp(cp, local_host) == 0)
1282 * If this data file does not exist, then wait up to MAXWAIT_ARRIVE
1283 * seconds for it to arrive.
1286 statres = stat(dfile, &statdf);
1287 while (statres < 0 && amtslept < MAXWAIT_ARRIVE) {
1289 pstatus(pp, "Waiting for data file from remote host");
1290 amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA);
1291 statres = stat(dfile, &statdf);
1294 /* The file still does not exist, so just give up on it. */
1295 syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s",
1296 pp->printer, dfile);
1301 * The file exists, so keep waiting until the data file has not
1302 * changed for some reasonable amount of time. Extra care is
1303 * taken when computing wait-times, just in case there are data
1304 * files with a last-modify time in the future. While that is
1305 * very unlikely to happen, it can happen when the system has
1306 * a flakey time-of-day clock.
1308 prev_mtime = statdf.st_mtime;
1309 cur_time = time(NULL);
1310 if (statdf.st_mtime >= cur_time - MINWAIT_4DATA) {
1311 if (statdf.st_mtime >= cur_time) /* some TOD oddity */
1312 sleepreq = MINWAIT_4DATA;
1314 sleepreq = cur_time - statdf.st_mtime;
1316 pstatus(pp, "Waiting for data file from remote host");
1317 amtslept += sleepreq - sleep(sleepreq);
1318 statres = stat(dfile, &statdf);
1320 sleepreq = MINWAIT_4DATA;
1321 while (statres == 0 && amtslept < MAXWAIT_4DATA) {
1322 if (statdf.st_mtime == prev_mtime)
1324 prev_mtime = statdf.st_mtime;
1325 amtslept += sleepreq - sleep(sleepreq);
1326 statres = stat(dfile, &statdf);
1330 syslog(LOG_WARNING, "%s: %s disappeared during wait4data()",
1331 pp->printer, dfile);
1332 else if (amtslept > MAXWAIT_4DATA)
1334 "%s: %s still changing after %lu secs in wait4data()",
1335 pp->printer, dfile, (unsigned long)amtslept);
1337 else if (amtslept > MINWAIT_4DATA)
1338 syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)",
1339 pp->printer, (unsigned long)amtslept, dfile);
1342 #undef MAXWAIT_ARRIVE
1343 #undef MAXWAIT_4DATA
1344 #undef MINWAIT_4DATA
1347 * This routine is called to execute one of the filters as was
1348 * specified in a printcap entry. While the child-process will read
1349 * all of 'infd', it is up to the caller to close that file descriptor
1350 * in the parent process.
1353 execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
1356 int errfd, retcode, wstatus;
1358 char buf[BUFSIZ], *slash;
1360 fpid = dofork(pp, DORETURN);
1363 * This is the parent process, which just waits for the child
1364 * to complete and then returns the result. Note that it is
1365 * the child process which reads the input stream.
1370 while ((wpid = wait(&wstatus)) > 0 &&
1376 "%s: after execv(%s), wait() returned: %m",
1377 pp->printer, f_cmd);
1379 retcode = WEXITSTATUS(wstatus);
1383 * Copy everything the filter wrote to stderr from our
1384 * temporary errors file to the "lf=" logfile.
1386 errfp = fopen(tempstderr, "r");
1388 while (fgets(buf, sizeof(buf), errfp))
1397 * This is the child process, which is the one that executes the
1401 * If the first parameter has any slashes in it, then change it
1402 * to point to the first character after the last slash.
1404 slash = strrchr(f_av[0], '/');
1406 f_av[0] = slash + 1;
1408 * XXX - in the future, this should setup an explicit list of
1409 * environment variables and use execve()!
1413 * Setup stdin, stdout, and stderr as we want them when the filter
1414 * is running. Stderr is setup so it points to a temporary errors
1415 * file, and the parent process will copy that temporary file to
1416 * the real logfile after the filter completes.
1418 dup2(infd, STDIN_FILENO);
1419 dup2(outfd, STDOUT_FILENO);
1420 errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
1422 dup2(errfd, STDERR_FILENO);
1426 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
1432 * Check to make sure there have been no errors and that both programs
1433 * are in sync with eachother.
1434 * Return non-zero if the connection was lost.
1437 response(const struct printer *pp)
1441 if (read(pfd, &resp, 1) != 1) {
1442 syslog(LOG_INFO, "%s: lost connection", pp->printer);
1449 * Banner printing stuff
1452 banner(struct printer *pp, char *name1, char *name2)
1457 if (!pp->no_formfeed && !pp->tof)
1458 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1459 if (pp->short_banner) { /* short banner only */
1461 (void) write(ofd, class, strlen(class));
1462 (void) write(ofd, ":", 1);
1464 (void) write(ofd, name1, strlen(name1));
1465 (void) write(ofd, " Job: ", 7);
1466 (void) write(ofd, name2, strlen(name2));
1467 (void) write(ofd, " Date: ", 8);
1468 (void) write(ofd, ctime(&tvec), 24);
1469 (void) write(ofd, "\n", 1);
1470 } else { /* normal banner */
1471 (void) write(ofd, "\n\n\n", 3);
1472 scan_out(pp, ofd, name1, '\0');
1473 (void) write(ofd, "\n\n", 2);
1474 scan_out(pp, ofd, name2, '\0');
1476 (void) write(ofd,"\n\n\n",3);
1477 scan_out(pp, ofd, class, '\0');
1479 (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15);
1480 (void) write(ofd, name2, strlen(name2));
1481 (void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
1482 (void) write(ofd, ctime(&tvec), 24);
1483 (void) write(ofd, "\n", 1);
1485 if (!pp->no_formfeed)
1486 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1491 scnline(int key, char *p, int c)
1493 register int scnwidth;
1495 for (scnwidth = WIDTH; --scnwidth;) {
1497 *p++ = key & 0200 ? c : BACKGND;
1502 #define TRC(q) (((q)-' ')&0177)
1505 scan_out(struct printer *pp, int scfd, char *scsp, int dlm)
1507 register char *strp;
1508 register int nchrs, j;
1509 char outbuf[LINELEN+1], *sp, c, cc;
1512 for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
1515 for (nchrs = 0; ; ) {
1516 d = dropit(c = TRC(cc = *sp++));
1517 if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
1518 for (j = WIDTH; --j;)
1521 strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
1522 if (*sp == dlm || *sp == '\0' ||
1523 nchrs++ >= pp->page_width/(WIDTH+1)-1)
1528 while (*--strp == BACKGND && strp >= outbuf)
1532 (void) write(scfd, outbuf, strp-outbuf);
1558 * tell people about job completion
1561 sendmail(struct printer *pp, char *userid, int bombed)
1565 register const char *cp;
1570 if ((s = dofork(pp, DORETURN)) == 0) { /* child */
1571 dup2(p[0], STDIN_FILENO);
1574 if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
1577 cp = _PATH_SENDMAIL;
1578 execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
1580 } else if (s > 0) { /* parent */
1581 dup2(p[1], STDOUT_FILENO);
1582 printf("To: %s@%s\n", userid, origin_host);
1583 printf("Subject: %s printer job \"%s\"\n", pp->printer,
1584 *jobname ? jobname : "<unknown>");
1585 printf("Reply-To: root@%s\n\n", local_host);
1586 printf("Your printer job ");
1588 printf("(%s) ", jobname);
1593 printf("\ncompleted successfully\n");
1598 printf("\ncould not be printed\n");
1602 printf("\ncould not be printed without an account on %s\n",
1607 if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
1608 || (fp = fopen(tempstderr, "r")) == NULL) {
1609 printf("\nhad some errors and may not have printed\n");
1612 printf("\nhad the following errors and may not have printed:\n");
1613 while ((i = getc(fp)) != EOF)
1619 printf("\nwas not printed because it was not linked to the original file\n");
1622 (void) close(STDOUT_FILENO);
1624 syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
1630 syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
1631 userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
1635 * dofork - fork with retries on failure
1638 dofork(const struct printer *pp, int action)
1645 if (daemon_uname == NULL) {
1646 pwd = getpwuid(pp->daemon_user);
1648 syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
1649 pp->printer, pp->daemon_user);
1652 daemon_uname = strdup(pwd->pw_name);
1653 daemon_defgid = pwd->pw_gid;
1656 for (i = 0; i < 20; i++) {
1659 sleep((unsigned)(i*i));
1663 * Child should run as daemon instead of root
1667 fail = initgroups(daemon_uname, daemon_defgid);
1669 syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
1670 pp->printer, daemon_uname, daemon_defgid);
1673 fail = setgid(daemon_defgid);
1675 syslog(LOG_ERR, "%s: setgid(%u): %m",
1676 pp->printer, daemon_defgid);
1679 fail = setuid(pp->daemon_user);
1681 syslog(LOG_ERR, "%s: setuid(%ld): %m",
1682 pp->printer, pp->daemon_user);
1690 * An error occurred. If the error is in the child process, then
1691 * this routine MUST always exit(). DORETURN only effects how
1692 * errors should be handled in the parent process.
1696 syslog(LOG_ERR, "%s: dofork(): aborting child process...",
1700 syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);
1702 sleep(1); /* throttle errors, as a safety measure */
1707 syslog(LOG_ERR, "bad action (%d) to dofork", action);
1716 * Kill child processes to abort current job.
1719 abortpr(int signo __unused)
1722 (void) unlink(tempstderr);
1725 kill(of_pid, SIGCONT);
1726 while (wait(NULL) > 0)
1728 if (of_pid > 0 && tfd != -1)
1734 init(struct printer *pp)
1738 sprintf(&width[2], "%ld", pp->page_width);
1739 sprintf(&length[2], "%ld", pp->page_length);
1740 sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
1741 sprintf(&pxlength[2], "%ld", pp->page_plength);
1742 if ((s = checkremote(pp)) != NULL) {
1743 syslog(LOG_WARNING, "%s", s);
1749 startprinting(const char *printer)
1751 struct printer myprinter, *pp = &myprinter;
1755 status = getprintcap(printer, pp);
1758 syslog(LOG_ERR, "can't open printer description file: %m");
1760 case PCAPERR_NOTFOUND:
1761 syslog(LOG_ERR, "unknown printer: %s", printer);
1763 case PCAPERR_TCLOOP:
1764 fatal(pp, "potential reference loop detected in printcap file");
1772 * Acquire line printer or remote connection.
1775 openpr(const struct printer *pp)
1783 * Lpd does support the setting of 'of=' filters for
1784 * jobs going to remote machines, but that does not
1785 * have the same meaning as 'of=' does when handling
1786 * local print queues. For remote machines, all 'of='
1787 * filter processing is handled in sendfile(), and that
1788 * does not use these global "output filter" variables.
1793 } else if (*pp->lp) {
1794 if (strchr(pp->lp, '@') != NULL)
1799 syslog(LOG_ERR, "%s: no line printer device or host name",
1805 * Start up an output filter, if needed.
1807 if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) {
1810 strcpy(tfile, TFILENAME);
1811 tfd = mkstemp(tfile);
1813 if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */
1814 dup2(p[0], STDIN_FILENO); /* pipe is std in */
1815 /* tfile/printer is stdout */
1816 dup2(pp->remote ? tfd : pfd, STDOUT_FILENO);
1819 if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
1820 cp = pp->filters[LPF_OUTPUT];
1823 execl(pp->filters[LPF_OUTPUT], cp, width, length,
1825 syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer,
1826 pp->filters[LPF_OUTPUT]);
1829 (void) close(p[0]); /* close input side */
1830 ofd = p[1]; /* use pipe for output */
1838 * Printer connected directly to the network
1839 * or to a terminal server on the net
1842 opennet(const struct printer *pp)
1848 void (*savealrm)(int);
1850 port = strtoul(pp->lp, &ep, 0);
1851 if (*ep != '@' || port > 65535) {
1852 syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
1858 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1860 savealrm = signal(SIGALRM, alarmhandler);
1861 alarm(pp->conn_timeout);
1862 pfd = getport(pp, ep, port);
1864 (void)signal(SIGALRM, savealrm);
1865 if (pfd < 0 && errno == ECONNREFUSED)
1867 else if (pfd >= 0) {
1869 * need to delay a bit for rs232 lines
1870 * to stabilize in case printer is
1871 * connected via a terminal server
1878 pstatus(pp, "waiting for %s to come up",
1882 "waiting for access to printer on %s",
1887 pstatus(pp, "sending to %s port %lu", ep, port);
1891 * Printer is connected to an RS232 port on this host
1894 opentty(const struct printer *pp)
1898 for (i = 1; ; i = i < 32 ? i << 1 : i) {
1899 pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
1904 if (errno == ENOENT) {
1905 syslog(LOG_ERR, "%s: %m", pp->lp);
1910 "waiting for %s to become ready (offline?)",
1916 pstatus(pp, "%s is ready and printing", pp->printer);
1920 * Printer is on a remote host
1923 openrem(const struct printer *pp)
1927 void (*savealrm)(int);
1929 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1931 savealrm = signal(SIGALRM, alarmhandler);
1932 alarm(pp->conn_timeout);
1933 pfd = getport(pp, pp->remote_host, 0);
1935 (void)signal(SIGALRM, savealrm);
1937 if ((writel(pfd, "\2", pp->remote_queue, "\n",
1939 == 2 + strlen(pp->remote_queue))
1940 && (resp = response(pp)) == 0)
1946 pstatus(pp, "waiting for %s to come up",
1950 "waiting for queue to be enabled on %s",
1957 pstatus(pp, "sending to %s", pp->remote_host);
1964 setty(const struct printer *pp)
1966 struct termios ttybuf;
1968 if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
1969 syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
1972 if (tcgetattr(pfd, &ttybuf) < 0) {
1973 syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
1976 if (pp->baud_rate > 0)
1977 cfsetspeed(&ttybuf, pp->baud_rate);
1979 char *s = strdup(pp->mode_set), *tmp;
1981 while ((tmp = strsep(&s, ",")) != NULL) {
1982 (void) msearch(tmp, &ttybuf);
1985 if (pp->mode_set != 0 || pp->baud_rate > 0) {
1986 if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
1987 syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
1995 pstatus(const struct printer *pp, const char *msg, ...)
2003 fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
2005 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
2010 vasprintf(&buf, msg, ap);
2012 writel(fd, buf, "\n", (char *)0);
2018 alarmhandler(int signo __unused)
2020 /* the signal is ignored */
2021 /* (the '__unused' is just to avoid a compile-time warning) */