2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95";
43 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
44 __FBSDID("$FreeBSD$");
47 * printjob -- print jobs in the queue.
49 * NOTE: the lock file is used to pass information to lpq and lprm.
50 * it does not need to be removed because file locks are dynamic.
53 #include <sys/param.h>
56 #include <sys/types.h>
70 #include <sys/ioctl.h>
75 #include "pathnames.h"
78 #define DORETURN 0 /* dofork should return "can't fork" error */
79 #define DOABORT 1 /* dofork should just die if fork() fails */
82 * The buffer size to use when reading/writing spool files.
84 #define SPL_BUFSIZ BUFSIZ
97 static dev_t fdev; /* device of file pointed to by symlink */
98 static ino_t fino; /* inode of file pointed to by symlink */
99 static FILE *cfp; /* control file */
100 static pid_t of_pid; /* process id of output filter, if any */
101 static int child; /* id of any filters */
102 static int job_dfcnt; /* count of datafiles in current user job */
103 static int lfd; /* lock file descriptor */
104 static int ofd; /* output filter file descriptor */
105 static int tfd = -1; /* output filter temp file output */
106 static int pfd; /* prstatic inter file descriptor */
107 static int prchild; /* id of pr process */
108 static char title[80]; /* ``pr'' title */
109 static char locale[80]; /* ``pr'' locale */
111 /* these two are set from pp->daemon_user, but only if they are needed */
112 static char *daemon_uname; /* set from pwd->pw_name */
113 static int daemon_defgid;
115 static char class[32]; /* classification field */
116 static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */
117 /* indentation size in static characters */
118 static char indent[10] = "-i0";
119 static char jobname[100]; /* job or file name */
120 static char length[10] = "-l"; /* page length in lines */
121 static char logname[32]; /* user's login name */
122 static char pxlength[10] = "-y"; /* page length in pixels */
123 static char pxwidth[10] = "-x"; /* page width in pixels */
124 /* tempstderr is the filename used to catch stderr from exec-ing filters */
125 static char tempstderr[] = "errs.XXXXXXX";
126 static char width[10] = "-w"; /* page width in static characters */
127 #define TFILENAME "fltXXXXXX"
128 static char tfile[] = TFILENAME; /* file name for filter output */
130 static void abortpr(int _signo);
131 static void alarmhandler(int _signo);
132 static void banner(struct printer *_pp, char *_name1, char *_name2);
133 static int dofork(const struct printer *_pp, int _action);
134 static int dropit(int _c);
135 static int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
136 int _infd, int _outfd);
137 static void init(struct printer *_pp);
138 static void openpr(const struct printer *_pp);
139 static void opennet(const struct printer *_pp);
140 static void opentty(const struct printer *_pp);
141 static void openrem(const struct printer *pp);
142 static int print(struct printer *_pp, int _format, char *_file);
143 static int printit(struct printer *_pp, char *_file);
144 static void pstatus(const struct printer *_pp, const char *_msg, ...)
146 static char response(const struct printer *_pp);
147 static void scan_out(struct printer *_pp, int _scfd, char *_scsp,
149 static char *scnline(int _key, char *_p, int _c);
150 static int sendfile(struct printer *_pp, int _type, char *_file,
151 char _format, int _copyreq);
152 static int sendit(struct printer *_pp, char *_file);
153 static void sendmail(struct printer *_pp, char *_userid, int _bombed);
154 static void setty(const struct printer *_pp);
155 static void wait4data(struct printer *_pp, const char *_dfile);
158 printjob(struct printer *pp)
161 register struct jobqueue *q, **qp;
162 struct jobqueue **queue;
163 register int i, nitems;
166 int errcnt, jobcount, statok, tempfd;
169 init(pp); /* set up capabilities */
170 (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */
171 (void) close(STDERR_FILENO); /* set up log file */
172 if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
173 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
175 (void) open(_PATH_DEVNULL, O_WRONLY);
177 if(setgid(getegid()) != 0) err(1, "setgid() failed");
178 printpid = getpid(); /* for use with lprm */
179 setpgid((pid_t)0, printpid);
182 * At initial lpd startup, printjob may be called with various
183 * signal handlers in effect. After that initial startup, any
184 * calls to printjob will have a *different* set of signal-handlers
185 * in effect. Make sure all handlers are the ones we want.
187 signal(SIGCHLD, SIG_DFL);
188 signal(SIGHUP, abortpr);
189 signal(SIGINT, abortpr);
190 signal(SIGQUIT, abortpr);
191 signal(SIGTERM, abortpr);
194 * uses short form file names
196 if (chdir(pp->spool_dir) < 0) {
197 syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer,
201 statok = stat(pp->lock_file, &stb);
202 if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS))
203 exit(0); /* printing disabled */
205 lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
208 if (errno == EWOULDBLOCK) /* active daemon present */
210 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
215 * If the initial call to stat() failed, then lock_file will have
216 * been created by open(). Update &stb to match that new file.
219 statok = stat(pp->lock_file, &stb);
220 /* turn off non-blocking mode (was turned on for lock effects only) */
221 if (fcntl(lfd, F_SETFL, 0) < 0) {
222 syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer,
228 * write process id for others to know
230 sprintf(line, "%u\n", printpid);
231 pidoff = i = strlen(line);
232 if (write(lfd, line, i) != i) {
233 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
238 * search the spool directory for work and sort by queue order.
240 if ((nitems = getq(pp, &queue)) < 0) {
241 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
245 if (nitems == 0) /* no work to do */
247 if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
248 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
249 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
253 /* create a file which will be used to hold stderr from filters */
254 if ((tempfd = mkstemp(tempstderr)) == -1) {
255 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
259 if ((i = fchmod(tempfd, 0664)) == -1) {
260 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
264 /* lpd doesn't need it to be open, it just needs it to exist */
267 openpr(pp); /* open printer or remote */
270 * we found something to do now do it --
271 * write the name of the current control file into the lock file
272 * so the spool queue program can tell what we're working on
274 for (qp = queue; nitems--; free((char *) q)) {
276 if (stat(q->job_cfname, &stb) < 0)
280 (void) lseek(lfd, pidoff, 0);
281 (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
283 if (write(lfd, line, i) != i)
284 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
287 i = printit(pp, q->job_cfname);
289 i = sendit(pp, q->job_cfname);
291 * Check to see if we are supposed to stop printing or
292 * if we are to rebuild the queue.
294 if (fstat(lfd, &stb) == 0) {
295 /* stop printing before starting next job? */
296 if (stb.st_mode & LFM_PRINT_DIS)
298 /* rebuild queue (after lpc topq) */
299 if (stb.st_mode & LFM_RESET_QUE) {
300 for (free(q); nitems--; free(q))
302 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
305 "%s: fchmod(%s): %m",
306 pp->printer, pp->lock_file);
310 if (i == OK) /* all files of this job printed */
312 else if (i == REPRINT && ++errcnt < 5) {
313 /* try reprinting the job */
314 syslog(LOG_INFO, "restarting %s", pp->printer);
316 kill(of_pid, SIGCONT); /* to be sure */
318 while ((i = wait(NULL)) > 0 && i != of_pid)
321 syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
322 pp->printer, of_pid);
325 (void) close(pfd); /* close printer */
326 if (ftruncate(lfd, pidoff) < 0)
327 syslog(LOG_WARNING, "%s: ftruncate(%s): %m",
328 pp->printer, pp->lock_file);
329 openpr(pp); /* try to reopen printer */
332 syslog(LOG_WARNING, "%s: job could not be %s (%s)",
334 pp->remote ? "sent to remote host" : "printed",
337 /* ensure we don't attempt this job again */
338 (void) unlink(q->job_cfname);
339 q->job_cfname[0] = 'd';
340 (void) unlink(q->job_cfname);
342 sendmail(pp, logname, FATALERR);
348 * search the spool directory for more work.
350 if ((nitems = getq(pp, &queue)) < 0) {
351 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
355 if (nitems == 0) { /* no more work to do */
357 if (jobcount > 0) { /* jobs actually printed */
358 if (!pp->no_formfeed && !pp->tof)
359 (void) write(ofd, pp->form_feed,
360 strlen(pp->form_feed));
361 if (pp->trailer != NULL) /* output trailer */
362 (void) write(ofd, pp->trailer,
363 strlen(pp->trailer));
367 (void) unlink(tempstderr);
373 char fonts[4][50]; /* fonts for troff */
375 char ifonts[4][40] = {
383 * The remaining part is the reading of the control file (cf)
384 * and performing the various actions.
387 printit(struct printer *pp, char *file)
391 int bombed, didignorehdr;
396 * open control file; ignore if no longer there.
398 if ((cfp = fopen(file, "r")) == NULL) {
399 syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file);
405 for (i = 0; i < 4; i++)
406 strcpy(fonts[i], ifonts[i]);
407 sprintf(&width[2], "%ld", pp->page_width);
408 strcpy(indent+2, "0");
410 /* initialize job-specific count of datafiles processed */
414 * read the control file for work to do
416 * file format -- first character in the line is a command
417 * rest of the line is the argument.
418 * valid commands are:
420 * S -- "stat info" for symbolic link protection
421 * J -- "job name" on banner page
422 * C -- "class name" on banner page
423 * L -- "literal" user's name to print on banner
424 * T -- "title" for pr
425 * H -- "host name" of machine where lpr was done
426 * P -- "person" user's login name
427 * I -- "indent" amount to indent output
428 * R -- laser dpi "resolution"
429 * f -- "file name" name of text file to print
430 * l -- "file name" text file with control chars
431 * o -- "file name" postscript file, according to
432 * the RFC. Here it is treated like an 'f'.
433 * p -- "file name" text file to print with pr(1)
434 * t -- "file name" troff(1) file to print
435 * n -- "file name" ditroff(1) file to print
436 * d -- "file name" dvi file to print
437 * g -- "file name" plot(1G) file to print
438 * v -- "file name" plain raster file to print
439 * c -- "file name" cifplot file to print
440 * 1 -- "R font file" for troff
441 * 2 -- "I font file" for troff
442 * 3 -- "B font file" for troff
443 * 4 -- "S font file" for troff
444 * N -- "name" of file (used by lpq)
445 * U -- "unlink" name of file to remove
446 * (after we print it. (Pass 2 only)).
447 * M -- "mail" to user when done printing
448 * Z -- "locale" for pr
450 * getline reads a line and expands tabs to blanks
458 strlcpy(origin_host, line + 1, sizeof(origin_host));
459 if (class[0] == '\0') {
460 strlcpy(class, line+1, sizeof(class));
465 strlcpy(logname, line + 1, sizeof(logname));
466 if (pp->restricted) { /* restricted */
467 if (getpwnam(logname) == NULL) {
469 sendmail(pp, line+1, bombed);
478 while (*cp >= '0' && *cp <= '9')
479 i = i * 10 + (*cp++ - '0');
483 while (*cp >= '0' && *cp <= '9')
484 i = i * 10 + (*cp++ - '0');
489 if (line[1] != '\0') {
490 strlcpy(jobname, line + 1, sizeof(jobname));
492 strcpy(jobname, " ");
497 strlcpy(class, line + 1, sizeof(class));
498 else if (class[0] == '\0') {
499 /* XXX - why call gethostname instead of
500 * just strlcpy'ing local_host? */
501 gethostname(class, sizeof(class));
502 class[sizeof(class) - 1] = '\0';
506 case 'T': /* header title for pr */
507 strlcpy(title, line + 1, sizeof(title));
510 case 'L': /* identification line */
511 if (!pp->no_header && !pp->header_last)
512 banner(pp, line+1, jobname);
515 case '1': /* troff fonts */
519 if (line[1] != '\0') {
520 strlcpy(fonts[line[0]-'1'], line + 1,
525 case 'W': /* page width */
526 strlcpy(width+2, line + 1, sizeof(width) - 2);
529 case 'I': /* indent amount */
530 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
533 case 'Z': /* locale for pr */
534 strlcpy(locale, line + 1, sizeof(locale));
537 default: /* some file to print */
538 /* only lowercase cmd-codes include a file-to-print */
539 if ((line[0] < 'a') || (line[0] > 'z')) {
540 /* ignore any other lines */
544 syslog(LOG_INFO, "%s: in %s :",
548 syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
549 pp->printer, line[0], &line[1]);
552 i = print(pp, line[0], line+1);
564 sendmail(pp, logname, bombed);
582 case 'L': /* identification line */
583 if (!pp->no_header && pp->header_last)
584 banner(pp, line+1, jobname);
588 if (bombed < NOACCT) /* already sent if >= NOACCT */
589 sendmail(pp, line+1, bombed);
593 if (strchr(line+1, '/'))
595 (void) unlink(line+1);
598 * clean-up in case another control file exists
602 return (bombed == OK ? OK : ERROR);
607 * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
608 * Return -1 if a non-recoverable error occurred,
609 * 2 if the filter detected some errors (but printed the job anyway),
610 * 1 if we should try to reprint this job and
612 * Note: all filters take stdin as the file, stdout as the printer,
613 * stderr as the log file, and must not ignore SIGINT.
616 print(struct printer *pp, int format, char *file)
622 char *av[15], buf[SPL_BUFSIZ];
624 int p[2], retcode, stopped, wstatus, wstatus_set;
627 /* Make sure the entire data file has arrived. */
630 if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
631 syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
632 pp->printer, file, format);
636 * Check to see if data file is a symbolic link. If so, it should
637 * still point to the same file or someone is trying to print
638 * something he shouldn't.
640 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
641 (stb.st_dev != fdev || stb.st_ino != fino))
644 job_dfcnt++; /* increment datafile counter for this job */
645 stopped = 0; /* output filter is not stopped */
647 /* everything seems OK, start it up */
648 if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
649 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
652 if (pp->filters[LPF_INPUT] == NULL
653 && (format == 'f' || format == 'l' || format == 'o')) {
655 while ((n = read(fi, buf, SPL_BUFSIZ)) > 0)
656 if (write(ofd, buf, n) != n) {
664 case 'p': /* print file using 'pr' */
665 if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */
672 av[i++] = *title ? title : " ";
674 av[i++] = *locale ? locale : "C";
681 if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */
682 dup2(fi, STDIN_FILENO); /* file is stdin */
683 dup2(p[1], STDOUT_FILENO); /* pipe is stdout */
686 execl(_PATH_PR, "pr", width, length,
687 "-h", *title ? title : " ",
688 "-L", *locale ? locale : "C",
690 syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
693 (void) close(p[1]); /* close output side */
700 fi = p[0]; /* use pipe for input */
701 case 'f': /* print plain text file */
702 prog = pp->filters[LPF_INPUT];
708 case 'o': /* print postscript file */
710 * Treat this as a "plain file with control characters", and
711 * assume the standard LPF_INPUT filter will recognize that
712 * the data is postscript and know what to do with it. These
713 * 'o'-file requests could come from MacOS 10.1 systems.
714 * (later versions of MacOS 10 will explicitly use 'l')
715 * A postscript file can contain binary data, which is why 'l'
716 * is somewhat more appropriate than 'f'.
719 case 'l': /* like 'f' but pass control characters */
720 prog = pp->filters[LPF_INPUT];
727 case 'r': /* print a fortran text file */
728 prog = pp->filters[LPF_FORTRAN];
733 case 't': /* print troff output */
734 case 'n': /* print ditroff output */
735 case 'd': /* print tex output */
736 (void) unlink(".railmag");
737 if ((fo = creat(".railmag", FILMOD)) < 0) {
738 syslog(LOG_ERR, "%s: cannot create .railmag",
740 (void) unlink(".railmag");
742 for (n = 0; n < 4; n++) {
743 if (fonts[n][0] != '/')
744 (void) write(fo, _PATH_VFONT,
745 sizeof(_PATH_VFONT) - 1);
746 (void) write(fo, fonts[n], strlen(fonts[n]));
747 (void) write(fo, "\n", 1);
751 prog = (format == 't') ? pp->filters[LPF_TROFF]
752 : ((format == 'n') ? pp->filters[LPF_DITROFF]
753 : pp->filters[LPF_DVI]);
758 case 'c': /* print cifplot output */
759 prog = pp->filters[LPF_CIFPLOT];
764 case 'g': /* print plot(1G) output */
765 prog = pp->filters[LPF_GRAPH];
770 case 'v': /* print raster output */
771 prog = pp->filters[LPF_RASTER];
778 syslog(LOG_ERR, "%s: illegal format character '%c'",
779 pp->printer, format);
785 "%s: no filter found in printcap for format character '%c'",
786 pp->printer, format);
789 if ((av[0] = strrchr(prog, '/')) != NULL)
796 av[n++] = origin_host;
797 av[n++] = pp->acct_file;
800 if (of_pid > 0) { /* stop output filter */
801 write(ofd, "\031\1", 2);
803 wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid)
807 "%s: after stopping 'of', wait3() returned: %m",
809 else if (!WIFSTOPPED(wstatus)) {
811 syslog(LOG_WARNING, "%s: output filter died "
812 "(pid=%d retcode=%d termsig=%d)",
813 pp->printer, of_pid, WEXITSTATUS(wstatus),
820 if ((child = dofork(pp, DORETURN)) == 0) { /* child */
821 dup2(fi, STDIN_FILENO);
822 dup2(fo, STDOUT_FILENO);
823 /* setup stderr for the filter (child process)
824 * so it goes to our temporary errors file */
825 n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
827 dup2(n, STDERR_FILENO);
831 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
840 while ((wpid = wait(&wstatus)) > 0 && wpid != child)
845 "%s: after execv(%s), wait() returned: %m",
849 retcode = WEXITSTATUS(wstatus);
854 if (stopped) { /* restart output filter */
855 if (kill(of_pid, SIGCONT) < 0) {
856 syslog(LOG_ERR, "cannot restart output filter");
862 /* Copy the filter's output to "lf" logfile */
863 if ((fp = fopen(tempstderr, "r"))) {
864 while (fgets(buf, sizeof(buf), fp))
869 if (wstatus_set && !WIFEXITED(wstatus)) {
870 syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
871 pp->printer, format, WTERMSIG(wstatus));
883 syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
884 pp->printer, format, retcode);
890 * Send the daemon control file (cf) and any data files.
891 * Return -1 if a non-recoverable error occurred, 1 if a recoverable error and
895 sendit(struct printer *pp, char *file)
897 int dfcopies, err, i;
898 char *cp, last[sizeof(line)];
903 if ((cfp = fopen(file, "r")) == NULL)
906 /* initialize job-specific count of datafiles processed */
910 * read the control file for work to do
912 * file format -- first character in the line is a command
913 * rest of the line is the argument.
914 * commands of interest are:
916 * a-z -- "file name" name of file to print
917 * U -- "unlink" name of file to remove
918 * (after we print it. (Pass 2 only)).
925 while (getline(cfp)) {
927 if (line[0] == 'S') {
930 while (*cp >= '0' && *cp <= '9')
931 i = i * 10 + (*cp++ - '0');
935 while (*cp >= '0' && *cp <= '9')
936 i = i * 10 + (*cp++ - '0');
938 } else if (line[0] == 'H') {
939 strlcpy(origin_host, line + 1, sizeof(origin_host));
940 if (class[0] == '\0') {
941 strlcpy(class, line + 1, sizeof(class));
943 } else if (line[0] == 'P') {
944 strlcpy(logname, line + 1, sizeof(logname));
945 if (pp->restricted) { /* restricted */
946 if (getpwnam(logname) == NULL) {
947 sendmail(pp, line+1, NOACCT);
952 } else if (line[0] == 'I') {
953 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
954 } else if (line[0] >= 'a' && line[0] <= 'z') {
957 while ((i = getline(cfp)) != 0) {
958 if (strcmp(last, line) != 0)
962 switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
971 sendmail(pp, logname, ACCESS);
978 if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
987 if (line[0] == 'U' && !strchr(line+1, '/'))
988 (void) unlink(line+1);
990 * clean-up in case another control file exists
998 * Send a data file to the remote machine and spool it.
999 * Return positive if we should try resending.
1002 sendfile(struct printer *pp, int type, char *file, char format, int copyreq)
1006 char *av[15], *filtcmd;
1007 char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
1008 int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;
1010 /* Make sure the entire data file has arrived. */
1011 wait4data(pp, file);
1013 statrc = lstat(file, &stb);
1015 syslog(LOG_ERR, "%s: error from lstat(%s): %m",
1019 sfd = open(file, O_RDONLY);
1021 syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
1026 * Check to see if data file is a symbolic link. If so, it should
1027 * still point to the same file or someone is trying to print something
1030 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
1031 (stb.st_dev != fdev || stb.st_ino != fino)) {
1036 /* Everything seems OK for reading the file, now to send it */
1042 * Type == 3 means this is a datafile, not a control file.
1043 * Increment the counter of data-files in this job, and
1044 * then check for input or output filters (which are only
1045 * applied to datafiles, not control files).
1050 * Note that here we are filtering datafiles, one at a time,
1051 * as they are sent to the remote machine. Here, the *only*
1052 * difference between an input filter (`if=') and an output
1053 * filter (`of=') is the argument list that the filter is
1054 * started up with. Here, the output filter is executed
1055 * for each individual file as it is sent. This is not the
1056 * same as local print queues, where the output filter is
1057 * started up once, and then all jobs are passed thru that
1058 * single invocation of the output filter.
1060 * Also note that a queue for a remote-machine can have an
1061 * input filter or an output filter, but not both.
1063 if (pp->filters[LPF_INPUT]) {
1064 filtcmd = pp->filters[LPF_INPUT];
1067 strcpy(opt_c, "-c");
1068 strcpy(opt_h, "-h");
1069 strcpy(opt_n, "-n");
1073 av[++narg] = length;
1074 av[++narg] = indent;
1076 av[++narg] = logname;
1078 av[++narg] = origin_host;
1079 av[++narg] = pp->acct_file;
1081 } else if (pp->filters[LPF_OUTPUT]) {
1082 filtcmd = pp->filters[LPF_OUTPUT];
1086 av[++narg] = length;
1092 * If there is an input or output filter, we have to run
1093 * the datafile thru that filter and store the result as
1094 * a temporary spool file, because the protocol requires
1095 * that we send the remote host the file-size before we
1096 * start to send any of the data.
1098 strcpy(tfile, TFILENAME);
1099 tfd = mkstemp(tfile);
1101 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
1106 filtstat = execfilter(pp, filtcmd, av, sfd, tfd);
1108 /* process the return-code from the filter */
1120 "%s: filter '%c' exited (retcode=%d)",
1121 pp->printer, format, filtstat);
1125 statrc = fstat(tfd, &stb); /* to find size of tfile */
1128 "%s: error processing 'if', fstat(%s): %m",
1129 pp->printer, tfile);
1135 lseek(sfd, 0, SEEK_SET);
1143 (void) sprintf(buf, "%c%" PRId64 " %s\n", type, stb.st_size,
1146 (void) sprintf(buf, "%c%" PRId64 " %s_c%d\n", type, stb.st_size,
1149 for (i = 0; ; i++) {
1150 if (write(pfd, buf, amt) != amt ||
1151 (resp = response(pp)) < 0 || resp == '\1') {
1154 } else if (resp == '\0')
1158 "no space on remote; waiting for queue to drain");
1160 syslog(LOG_ALERT, "%s: can't send to %s; queue full",
1161 pp->printer, pp->remote_host);
1165 pstatus(pp, "sending to %s", pp->remote_host);
1167 * XXX - we should change trstat_init()/trstat_write() to include
1168 * the copycnt in the statistics record it may write.
1171 trstat_init(pp, file, job_dfcnt);
1172 for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) {
1174 if (i + amt > stb.st_size)
1175 amt = stb.st_size - i;
1176 if (sizerr == 0 && read(sfd, buf, amt) != amt)
1178 if (write(pfd, buf, amt) != amt) {
1185 syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
1186 /* tell recvjob to ignore this file */
1187 (void) write(pfd, "\1", 1);
1191 if (write(pfd, "", 1) != 1 || response(pp)) {
1196 trstat_write(pp, TR_SENDING, stb.st_size, logname,
1197 pp->remote_host, origin_host);
1199 * Usually we only need to send one copy of a datafile,
1200 * because the control-file will simply print the same
1201 * file multiple times. However, some printers ignore
1202 * the control file, and simply print each data file as
1203 * it arrives. For such "remote hosts", we need to
1204 * transfer the same data file multiple times. Such a
1205 * a host is indicated by adding 'rc' to the printcap
1207 * XXX - Right now this ONLY works for remote hosts which
1208 * do ignore the name of the data file, because
1209 * this sends the file multiple times with slight
1210 * changes to the filename. To do this right would
1211 * require that we also rewrite the control file
1212 * to match those filenames.
1214 if (pp->resend_copies && (copycnt < copyreq)) {
1215 lseek(sfd, 0, SEEK_SET);
1225 * If tfd is set, then it is the same value as sfd, and
1226 * therefore it is already closed at this point. All
1227 * we need to do is remove the temporary file.
1236 * Some print servers send the control-file first, and then start sending the
1237 * matching data file(s). That is not the correct order. If some queue is
1238 * already printing an active job, then when that job is finished the queue
1239 * may proceed to the control file of any incoming print job. This turns
1240 * into a race between the process which is receiving the data file, and the
1241 * process which is actively printing the very same file. When the remote
1242 * server sends files in the wrong order, it is even possible that a queue
1243 * will start to print a data file before the file has been created!
1245 * So before we start to print() or send() a data file, we call this routine
1246 * to make sure the data file is not still changing in size. Note that this
1247 * problem will only happen for jobs arriving from a remote host, and that
1248 * the process which has decided to print this job (and is thus making this
1249 * check) is *not* the process which is receiving the job.
1251 * A second benefit of this is that any incoming job is guaranteed to appear
1252 * in a queue listing for at least a few seconds after it has arrived. Some
1253 * lpr implementations get confused if they send a job and it disappears
1254 * from the queue before they can check on it.
1256 #define MAXWAIT_ARRIVE 16 /* max to wait for the file to *exist* */
1257 #define MAXWAIT_4DATA (20*60) /* max to wait for it to stop changing */
1258 #define MINWAIT_4DATA 4 /* This value must be >= 1 */
1259 #define DEBUG_MINWAIT 1
1261 wait4data(struct printer *pp, const char *dfile)
1267 time_t amtslept, cur_time, prev_mtime;
1270 /* Skip these checks if the print job is from the local host. */
1271 dlen = strlen(dfile);
1272 hlen = strlen(local_host);
1274 cp = dfile + dlen - hlen;
1275 if (strcmp(cp, local_host) == 0)
1280 * If this data file does not exist, then wait up to MAXWAIT_ARRIVE
1281 * seconds for it to arrive.
1284 statres = stat(dfile, &statdf);
1285 while (statres < 0 && amtslept < MAXWAIT_ARRIVE) {
1287 pstatus(pp, "Waiting for data file from remote host");
1288 amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA);
1289 statres = stat(dfile, &statdf);
1292 /* The file still does not exist, so just give up on it. */
1293 syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s",
1294 pp->printer, dfile);
1299 * The file exists, so keep waiting until the data file has not
1300 * changed for some reasonable amount of time. Extra care is
1301 * taken when computing wait-times, just in case there are data
1302 * files with a last-modify time in the future. While that is
1303 * very unlikely to happen, it can happen when the system has
1304 * a flakey time-of-day clock.
1306 prev_mtime = statdf.st_mtime;
1307 cur_time = time(NULL);
1308 if (statdf.st_mtime >= cur_time - MINWAIT_4DATA) {
1309 if (statdf.st_mtime >= cur_time) /* some TOD oddity */
1310 sleepreq = MINWAIT_4DATA;
1312 sleepreq = cur_time - statdf.st_mtime;
1314 pstatus(pp, "Waiting for data file from remote host");
1315 amtslept += sleepreq - sleep(sleepreq);
1316 statres = stat(dfile, &statdf);
1318 sleepreq = MINWAIT_4DATA;
1319 while (statres == 0 && amtslept < MAXWAIT_4DATA) {
1320 if (statdf.st_mtime == prev_mtime)
1322 prev_mtime = statdf.st_mtime;
1323 amtslept += sleepreq - sleep(sleepreq);
1324 statres = stat(dfile, &statdf);
1328 syslog(LOG_WARNING, "%s: %s disappeared during wait4data()",
1329 pp->printer, dfile);
1330 else if (amtslept > MAXWAIT_4DATA)
1332 "%s: %s still changing after %lu secs in wait4data()",
1333 pp->printer, dfile, (unsigned long)amtslept);
1335 else if (amtslept > MINWAIT_4DATA)
1336 syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)",
1337 pp->printer, (unsigned long)amtslept, dfile);
1340 #undef MAXWAIT_ARRIVE
1341 #undef MAXWAIT_4DATA
1342 #undef MINWAIT_4DATA
1345 * This routine is called to execute one of the filters as was
1346 * specified in a printcap entry. While the child-process will read
1347 * all of 'infd', it is up to the caller to close that file descriptor
1348 * in the parent process.
1351 execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
1354 int errfd, retcode, wstatus;
1356 char buf[BUFSIZ], *slash;
1358 fpid = dofork(pp, DORETURN);
1361 * This is the parent process, which just waits for the child
1362 * to complete and then returns the result. Note that it is
1363 * the child process which reads the input stream.
1368 while ((wpid = wait(&wstatus)) > 0 &&
1374 "%s: after execv(%s), wait() returned: %m",
1375 pp->printer, f_cmd);
1377 retcode = WEXITSTATUS(wstatus);
1381 * Copy everything the filter wrote to stderr from our
1382 * temporary errors file to the "lf=" logfile.
1384 errfp = fopen(tempstderr, "r");
1386 while (fgets(buf, sizeof(buf), errfp))
1395 * This is the child process, which is the one that executes the
1399 * If the first parameter has any slashes in it, then change it
1400 * to point to the first character after the last slash.
1402 slash = strrchr(f_av[0], '/');
1404 f_av[0] = slash + 1;
1406 * XXX - in the future, this should setup an explicit list of
1407 * environment variables and use execve()!
1411 * Setup stdin, stdout, and stderr as we want them when the filter
1412 * is running. Stderr is setup so it points to a temporary errors
1413 * file, and the parent process will copy that temporary file to
1414 * the real logfile after the filter completes.
1416 dup2(infd, STDIN_FILENO);
1417 dup2(outfd, STDOUT_FILENO);
1418 errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
1420 dup2(errfd, STDERR_FILENO);
1424 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
1430 * Check to make sure there have been no errors and that both programs
1431 * are in sync with eachother.
1432 * Return non-zero if the connection was lost.
1435 response(const struct printer *pp)
1439 if (read(pfd, &resp, 1) != 1) {
1440 syslog(LOG_INFO, "%s: lost connection", pp->printer);
1447 * Banner printing stuff
1450 banner(struct printer *pp, char *name1, char *name2)
1455 if (!pp->no_formfeed && !pp->tof)
1456 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1457 if (pp->short_banner) { /* short banner only */
1459 (void) write(ofd, class, strlen(class));
1460 (void) write(ofd, ":", 1);
1462 (void) write(ofd, name1, strlen(name1));
1463 (void) write(ofd, " Job: ", 7);
1464 (void) write(ofd, name2, strlen(name2));
1465 (void) write(ofd, " Date: ", 8);
1466 (void) write(ofd, ctime(&tvec), 24);
1467 (void) write(ofd, "\n", 1);
1468 } else { /* normal banner */
1469 (void) write(ofd, "\n\n\n", 3);
1470 scan_out(pp, ofd, name1, '\0');
1471 (void) write(ofd, "\n\n", 2);
1472 scan_out(pp, ofd, name2, '\0');
1474 (void) write(ofd,"\n\n\n",3);
1475 scan_out(pp, ofd, class, '\0');
1477 (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15);
1478 (void) write(ofd, name2, strlen(name2));
1479 (void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
1480 (void) write(ofd, ctime(&tvec), 24);
1481 (void) write(ofd, "\n", 1);
1483 if (!pp->no_formfeed)
1484 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1489 scnline(int key, char *p, int c)
1491 register int scnwidth;
1493 for (scnwidth = WIDTH; --scnwidth;) {
1495 *p++ = key & 0200 ? c : BACKGND;
1500 #define TRC(q) (((q)-' ')&0177)
1503 scan_out(struct printer *pp, int scfd, char *scsp, int dlm)
1505 register char *strp;
1506 register int nchrs, j;
1507 char outbuf[LINELEN+1], *sp, c, cc;
1510 for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
1513 for (nchrs = 0; ; ) {
1514 d = dropit(c = TRC(cc = *sp++));
1515 if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
1516 for (j = WIDTH; --j;)
1519 strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
1520 if (*sp == dlm || *sp == '\0' ||
1521 nchrs++ >= pp->page_width/(WIDTH+1)-1)
1526 while (*--strp == BACKGND && strp >= outbuf)
1530 (void) write(scfd, outbuf, strp-outbuf);
1556 * tell people about job completion
1559 sendmail(struct printer *pp, char *userid, int bombed)
1563 register const char *cp;
1568 if ((s = dofork(pp, DORETURN)) == 0) { /* child */
1569 dup2(p[0], STDIN_FILENO);
1572 if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
1575 cp = _PATH_SENDMAIL;
1576 execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
1578 } else if (s > 0) { /* parent */
1579 dup2(p[1], STDOUT_FILENO);
1580 printf("To: %s@%s\n", userid, origin_host);
1581 printf("Subject: %s printer job \"%s\"\n", pp->printer,
1582 *jobname ? jobname : "<unknown>");
1583 printf("Reply-To: root@%s\n\n", local_host);
1584 printf("Your printer job ");
1586 printf("(%s) ", jobname);
1591 printf("\ncompleted successfully\n");
1596 printf("\ncould not be printed\n");
1600 printf("\ncould not be printed without an account on %s\n",
1605 if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
1606 || (fp = fopen(tempstderr, "r")) == NULL) {
1607 printf("\nhad some errors and may not have printed\n");
1610 printf("\nhad the following errors and may not have printed:\n");
1611 while ((i = getc(fp)) != EOF)
1617 printf("\nwas not printed because it was not linked to the original file\n");
1620 (void) close(STDOUT_FILENO);
1622 syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
1628 syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
1629 userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
1633 * dofork - fork with retries on failure
1636 dofork(const struct printer *pp, int action)
1643 if (daemon_uname == NULL) {
1644 pwd = getpwuid(pp->daemon_user);
1646 syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
1647 pp->printer, pp->daemon_user);
1650 daemon_uname = strdup(pwd->pw_name);
1651 daemon_defgid = pwd->pw_gid;
1654 for (i = 0; i < 20; i++) {
1657 sleep((unsigned)(i*i));
1661 * Child should run as daemon instead of root
1665 fail = initgroups(daemon_uname, daemon_defgid);
1667 syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
1668 pp->printer, daemon_uname, daemon_defgid);
1671 fail = setgid(daemon_defgid);
1673 syslog(LOG_ERR, "%s: setgid(%u): %m",
1674 pp->printer, daemon_defgid);
1677 fail = setuid(pp->daemon_user);
1679 syslog(LOG_ERR, "%s: setuid(%ld): %m",
1680 pp->printer, pp->daemon_user);
1688 * An error occurred. If the error is in the child process, then
1689 * this routine MUST always exit(). DORETURN only effects how
1690 * errors should be handled in the parent process.
1694 syslog(LOG_ERR, "%s: dofork(): aborting child process...",
1698 syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);
1700 sleep(1); /* throttle errors, as a safety measure */
1705 syslog(LOG_ERR, "bad action (%d) to dofork", action);
1714 * Kill child processes to abort current job.
1717 abortpr(int signo __unused)
1720 (void) unlink(tempstderr);
1723 kill(of_pid, SIGCONT);
1724 while (wait(NULL) > 0)
1726 if (of_pid > 0 && tfd != -1)
1732 init(struct printer *pp)
1736 sprintf(&width[2], "%ld", pp->page_width);
1737 sprintf(&length[2], "%ld", pp->page_length);
1738 sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
1739 sprintf(&pxlength[2], "%ld", pp->page_plength);
1740 if ((s = checkremote(pp)) != 0) {
1741 syslog(LOG_WARNING, "%s", s);
1747 startprinting(const char *printer)
1749 struct printer myprinter, *pp = &myprinter;
1753 status = getprintcap(printer, pp);
1756 syslog(LOG_ERR, "can't open printer description file: %m");
1758 case PCAPERR_NOTFOUND:
1759 syslog(LOG_ERR, "unknown printer: %s", printer);
1761 case PCAPERR_TCLOOP:
1762 fatal(pp, "potential reference loop detected in printcap file");
1770 * Acquire line printer or remote connection.
1773 openpr(const struct printer *pp)
1781 * Lpd does support the setting of 'of=' filters for
1782 * jobs going to remote machines, but that does not
1783 * have the same meaning as 'of=' does when handling
1784 * local print queues. For remote machines, all 'of='
1785 * filter processing is handled in sendfile(), and that
1786 * does not use these global "output filter" variables.
1791 } else if (*pp->lp) {
1792 if (strchr(pp->lp, '@') != NULL)
1797 syslog(LOG_ERR, "%s: no line printer device or host name",
1803 * Start up an output filter, if needed.
1805 if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) {
1808 strcpy(tfile, TFILENAME);
1809 tfd = mkstemp(tfile);
1811 if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */
1812 dup2(p[0], STDIN_FILENO); /* pipe is std in */
1813 /* tfile/printer is stdout */
1814 dup2(pp->remote ? tfd : pfd, STDOUT_FILENO);
1817 if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
1818 cp = pp->filters[LPF_OUTPUT];
1821 execl(pp->filters[LPF_OUTPUT], cp, width, length,
1823 syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer,
1824 pp->filters[LPF_OUTPUT]);
1827 (void) close(p[0]); /* close input side */
1828 ofd = p[1]; /* use pipe for output */
1836 * Printer connected directly to the network
1837 * or to a terminal server on the net
1840 opennet(const struct printer *pp)
1846 void (*savealrm)(int);
1848 port = strtoul(pp->lp, &ep, 0);
1849 if (*ep != '@' || port > 65535) {
1850 syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
1856 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1858 savealrm = signal(SIGALRM, alarmhandler);
1859 alarm(pp->conn_timeout);
1860 pfd = getport(pp, ep, port);
1862 (void)signal(SIGALRM, savealrm);
1863 if (pfd < 0 && errno == ECONNREFUSED)
1865 else if (pfd >= 0) {
1867 * need to delay a bit for rs232 lines
1868 * to stabilize in case printer is
1869 * connected via a terminal server
1876 pstatus(pp, "waiting for %s to come up",
1880 "waiting for access to printer on %s",
1885 pstatus(pp, "sending to %s port %lu", ep, port);
1889 * Printer is connected to an RS232 port on this host
1892 opentty(const struct printer *pp)
1896 for (i = 1; ; i = i < 32 ? i << 1 : i) {
1897 pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
1902 if (errno == ENOENT) {
1903 syslog(LOG_ERR, "%s: %m", pp->lp);
1908 "waiting for %s to become ready (offline?)",
1914 pstatus(pp, "%s is ready and printing", pp->printer);
1918 * Printer is on a remote host
1921 openrem(const struct printer *pp)
1925 void (*savealrm)(int);
1927 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1929 savealrm = signal(SIGALRM, alarmhandler);
1930 alarm(pp->conn_timeout);
1931 pfd = getport(pp, pp->remote_host, 0);
1933 (void)signal(SIGALRM, savealrm);
1935 if ((writel(pfd, "\2", pp->remote_queue, "\n",
1937 == 2 + strlen(pp->remote_queue))
1938 && (resp = response(pp)) == 0)
1944 pstatus(pp, "waiting for %s to come up",
1948 "waiting for queue to be enabled on %s",
1955 pstatus(pp, "sending to %s", pp->remote_host);
1962 setty(const struct printer *pp)
1964 struct termios ttybuf;
1966 if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
1967 syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
1970 if (tcgetattr(pfd, &ttybuf) < 0) {
1971 syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
1974 if (pp->baud_rate > 0)
1975 cfsetspeed(&ttybuf, pp->baud_rate);
1977 char *s = strdup(pp->mode_set), *tmp;
1979 while ((tmp = strsep(&s, ",")) != NULL) {
1980 (void) msearch(tmp, &ttybuf);
1983 if (pp->mode_set != 0 || pp->baud_rate > 0) {
1984 if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
1985 syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
1993 pstatus(const struct printer *pp, const char *msg, ...)
2001 fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
2003 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
2008 vasprintf(&buf, msg, ap);
2010 writel(fd, buf, "\n", (char *)0);
2016 alarmhandler(int signo __unused)
2018 /* the signal is ignored */
2019 /* (the '__unused' is just to avoid a compile-time warning) */