printjob.c revision 38470
11553Srgrimes/* 21553Srgrimes * Copyright (c) 1983, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * 61553Srgrimes * Redistribution and use in source and binary forms, with or without 71553Srgrimes * modification, are permitted provided that the following conditions 81553Srgrimes * are met: 91553Srgrimes * 1. Redistributions of source code must retain the above copyright 101553Srgrimes * notice, this list of conditions and the following disclaimer. 111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121553Srgrimes * notice, this list of conditions and the following disclaimer in the 131553Srgrimes * documentation and/or other materials provided with the distribution. 141553Srgrimes * 3. All advertising materials mentioning features or use of this software 151553Srgrimes * must display the following acknowledgement: 161553Srgrimes * This product includes software developed by the University of 171553Srgrimes * California, Berkeley and its contributors. 181553Srgrimes * 4. Neither the name of the University nor the names of its contributors 191553Srgrimes * may be used to endorse or promote products derived from this software 201553Srgrimes * without specific prior written permission. 211553Srgrimes * 221553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321553Srgrimes * SUCH DAMAGE. 331553Srgrimes */ 341553Srgrimes 351553Srgrimes#ifndef lint 3631492Swollmanstatic const char copyright[] = 371553Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 381553Srgrimes The Regents of the University of California. All rights reserved.\n"; 391553Srgrimes#endif /* not lint */ 401553Srgrimes 411553Srgrimes#ifndef lint 4231492Swollman/* 4315648Sjoergstatic char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95"; 4431492Swollman*/ 4531492Swollmanstatic const char rcsid[] = 4638470Sbrian "$Id: printjob.c,v 1.18 1997/12/02 20:45:58 wollman Exp $"; 471553Srgrimes#endif /* not lint */ 481553Srgrimes 491553Srgrimes 501553Srgrimes/* 511553Srgrimes * printjob -- print jobs in the queue. 521553Srgrimes * 531553Srgrimes * NOTE: the lock file is used to pass information to lpq and lprm. 541553Srgrimes * it does not need to be removed because file locks are dynamic. 551553Srgrimes */ 561553Srgrimes 571553Srgrimes#include <sys/param.h> 581553Srgrimes#include <sys/wait.h> 591553Srgrimes#include <sys/stat.h> 601553Srgrimes#include <sys/types.h> 611553Srgrimes 621553Srgrimes#include <pwd.h> 631553Srgrimes#include <unistd.h> 641553Srgrimes#include <signal.h> 651553Srgrimes#include <syslog.h> 661553Srgrimes#include <fcntl.h> 671553Srgrimes#include <dirent.h> 681553Srgrimes#include <errno.h> 691553Srgrimes#include <stdio.h> 701553Srgrimes#include <string.h> 711553Srgrimes#include <stdlib.h> 7215032Ssef#include <sys/ioctl.h> 7315032Ssef#include <termios.h> 7415703Sjoerg#include <time.h> 751553Srgrimes#include "lp.h" 761553Srgrimes#include "lp.local.h" 771553Srgrimes#include "pathnames.h" 781553Srgrimes#include "extern.h" 791553Srgrimes 801553Srgrimes#define DORETURN 0 /* absorb fork error */ 811553Srgrimes#define DOABORT 1 /* abort if dofork fails */ 821553Srgrimes 831553Srgrimes/* 841553Srgrimes * Error tokens 851553Srgrimes */ 861553Srgrimes#define REPRINT -2 871553Srgrimes#define ERROR -1 881553Srgrimes#define OK 0 891553Srgrimes#define FATALERR 1 901553Srgrimes#define NOACCT 2 911553Srgrimes#define FILTERERR 3 921553Srgrimes#define ACCESS 4 931553Srgrimes 941553Srgrimesstatic dev_t fdev; /* device of file pointed to by symlink */ 951553Srgrimesstatic ino_t fino; /* inode of file pointed to by symlink */ 961553Srgrimesstatic FILE *cfp; /* control file */ 971553Srgrimesstatic int child; /* id of any filters */ 981553Srgrimesstatic int lfd; /* lock file descriptor */ 991553Srgrimesstatic int ofd; /* output filter file descriptor */ 1001553Srgrimesstatic int ofilter; /* id of output filter, if any */ 10124831Sbrianstatic int tfd = -1; /* output filter temp file output */ 1021553Srgrimesstatic int pfd; /* prstatic inter file descriptor */ 1031553Srgrimesstatic int pid; /* pid of lpd process */ 1041553Srgrimesstatic int prchild; /* id of pr process */ 1051553Srgrimesstatic char title[80]; /* ``pr'' title */ 1061553Srgrimes 1071553Srgrimesstatic char class[32]; /* classification field */ 1081553Srgrimesstatic char fromhost[32]; /* user's host machine */ 1091553Srgrimes /* indentation size in static characters */ 1108857Srgrimesstatic char indent[10] = "-i0"; 1111553Srgrimesstatic char jobname[100]; /* job or file name */ 1121553Srgrimesstatic char length[10] = "-l"; /* page length in lines */ 1131553Srgrimesstatic char logname[32]; /* user's login name */ 1141553Srgrimesstatic char pxlength[10] = "-y"; /* page length in pixels */ 1151553Srgrimesstatic char pxwidth[10] = "-x"; /* page width in pixels */ 11624831Sbrianstatic char tempfile[] = "errsXXXXXX"; /* file name for filter errors */ 1171553Srgrimesstatic char width[10] = "-w"; /* page width in static characters */ 11824831Sbrian#define TFILENAME "fltXXXXXX" 11924831Sbrianstatic char tfile[] = TFILENAME; /* file name for filter output */ 1201553Srgrimes 1211553Srgrimesstatic void abortpr __P((int)); 12230407Sjoergstatic void alarmhandler __P((int)); 12331492Swollmanstatic void banner __P((struct printer *pp, char *name1, char *name2)); 12431492Swollmanstatic int dofork __P((const struct printer *pp, int action)); 1251553Srgrimesstatic int dropit __P((int)); 12631492Swollmanstatic void init __P((struct printer *pp)); 12731492Swollmanstatic void openpr __P((const struct printer *pp)); 12831492Swollmanstatic void opennet __P((const struct printer *pp)); 12931492Swollmanstatic void opentty __P((const struct printer *pp)); 13031492Swollmanstatic void openrem __P((const struct printer *pp)); 13131492Swollmanstatic int print __P((struct printer *pp, int format, char *file)); 13231492Swollmanstatic int printit __P((struct printer *pp, char *file)); 13331492Swollmanstatic void pstatus __P((const struct printer *, const char *, ...)); 13431492Swollmanstatic char response __P((const struct printer *pp)); 13531492Swollmanstatic void scan_out __P((struct printer *pp, int scfd, char *scsp, 13631492Swollman int dlm)); 1371553Srgrimesstatic char *scnline __P((int, char *, int)); 13831492Swollmanstatic int sendfile __P((struct printer *pp, int type, char *file, 13931492Swollman int format)); 14031492Swollmanstatic int sendit __P((struct printer *pp, char *file)); 14131492Swollmanstatic void sendmail __P((struct printer *pp, char *user, int bombed)); 14231492Swollmanstatic void setty __P((const struct printer *pp)); 1431553Srgrimes 14431492Swollmanvoid msearch __P((char *, struct termios *)); 14531492Swollman 1461553Srgrimesvoid 14731492Swollmanprintjob(pp) 14831492Swollman struct printer *pp; 1491553Srgrimes{ 1501553Srgrimes struct stat stb; 1511553Srgrimes register struct queue *q, **qp; 1521553Srgrimes struct queue **queue; 1531553Srgrimes register int i, nitems; 15415648Sjoerg off_t pidoff; 15515648Sjoerg int errcnt, count = 0; 1561553Srgrimes 15731492Swollman init(pp); /* set up capabilities */ 15831492Swollman (void) write(1, "", 1); /* ack that daemon is started */ 1591553Srgrimes (void) close(2); /* set up log file */ 16031492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { 16131492Swollman syslog(LOG_ERR, "%s: %m", pp->log_file); 1621553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1631553Srgrimes } 1641553Srgrimes setgid(getegid()); 1651553Srgrimes pid = getpid(); /* for use with lprm */ 1661553Srgrimes setpgrp(0, pid); 1671553Srgrimes signal(SIGHUP, abortpr); 1681553Srgrimes signal(SIGINT, abortpr); 1691553Srgrimes signal(SIGQUIT, abortpr); 1701553Srgrimes signal(SIGTERM, abortpr); 1711553Srgrimes 1721553Srgrimes (void) mktemp(tempfile); 1731553Srgrimes 1741553Srgrimes /* 1751553Srgrimes * uses short form file names 1761553Srgrimes */ 17731492Swollman if (chdir(pp->spool_dir) < 0) { 17831492Swollman syslog(LOG_ERR, "%s: %m", pp->spool_dir); 1791553Srgrimes exit(1); 1801553Srgrimes } 18131492Swollman if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS)) 1821553Srgrimes exit(0); /* printing disabled */ 18331492Swollman lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 18431492Swollman LOCK_FILE_MODE); 1851553Srgrimes if (lfd < 0) { 18631492Swollman if (errno == EWOULDBLOCK) /* active daemon present */ 18731492Swollman exit(0); 18831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 1891553Srgrimes exit(1); 1901553Srgrimes } 19131492Swollman /* turn off non-blocking mode (was turned on for lock effects only) */ 19231492Swollman if (fcntl(lfd, F_SETFL, 0) < 0) { 19331492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 1941553Srgrimes exit(1); 1951553Srgrimes } 1961553Srgrimes ftruncate(lfd, 0); 1971553Srgrimes /* 1981553Srgrimes * write process id for others to know 1991553Srgrimes */ 2001553Srgrimes sprintf(line, "%u\n", pid); 2011553Srgrimes pidoff = i = strlen(line); 2021553Srgrimes if (write(lfd, line, i) != i) { 20331492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 2041553Srgrimes exit(1); 2051553Srgrimes } 2061553Srgrimes /* 2071553Srgrimes * search the spool directory for work and sort by queue order. 2081553Srgrimes */ 20931492Swollman if ((nitems = getq(pp, &queue)) < 0) { 21031492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 21131492Swollman pp->spool_dir); 2121553Srgrimes exit(1); 2131553Srgrimes } 2141553Srgrimes if (nitems == 0) /* no work to do */ 2151553Srgrimes exit(0); 21631492Swollman if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ 21731492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) 21831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 21931492Swollman pp->lock_file); 2201553Srgrimes } 22131492Swollman openpr(pp); /* open printer or remote */ 2221553Srgrimesagain: 2231553Srgrimes /* 2241553Srgrimes * we found something to do now do it -- 2251553Srgrimes * write the name of the current control file into the lock file 2261553Srgrimes * so the spool queue program can tell what we're working on 2271553Srgrimes */ 2281553Srgrimes for (qp = queue; nitems--; free((char *) q)) { 2291553Srgrimes q = *qp++; 2301553Srgrimes if (stat(q->q_name, &stb) < 0) 2311553Srgrimes continue; 23215648Sjoerg errcnt = 0; 2331553Srgrimes restart: 23415648Sjoerg (void) lseek(lfd, pidoff, 0); 23527748Simp (void) snprintf(line, sizeof(line), "%s\n", q->q_name); 2361553Srgrimes i = strlen(line); 2371553Srgrimes if (write(lfd, line, i) != i) 23831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 23931492Swollman pp->lock_file); 24031492Swollman if (!pp->remote) 24131492Swollman i = printit(pp, q->q_name); 2421553Srgrimes else 24331492Swollman i = sendit(pp, q->q_name); 2441553Srgrimes /* 2451553Srgrimes * Check to see if we are supposed to stop printing or 2461553Srgrimes * if we are to rebuild the queue. 2471553Srgrimes */ 2481553Srgrimes if (fstat(lfd, &stb) == 0) { 2491553Srgrimes /* stop printing before starting next job? */ 25031492Swollman if (stb.st_mode & LFM_PRINT_DIS) 2511553Srgrimes goto done; 2521553Srgrimes /* rebuild queue (after lpc topq) */ 25331492Swollman if (stb.st_mode & LFM_RESET_QUE) { 25431492Swollman for (free(q); nitems--; free(q)) 2551553Srgrimes q = *qp++; 25631492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) 25731492Swollman < 0) 2581553Srgrimes syslog(LOG_WARNING, "%s: %s: %m", 25931492Swollman pp->printer, pp->lock_file); 2601553Srgrimes break; 2611553Srgrimes } 2621553Srgrimes } 2631553Srgrimes if (i == OK) /* file ok and printed */ 2641553Srgrimes count++; 26515648Sjoerg else if (i == REPRINT && ++errcnt < 5) { 26615648Sjoerg /* try reprinting the job */ 26731492Swollman syslog(LOG_INFO, "restarting %s", pp->printer); 2681553Srgrimes if (ofilter > 0) { 2691553Srgrimes kill(ofilter, SIGCONT); /* to be sure */ 2701553Srgrimes (void) close(ofd); 27115648Sjoerg while ((i = wait(NULL)) > 0 && i != ofilter) 2721553Srgrimes ; 2731553Srgrimes ofilter = 0; 2741553Srgrimes } 2751553Srgrimes (void) close(pfd); /* close printer */ 2761553Srgrimes if (ftruncate(lfd, pidoff) < 0) 27731492Swollman syslog(LOG_WARNING, "%s: %s: %m", 27831492Swollman pp->printer, pp->lock_file); 27931492Swollman openpr(pp); /* try to reopen printer */ 2801553Srgrimes goto restart; 28115648Sjoerg } else { 28231492Swollman syslog(LOG_WARNING, "%s: job could not be %s (%s)", 28331492Swollman pp->printer, 28431492Swollman pp->remote ? "sent to remote host" : "printed", 28531492Swollman q->q_name); 28615648Sjoerg if (i == REPRINT) { 28727748Simp /* ensure we don't attempt this job again */ 28815648Sjoerg (void) unlink(q->q_name); 28915648Sjoerg q->q_name[0] = 'd'; 29015648Sjoerg (void) unlink(q->q_name); 29115648Sjoerg if (logname[0]) 29231492Swollman sendmail(pp, logname, FATALERR); 29315648Sjoerg } 2941553Srgrimes } 2951553Srgrimes } 29631492Swollman free(queue); 2971553Srgrimes /* 2981553Srgrimes * search the spool directory for more work. 2991553Srgrimes */ 30031492Swollman if ((nitems = getq(pp, &queue)) < 0) { 30131492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 30231492Swollman pp->spool_dir); 3031553Srgrimes exit(1); 3041553Srgrimes } 3051553Srgrimes if (nitems == 0) { /* no more work to do */ 3061553Srgrimes done: 3071553Srgrimes if (count > 0) { /* Files actually printed */ 30831492Swollman if (!pp->no_formfeed && !pp->tof) 30931492Swollman (void) write(ofd, pp->form_feed, 31031492Swollman strlen(pp->form_feed)); 31131492Swollman if (pp->trailer != NULL) /* output trailer */ 31231492Swollman (void) write(ofd, pp->trailer, 31331492Swollman strlen(pp->trailer)); 3141553Srgrimes } 31519202Simp (void) close(ofd); 31619202Simp (void) wait(NULL); 3171553Srgrimes (void) unlink(tempfile); 3181553Srgrimes exit(0); 3191553Srgrimes } 3201553Srgrimes goto again; 3211553Srgrimes} 3221553Srgrimes 3231553Srgrimeschar fonts[4][50]; /* fonts for troff */ 3241553Srgrimes 3251553Srgrimeschar ifonts[4][40] = { 3261553Srgrimes _PATH_VFONTR, 3271553Srgrimes _PATH_VFONTI, 3281553Srgrimes _PATH_VFONTB, 3291553Srgrimes _PATH_VFONTS, 3301553Srgrimes}; 3311553Srgrimes 3321553Srgrimes/* 3331553Srgrimes * The remaining part is the reading of the control file (cf) 3341553Srgrimes * and performing the various actions. 3351553Srgrimes */ 3361553Srgrimesstatic int 33731492Swollmanprintit(pp, file) 33831492Swollman struct printer *pp; 3391553Srgrimes char *file; 3401553Srgrimes{ 3411553Srgrimes register int i; 3421553Srgrimes char *cp; 3431553Srgrimes int bombed = OK; 3441553Srgrimes 3451553Srgrimes /* 3461553Srgrimes * open control file; ignore if no longer there. 3471553Srgrimes */ 3481553Srgrimes if ((cfp = fopen(file, "r")) == NULL) { 34931492Swollman syslog(LOG_INFO, "%s: %s: %m", pp->printer, file); 3501553Srgrimes return(OK); 3511553Srgrimes } 3521553Srgrimes /* 3531553Srgrimes * Reset troff fonts. 3541553Srgrimes */ 3551553Srgrimes for (i = 0; i < 4; i++) 3561553Srgrimes strcpy(fonts[i], ifonts[i]); 35731492Swollman sprintf(&width[2], "%ld", pp->page_width); 3581553Srgrimes strcpy(indent+2, "0"); 3591553Srgrimes 3601553Srgrimes /* 3611553Srgrimes * read the control file for work to do 3621553Srgrimes * 3631553Srgrimes * file format -- first character in the line is a command 3641553Srgrimes * rest of the line is the argument. 3651553Srgrimes * valid commands are: 3661553Srgrimes * 3671553Srgrimes * S -- "stat info" for symbolic link protection 3681553Srgrimes * J -- "job name" on banner page 3691553Srgrimes * C -- "class name" on banner page 3701553Srgrimes * L -- "literal" user's name to print on banner 3711553Srgrimes * T -- "title" for pr 3721553Srgrimes * H -- "host name" of machine where lpr was done 3731553Srgrimes * P -- "person" user's login name 3741553Srgrimes * I -- "indent" amount to indent output 37515648Sjoerg * R -- laser dpi "resolution" 3761553Srgrimes * f -- "file name" name of text file to print 3771553Srgrimes * l -- "file name" text file with control chars 3781553Srgrimes * p -- "file name" text file to print with pr(1) 3791553Srgrimes * t -- "file name" troff(1) file to print 3801553Srgrimes * n -- "file name" ditroff(1) file to print 3811553Srgrimes * d -- "file name" dvi file to print 3821553Srgrimes * g -- "file name" plot(1G) file to print 3831553Srgrimes * v -- "file name" plain raster file to print 3841553Srgrimes * c -- "file name" cifplot file to print 3851553Srgrimes * 1 -- "R font file" for troff 3861553Srgrimes * 2 -- "I font file" for troff 3871553Srgrimes * 3 -- "B font file" for troff 3881553Srgrimes * 4 -- "S font file" for troff 3891553Srgrimes * N -- "name" of file (used by lpq) 3901553Srgrimes * U -- "unlink" name of file to remove 3911553Srgrimes * (after we print it. (Pass 2 only)). 3921553Srgrimes * M -- "mail" to user when done printing 3931553Srgrimes * 3941553Srgrimes * getline reads a line and expands tabs to blanks 3951553Srgrimes */ 3961553Srgrimes 3971553Srgrimes /* pass 1 */ 3981553Srgrimes 3991553Srgrimes while (getline(cfp)) 4001553Srgrimes switch (line[0]) { 4011553Srgrimes case 'H': 40227748Simp strncpy(fromhost, line+1, sizeof(fromhost) - 1); 40327748Simp fromhost[sizeof(fromhost) - 1] = '\0'; 40427748Simp if (class[0] == '\0') { 40527748Simp strncpy(class, line+1, sizeof(class) - 1); 40627748Simp class[sizeof(class) - 1] = '\0'; 40727748Simp } 4081553Srgrimes continue; 4091553Srgrimes 4101553Srgrimes case 'P': 41127748Simp strncpy(logname, line+1, sizeof(logname) - 1); 41227748Simp logname[sizeof(logname) - 1] = '\0'; 41331492Swollman if (pp->restricted) { /* restricted */ 4141553Srgrimes if (getpwnam(logname) == NULL) { 4151553Srgrimes bombed = NOACCT; 41631492Swollman sendmail(pp, line+1, bombed); 4171553Srgrimes goto pass2; 4181553Srgrimes } 4191553Srgrimes } 4201553Srgrimes continue; 4211553Srgrimes 4221553Srgrimes case 'S': 4231553Srgrimes cp = line+1; 4241553Srgrimes i = 0; 4251553Srgrimes while (*cp >= '0' && *cp <= '9') 4261553Srgrimes i = i * 10 + (*cp++ - '0'); 4271553Srgrimes fdev = i; 4281553Srgrimes cp++; 4291553Srgrimes i = 0; 4301553Srgrimes while (*cp >= '0' && *cp <= '9') 4311553Srgrimes i = i * 10 + (*cp++ - '0'); 4321553Srgrimes fino = i; 4331553Srgrimes continue; 4341553Srgrimes 4351553Srgrimes case 'J': 43627748Simp if (line[1] != '\0') { 43727748Simp strncpy(jobname, line+1, sizeof(jobname) - 1); 43827748Simp jobname[sizeof(jobname) - 1] = '\0'; 43927748Simp } else 4401553Srgrimes strcpy(jobname, " "); 4411553Srgrimes continue; 4421553Srgrimes 4431553Srgrimes case 'C': 4441553Srgrimes if (line[1] != '\0') 44527748Simp strncpy(class, line+1, sizeof(class) - 1); 4461553Srgrimes else if (class[0] == '\0') 4471553Srgrimes gethostname(class, sizeof(class)); 44827748Simp class[sizeof(class) - 1] = '\0'; 4491553Srgrimes continue; 4501553Srgrimes 4511553Srgrimes case 'T': /* header title for pr */ 45227748Simp strncpy(title, line+1, sizeof(title) - 1); 45327748Simp title[sizeof(title) - 1] = '\0'; 4541553Srgrimes continue; 4551553Srgrimes 4561553Srgrimes case 'L': /* identification line */ 45731492Swollman if (!pp->no_header && !pp->header_last) 45831492Swollman banner(pp, line+1, jobname); 4591553Srgrimes continue; 4601553Srgrimes 4611553Srgrimes case '1': /* troff fonts */ 4621553Srgrimes case '2': 4631553Srgrimes case '3': 4641553Srgrimes case '4': 46527748Simp if (line[1] != '\0') { 46627748Simp strncpy(fonts[line[0]-'1'], line+1, 46727748Simp 50-1); 46827748Simp fonts[line[0]-'1'][50-1] = '\0'; 46927748Simp } 4701553Srgrimes continue; 4711553Srgrimes 4721553Srgrimes case 'W': /* page width */ 47327748Simp strncpy(width+2, line+1, sizeof(width) - 3); 47427748Simp width[2+sizeof(width) - 3] = '\0'; 4751553Srgrimes continue; 4761553Srgrimes 4771553Srgrimes case 'I': /* indent amount */ 47827748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 47927748Simp indent[2+sizeof(indent) - 3] = '\0'; 4801553Srgrimes continue; 4811553Srgrimes 4821553Srgrimes default: /* some file to print */ 48331492Swollman switch (i = print(pp, line[0], line+1)) { 4841553Srgrimes case ERROR: 4851553Srgrimes if (bombed == OK) 4861553Srgrimes bombed = FATALERR; 4871553Srgrimes break; 4881553Srgrimes case REPRINT: 4891553Srgrimes (void) fclose(cfp); 4901553Srgrimes return(REPRINT); 4911553Srgrimes case FILTERERR: 4921553Srgrimes case ACCESS: 4931553Srgrimes bombed = i; 49431492Swollman sendmail(pp, logname, bombed); 4951553Srgrimes } 4961553Srgrimes title[0] = '\0'; 4971553Srgrimes continue; 4981553Srgrimes 4991553Srgrimes case 'N': 5001553Srgrimes case 'U': 5011553Srgrimes case 'M': 50215648Sjoerg case 'R': 5031553Srgrimes continue; 5041553Srgrimes } 5051553Srgrimes 5061553Srgrimes /* pass 2 */ 5071553Srgrimes 5081553Srgrimespass2: 5091553Srgrimes fseek(cfp, 0L, 0); 5101553Srgrimes while (getline(cfp)) 5111553Srgrimes switch (line[0]) { 5121553Srgrimes case 'L': /* identification line */ 51331492Swollman if (!pp->no_header && pp->header_last) 51431492Swollman banner(pp, line+1, jobname); 5151553Srgrimes continue; 5161553Srgrimes 5171553Srgrimes case 'M': 5181553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 51931492Swollman sendmail(pp, line+1, bombed); 5201553Srgrimes continue; 5211553Srgrimes 5221553Srgrimes case 'U': 52327748Simp if (strchr(line+1, '/')) 52427748Simp continue; 5251553Srgrimes (void) unlink(line+1); 5261553Srgrimes } 5271553Srgrimes /* 5281553Srgrimes * clean-up in case another control file exists 5291553Srgrimes */ 5301553Srgrimes (void) fclose(cfp); 5311553Srgrimes (void) unlink(file); 5321553Srgrimes return(bombed == OK ? OK : ERROR); 5331553Srgrimes} 5341553Srgrimes 5351553Srgrimes/* 5361553Srgrimes * Print a file. 5371553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 5381553Srgrimes * Return -1 if a non-recoverable error occured, 5391553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 5401553Srgrimes * 1 if we should try to reprint this job and 5411553Srgrimes * 0 if all is well. 5421553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 5431553Srgrimes * stderr as the log file, and must not ignore SIGINT. 5441553Srgrimes */ 5451553Srgrimesstatic int 54631492Swollmanprint(pp, format, file) 54731492Swollman struct printer *pp; 5481553Srgrimes int format; 5491553Srgrimes char *file; 5501553Srgrimes{ 5511553Srgrimes register int n; 5521553Srgrimes register char *prog; 55331492Swollman int fi, fo; 5541553Srgrimes FILE *fp; 5551553Srgrimes char *av[15], buf[BUFSIZ]; 5561553Srgrimes int pid, p[2], stopped = 0; 5571553Srgrimes union wait status; 5581553Srgrimes struct stat stb; 5591553Srgrimes 5601553Srgrimes if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) 5611553Srgrimes return(ERROR); 5621553Srgrimes /* 5631553Srgrimes * Check to see if data file is a symbolic link. If so, it should 5641553Srgrimes * still point to the same file or someone is trying to print 5651553Srgrimes * something he shouldn't. 5661553Srgrimes */ 5671553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 5681553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 5691553Srgrimes return(ACCESS); 57031492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 57131492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 57231492Swollman pp->tof = 1; 5731553Srgrimes } 57431492Swollman if (pp->filters[LPF_INPUT] == NULL 57531492Swollman && (format == 'f' || format == 'l')) { 57631492Swollman pp->tof = 0; 5771553Srgrimes while ((n = read(fi, buf, BUFSIZ)) > 0) 5781553Srgrimes if (write(ofd, buf, n) != n) { 5791553Srgrimes (void) close(fi); 5801553Srgrimes return(REPRINT); 5811553Srgrimes } 5821553Srgrimes (void) close(fi); 5831553Srgrimes return(OK); 5841553Srgrimes } 5851553Srgrimes switch (format) { 5861553Srgrimes case 'p': /* print file using 'pr' */ 58731492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 5881553Srgrimes prog = _PATH_PR; 5891553Srgrimes av[0] = "pr"; 5901553Srgrimes av[1] = width; 5911553Srgrimes av[2] = length; 5921553Srgrimes av[3] = "-h"; 5931553Srgrimes av[4] = *title ? title : " "; 5945445Sjoerg av[5] = "-F"; 5955445Sjoerg av[6] = 0; 5961553Srgrimes fo = ofd; 5971553Srgrimes goto start; 5981553Srgrimes } 5991553Srgrimes pipe(p); 60031492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 6011553Srgrimes dup2(fi, 0); /* file is stdin */ 6021553Srgrimes dup2(p[1], 1); /* pipe is stdout */ 6038094Sjkh closelog(); 60431492Swollman closeallfds(3); 6051553Srgrimes execl(_PATH_PR, "pr", width, length, 6065445Sjoerg "-h", *title ? title : " ", "-F", 0); 6071553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6081553Srgrimes exit(2); 6091553Srgrimes } 6101553Srgrimes (void) close(p[1]); /* close output side */ 6111553Srgrimes (void) close(fi); 6121553Srgrimes if (prchild < 0) { 6131553Srgrimes prchild = 0; 6141553Srgrimes (void) close(p[0]); 6151553Srgrimes return(ERROR); 6161553Srgrimes } 6171553Srgrimes fi = p[0]; /* use pipe for input */ 6181553Srgrimes case 'f': /* print plain text file */ 61931492Swollman prog = pp->filters[LPF_INPUT]; 6201553Srgrimes av[1] = width; 6211553Srgrimes av[2] = length; 6221553Srgrimes av[3] = indent; 6231553Srgrimes n = 4; 6241553Srgrimes break; 6251553Srgrimes case 'l': /* like 'f' but pass control characters */ 62631492Swollman prog = pp->filters[LPF_INPUT]; 6271553Srgrimes av[1] = "-c"; 6281553Srgrimes av[2] = width; 6291553Srgrimes av[3] = length; 6301553Srgrimes av[4] = indent; 6311553Srgrimes n = 5; 6321553Srgrimes break; 6331553Srgrimes case 'r': /* print a fortran text file */ 63431492Swollman prog = pp->filters[LPF_FORTRAN]; 6351553Srgrimes av[1] = width; 6361553Srgrimes av[2] = length; 6371553Srgrimes n = 3; 6381553Srgrimes break; 6391553Srgrimes case 't': /* print troff output */ 6401553Srgrimes case 'n': /* print ditroff output */ 6411553Srgrimes case 'd': /* print tex output */ 6421553Srgrimes (void) unlink(".railmag"); 6431553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 64431492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 64531492Swollman pp->printer); 6461553Srgrimes (void) unlink(".railmag"); 6471553Srgrimes } else { 6481553Srgrimes for (n = 0; n < 4; n++) { 6491553Srgrimes if (fonts[n][0] != '/') 6501553Srgrimes (void) write(fo, _PATH_VFONT, 6511553Srgrimes sizeof(_PATH_VFONT) - 1); 6521553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 6531553Srgrimes (void) write(fo, "\n", 1); 6541553Srgrimes } 6551553Srgrimes (void) close(fo); 6561553Srgrimes } 65731492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 65831492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 65931492Swollman : pp->filters[LPF_DVI]); 6601553Srgrimes av[1] = pxwidth; 6611553Srgrimes av[2] = pxlength; 6621553Srgrimes n = 3; 6631553Srgrimes break; 6641553Srgrimes case 'c': /* print cifplot output */ 66531492Swollman prog = pp->filters[LPF_CIFPLOT]; 6661553Srgrimes av[1] = pxwidth; 6671553Srgrimes av[2] = pxlength; 6681553Srgrimes n = 3; 6691553Srgrimes break; 6701553Srgrimes case 'g': /* print plot(1G) output */ 67131492Swollman prog = pp->filters[LPF_GRAPH]; 6721553Srgrimes av[1] = pxwidth; 6731553Srgrimes av[2] = pxlength; 6741553Srgrimes n = 3; 6751553Srgrimes break; 6761553Srgrimes case 'v': /* print raster output */ 67731492Swollman prog = pp->filters[LPF_RASTER]; 6781553Srgrimes av[1] = pxwidth; 6791553Srgrimes av[2] = pxlength; 6801553Srgrimes n = 3; 6811553Srgrimes break; 6821553Srgrimes default: 6831553Srgrimes (void) close(fi); 6841553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 68531492Swollman pp->printer, format); 6861553Srgrimes return(ERROR); 6871553Srgrimes } 68815648Sjoerg if (prog == NULL) { 68915648Sjoerg (void) close(fi); 69015648Sjoerg syslog(LOG_ERR, 69115648Sjoerg "%s: no filter found in printcap for format character '%c'", 69231492Swollman pp->printer, format); 69315648Sjoerg return(ERROR); 69415648Sjoerg } 69527635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 6961553Srgrimes av[0]++; 6971553Srgrimes else 6981553Srgrimes av[0] = prog; 6991553Srgrimes av[n++] = "-n"; 7001553Srgrimes av[n++] = logname; 7011553Srgrimes av[n++] = "-h"; 7021553Srgrimes av[n++] = fromhost; 70331492Swollman av[n++] = pp->acct_file; 7041553Srgrimes av[n] = 0; 7051553Srgrimes fo = pfd; 7061553Srgrimes if (ofilter > 0) { /* stop output filter */ 7071553Srgrimes write(ofd, "\031\1", 2); 7081553Srgrimes while ((pid = 7091553Srgrimes wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter) 7101553Srgrimes ; 7111553Srgrimes if (status.w_stopval != WSTOPPED) { 7121553Srgrimes (void) close(fi); 71315648Sjoerg syslog(LOG_WARNING, 71431492Swollman "%s: output filter died " 71531492Swollman "(retcode=%d termsig=%d)", 71631492Swollman pp->printer, status.w_retcode, 71731492Swollman status.w_termsig); 7181553Srgrimes return(REPRINT); 7191553Srgrimes } 7201553Srgrimes stopped++; 7211553Srgrimes } 7221553Srgrimesstart: 72331492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 7241553Srgrimes dup2(fi, 0); 7251553Srgrimes dup2(fo, 1); 7261553Srgrimes n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0664); 7271553Srgrimes if (n >= 0) 7281553Srgrimes dup2(n, 2); 7298094Sjkh closelog(); 73031492Swollman closeallfds(3); 7311553Srgrimes execv(prog, av); 7321553Srgrimes syslog(LOG_ERR, "cannot execv %s", prog); 7331553Srgrimes exit(2); 7341553Srgrimes } 7351553Srgrimes (void) close(fi); 7361553Srgrimes if (child < 0) 7371553Srgrimes status.w_retcode = 100; 7381553Srgrimes else 7391553Srgrimes while ((pid = wait((int *)&status)) > 0 && pid != child) 7401553Srgrimes ; 7411553Srgrimes child = 0; 7421553Srgrimes prchild = 0; 7431553Srgrimes if (stopped) { /* restart output filter */ 7441553Srgrimes if (kill(ofilter, SIGCONT) < 0) { 7451553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 7461553Srgrimes exit(1); 7471553Srgrimes } 7481553Srgrimes } 74931492Swollman pp->tof = 0; 7501553Srgrimes 7511553Srgrimes /* Copy filter output to "lf" logfile */ 75227748Simp if ((fp = fopen(tempfile, "r"))) { 7531553Srgrimes while (fgets(buf, sizeof(buf), fp)) 7541553Srgrimes fputs(buf, stderr); 7551553Srgrimes fclose(fp); 7561553Srgrimes } 7571553Srgrimes 7581553Srgrimes if (!WIFEXITED(status)) { 75915648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 76031492Swollman pp->printer, format, status.w_termsig); 7611553Srgrimes return(ERROR); 7621553Srgrimes } 7631553Srgrimes switch (status.w_retcode) { 7641553Srgrimes case 0: 76531492Swollman pp->tof = 1; 7661553Srgrimes return(OK); 7671553Srgrimes case 1: 7681553Srgrimes return(REPRINT); 76915648Sjoerg case 2: 77015648Sjoerg return(ERROR); 7711553Srgrimes default: 77215648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 77331492Swollman pp->printer, format, status.w_retcode); 77415648Sjoerg return(FILTERERR); 7751553Srgrimes } 7761553Srgrimes} 7771553Srgrimes 7781553Srgrimes/* 7791553Srgrimes * Send the daemon control file (cf) and any data files. 7801553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 7811553Srgrimes * 0 if all is well. 7821553Srgrimes */ 7831553Srgrimesstatic int 78431492Swollmansendit(pp, file) 78531492Swollman struct printer *pp; 7861553Srgrimes char *file; 7871553Srgrimes{ 7881553Srgrimes register int i, err = OK; 7891553Srgrimes char *cp, last[BUFSIZ]; 7901553Srgrimes 7911553Srgrimes /* 7921553Srgrimes * open control file 7931553Srgrimes */ 7941553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 7951553Srgrimes return(OK); 7961553Srgrimes /* 7971553Srgrimes * read the control file for work to do 7981553Srgrimes * 7991553Srgrimes * file format -- first character in the line is a command 8001553Srgrimes * rest of the line is the argument. 8011553Srgrimes * commands of interest are: 8021553Srgrimes * 8031553Srgrimes * a-z -- "file name" name of file to print 8041553Srgrimes * U -- "unlink" name of file to remove 8051553Srgrimes * (after we print it. (Pass 2 only)). 8061553Srgrimes */ 8071553Srgrimes 8081553Srgrimes /* 8091553Srgrimes * pass 1 8101553Srgrimes */ 8111553Srgrimes while (getline(cfp)) { 8121553Srgrimes again: 8131553Srgrimes if (line[0] == 'S') { 8141553Srgrimes cp = line+1; 8151553Srgrimes i = 0; 8161553Srgrimes while (*cp >= '0' && *cp <= '9') 8171553Srgrimes i = i * 10 + (*cp++ - '0'); 8181553Srgrimes fdev = i; 8191553Srgrimes cp++; 8201553Srgrimes i = 0; 8211553Srgrimes while (*cp >= '0' && *cp <= '9') 8221553Srgrimes i = i * 10 + (*cp++ - '0'); 8231553Srgrimes fino = i; 82424831Sbrian } else if (line[0] == 'H') { 82524831Sbrian strcpy(fromhost, line+1); 82624831Sbrian if (class[0] == '\0') 82727748Simp strncpy(class, line+1, sizeof(class) - 1); 82824831Sbrian } else if (line[0] == 'P') { 82927748Simp strncpy(logname, line+1, sizeof(logname) - 1); 83031492Swollman if (pp->restricted) { /* restricted */ 83124831Sbrian if (getpwnam(logname) == NULL) { 83231492Swollman sendmail(pp, line+1, NOACCT); 83324831Sbrian err = ERROR; 83424831Sbrian break; 83524831Sbrian } 83624831Sbrian } 83724831Sbrian } else if (line[0] == 'I') { 83827748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 83924831Sbrian } else if (line[0] >= 'a' && line[0] <= 'z') { 8401553Srgrimes strcpy(last, line); 84131492Swollman while ((i = getline(cfp)) != 0) 8421553Srgrimes if (strcmp(last, line)) 8431553Srgrimes break; 84431492Swollman switch (sendfile(pp, '\3', last+1, *last)) { 8451553Srgrimes case OK: 8461553Srgrimes if (i) 8471553Srgrimes goto again; 8481553Srgrimes break; 8491553Srgrimes case REPRINT: 8501553Srgrimes (void) fclose(cfp); 8511553Srgrimes return(REPRINT); 8521553Srgrimes case ACCESS: 85331492Swollman sendmail(pp, logname, ACCESS); 8541553Srgrimes case ERROR: 8551553Srgrimes err = ERROR; 8561553Srgrimes } 8571553Srgrimes break; 8581553Srgrimes } 8591553Srgrimes } 86031492Swollman if (err == OK && sendfile(pp, '\2', file, '\0') > 0) { 8611553Srgrimes (void) fclose(cfp); 8621553Srgrimes return(REPRINT); 8631553Srgrimes } 8641553Srgrimes /* 8651553Srgrimes * pass 2 8661553Srgrimes */ 8671553Srgrimes fseek(cfp, 0L, 0); 8681553Srgrimes while (getline(cfp)) 86927748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 8701553Srgrimes (void) unlink(line+1); 8711553Srgrimes /* 8721553Srgrimes * clean-up in case another control file exists 8731553Srgrimes */ 8741553Srgrimes (void) fclose(cfp); 8751553Srgrimes (void) unlink(file); 8761553Srgrimes return(err); 8771553Srgrimes} 8781553Srgrimes 8791553Srgrimes/* 8801553Srgrimes * Send a data file to the remote machine and spool it. 8811553Srgrimes * Return positive if we should try resending. 8821553Srgrimes */ 8831553Srgrimesstatic int 88431492Swollmansendfile(pp, type, file, format) 88531492Swollman struct printer *pp; 8861553Srgrimes int type; 8871553Srgrimes char *file; 88824831Sbrian char format; 8891553Srgrimes{ 8901553Srgrimes register int f, i, amt; 8911553Srgrimes struct stat stb; 8921553Srgrimes char buf[BUFSIZ]; 89324831Sbrian int sizerr, resp, closedpr; 8941553Srgrimes 8951553Srgrimes if (lstat(file, &stb) < 0 || (f = open(file, O_RDONLY)) < 0) 8961553Srgrimes return(ERROR); 8971553Srgrimes /* 8981553Srgrimes * Check to see if data file is a symbolic link. If so, it should 8991553Srgrimes * still point to the same file or someone is trying to print something 9001553Srgrimes * he shouldn't. 9011553Srgrimes */ 9021553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 && 9031553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 9041553Srgrimes return(ACCESS); 90524831Sbrian 90624831Sbrian sizerr = 0; 90724831Sbrian closedpr = 0; 90824831Sbrian if (type == '\3') { 90931492Swollman if (pp->filters[LPF_INPUT]) { 91024831Sbrian /* 91124831Sbrian * We're sending something with an ifilter, we have to 91224831Sbrian * run the ifilter and store the output as a 91324831Sbrian * temporary file (tfile)... the protocol requires us 91424831Sbrian * to send the file size 91524831Sbrian */ 91624831Sbrian char *av[15]; 91724831Sbrian int n; 91824831Sbrian int ifilter; 91931492Swollman union wait status; /* XXX */ 92024831Sbrian 92124831Sbrian strcpy(tfile,TFILENAME); 92224831Sbrian if ((tfd = mkstemp(tfile)) == -1) { 92324831Sbrian syslog(LOG_ERR, "mkstemp: %m"); 92424831Sbrian return(ERROR); 92524831Sbrian } 92631492Swollman if ((av[0] = strrchr(pp->filters[LPF_INPUT], '/')) == NULL) 92731492Swollman av[0] = pp->filters[LPF_INPUT]; 92824831Sbrian else 92924831Sbrian av[0]++; 93024831Sbrian if (format == 'l') 93124831Sbrian av[n=1] = "-c"; 93224831Sbrian else 93324831Sbrian n = 0; 93424831Sbrian av[++n] = width; 93524831Sbrian av[++n] = length; 93624831Sbrian av[++n] = indent; 93724831Sbrian av[++n] = "-n"; 93824831Sbrian av[++n] = logname; 93924831Sbrian av[++n] = "-h"; 94024831Sbrian av[++n] = fromhost; 94131492Swollman av[++n] = pp->acct_file; 94224831Sbrian av[++n] = 0; 94331492Swollman if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */ 94424831Sbrian dup2(f, 0); 94524831Sbrian dup2(tfd, 1); 94631492Swollman n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 94731492Swollman TEMP_FILE_MODE); 94824831Sbrian if (n >= 0) 94924831Sbrian dup2(n, 2); 95024831Sbrian closelog(); 95131492Swollman closeallfds(3); 95231492Swollman execv(pp->filters[LPF_INPUT], av); 95331492Swollman syslog(LOG_ERR, "cannot execv %s", 95431492Swollman pp->filters[LPF_INPUT]); 95524831Sbrian exit(2); 95624831Sbrian } 95724831Sbrian (void) close(f); 95824831Sbrian if (ifilter < 0) 95924831Sbrian status.w_retcode = 100; 96024831Sbrian else 96124831Sbrian while ((pid = wait((int *)&status)) > 0 && 96224831Sbrian pid != ifilter) 96324831Sbrian ; 96424831Sbrian switch (status.w_retcode) { 96524831Sbrian case 0: 96624831Sbrian break; 96724831Sbrian case 1: 96824831Sbrian unlink(tfile); 96924831Sbrian return(REPRINT); 97024831Sbrian case 2: 97124831Sbrian unlink(tfile); 97224831Sbrian return(ERROR); 97324831Sbrian default: 97424831Sbrian syslog(LOG_WARNING, "%s: filter '%c' exited" 97524831Sbrian " (retcode=%d)", 97631492Swollman pp->printer, format, status.w_retcode); 97724831Sbrian unlink(tfile); 97824831Sbrian return(FILTERERR); 97924831Sbrian } 98024831Sbrian if (fstat(tfd, &stb) < 0) /* the size of tfile */ 98124831Sbrian return(ERROR); 98224831Sbrian f = tfd; 98324831Sbrian lseek(f,0,SEEK_SET); 98424831Sbrian } else if (ofilter) { 98524831Sbrian /* 98624831Sbrian * We're sending something with an ofilter, we have to 98724831Sbrian * store the output as a temporary file (tfile)... the 98824831Sbrian * protocol requires us to send the file size 98924831Sbrian */ 99024831Sbrian int i; 99124831Sbrian for (i = 0; i < stb.st_size; i += BUFSIZ) { 99224831Sbrian amt = BUFSIZ; 99324831Sbrian if (i + amt > stb.st_size) 99424831Sbrian amt = stb.st_size - i; 99524831Sbrian if (sizerr == 0 && read(f, buf, amt) != amt) { 99624831Sbrian sizerr = 1; 99724831Sbrian break; 99824831Sbrian } 99924831Sbrian if (write(ofd, buf, amt) != amt) { 100024831Sbrian (void) close(f); 100124831Sbrian return(REPRINT); 100224831Sbrian } 100324831Sbrian } 100424831Sbrian close(ofd); 100524831Sbrian close(f); 100624831Sbrian while ((i = wait(NULL)) > 0 && i != ofilter) 100724831Sbrian ; 100824831Sbrian ofilter = 0; 100924831Sbrian if (fstat(tfd, &stb) < 0) { /* the size of tfile */ 101031492Swollman openpr(pp); 101124831Sbrian return(ERROR); 101224831Sbrian } 101324831Sbrian f = tfd; 101424831Sbrian lseek(f,0,SEEK_SET); 101524831Sbrian closedpr = 1; 101624831Sbrian } 101724831Sbrian } 101824831Sbrian 10191553Srgrimes (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 10201553Srgrimes amt = strlen(buf); 10211553Srgrimes for (i = 0; ; i++) { 10221553Srgrimes if (write(pfd, buf, amt) != amt || 102331492Swollman (resp = response(pp)) < 0 || resp == '\1') { 10241553Srgrimes (void) close(f); 102524831Sbrian if (tfd != -1 && type == '\3') { 102624831Sbrian tfd = -1; 102724831Sbrian unlink(tfile); 102824831Sbrian if (closedpr) 102931492Swollman openpr(pp); 103024831Sbrian } 10311553Srgrimes return(REPRINT); 10321553Srgrimes } else if (resp == '\0') 10331553Srgrimes break; 10341553Srgrimes if (i == 0) 103531492Swollman pstatus(pp, 103631492Swollman "no space on remote; waiting for queue to drain"); 10371553Srgrimes if (i == 10) 10381553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 103931492Swollman pp->printer, pp->remote_host); 10401553Srgrimes sleep(5 * 60); 10411553Srgrimes } 10421553Srgrimes if (i) 104331492Swollman pstatus(pp, "sending to %s", pp->remote_host); 10441553Srgrimes for (i = 0; i < stb.st_size; i += BUFSIZ) { 10451553Srgrimes amt = BUFSIZ; 10461553Srgrimes if (i + amt > stb.st_size) 10471553Srgrimes amt = stb.st_size - i; 10481553Srgrimes if (sizerr == 0 && read(f, buf, amt) != amt) 10491553Srgrimes sizerr = 1; 10501553Srgrimes if (write(pfd, buf, amt) != amt) { 10511553Srgrimes (void) close(f); 105224831Sbrian if (tfd != -1 && type == '\3') { 105324831Sbrian tfd = -1; 105424831Sbrian unlink(tfile); 105524831Sbrian if (closedpr) 105631492Swollman openpr(pp); 105724831Sbrian } 10581553Srgrimes return(REPRINT); 10591553Srgrimes } 10601553Srgrimes } 10611553Srgrimes 10621553Srgrimes (void) close(f); 106324831Sbrian if (tfd != -1 && type == '\3') { 106424831Sbrian tfd = -1; 106524831Sbrian unlink(tfile); 106624831Sbrian } 10671553Srgrimes if (sizerr) { 106831492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 10691553Srgrimes /* tell recvjob to ignore this file */ 10701553Srgrimes (void) write(pfd, "\1", 1); 107124831Sbrian if (closedpr) 107231492Swollman openpr(pp); 10731553Srgrimes return(ERROR); 10741553Srgrimes } 107531492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 107624831Sbrian if (closedpr) 107731492Swollman openpr(pp); 10781553Srgrimes return(REPRINT); 107924831Sbrian } 108024831Sbrian if (closedpr) 108131492Swollman openpr(pp); 10821553Srgrimes return(OK); 10831553Srgrimes} 10841553Srgrimes 10851553Srgrimes/* 10861553Srgrimes * Check to make sure there have been no errors and that both programs 10871553Srgrimes * are in sync with eachother. 10881553Srgrimes * Return non-zero if the connection was lost. 10891553Srgrimes */ 10901553Srgrimesstatic char 109131492Swollmanresponse(pp) 109231492Swollman const struct printer *pp; 10931553Srgrimes{ 10941553Srgrimes char resp; 10951553Srgrimes 10961553Srgrimes if (read(pfd, &resp, 1) != 1) { 109731492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 10981553Srgrimes return(-1); 10991553Srgrimes } 11001553Srgrimes return(resp); 11011553Srgrimes} 11021553Srgrimes 11031553Srgrimes/* 11041553Srgrimes * Banner printing stuff 11051553Srgrimes */ 11061553Srgrimesstatic void 110731492Swollmanbanner(pp, name1, name2) 110831492Swollman struct printer *pp; 11091553Srgrimes char *name1, *name2; 11101553Srgrimes{ 11111553Srgrimes time_t tvec; 11121553Srgrimes 11131553Srgrimes time(&tvec); 111431492Swollman if (!pp->no_formfeed && !pp->tof) 111531492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 111631492Swollman if (pp->short_banner) { /* short banner only */ 11171553Srgrimes if (class[0]) { 11181553Srgrimes (void) write(ofd, class, strlen(class)); 11191553Srgrimes (void) write(ofd, ":", 1); 11201553Srgrimes } 11211553Srgrimes (void) write(ofd, name1, strlen(name1)); 11221553Srgrimes (void) write(ofd, " Job: ", 7); 11231553Srgrimes (void) write(ofd, name2, strlen(name2)); 11241553Srgrimes (void) write(ofd, " Date: ", 8); 11251553Srgrimes (void) write(ofd, ctime(&tvec), 24); 11261553Srgrimes (void) write(ofd, "\n", 1); 11271553Srgrimes } else { /* normal banner */ 11281553Srgrimes (void) write(ofd, "\n\n\n", 3); 112931492Swollman scan_out(pp, ofd, name1, '\0'); 11301553Srgrimes (void) write(ofd, "\n\n", 2); 113131492Swollman scan_out(pp, ofd, name2, '\0'); 11321553Srgrimes if (class[0]) { 11331553Srgrimes (void) write(ofd,"\n\n\n",3); 113431492Swollman scan_out(pp, ofd, class, '\0'); 11351553Srgrimes } 11361553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 11371553Srgrimes (void) write(ofd, name2, strlen(name2)); 11381553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 11391553Srgrimes (void) write(ofd, ctime(&tvec), 24); 11401553Srgrimes (void) write(ofd, "\n", 1); 11411553Srgrimes } 114231492Swollman if (!pp->no_formfeed) 114331492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 114431492Swollman pp->tof = 1; 11451553Srgrimes} 11461553Srgrimes 11471553Srgrimesstatic char * 11481553Srgrimesscnline(key, p, c) 11491553Srgrimes register int key; 11501553Srgrimes register char *p; 11511553Srgrimes int c; 11521553Srgrimes{ 11531553Srgrimes register scnwidth; 11541553Srgrimes 11551553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 11561553Srgrimes key <<= 1; 11571553Srgrimes *p++ = key & 0200 ? c : BACKGND; 11581553Srgrimes } 11591553Srgrimes return (p); 11601553Srgrimes} 11611553Srgrimes 11621553Srgrimes#define TRC(q) (((q)-' ')&0177) 11631553Srgrimes 11641553Srgrimesstatic void 116531492Swollmanscan_out(pp, scfd, scsp, dlm) 116631492Swollman struct printer *pp; 11671553Srgrimes int scfd, dlm; 11681553Srgrimes char *scsp; 11691553Srgrimes{ 11701553Srgrimes register char *strp; 11711553Srgrimes register nchrs, j; 11721553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 11731553Srgrimes int d, scnhgt; 11741553Srgrimes 11751553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 11761553Srgrimes strp = &outbuf[0]; 11771553Srgrimes sp = scsp; 11781553Srgrimes for (nchrs = 0; ; ) { 11791553Srgrimes d = dropit(c = TRC(cc = *sp++)); 11801553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 11811553Srgrimes for (j = WIDTH; --j;) 11821553Srgrimes *strp++ = BACKGND; 11831553Srgrimes else 118431492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 118531492Swollman if (*sp == dlm || *sp == '\0' || 118631492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 11871553Srgrimes break; 11881553Srgrimes *strp++ = BACKGND; 11891553Srgrimes *strp++ = BACKGND; 11901553Srgrimes } 11911553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 11921553Srgrimes ; 11931553Srgrimes strp++; 11948857Srgrimes *strp++ = '\n'; 11951553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 11961553Srgrimes } 11971553Srgrimes} 11981553Srgrimes 11991553Srgrimesstatic int 12001553Srgrimesdropit(c) 12011553Srgrimes int c; 12021553Srgrimes{ 12031553Srgrimes switch(c) { 12041553Srgrimes 12051553Srgrimes case TRC('_'): 12061553Srgrimes case TRC(';'): 12071553Srgrimes case TRC(','): 12081553Srgrimes case TRC('g'): 12091553Srgrimes case TRC('j'): 12101553Srgrimes case TRC('p'): 12111553Srgrimes case TRC('q'): 12121553Srgrimes case TRC('y'): 12131553Srgrimes return (DROP); 12141553Srgrimes 12151553Srgrimes default: 12161553Srgrimes return (0); 12171553Srgrimes } 12181553Srgrimes} 12191553Srgrimes 12201553Srgrimes/* 12211553Srgrimes * sendmail --- 12221553Srgrimes * tell people about job completion 12231553Srgrimes */ 12241553Srgrimesstatic void 122531492Swollmansendmail(pp, user, bombed) 122631492Swollman struct printer *pp; 12271553Srgrimes char *user; 12281553Srgrimes int bombed; 12291553Srgrimes{ 12301553Srgrimes register int i; 12311553Srgrimes int p[2], s; 12321553Srgrimes register char *cp; 12331553Srgrimes struct stat stb; 12341553Srgrimes FILE *fp; 12351553Srgrimes 12361553Srgrimes pipe(p); 123731492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 12381553Srgrimes dup2(p[0], 0); 12398094Sjkh closelog(); 124031492Swollman closeallfds(3); 124127635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 12421553Srgrimes cp++; 124331492Swollman else 12441553Srgrimes cp = _PATH_SENDMAIL; 124527757Simp execl(_PATH_SENDMAIL, cp, "-t", 0); 124631492Swollman _exit(0); 12471553Srgrimes } else if (s > 0) { /* parent */ 12481553Srgrimes dup2(p[1], 1); 12491553Srgrimes printf("To: %s@%s\n", user, fromhost); 125031492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 125115648Sjoerg *jobname ? jobname : "<unknown>"); 125215648Sjoerg printf("Reply-To: root@%s\n\n", host); 12531553Srgrimes printf("Your printer job "); 12541553Srgrimes if (*jobname) 12551553Srgrimes printf("(%s) ", jobname); 125631492Swollman 125731492Swollman cp = "XXX compiler confusion"; /* XXX shut GCC up */ 12581553Srgrimes switch (bombed) { 12591553Srgrimes case OK: 12601553Srgrimes printf("\ncompleted successfully\n"); 126115648Sjoerg cp = "OK"; 12621553Srgrimes break; 12631553Srgrimes default: 12641553Srgrimes case FATALERR: 12651553Srgrimes printf("\ncould not be printed\n"); 126615648Sjoerg cp = "FATALERR"; 12671553Srgrimes break; 12681553Srgrimes case NOACCT: 12691553Srgrimes printf("\ncould not be printed without an account on %s\n", host); 127015648Sjoerg cp = "NOACCT"; 12711553Srgrimes break; 12721553Srgrimes case FILTERERR: 12731553Srgrimes if (stat(tempfile, &stb) < 0 || stb.st_size == 0 || 12741553Srgrimes (fp = fopen(tempfile, "r")) == NULL) { 127515648Sjoerg printf("\nhad some errors and may not have printed\n"); 12761553Srgrimes break; 12771553Srgrimes } 127815648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 12791553Srgrimes while ((i = getc(fp)) != EOF) 12801553Srgrimes putchar(i); 12811553Srgrimes (void) fclose(fp); 128215648Sjoerg cp = "FILTERERR"; 12831553Srgrimes break; 12841553Srgrimes case ACCESS: 12851553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 128615648Sjoerg cp = "ACCESS"; 12871553Srgrimes } 12881553Srgrimes fflush(stdout); 12891553Srgrimes (void) close(1); 129031492Swollman } else { 129131492Swollman syslog(LOG_WARNING, "unable to send mail to %s: %m", user); 129231492Swollman return; 12931553Srgrimes } 12941553Srgrimes (void) close(p[0]); 12951553Srgrimes (void) close(p[1]); 129615648Sjoerg wait(NULL); 129715648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 129831492Swollman user, *jobname ? jobname : "<unknown>", pp->printer, cp); 12991553Srgrimes} 13001553Srgrimes 13011553Srgrimes/* 13021553Srgrimes * dofork - fork with retries on failure 13031553Srgrimes */ 13041553Srgrimesstatic int 130531492Swollmandofork(pp, action) 130631492Swollman const struct printer *pp; 13071553Srgrimes int action; 13081553Srgrimes{ 13091553Srgrimes register int i, pid; 13101553Srgrimes 13111553Srgrimes for (i = 0; i < 20; i++) { 13121553Srgrimes if ((pid = fork()) < 0) { 13131553Srgrimes sleep((unsigned)(i*i)); 13141553Srgrimes continue; 13151553Srgrimes } 13161553Srgrimes /* 13171553Srgrimes * Child should run as daemon instead of root 13181553Srgrimes */ 131915648Sjoerg if (pid == 0) 132031492Swollman setuid(pp->daemon_user); 13211553Srgrimes return(pid); 13221553Srgrimes } 13231553Srgrimes syslog(LOG_ERR, "can't fork"); 13241553Srgrimes 13251553Srgrimes switch (action) { 13261553Srgrimes case DORETURN: 13271553Srgrimes return (-1); 13281553Srgrimes default: 13291553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 13301553Srgrimes /*FALL THRU*/ 13311553Srgrimes case DOABORT: 13321553Srgrimes exit(1); 13331553Srgrimes } 13341553Srgrimes /*NOTREACHED*/ 13351553Srgrimes} 13361553Srgrimes 13371553Srgrimes/* 13381553Srgrimes * Kill child processes to abort current job. 13391553Srgrimes */ 13401553Srgrimesstatic void 13411553Srgrimesabortpr(signo) 13421553Srgrimes int signo; 13431553Srgrimes{ 13441553Srgrimes (void) unlink(tempfile); 13451553Srgrimes kill(0, SIGINT); 13461553Srgrimes if (ofilter > 0) 13471553Srgrimes kill(ofilter, SIGCONT); 13481553Srgrimes while (wait(NULL) > 0) 13491553Srgrimes ; 135024831Sbrian if (ofilter > 0 && tfd != -1) 135124831Sbrian unlink(tfile); 13521553Srgrimes exit(0); 13531553Srgrimes} 13541553Srgrimes 13551553Srgrimesstatic void 135631492Swollmaninit(pp) 135731492Swollman struct printer *pp; 13581553Srgrimes{ 13591553Srgrimes char *s; 13601553Srgrimes 136131492Swollman sprintf(&width[2], "%ld", pp->page_width); 136231492Swollman sprintf(&length[2], "%ld", pp->page_length); 136331492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 136431492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 136531492Swollman if ((s = checkremote(pp)) != 0) { 136631492Swollman syslog(LOG_WARNING, "%s", s); 136731492Swollman free(s); 136831492Swollman } 136931492Swollman} 137031492Swollman 137131492Swollmanvoid 137231492Swollmanstartprinting(printer) 137331492Swollman const char *printer; 137431492Swollman{ 137531492Swollman struct printer myprinter, *pp = &myprinter; 137631492Swollman int status; 137731492Swollman 137831492Swollman init_printer(pp); 137931492Swollman status = getprintcap(printer, pp); 138031492Swollman switch(status) { 138131492Swollman case PCAPERR_OSERR: 138231492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 13831553Srgrimes exit(1); 138431492Swollman case PCAPERR_NOTFOUND: 13851553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 13861553Srgrimes exit(1); 138731492Swollman case PCAPERR_TCLOOP: 138831492Swollman fatal(pp, "potential reference loop detected in printcap file"); 138931492Swollman default: 139031492Swollman break; 139131492Swollman } 139231492Swollman printjob(pp); 13931553Srgrimes} 13941553Srgrimes 13951553Srgrimes/* 13961553Srgrimes * Acquire line printer or remote connection. 13971553Srgrimes */ 13981553Srgrimesstatic void 139931492Swollmanopenpr(pp) 140031492Swollman const struct printer *pp; 14011553Srgrimes{ 140231492Swollman int p[2]; 140315648Sjoerg char *cp; 14041553Srgrimes 140531492Swollman if (pp->remote) { 140631492Swollman openrem(pp); 140731492Swollman } else if (*pp->lp) { 140831492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 140931492Swollman opennet(pp); 141015648Sjoerg else 141131492Swollman opentty(pp); 14121553Srgrimes } else { 14131553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 141431492Swollman pp->printer); 14151553Srgrimes exit(1); 14161553Srgrimes } 141715648Sjoerg 14181553Srgrimes /* 14191553Srgrimes * Start up an output filter, if needed. 14201553Srgrimes */ 142131492Swollman if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) { 14221553Srgrimes pipe(p); 142331492Swollman if (pp->remote) { 142431492Swollman strcpy(tfile, TFILENAME); 142524831Sbrian tfd = mkstemp(tfile); 142624831Sbrian } 142731492Swollman if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */ 14281553Srgrimes dup2(p[0], 0); /* pipe is std in */ 142924831Sbrian /* tfile/printer is stdout */ 143031492Swollman dup2(pp->remote ? tfd : pfd, 1); 14318094Sjkh closelog(); 143231492Swollman closeallfds(3); 143331492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 143431492Swollman cp = pp->filters[LPF_OUTPUT]; 14351553Srgrimes else 14361553Srgrimes cp++; 143731492Swollman execl(pp->filters[LPF_OUTPUT], cp, width, length, 0); 143831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 143931492Swollman pp->filters[LPF_OUTPUT]); 14401553Srgrimes exit(1); 14411553Srgrimes } 14421553Srgrimes (void) close(p[0]); /* close input side */ 14431553Srgrimes ofd = p[1]; /* use pipe for output */ 14441553Srgrimes } else { 14451553Srgrimes ofd = pfd; 14461553Srgrimes ofilter = 0; 14471553Srgrimes } 14481553Srgrimes} 14491553Srgrimes 145015648Sjoerg/* 145115648Sjoerg * Printer connected directly to the network 145215648Sjoerg * or to a terminal server on the net 145315648Sjoerg */ 145415648Sjoergstatic void 145531492Swollmanopennet(pp) 145631492Swollman const struct printer *pp; 145715648Sjoerg{ 145815648Sjoerg register int i; 145931492Swollman int resp; 146031492Swollman u_long port; 146131492Swollman char *ep; 146230407Sjoerg void (*savealrm)(int); 146315648Sjoerg 146431492Swollman port = strtoul(pp->lp, &ep, 0); 146538470Sbrian if (*ep != '@' || port > 65535) { 146631492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 146731492Swollman pp->lp); 146815648Sjoerg exit(1); 146915648Sjoerg } 147031492Swollman ep++; 147115648Sjoerg 147215648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 147315648Sjoerg resp = -1; 147430407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 147531492Swollman alarm(pp->conn_timeout); 147631492Swollman pfd = getport(pp, ep, port); 147731020Sjoerg alarm(0); 147830407Sjoerg (void)signal(SIGALRM, savealrm); 147915648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 148015648Sjoerg resp = 1; 148115648Sjoerg else if (pfd >= 0) { 148215648Sjoerg /* 148315648Sjoerg * need to delay a bit for rs232 lines 148415648Sjoerg * to stabilize in case printer is 148515648Sjoerg * connected via a terminal server 148615648Sjoerg */ 148715648Sjoerg delay(500); 148815648Sjoerg break; 148915648Sjoerg } 149015648Sjoerg if (i == 1) { 149131492Swollman if (resp < 0) 149231492Swollman pstatus(pp, "waiting for %s to come up", 149331492Swollman pp->lp); 149431492Swollman else 149531492Swollman pstatus(pp, 149631492Swollman "waiting for access to printer on %s", 149731492Swollman pp->lp); 149815648Sjoerg } 149915648Sjoerg sleep(i); 150015648Sjoerg } 150131492Swollman pstatus(pp, "sending to %s port %d", ep, port); 150215648Sjoerg} 150315648Sjoerg 150415648Sjoerg/* 150515648Sjoerg * Printer is connected to an RS232 port on this host 150615648Sjoerg */ 150715648Sjoergstatic void 150831492Swollmanopentty(pp) 150931492Swollman const struct printer *pp; 151015648Sjoerg{ 151115648Sjoerg register int i; 151215648Sjoerg 151315648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 151431492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 151515648Sjoerg if (pfd >= 0) { 151615648Sjoerg delay(500); 151715648Sjoerg break; 151815648Sjoerg } 151915648Sjoerg if (errno == ENOENT) { 152031492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 152115648Sjoerg exit(1); 152215648Sjoerg } 152315648Sjoerg if (i == 1) 152431492Swollman pstatus(pp, 152531492Swollman "waiting for %s to become ready (offline?)", 152631492Swollman pp->printer); 152715648Sjoerg sleep(i); 152815648Sjoerg } 152915648Sjoerg if (isatty(pfd)) 153031492Swollman setty(pp); 153131492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 153215648Sjoerg} 153315648Sjoerg 153415648Sjoerg/* 153515648Sjoerg * Printer is on a remote host 153615648Sjoerg */ 153715648Sjoergstatic void 153831492Swollmanopenrem(pp) 153931492Swollman const struct printer *pp; 154015648Sjoerg{ 154131492Swollman register int i; 154227748Simp int resp; 154330407Sjoerg void (*savealrm)(int); 154415648Sjoerg 154515648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 154615648Sjoerg resp = -1; 154730407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 154831492Swollman alarm(pp->conn_timeout); 154931492Swollman pfd = getport(pp, pp->remote_host, 0); 155031020Sjoerg alarm(0); 155130407Sjoerg (void)signal(SIGALRM, savealrm); 155215648Sjoerg if (pfd >= 0) { 155331492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 155431492Swollman (char *)0) 155531492Swollman == 2 + strlen(pp->remote_queue)) 155631492Swollman && (resp = response(pp)) == 0) 155715648Sjoerg break; 155815648Sjoerg (void) close(pfd); 155915648Sjoerg } 156015648Sjoerg if (i == 1) { 156115648Sjoerg if (resp < 0) 156231492Swollman pstatus(pp, "waiting for %s to come up", 156331492Swollman pp->remote_host); 156415648Sjoerg else { 156531492Swollman pstatus(pp, 156631492Swollman "waiting for queue to be enabled on %s", 156731492Swollman pp->remote_host); 156815648Sjoerg i = 256; 156915648Sjoerg } 157015648Sjoerg } 157115648Sjoerg sleep(i); 157215648Sjoerg } 157331492Swollman pstatus(pp, "sending to %s", pp->remote_host); 157415648Sjoerg} 157515648Sjoerg 15761553Srgrimes/* 15771553Srgrimes * setup tty lines. 15781553Srgrimes */ 15791553Srgrimesstatic void 158031492Swollmansetty(pp) 158131492Swollman const struct printer *pp; 15821553Srgrimes{ 158315032Ssef struct termios ttybuf; 15841553Srgrimes 15851553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 158631492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 15871553Srgrimes exit(1); 15881553Srgrimes } 158915032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 159031492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 15911553Srgrimes exit(1); 15921553Srgrimes } 159331492Swollman if (pp->baud_rate > 0) 159431492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 159531492Swollman if (pp->mode_set) { 159631492Swollman char *s = strdup(pp->mode_set), *tmp; 159715032Ssef 159831492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 159915032Ssef msearch(tmp, &ttybuf); 16001553Srgrimes } 16011553Srgrimes } 160231492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 160315032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 160431492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 16051553Srgrimes } 16061553Srgrimes } 16071553Srgrimes} 16081553Srgrimes 160927757Simp#ifdef __STDC__ 16101553Srgrimes#include <stdarg.h> 16111553Srgrimes#else 16121553Srgrimes#include <varargs.h> 16131553Srgrimes#endif 16141553Srgrimes 161515648Sjoergstatic void 161627757Simp#ifdef __STDC__ 161731492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 16181553Srgrimes#else 161931492Swollmanpstatus(pp, msg, va_alist) 162031492Swollman const struct printer *pp; 16211553Srgrimes char *msg; 16221553Srgrimes va_dcl 16231553Srgrimes#endif 16241553Srgrimes{ 162531492Swollman int fd; 162631492Swollman char *buf; 16271553Srgrimes va_list ap; 162827757Simp#ifdef __STDC__ 16291553Srgrimes va_start(ap, msg); 16301553Srgrimes#else 16311553Srgrimes va_start(ap); 16321553Srgrimes#endif 16331553Srgrimes 16341553Srgrimes umask(0); 163531492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 163631492Swollman if (fd < 0) { 163731492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file); 16381553Srgrimes exit(1); 16391553Srgrimes } 16401553Srgrimes ftruncate(fd, 0); 164131492Swollman vasprintf(&buf, msg, ap); 16421553Srgrimes va_end(ap); 164331492Swollman writel(fd, buf, "\n", (char *)0); 164431492Swollman close(fd); 164531492Swollman free(buf); 16461553Srgrimes} 164730407Sjoerg 164830407Sjoergvoid 164930407Sjoergalarmhandler(signo) 165030407Sjoerg{ 165130407Sjoerg /* ignored */ 165230407Sjoerg} 1653