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>
68 #include <sys/ioctl.h>
73 #include "pathnames.h"
76 #define DORETURN 0 /* dofork should return "can't fork" error */
77 #define DOABORT 1 /* dofork should just die if fork() fails */
80 * The buffer size to use when reading/writing spool files.
82 #define SPL_BUFSIZ BUFSIZ
95 static dev_t fdev; /* device of file pointed to by symlink */
96 static ino_t fino; /* inode of file pointed to by symlink */
97 static FILE *cfp; /* control file */
98 static pid_t of_pid; /* process id of output filter, if any */
99 static int child; /* id of any filters */
100 static int job_dfcnt; /* count of datafiles in current user job */
101 static int lfd; /* lock file descriptor */
102 static int ofd; /* output filter file descriptor */
103 static int tfd = -1; /* output filter temp file output */
104 static int pfd; /* prstatic inter file descriptor */
105 static int prchild; /* id of pr process */
106 static char title[80]; /* ``pr'' title */
107 static char locale[80]; /* ``pr'' locale */
109 /* these two are set from pp->daemon_user, but only if they are needed */
110 static char *daemon_uname; /* set from pwd->pw_name */
111 static int daemon_defgid;
113 static char class[32]; /* classification field */
114 static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */
115 /* indentation size in static characters */
116 static char indent[10] = "-i0";
117 static char jobname[100]; /* job or file name */
118 static char length[10] = "-l"; /* page length in lines */
119 static char logname[32]; /* user's login name */
120 static char pxlength[10] = "-y"; /* page length in pixels */
121 static char pxwidth[10] = "-x"; /* page width in pixels */
122 /* tempstderr is the filename used to catch stderr from exec-ing filters */
123 static char tempstderr[] = "errs.XXXXXXX";
124 static char width[10] = "-w"; /* page width in static characters */
125 #define TFILENAME "fltXXXXXX"
126 static char tfile[] = TFILENAME; /* file name for filter output */
128 static void abortpr(int _signo);
129 static void alarmhandler(int _signo);
130 static void banner(struct printer *_pp, char *_name1, char *_name2);
131 static int dofork(const struct printer *_pp, int _action);
132 static int dropit(int _c);
133 static int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
134 int _infd, int _outfd);
135 static void init(struct printer *_pp);
136 static void openpr(const struct printer *_pp);
137 static void opennet(const struct printer *_pp);
138 static void opentty(const struct printer *_pp);
139 static void openrem(const struct printer *pp);
140 static int print(struct printer *_pp, int _format, char *_file);
141 static int printit(struct printer *_pp, char *_file);
142 static void pstatus(const struct printer *_pp, const char *_msg, ...)
144 static char response(const struct printer *_pp);
145 static void scan_out(struct printer *_pp, int _scfd, char *_scsp,
147 static char *scnline(int _key, char *_p, int _c);
148 static int sendfile(struct printer *_pp, int _type, char *_file,
149 char _format, int _copyreq);
150 static int sendit(struct printer *_pp, char *_file);
151 static void sendmail(struct printer *_pp, char *_userid, int _bombed);
152 static void setty(const struct printer *_pp);
153 static void wait4data(struct printer *_pp, const char *_dfile);
156 printjob(struct printer *pp)
159 register struct jobqueue *q, **qp;
160 struct jobqueue **queue;
161 register int i, nitems;
164 int errcnt, jobcount, statok, tempfd;
167 init(pp); /* set up capabilities */
168 (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */
169 (void) close(STDERR_FILENO); /* set up log file */
170 if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
171 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
173 (void) open(_PATH_DEVNULL, O_WRONLY);
176 printpid = getpid(); /* for use with lprm */
177 setpgrp(0, printpid);
180 * At initial lpd startup, printjob may be called with various
181 * signal handlers in effect. After that initial startup, any
182 * calls to printjob will have a *different* set of signal-handlers
183 * in effect. Make sure all handlers are the ones we want.
185 signal(SIGCHLD, SIG_DFL);
186 signal(SIGHUP, abortpr);
187 signal(SIGINT, abortpr);
188 signal(SIGQUIT, abortpr);
189 signal(SIGTERM, abortpr);
192 * uses short form file names
194 if (chdir(pp->spool_dir) < 0) {
195 syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer,
199 statok = stat(pp->lock_file, &stb);
200 if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS))
201 exit(0); /* printing disabled */
203 lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
206 if (errno == EWOULDBLOCK) /* active daemon present */
208 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
213 * If the initial call to stat() failed, then lock_file will have
214 * been created by open(). Update &stb to match that new file.
217 statok = stat(pp->lock_file, &stb);
218 /* turn off non-blocking mode (was turned on for lock effects only) */
219 if (fcntl(lfd, F_SETFL, 0) < 0) {
220 syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer,
226 * write process id for others to know
228 sprintf(line, "%u\n", printpid);
229 pidoff = i = strlen(line);
230 if (write(lfd, line, i) != i) {
231 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
236 * search the spool directory for work and sort by queue order.
238 if ((nitems = getq(pp, &queue)) < 0) {
239 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
243 if (nitems == 0) /* no work to do */
245 if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
246 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
247 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
251 /* create a file which will be used to hold stderr from filters */
252 if ((tempfd = mkstemp(tempstderr)) == -1) {
253 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
257 if ((i = fchmod(tempfd, 0664)) == -1) {
258 syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
262 /* lpd doesn't need it to be open, it just needs it to exist */
265 openpr(pp); /* open printer or remote */
268 * we found something to do now do it --
269 * write the name of the current control file into the lock file
270 * so the spool queue program can tell what we're working on
272 for (qp = queue; nitems--; free((char *) q)) {
274 if (stat(q->job_cfname, &stb) < 0)
278 (void) lseek(lfd, pidoff, 0);
279 (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
281 if (write(lfd, line, i) != i)
282 syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
285 i = printit(pp, q->job_cfname);
287 i = sendit(pp, q->job_cfname);
289 * Check to see if we are supposed to stop printing or
290 * if we are to rebuild the queue.
292 if (fstat(lfd, &stb) == 0) {
293 /* stop printing before starting next job? */
294 if (stb.st_mode & LFM_PRINT_DIS)
296 /* rebuild queue (after lpc topq) */
297 if (stb.st_mode & LFM_RESET_QUE) {
298 for (free(q); nitems--; free(q))
300 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
303 "%s: fchmod(%s): %m",
304 pp->printer, pp->lock_file);
308 if (i == OK) /* all files of this job printed */
310 else if (i == REPRINT && ++errcnt < 5) {
311 /* try reprinting the job */
312 syslog(LOG_INFO, "restarting %s", pp->printer);
314 kill(of_pid, SIGCONT); /* to be sure */
316 while ((i = wait(NULL)) > 0 && i != of_pid)
319 syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
320 pp->printer, of_pid);
323 (void) close(pfd); /* close printer */
324 if (ftruncate(lfd, pidoff) < 0)
325 syslog(LOG_WARNING, "%s: ftruncate(%s): %m",
326 pp->printer, pp->lock_file);
327 openpr(pp); /* try to reopen printer */
330 syslog(LOG_WARNING, "%s: job could not be %s (%s)",
332 pp->remote ? "sent to remote host" : "printed",
335 /* ensure we don't attempt this job again */
336 (void) unlink(q->job_cfname);
337 q->job_cfname[0] = 'd';
338 (void) unlink(q->job_cfname);
340 sendmail(pp, logname, FATALERR);
346 * search the spool directory for more work.
348 if ((nitems = getq(pp, &queue)) < 0) {
349 syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
353 if (nitems == 0) { /* no more work to do */
355 if (jobcount > 0) { /* jobs actually printed */
356 if (!pp->no_formfeed && !pp->tof)
357 (void) write(ofd, pp->form_feed,
358 strlen(pp->form_feed));
359 if (pp->trailer != NULL) /* output trailer */
360 (void) write(ofd, pp->trailer,
361 strlen(pp->trailer));
365 (void) unlink(tempstderr);
371 char fonts[4][50]; /* fonts for troff */
373 char ifonts[4][40] = {
381 * The remaining part is the reading of the control file (cf)
382 * and performing the various actions.
385 printit(struct printer *pp, char *file)
389 int bombed, didignorehdr;
394 * open control file; ignore if no longer there.
396 if ((cfp = fopen(file, "r")) == NULL) {
397 syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file);
403 for (i = 0; i < 4; i++)
404 strcpy(fonts[i], ifonts[i]);
405 sprintf(&width[2], "%ld", pp->page_width);
406 strcpy(indent+2, "0");
408 /* initialize job-specific count of datafiles processed */
412 * read the control file for work to do
414 * file format -- first character in the line is a command
415 * rest of the line is the argument.
416 * valid commands are:
418 * S -- "stat info" for symbolic link protection
419 * J -- "job name" on banner page
420 * C -- "class name" on banner page
421 * L -- "literal" user's name to print on banner
422 * T -- "title" for pr
423 * H -- "host name" of machine where lpr was done
424 * P -- "person" user's login name
425 * I -- "indent" amount to indent output
426 * R -- laser dpi "resolution"
427 * f -- "file name" name of text file to print
428 * l -- "file name" text file with control chars
429 * o -- "file name" postscript file, according to
430 * the RFC. Here it is treated like an 'f'.
431 * p -- "file name" text file to print with pr(1)
432 * t -- "file name" troff(1) file to print
433 * n -- "file name" ditroff(1) file to print
434 * d -- "file name" dvi file to print
435 * g -- "file name" plot(1G) file to print
436 * v -- "file name" plain raster file to print
437 * c -- "file name" cifplot file to print
438 * 1 -- "R font file" for troff
439 * 2 -- "I font file" for troff
440 * 3 -- "B font file" for troff
441 * 4 -- "S font file" for troff
442 * N -- "name" of file (used by lpq)
443 * U -- "unlink" name of file to remove
444 * (after we print it. (Pass 2 only)).
445 * M -- "mail" to user when done printing
446 * Z -- "locale" for pr
448 * getline reads a line and expands tabs to blanks
456 strlcpy(origin_host, line + 1, sizeof(origin_host));
457 if (class[0] == '\0') {
458 strlcpy(class, line+1, sizeof(class));
463 strlcpy(logname, line + 1, sizeof(logname));
464 if (pp->restricted) { /* restricted */
465 if (getpwnam(logname) == NULL) {
467 sendmail(pp, line+1, bombed);
476 while (*cp >= '0' && *cp <= '9')
477 i = i * 10 + (*cp++ - '0');
481 while (*cp >= '0' && *cp <= '9')
482 i = i * 10 + (*cp++ - '0');
487 if (line[1] != '\0') {
488 strlcpy(jobname, line + 1, sizeof(jobname));
490 strcpy(jobname, " ");
495 strlcpy(class, line + 1, sizeof(class));
496 else if (class[0] == '\0') {
497 /* XXX - why call gethostname instead of
498 * just strlcpy'ing local_host? */
499 gethostname(class, sizeof(class));
500 class[sizeof(class) - 1] = '\0';
504 case 'T': /* header title for pr */
505 strlcpy(title, line + 1, sizeof(title));
508 case 'L': /* identification line */
509 if (!pp->no_header && !pp->header_last)
510 banner(pp, line+1, jobname);
513 case '1': /* troff fonts */
517 if (line[1] != '\0') {
518 strlcpy(fonts[line[0]-'1'], line + 1,
523 case 'W': /* page width */
524 strlcpy(width+2, line + 1, sizeof(width) - 2);
527 case 'I': /* indent amount */
528 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
531 case 'Z': /* locale for pr */
532 strlcpy(locale, line + 1, sizeof(locale));
535 default: /* some file to print */
536 /* only lowercase cmd-codes include a file-to-print */
537 if ((line[0] < 'a') || (line[0] > 'z')) {
538 /* ignore any other lines */
542 syslog(LOG_INFO, "%s: in %s :",
546 syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
547 pp->printer, line[0], &line[1]);
550 i = print(pp, line[0], line+1);
562 sendmail(pp, logname, bombed);
580 case 'L': /* identification line */
581 if (!pp->no_header && pp->header_last)
582 banner(pp, line+1, jobname);
586 if (bombed < NOACCT) /* already sent if >= NOACCT */
587 sendmail(pp, line+1, bombed);
591 if (strchr(line+1, '/'))
593 (void) unlink(line+1);
596 * clean-up in case another control file exists
600 return (bombed == OK ? OK : ERROR);
605 * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
606 * Return -1 if a non-recoverable error occured,
607 * 2 if the filter detected some errors (but printed the job anyway),
608 * 1 if we should try to reprint this job and
610 * Note: all filters take stdin as the file, stdout as the printer,
611 * stderr as the log file, and must not ignore SIGINT.
614 print(struct printer *pp, int format, char *file)
620 char *av[15], buf[SPL_BUFSIZ];
622 int p[2], retcode, stopped, wstatus, wstatus_set;
625 /* Make sure the entire data file has arrived. */
628 if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
629 syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
630 pp->printer, file, format);
634 * Check to see if data file is a symbolic link. If so, it should
635 * still point to the same file or someone is trying to print
636 * something he shouldn't.
638 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
639 (stb.st_dev != fdev || stb.st_ino != fino))
642 job_dfcnt++; /* increment datafile counter for this job */
643 stopped = 0; /* output filter is not stopped */
645 /* everything seems OK, start it up */
646 if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
647 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
650 if (pp->filters[LPF_INPUT] == NULL
651 && (format == 'f' || format == 'l' || format == 'o')) {
653 while ((n = read(fi, buf, SPL_BUFSIZ)) > 0)
654 if (write(ofd, buf, n) != n) {
662 case 'p': /* print file using 'pr' */
663 if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */
670 av[i++] = *title ? title : " ";
672 av[i++] = *locale ? locale : "C";
679 if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */
680 dup2(fi, STDIN_FILENO); /* file is stdin */
681 dup2(p[1], STDOUT_FILENO); /* pipe is stdout */
684 execl(_PATH_PR, "pr", width, length,
685 "-h", *title ? title : " ",
686 "-L", *locale ? locale : "C",
688 syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
691 (void) close(p[1]); /* close output side */
698 fi = p[0]; /* use pipe for input */
699 case 'f': /* print plain text file */
700 prog = pp->filters[LPF_INPUT];
706 case 'o': /* print postscript file */
708 * Treat this as a "plain file with control characters", and
709 * assume the standard LPF_INPUT filter will recognize that
710 * the data is postscript and know what to do with it. These
711 * 'o'-file requests could come from MacOS 10.1 systems.
712 * (later versions of MacOS 10 will explicitly use 'l')
713 * A postscript file can contain binary data, which is why 'l'
714 * is somewhat more appropriate than 'f'.
717 case 'l': /* like 'f' but pass control characters */
718 prog = pp->filters[LPF_INPUT];
725 case 'r': /* print a fortran text file */
726 prog = pp->filters[LPF_FORTRAN];
731 case 't': /* print troff output */
732 case 'n': /* print ditroff output */
733 case 'd': /* print tex output */
734 (void) unlink(".railmag");
735 if ((fo = creat(".railmag", FILMOD)) < 0) {
736 syslog(LOG_ERR, "%s: cannot create .railmag",
738 (void) unlink(".railmag");
740 for (n = 0; n < 4; n++) {
741 if (fonts[n][0] != '/')
742 (void) write(fo, _PATH_VFONT,
743 sizeof(_PATH_VFONT) - 1);
744 (void) write(fo, fonts[n], strlen(fonts[n]));
745 (void) write(fo, "\n", 1);
749 prog = (format == 't') ? pp->filters[LPF_TROFF]
750 : ((format == 'n') ? pp->filters[LPF_DITROFF]
751 : pp->filters[LPF_DVI]);
756 case 'c': /* print cifplot output */
757 prog = pp->filters[LPF_CIFPLOT];
762 case 'g': /* print plot(1G) output */
763 prog = pp->filters[LPF_GRAPH];
768 case 'v': /* print raster output */
769 prog = pp->filters[LPF_RASTER];
776 syslog(LOG_ERR, "%s: illegal format character '%c'",
777 pp->printer, format);
783 "%s: no filter found in printcap for format character '%c'",
784 pp->printer, format);
787 if ((av[0] = strrchr(prog, '/')) != NULL)
794 av[n++] = origin_host;
795 av[n++] = pp->acct_file;
798 if (of_pid > 0) { /* stop output filter */
799 write(ofd, "\031\1", 2);
801 wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid)
805 "%s: after stopping 'of', wait3() returned: %m",
807 else if (!WIFSTOPPED(wstatus)) {
809 syslog(LOG_WARNING, "%s: output filter died "
810 "(pid=%d retcode=%d termsig=%d)",
811 pp->printer, of_pid, WEXITSTATUS(wstatus),
818 if ((child = dofork(pp, DORETURN)) == 0) { /* child */
819 dup2(fi, STDIN_FILENO);
820 dup2(fo, STDOUT_FILENO);
821 /* setup stderr for the filter (child process)
822 * so it goes to our temporary errors file */
823 n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
825 dup2(n, STDERR_FILENO);
829 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
838 while ((wpid = wait(&wstatus)) > 0 && wpid != child)
843 "%s: after execv(%s), wait() returned: %m",
847 retcode = WEXITSTATUS(wstatus);
852 if (stopped) { /* restart output filter */
853 if (kill(of_pid, SIGCONT) < 0) {
854 syslog(LOG_ERR, "cannot restart output filter");
860 /* Copy the filter's output to "lf" logfile */
861 if ((fp = fopen(tempstderr, "r"))) {
862 while (fgets(buf, sizeof(buf), fp))
867 if (wstatus_set && !WIFEXITED(wstatus)) {
868 syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
869 pp->printer, format, WTERMSIG(wstatus));
881 syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
882 pp->printer, format, retcode);
888 * Send the daemon control file (cf) and any data files.
889 * Return -1 if a non-recoverable error occured, 1 if a recoverable error and
893 sendit(struct printer *pp, char *file)
895 int dfcopies, err, i;
896 char *cp, last[sizeof(line)];
901 if ((cfp = fopen(file, "r")) == NULL)
904 /* initialize job-specific count of datafiles processed */
908 * read the control file for work to do
910 * file format -- first character in the line is a command
911 * rest of the line is the argument.
912 * commands of interest are:
914 * a-z -- "file name" name of file to print
915 * U -- "unlink" name of file to remove
916 * (after we print it. (Pass 2 only)).
923 while (getline(cfp)) {
925 if (line[0] == 'S') {
928 while (*cp >= '0' && *cp <= '9')
929 i = i * 10 + (*cp++ - '0');
933 while (*cp >= '0' && *cp <= '9')
934 i = i * 10 + (*cp++ - '0');
936 } else if (line[0] == 'H') {
937 strlcpy(origin_host, line + 1, sizeof(origin_host));
938 if (class[0] == '\0') {
939 strlcpy(class, line + 1, sizeof(class));
941 } else if (line[0] == 'P') {
942 strlcpy(logname, line + 1, sizeof(logname));
943 if (pp->restricted) { /* restricted */
944 if (getpwnam(logname) == NULL) {
945 sendmail(pp, line+1, NOACCT);
950 } else if (line[0] == 'I') {
951 strlcpy(indent+2, line + 1, sizeof(indent) - 2);
952 } else if (line[0] >= 'a' && line[0] <= 'z') {
955 while ((i = getline(cfp)) != 0) {
956 if (strcmp(last, line) != 0)
960 switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
969 sendmail(pp, logname, ACCESS);
976 if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
985 if (line[0] == 'U' && !strchr(line+1, '/'))
986 (void) unlink(line+1);
988 * clean-up in case another control file exists
996 * Send a data file to the remote machine and spool it.
997 * Return positive if we should try resending.
1000 sendfile(struct printer *pp, int type, char *file, char format, int copyreq)
1004 char *av[15], *filtcmd;
1005 char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
1006 int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;
1008 /* Make sure the entire data file has arrived. */
1009 wait4data(pp, file);
1011 statrc = lstat(file, &stb);
1013 syslog(LOG_ERR, "%s: error from lstat(%s): %m",
1017 sfd = open(file, O_RDONLY);
1019 syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
1024 * Check to see if data file is a symbolic link. If so, it should
1025 * still point to the same file or someone is trying to print something
1028 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
1029 (stb.st_dev != fdev || stb.st_ino != fino)) {
1034 /* Everything seems OK for reading the file, now to send it */
1040 * Type == 3 means this is a datafile, not a control file.
1041 * Increment the counter of data-files in this job, and
1042 * then check for input or output filters (which are only
1043 * applied to datafiles, not control files).
1048 * Note that here we are filtering datafiles, one at a time,
1049 * as they are sent to the remote machine. Here, the *only*
1050 * difference between an input filter (`if=') and an output
1051 * filter (`of=') is the argument list that the filter is
1052 * started up with. Here, the output filter is executed
1053 * for each individual file as it is sent. This is not the
1054 * same as local print queues, where the output filter is
1055 * started up once, and then all jobs are passed thru that
1056 * single invocation of the output filter.
1058 * Also note that a queue for a remote-machine can have an
1059 * input filter or an output filter, but not both.
1061 if (pp->filters[LPF_INPUT]) {
1062 filtcmd = pp->filters[LPF_INPUT];
1065 strcpy(opt_c, "-c");
1066 strcpy(opt_h, "-h");
1067 strcpy(opt_n, "-n");
1071 av[++narg] = length;
1072 av[++narg] = indent;
1074 av[++narg] = logname;
1076 av[++narg] = origin_host;
1077 av[++narg] = pp->acct_file;
1079 } else if (pp->filters[LPF_OUTPUT]) {
1080 filtcmd = pp->filters[LPF_OUTPUT];
1084 av[++narg] = length;
1090 * If there is an input or output filter, we have to run
1091 * the datafile thru that filter and store the result as
1092 * a temporary spool file, because the protocol requires
1093 * that we send the remote host the file-size before we
1094 * start to send any of the data.
1096 strcpy(tfile, TFILENAME);
1097 tfd = mkstemp(tfile);
1099 syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
1104 filtstat = execfilter(pp, filtcmd, av, sfd, tfd);
1106 /* process the return-code from the filter */
1118 "%s: filter '%c' exited (retcode=%d)",
1119 pp->printer, format, filtstat);
1123 statrc = fstat(tfd, &stb); /* to find size of tfile */
1126 "%s: error processing 'if', fstat(%s): %m",
1127 pp->printer, tfile);
1133 lseek(sfd, 0, SEEK_SET);
1141 (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file);
1143 (void) sprintf(buf, "%c%qd %s_c%d\n", type, stb.st_size,
1146 for (i = 0; ; i++) {
1147 if (write(pfd, buf, amt) != amt ||
1148 (resp = response(pp)) < 0 || resp == '\1') {
1151 } else if (resp == '\0')
1155 "no space on remote; waiting for queue to drain");
1157 syslog(LOG_ALERT, "%s: can't send to %s; queue full",
1158 pp->printer, pp->remote_host);
1162 pstatus(pp, "sending to %s", pp->remote_host);
1164 * XXX - we should change trstat_init()/trstat_write() to include
1165 * the copycnt in the statistics record it may write.
1168 trstat_init(pp, file, job_dfcnt);
1169 for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) {
1171 if (i + amt > stb.st_size)
1172 amt = stb.st_size - i;
1173 if (sizerr == 0 && read(sfd, buf, amt) != amt)
1175 if (write(pfd, buf, amt) != amt) {
1182 syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
1183 /* tell recvjob to ignore this file */
1184 (void) write(pfd, "\1", 1);
1188 if (write(pfd, "", 1) != 1 || response(pp)) {
1193 trstat_write(pp, TR_SENDING, stb.st_size, logname,
1194 pp->remote_host, origin_host);
1196 * Usually we only need to send one copy of a datafile,
1197 * because the control-file will simply print the same
1198 * file multiple times. However, some printers ignore
1199 * the control file, and simply print each data file as
1200 * it arrives. For such "remote hosts", we need to
1201 * transfer the same data file multiple times. Such a
1202 * a host is indicated by adding 'rc' to the printcap
1204 * XXX - Right now this ONLY works for remote hosts which
1205 * do ignore the name of the data file, because
1206 * this sends the file multiple times with slight
1207 * changes to the filename. To do this right would
1208 * require that we also rewrite the control file
1209 * to match those filenames.
1211 if (pp->resend_copies && (copycnt < copyreq)) {
1212 lseek(sfd, 0, SEEK_SET);
1222 * If tfd is set, then it is the same value as sfd, and
1223 * therefore it is already closed at this point. All
1224 * we need to do is remove the temporary file.
1233 * Some print servers send the control-file first, and then start sending the
1234 * matching data file(s). That is not the correct order. If some queue is
1235 * already printing an active job, then when that job is finished the queue
1236 * may proceed to the control file of any incoming print job. This turns
1237 * into a race between the process which is receiving the data file, and the
1238 * process which is actively printing the very same file. When the remote
1239 * server sends files in the wrong order, it is even possible that a queue
1240 * will start to print a data file before the file has been created!
1242 * So before we start to print() or send() a data file, we call this routine
1243 * to make sure the data file is not still changing in size. Note that this
1244 * problem will only happen for jobs arriving from a remote host, and that
1245 * the process which has decided to print this job (and is thus making this
1246 * check) is *not* the process which is receiving the job.
1248 * A second benefit of this is that any incoming job is guaranteed to appear
1249 * in a queue listing for at least a few seconds after it has arrived. Some
1250 * lpr implementations get confused if they send a job and it disappears
1251 * from the queue before they can check on it.
1253 #define MAXWAIT_ARRIVE 16 /* max to wait for the file to *exist* */
1254 #define MAXWAIT_4DATA (20*60) /* max to wait for it to stop changing */
1255 #define MINWAIT_4DATA 4 /* This value must be >= 1 */
1256 #define DEBUG_MINWAIT 1
1258 wait4data(struct printer *pp, const char *dfile)
1264 time_t amtslept, cur_time, prev_mtime;
1267 /* Skip these checks if the print job is from the local host. */
1268 dlen = strlen(dfile);
1269 hlen = strlen(local_host);
1271 cp = dfile + dlen - hlen;
1272 if (strcmp(cp, local_host) == 0)
1277 * If this data file does not exist, then wait up to MAXWAIT_ARRIVE
1278 * seconds for it to arrive.
1281 statres = stat(dfile, &statdf);
1282 while (statres < 0 && amtslept < MAXWAIT_ARRIVE) {
1284 pstatus(pp, "Waiting for data file from remote host");
1285 amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA);
1286 statres = stat(dfile, &statdf);
1289 /* The file still does not exist, so just give up on it. */
1290 syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s",
1291 pp->printer, dfile);
1296 * The file exists, so keep waiting until the data file has not
1297 * changed for some reasonable amount of time. Extra care is
1298 * taken when computing wait-times, just in case there are data
1299 * files with a last-modify time in the future. While that is
1300 * very unlikely to happen, it can happen when the system has
1301 * a flakey time-of-day clock.
1303 prev_mtime = statdf.st_mtime;
1304 cur_time = time(NULL);
1305 if (statdf.st_mtime >= cur_time - MINWAIT_4DATA) {
1306 if (statdf.st_mtime >= cur_time) /* some TOD oddity */
1307 sleepreq = MINWAIT_4DATA;
1309 sleepreq = cur_time - statdf.st_mtime;
1311 pstatus(pp, "Waiting for data file from remote host");
1312 amtslept += sleepreq - sleep(sleepreq);
1313 statres = stat(dfile, &statdf);
1315 sleepreq = MINWAIT_4DATA;
1316 while (statres == 0 && amtslept < MAXWAIT_4DATA) {
1317 if (statdf.st_mtime == prev_mtime)
1319 prev_mtime = statdf.st_mtime;
1320 amtslept += sleepreq - sleep(sleepreq);
1321 statres = stat(dfile, &statdf);
1325 syslog(LOG_WARNING, "%s: %s disappeared during wait4data()",
1326 pp->printer, dfile);
1327 else if (amtslept > MAXWAIT_4DATA)
1329 "%s: %s still changing after %lu secs in wait4data()",
1330 pp->printer, dfile, (unsigned long)amtslept);
1332 else if (amtslept > MINWAIT_4DATA)
1333 syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)",
1334 pp->printer, (unsigned long)amtslept, dfile);
1337 #undef MAXWAIT_ARRIVE
1338 #undef MAXWAIT_4DATA
1339 #undef MINWAIT_4DATA
1342 * This routine is called to execute one of the filters as was
1343 * specified in a printcap entry. While the child-process will read
1344 * all of 'infd', it is up to the caller to close that file descriptor
1345 * in the parent process.
1348 execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
1351 int errfd, retcode, wstatus;
1353 char buf[BUFSIZ], *slash;
1355 fpid = dofork(pp, DORETURN);
1358 * This is the parent process, which just waits for the child
1359 * to complete and then returns the result. Note that it is
1360 * the child process which reads the input stream.
1365 while ((wpid = wait(&wstatus)) > 0 &&
1371 "%s: after execv(%s), wait() returned: %m",
1372 pp->printer, f_cmd);
1374 retcode = WEXITSTATUS(wstatus);
1378 * Copy everything the filter wrote to stderr from our
1379 * temporary errors file to the "lf=" logfile.
1381 errfp = fopen(tempstderr, "r");
1383 while (fgets(buf, sizeof(buf), errfp))
1392 * This is the child process, which is the one that executes the
1396 * If the first parameter has any slashes in it, then change it
1397 * to point to the first character after the last slash.
1399 slash = strrchr(f_av[0], '/');
1401 f_av[0] = slash + 1;
1403 * XXX - in the future, this should setup an explicit list of
1404 * environment variables and use execve()!
1408 * Setup stdin, stdout, and stderr as we want them when the filter
1409 * is running. Stderr is setup so it points to a temporary errors
1410 * file, and the parent process will copy that temporary file to
1411 * the real logfile after the filter completes.
1413 dup2(infd, STDIN_FILENO);
1414 dup2(outfd, STDOUT_FILENO);
1415 errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
1417 dup2(errfd, STDERR_FILENO);
1421 syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
1427 * Check to make sure there have been no errors and that both programs
1428 * are in sync with eachother.
1429 * Return non-zero if the connection was lost.
1432 response(const struct printer *pp)
1436 if (read(pfd, &resp, 1) != 1) {
1437 syslog(LOG_INFO, "%s: lost connection", pp->printer);
1444 * Banner printing stuff
1447 banner(struct printer *pp, char *name1, char *name2)
1452 if (!pp->no_formfeed && !pp->tof)
1453 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1454 if (pp->short_banner) { /* short banner only */
1456 (void) write(ofd, class, strlen(class));
1457 (void) write(ofd, ":", 1);
1459 (void) write(ofd, name1, strlen(name1));
1460 (void) write(ofd, " Job: ", 7);
1461 (void) write(ofd, name2, strlen(name2));
1462 (void) write(ofd, " Date: ", 8);
1463 (void) write(ofd, ctime(&tvec), 24);
1464 (void) write(ofd, "\n", 1);
1465 } else { /* normal banner */
1466 (void) write(ofd, "\n\n\n", 3);
1467 scan_out(pp, ofd, name1, '\0');
1468 (void) write(ofd, "\n\n", 2);
1469 scan_out(pp, ofd, name2, '\0');
1471 (void) write(ofd,"\n\n\n",3);
1472 scan_out(pp, ofd, class, '\0');
1474 (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15);
1475 (void) write(ofd, name2, strlen(name2));
1476 (void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
1477 (void) write(ofd, ctime(&tvec), 24);
1478 (void) write(ofd, "\n", 1);
1480 if (!pp->no_formfeed)
1481 (void) write(ofd, pp->form_feed, strlen(pp->form_feed));
1486 scnline(int key, char *p, int c)
1488 register int scnwidth;
1490 for (scnwidth = WIDTH; --scnwidth;) {
1492 *p++ = key & 0200 ? c : BACKGND;
1497 #define TRC(q) (((q)-' ')&0177)
1500 scan_out(struct printer *pp, int scfd, char *scsp, int dlm)
1502 register char *strp;
1503 register int nchrs, j;
1504 char outbuf[LINELEN+1], *sp, c, cc;
1507 for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
1510 for (nchrs = 0; ; ) {
1511 d = dropit(c = TRC(cc = *sp++));
1512 if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
1513 for (j = WIDTH; --j;)
1516 strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
1517 if (*sp == dlm || *sp == '\0' ||
1518 nchrs++ >= pp->page_width/(WIDTH+1)-1)
1523 while (*--strp == BACKGND && strp >= outbuf)
1527 (void) write(scfd, outbuf, strp-outbuf);
1553 * tell people about job completion
1556 sendmail(struct printer *pp, char *userid, int bombed)
1560 register const char *cp;
1565 if ((s = dofork(pp, DORETURN)) == 0) { /* child */
1566 dup2(p[0], STDIN_FILENO);
1569 if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
1572 cp = _PATH_SENDMAIL;
1573 execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
1575 } else if (s > 0) { /* parent */
1576 dup2(p[1], STDOUT_FILENO);
1577 printf("To: %s@%s\n", userid, origin_host);
1578 printf("Subject: %s printer job \"%s\"\n", pp->printer,
1579 *jobname ? jobname : "<unknown>");
1580 printf("Reply-To: root@%s\n\n", local_host);
1581 printf("Your printer job ");
1583 printf("(%s) ", jobname);
1588 printf("\ncompleted successfully\n");
1593 printf("\ncould not be printed\n");
1597 printf("\ncould not be printed without an account on %s\n",
1602 if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
1603 || (fp = fopen(tempstderr, "r")) == NULL) {
1604 printf("\nhad some errors and may not have printed\n");
1607 printf("\nhad the following errors and may not have printed:\n");
1608 while ((i = getc(fp)) != EOF)
1614 printf("\nwas not printed because it was not linked to the original file\n");
1617 (void) close(STDOUT_FILENO);
1619 syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
1625 syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
1626 userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
1630 * dofork - fork with retries on failure
1633 dofork(const struct printer *pp, int action)
1640 if (daemon_uname == NULL) {
1641 pwd = getpwuid(pp->daemon_user);
1643 syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
1644 pp->printer, pp->daemon_user);
1647 daemon_uname = strdup(pwd->pw_name);
1648 daemon_defgid = pwd->pw_gid;
1651 for (i = 0; i < 20; i++) {
1654 sleep((unsigned)(i*i));
1658 * Child should run as daemon instead of root
1662 fail = initgroups(daemon_uname, daemon_defgid);
1664 syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
1665 pp->printer, daemon_uname, daemon_defgid);
1668 fail = setgid(daemon_defgid);
1670 syslog(LOG_ERR, "%s: setgid(%u): %m",
1671 pp->printer, daemon_defgid);
1674 fail = setuid(pp->daemon_user);
1676 syslog(LOG_ERR, "%s: setuid(%ld): %m",
1677 pp->printer, pp->daemon_user);
1685 * An error occurred. If the error is in the child process, then
1686 * this routine MUST always exit(). DORETURN only effects how
1687 * errors should be handled in the parent process.
1691 syslog(LOG_ERR, "%s: dofork(): aborting child process...",
1695 syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);
1697 sleep(1); /* throttle errors, as a safety measure */
1702 syslog(LOG_ERR, "bad action (%d) to dofork", action);
1711 * Kill child processes to abort current job.
1714 abortpr(int signo __unused)
1717 (void) unlink(tempstderr);
1720 kill(of_pid, SIGCONT);
1721 while (wait(NULL) > 0)
1723 if (of_pid > 0 && tfd != -1)
1729 init(struct printer *pp)
1733 sprintf(&width[2], "%ld", pp->page_width);
1734 sprintf(&length[2], "%ld", pp->page_length);
1735 sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
1736 sprintf(&pxlength[2], "%ld", pp->page_plength);
1737 if ((s = checkremote(pp)) != 0) {
1738 syslog(LOG_WARNING, "%s", s);
1744 startprinting(const char *printer)
1746 struct printer myprinter, *pp = &myprinter;
1750 status = getprintcap(printer, pp);
1753 syslog(LOG_ERR, "can't open printer description file: %m");
1755 case PCAPERR_NOTFOUND:
1756 syslog(LOG_ERR, "unknown printer: %s", printer);
1758 case PCAPERR_TCLOOP:
1759 fatal(pp, "potential reference loop detected in printcap file");
1767 * Acquire line printer or remote connection.
1770 openpr(const struct printer *pp)
1778 * Lpd does support the setting of 'of=' filters for
1779 * jobs going to remote machines, but that does not
1780 * have the same meaning as 'of=' does when handling
1781 * local print queues. For remote machines, all 'of='
1782 * filter processing is handled in sendfile(), and that
1783 * does not use these global "output filter" variables.
1788 } else if (*pp->lp) {
1789 if ((cp = strchr(pp->lp, '@')) != NULL)
1794 syslog(LOG_ERR, "%s: no line printer device or host name",
1800 * Start up an output filter, if needed.
1802 if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) {
1805 strcpy(tfile, TFILENAME);
1806 tfd = mkstemp(tfile);
1808 if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */
1809 dup2(p[0], STDIN_FILENO); /* pipe is std in */
1810 /* tfile/printer is stdout */
1811 dup2(pp->remote ? tfd : pfd, STDOUT_FILENO);
1814 if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
1815 cp = pp->filters[LPF_OUTPUT];
1818 execl(pp->filters[LPF_OUTPUT], cp, width, length,
1820 syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer,
1821 pp->filters[LPF_OUTPUT]);
1824 (void) close(p[0]); /* close input side */
1825 ofd = p[1]; /* use pipe for output */
1833 * Printer connected directly to the network
1834 * or to a terminal server on the net
1837 opennet(const struct printer *pp)
1843 void (*savealrm)(int);
1845 port = strtoul(pp->lp, &ep, 0);
1846 if (*ep != '@' || port > 65535) {
1847 syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
1853 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1855 savealrm = signal(SIGALRM, alarmhandler);
1856 alarm(pp->conn_timeout);
1857 pfd = getport(pp, ep, port);
1859 (void)signal(SIGALRM, savealrm);
1860 if (pfd < 0 && errno == ECONNREFUSED)
1862 else if (pfd >= 0) {
1864 * need to delay a bit for rs232 lines
1865 * to stabilize in case printer is
1866 * connected via a terminal server
1873 pstatus(pp, "waiting for %s to come up",
1877 "waiting for access to printer on %s",
1882 pstatus(pp, "sending to %s port %lu", ep, port);
1886 * Printer is connected to an RS232 port on this host
1889 opentty(const struct printer *pp)
1893 for (i = 1; ; i = i < 32 ? i << 1 : i) {
1894 pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
1899 if (errno == ENOENT) {
1900 syslog(LOG_ERR, "%s: %m", pp->lp);
1905 "waiting for %s to become ready (offline?)",
1911 pstatus(pp, "%s is ready and printing", pp->printer);
1915 * Printer is on a remote host
1918 openrem(const struct printer *pp)
1922 void (*savealrm)(int);
1924 for (i = 1; ; i = i < 256 ? i << 1 : i) {
1926 savealrm = signal(SIGALRM, alarmhandler);
1927 alarm(pp->conn_timeout);
1928 pfd = getport(pp, pp->remote_host, 0);
1930 (void)signal(SIGALRM, savealrm);
1932 if ((writel(pfd, "\2", pp->remote_queue, "\n",
1934 == 2 + strlen(pp->remote_queue))
1935 && (resp = response(pp)) == 0)
1941 pstatus(pp, "waiting for %s to come up",
1945 "waiting for queue to be enabled on %s",
1952 pstatus(pp, "sending to %s", pp->remote_host);
1959 setty(const struct printer *pp)
1961 struct termios ttybuf;
1963 if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
1964 syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
1967 if (tcgetattr(pfd, &ttybuf) < 0) {
1968 syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
1971 if (pp->baud_rate > 0)
1972 cfsetspeed(&ttybuf, pp->baud_rate);
1974 char *s = strdup(pp->mode_set), *tmp;
1976 while ((tmp = strsep(&s, ",")) != NULL) {
1977 (void) msearch(tmp, &ttybuf);
1980 if (pp->mode_set != 0 || pp->baud_rate > 0) {
1981 if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
1982 syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
1990 pstatus(const struct printer *pp, const char *msg, ...)
1998 fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
2000 syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
2005 vasprintf(&buf, msg, ap);
2007 writel(fd, buf, "\n", (char *)0);
2013 alarmhandler(int signo __unused)
2015 /* the signal is ignored */
2016 /* (the '__unused' is just to avoid a compile-time warning) */