printjob.c revision 68253
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35#ifndef lint 36static const char copyright[] = 37"@(#) Copyright (c) 1983, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39#endif /* not lint */ 40 41#ifndef lint 42/* 43static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95"; 44*/ 45static const char rcsid[] = 46 "$FreeBSD: head/usr.sbin/lpr/lpd/printjob.c 68253 2000-11-02 19:22:06Z gad $"; 47#endif /* not lint */ 48 49 50/* 51 * printjob -- print jobs in the queue. 52 * 53 * NOTE: the lock file is used to pass information to lpq and lprm. 54 * it does not need to be removed because file locks are dynamic. 55 */ 56 57#include <sys/param.h> 58#include <sys/wait.h> 59#include <sys/stat.h> 60#include <sys/types.h> 61 62#include <pwd.h> 63#include <unistd.h> 64#include <signal.h> 65#include <syslog.h> 66#include <fcntl.h> 67#include <dirent.h> 68#include <errno.h> 69#include <stdio.h> 70#include <string.h> 71#include <stdlib.h> 72#include <sys/ioctl.h> 73#include <termios.h> 74#include <time.h> 75#include "lp.h" 76#include "lp.local.h" 77#include "pathnames.h" 78#include "extern.h" 79 80#define DORETURN 0 /* absorb fork error */ 81#define DOABORT 1 /* abort if dofork fails */ 82 83/* 84 * Error tokens 85 */ 86#define REPRINT -2 87#define ERROR -1 88#define OK 0 89#define FATALERR 1 90#define NOACCT 2 91#define FILTERERR 3 92#define ACCESS 4 93 94static dev_t fdev; /* device of file pointed to by symlink */ 95static ino_t fino; /* inode of file pointed to by symlink */ 96static FILE *cfp; /* control file */ 97static int child; /* id of any filters */ 98static int job_dfcnt; /* count of datafiles in current user job */ 99static int lfd; /* lock file descriptor */ 100static int ofd; /* output filter file descriptor */ 101static int ofilter; /* id of output filter, if any */ 102static int tfd = -1; /* output filter temp file output */ 103static int pfd; /* prstatic inter file descriptor */ 104static int pid; /* pid of lpd process */ 105static int prchild; /* id of pr process */ 106static char title[80]; /* ``pr'' title */ 107static char locale[80]; /* ``pr'' locale */ 108 109static char class[32]; /* classification field */ 110static char fromhost[32]; /* user's host machine */ 111 /* indentation size in static characters */ 112static char indent[10] = "-i0"; 113static char jobname[100]; /* job or file name */ 114static char length[10] = "-l"; /* page length in lines */ 115static char logname[32]; /* user's login name */ 116static char pxlength[10] = "-y"; /* page length in pixels */ 117static char pxwidth[10] = "-x"; /* page width in pixels */ 118static char tempfile[] = "errsXXXXXX"; /* file name for filter errors */ 119static char width[10] = "-w"; /* page width in static characters */ 120#define TFILENAME "fltXXXXXX" 121static char tfile[] = TFILENAME; /* file name for filter output */ 122 123static void abortpr __P((int)); 124static void alarmhandler __P((int)); 125static void banner __P((struct printer *pp, char *name1, char *name2)); 126static int dofork __P((const struct printer *pp, int action)); 127static int dropit __P((int)); 128static void init __P((struct printer *pp)); 129static void openpr __P((const struct printer *pp)); 130static void opennet __P((const struct printer *pp)); 131static void opentty __P((const struct printer *pp)); 132static void openrem __P((const struct printer *pp)); 133static int print __P((struct printer *pp, int format, char *file)); 134static int printit __P((struct printer *pp, char *file)); 135static void pstatus __P((const struct printer *, const char *, ...)); 136static char response __P((const struct printer *pp)); 137static void scan_out __P((struct printer *pp, int scfd, char *scsp, 138 int dlm)); 139static char *scnline __P((int, char *, int)); 140static int sendfile __P((struct printer *pp, int type, char *file, 141 int format)); 142static int sendit __P((struct printer *pp, char *file)); 143static void sendmail __P((struct printer *pp, char *user, int bombed)); 144static void setty __P((const struct printer *pp)); 145 146void 147printjob(pp) 148 struct printer *pp; 149{ 150 struct stat stb; 151 register struct queue *q, **qp; 152 struct queue **queue; 153 register int i, nitems; 154 off_t pidoff; 155 int errcnt, count = 0; 156 157 init(pp); /* set up capabilities */ 158 (void) write(1, "", 1); /* ack that daemon is started */ 159 (void) close(2); /* set up log file */ 160 if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { 161 syslog(LOG_ERR, "%s: %m", pp->log_file); 162 (void) open(_PATH_DEVNULL, O_WRONLY); 163 } 164 setgid(getegid()); 165 pid = getpid(); /* for use with lprm */ 166 setpgrp(0, pid); 167 signal(SIGHUP, abortpr); 168 signal(SIGINT, abortpr); 169 signal(SIGQUIT, abortpr); 170 signal(SIGTERM, abortpr); 171 172 (void) mktemp(tempfile); 173 174 /* 175 * uses short form file names 176 */ 177 if (chdir(pp->spool_dir) < 0) { 178 syslog(LOG_ERR, "%s: %m", pp->spool_dir); 179 exit(1); 180 } 181 if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS)) 182 exit(0); /* printing disabled */ 183 lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 184 LOCK_FILE_MODE); 185 if (lfd < 0) { 186 if (errno == EWOULDBLOCK) /* active daemon present */ 187 exit(0); 188 syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 189 exit(1); 190 } 191 /* turn off non-blocking mode (was turned on for lock effects only) */ 192 if (fcntl(lfd, F_SETFL, 0) < 0) { 193 syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 194 exit(1); 195 } 196 ftruncate(lfd, 0); 197 /* 198 * write process id for others to know 199 */ 200 sprintf(line, "%u\n", pid); 201 pidoff = i = strlen(line); 202 if (write(lfd, line, i) != i) { 203 syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 204 exit(1); 205 } 206 /* 207 * search the spool directory for work and sort by queue order. 208 */ 209 if ((nitems = getq(pp, &queue)) < 0) { 210 syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 211 pp->spool_dir); 212 exit(1); 213 } 214 if (nitems == 0) /* no work to do */ 215 exit(0); 216 if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ 217 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) 218 syslog(LOG_ERR, "%s: %s: %m", pp->printer, 219 pp->lock_file); 220 } 221 openpr(pp); /* open printer or remote */ 222again: 223 /* 224 * we found something to do now do it -- 225 * write the name of the current control file into the lock file 226 * so the spool queue program can tell what we're working on 227 */ 228 for (qp = queue; nitems--; free((char *) q)) { 229 q = *qp++; 230 if (stat(q->q_name, &stb) < 0) 231 continue; 232 errcnt = 0; 233 restart: 234 (void) lseek(lfd, pidoff, 0); 235 (void) snprintf(line, sizeof(line), "%s\n", q->q_name); 236 i = strlen(line); 237 if (write(lfd, line, i) != i) 238 syslog(LOG_ERR, "%s: %s: %m", pp->printer, 239 pp->lock_file); 240 if (!pp->remote) 241 i = printit(pp, q->q_name); 242 else 243 i = sendit(pp, q->q_name); 244 /* 245 * Check to see if we are supposed to stop printing or 246 * if we are to rebuild the queue. 247 */ 248 if (fstat(lfd, &stb) == 0) { 249 /* stop printing before starting next job? */ 250 if (stb.st_mode & LFM_PRINT_DIS) 251 goto done; 252 /* rebuild queue (after lpc topq) */ 253 if (stb.st_mode & LFM_RESET_QUE) { 254 for (free(q); nitems--; free(q)) 255 q = *qp++; 256 if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) 257 < 0) 258 syslog(LOG_WARNING, "%s: %s: %m", 259 pp->printer, pp->lock_file); 260 break; 261 } 262 } 263 if (i == OK) /* file ok and printed */ 264 count++; 265 else if (i == REPRINT && ++errcnt < 5) { 266 /* try reprinting the job */ 267 syslog(LOG_INFO, "restarting %s", pp->printer); 268 if (ofilter > 0) { 269 kill(ofilter, SIGCONT); /* to be sure */ 270 (void) close(ofd); 271 while ((i = wait(NULL)) > 0 && i != ofilter) 272 ; 273 ofilter = 0; 274 } 275 (void) close(pfd); /* close printer */ 276 if (ftruncate(lfd, pidoff) < 0) 277 syslog(LOG_WARNING, "%s: %s: %m", 278 pp->printer, pp->lock_file); 279 openpr(pp); /* try to reopen printer */ 280 goto restart; 281 } else { 282 syslog(LOG_WARNING, "%s: job could not be %s (%s)", 283 pp->printer, 284 pp->remote ? "sent to remote host" : "printed", 285 q->q_name); 286 if (i == REPRINT) { 287 /* ensure we don't attempt this job again */ 288 (void) unlink(q->q_name); 289 q->q_name[0] = 'd'; 290 (void) unlink(q->q_name); 291 if (logname[0]) 292 sendmail(pp, logname, FATALERR); 293 } 294 } 295 } 296 free(queue); 297 /* 298 * search the spool directory for more work. 299 */ 300 if ((nitems = getq(pp, &queue)) < 0) { 301 syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 302 pp->spool_dir); 303 exit(1); 304 } 305 if (nitems == 0) { /* no more work to do */ 306 done: 307 if (count > 0) { /* Files actually printed */ 308 if (!pp->no_formfeed && !pp->tof) 309 (void) write(ofd, pp->form_feed, 310 strlen(pp->form_feed)); 311 if (pp->trailer != NULL) /* output trailer */ 312 (void) write(ofd, pp->trailer, 313 strlen(pp->trailer)); 314 } 315 (void) close(ofd); 316 (void) wait(NULL); 317 (void) unlink(tempfile); 318 exit(0); 319 } 320 goto again; 321} 322 323char fonts[4][50]; /* fonts for troff */ 324 325char ifonts[4][40] = { 326 _PATH_VFONTR, 327 _PATH_VFONTI, 328 _PATH_VFONTB, 329 _PATH_VFONTS, 330}; 331 332/* 333 * The remaining part is the reading of the control file (cf) 334 * and performing the various actions. 335 */ 336static int 337printit(pp, file) 338 struct printer *pp; 339 char *file; 340{ 341 register int i; 342 char *cp; 343 int bombed = OK; 344 345 /* 346 * open control file; ignore if no longer there. 347 */ 348 if ((cfp = fopen(file, "r")) == NULL) { 349 syslog(LOG_INFO, "%s: %s: %m", pp->printer, file); 350 return(OK); 351 } 352 /* 353 * Reset troff fonts. 354 */ 355 for (i = 0; i < 4; i++) 356 strcpy(fonts[i], ifonts[i]); 357 sprintf(&width[2], "%ld", pp->page_width); 358 strcpy(indent+2, "0"); 359 360 /* initialize job-specific count of datafiles processed */ 361 job_dfcnt = 0; 362 363 /* 364 * read the control file for work to do 365 * 366 * file format -- first character in the line is a command 367 * rest of the line is the argument. 368 * valid commands are: 369 * 370 * S -- "stat info" for symbolic link protection 371 * J -- "job name" on banner page 372 * C -- "class name" on banner page 373 * L -- "literal" user's name to print on banner 374 * T -- "title" for pr 375 * H -- "host name" of machine where lpr was done 376 * P -- "person" user's login name 377 * I -- "indent" amount to indent output 378 * R -- laser dpi "resolution" 379 * f -- "file name" name of text file to print 380 * l -- "file name" text file with control chars 381 * p -- "file name" text file to print with pr(1) 382 * t -- "file name" troff(1) file to print 383 * n -- "file name" ditroff(1) file to print 384 * d -- "file name" dvi file to print 385 * g -- "file name" plot(1G) file to print 386 * v -- "file name" plain raster file to print 387 * c -- "file name" cifplot file to print 388 * 1 -- "R font file" for troff 389 * 2 -- "I font file" for troff 390 * 3 -- "B font file" for troff 391 * 4 -- "S font file" for troff 392 * N -- "name" of file (used by lpq) 393 * U -- "unlink" name of file to remove 394 * (after we print it. (Pass 2 only)). 395 * M -- "mail" to user when done printing 396 * Z -- "locale" for pr 397 * 398 * getline reads a line and expands tabs to blanks 399 */ 400 401 /* pass 1 */ 402 403 while (getline(cfp)) 404 switch (line[0]) { 405 case 'H': 406 strncpy(fromhost, line+1, sizeof(fromhost) - 1); 407 fromhost[sizeof(fromhost) - 1] = '\0'; 408 if (class[0] == '\0') { 409 strncpy(class, line+1, sizeof(class) - 1); 410 class[sizeof(class) - 1] = '\0'; 411 } 412 continue; 413 414 case 'P': 415 strncpy(logname, line+1, sizeof(logname) - 1); 416 logname[sizeof(logname) - 1] = '\0'; 417 if (pp->restricted) { /* restricted */ 418 if (getpwnam(logname) == NULL) { 419 bombed = NOACCT; 420 sendmail(pp, line+1, bombed); 421 goto pass2; 422 } 423 } 424 continue; 425 426 case 'S': 427 cp = line+1; 428 i = 0; 429 while (*cp >= '0' && *cp <= '9') 430 i = i * 10 + (*cp++ - '0'); 431 fdev = i; 432 cp++; 433 i = 0; 434 while (*cp >= '0' && *cp <= '9') 435 i = i * 10 + (*cp++ - '0'); 436 fino = i; 437 continue; 438 439 case 'J': 440 if (line[1] != '\0') { 441 strncpy(jobname, line+1, sizeof(jobname) - 1); 442 jobname[sizeof(jobname) - 1] = '\0'; 443 } else 444 strcpy(jobname, " "); 445 continue; 446 447 case 'C': 448 if (line[1] != '\0') 449 strncpy(class, line+1, sizeof(class) - 1); 450 else if (class[0] == '\0') 451 gethostname(class, sizeof(class)); 452 class[sizeof(class) - 1] = '\0'; 453 continue; 454 455 case 'T': /* header title for pr */ 456 strncpy(title, line+1, sizeof(title) - 1); 457 title[sizeof(title) - 1] = '\0'; 458 continue; 459 460 case 'L': /* identification line */ 461 if (!pp->no_header && !pp->header_last) 462 banner(pp, line+1, jobname); 463 continue; 464 465 case '1': /* troff fonts */ 466 case '2': 467 case '3': 468 case '4': 469 if (line[1] != '\0') { 470 strncpy(fonts[line[0]-'1'], line+1, 471 50-1); 472 fonts[line[0]-'1'][50-1] = '\0'; 473 } 474 continue; 475 476 case 'W': /* page width */ 477 strncpy(width+2, line+1, sizeof(width) - 3); 478 width[2+sizeof(width) - 3] = '\0'; 479 continue; 480 481 case 'I': /* indent amount */ 482 strncpy(indent+2, line+1, sizeof(indent) - 3); 483 indent[2+sizeof(indent) - 3] = '\0'; 484 continue; 485 486 case 'Z': /* locale for pr */ 487 strncpy(locale, line+1, sizeof(locale) - 1); 488 locale[sizeof(locale) - 1] = '\0'; 489 continue; 490 491 default: /* some file to print */ 492 switch (i = print(pp, line[0], line+1)) { 493 case ERROR: 494 if (bombed == OK) 495 bombed = FATALERR; 496 break; 497 case REPRINT: 498 (void) fclose(cfp); 499 return(REPRINT); 500 case FILTERERR: 501 case ACCESS: 502 bombed = i; 503 sendmail(pp, logname, bombed); 504 } 505 title[0] = '\0'; 506 continue; 507 508 case 'N': 509 case 'U': 510 case 'M': 511 case 'R': 512 continue; 513 } 514 515 /* pass 2 */ 516 517pass2: 518 fseek(cfp, 0L, 0); 519 while (getline(cfp)) 520 switch (line[0]) { 521 case 'L': /* identification line */ 522 if (!pp->no_header && pp->header_last) 523 banner(pp, line+1, jobname); 524 continue; 525 526 case 'M': 527 if (bombed < NOACCT) /* already sent if >= NOACCT */ 528 sendmail(pp, line+1, bombed); 529 continue; 530 531 case 'U': 532 if (strchr(line+1, '/')) 533 continue; 534 (void) unlink(line+1); 535 } 536 /* 537 * clean-up in case another control file exists 538 */ 539 (void) fclose(cfp); 540 (void) unlink(file); 541 return(bombed == OK ? OK : ERROR); 542} 543 544/* 545 * Print a file. 546 * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 547 * Return -1 if a non-recoverable error occured, 548 * 2 if the filter detected some errors (but printed the job anyway), 549 * 1 if we should try to reprint this job and 550 * 0 if all is well. 551 * Note: all filters take stdin as the file, stdout as the printer, 552 * stderr as the log file, and must not ignore SIGINT. 553 */ 554static int 555print(pp, format, file) 556 struct printer *pp; 557 int format; 558 char *file; 559{ 560 register int n, i; 561 register char *prog; 562 int fi, fo; 563 FILE *fp; 564 char *av[15], buf[BUFSIZ]; 565 int pid, p[2], stopped = 0; 566 union wait status; 567 struct stat stb; 568 569 if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) 570 return(ERROR); 571 /* 572 * Check to see if data file is a symbolic link. If so, it should 573 * still point to the same file or someone is trying to print 574 * something he shouldn't. 575 */ 576 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 577 (stb.st_dev != fdev || stb.st_ino != fino)) 578 return(ACCESS); 579 580 job_dfcnt++; /* increment datafile counter for this job */ 581 582 /* everything seems OK, start it up */ 583 if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 584 (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 585 pp->tof = 1; 586 } 587 if (pp->filters[LPF_INPUT] == NULL 588 && (format == 'f' || format == 'l')) { 589 pp->tof = 0; 590 while ((n = read(fi, buf, BUFSIZ)) > 0) 591 if (write(ofd, buf, n) != n) { 592 (void) close(fi); 593 return(REPRINT); 594 } 595 (void) close(fi); 596 return(OK); 597 } 598 switch (format) { 599 case 'p': /* print file using 'pr' */ 600 if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 601 prog = _PATH_PR; 602 i = 0; 603 av[i++] = "pr"; 604 av[i++] = width; 605 av[i++] = length; 606 av[i++] = "-h"; 607 av[i++] = *title ? title : " "; 608 av[i++] = "-L"; 609 av[i++] = *locale ? locale : "C"; 610 av[i++] = "-F"; 611 av[i] = 0; 612 fo = ofd; 613 goto start; 614 } 615 pipe(p); 616 if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 617 dup2(fi, 0); /* file is stdin */ 618 dup2(p[1], 1); /* pipe is stdout */ 619 closelog(); 620 closeallfds(3); 621 execl(_PATH_PR, "pr", width, length, 622 "-h", *title ? title : " ", 623 "-L", *locale ? locale : "C", 624 "-F", 0); 625 syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 626 exit(2); 627 } 628 (void) close(p[1]); /* close output side */ 629 (void) close(fi); 630 if (prchild < 0) { 631 prchild = 0; 632 (void) close(p[0]); 633 return(ERROR); 634 } 635 fi = p[0]; /* use pipe for input */ 636 case 'f': /* print plain text file */ 637 prog = pp->filters[LPF_INPUT]; 638 av[1] = width; 639 av[2] = length; 640 av[3] = indent; 641 n = 4; 642 break; 643 case 'l': /* like 'f' but pass control characters */ 644 prog = pp->filters[LPF_INPUT]; 645 av[1] = "-c"; 646 av[2] = width; 647 av[3] = length; 648 av[4] = indent; 649 n = 5; 650 break; 651 case 'r': /* print a fortran text file */ 652 prog = pp->filters[LPF_FORTRAN]; 653 av[1] = width; 654 av[2] = length; 655 n = 3; 656 break; 657 case 't': /* print troff output */ 658 case 'n': /* print ditroff output */ 659 case 'd': /* print tex output */ 660 (void) unlink(".railmag"); 661 if ((fo = creat(".railmag", FILMOD)) < 0) { 662 syslog(LOG_ERR, "%s: cannot create .railmag", 663 pp->printer); 664 (void) unlink(".railmag"); 665 } else { 666 for (n = 0; n < 4; n++) { 667 if (fonts[n][0] != '/') 668 (void) write(fo, _PATH_VFONT, 669 sizeof(_PATH_VFONT) - 1); 670 (void) write(fo, fonts[n], strlen(fonts[n])); 671 (void) write(fo, "\n", 1); 672 } 673 (void) close(fo); 674 } 675 prog = (format == 't') ? pp->filters[LPF_TROFF] 676 : ((format == 'n') ? pp->filters[LPF_DITROFF] 677 : pp->filters[LPF_DVI]); 678 av[1] = pxwidth; 679 av[2] = pxlength; 680 n = 3; 681 break; 682 case 'c': /* print cifplot output */ 683 prog = pp->filters[LPF_CIFPLOT]; 684 av[1] = pxwidth; 685 av[2] = pxlength; 686 n = 3; 687 break; 688 case 'g': /* print plot(1G) output */ 689 prog = pp->filters[LPF_GRAPH]; 690 av[1] = pxwidth; 691 av[2] = pxlength; 692 n = 3; 693 break; 694 case 'v': /* print raster output */ 695 prog = pp->filters[LPF_RASTER]; 696 av[1] = pxwidth; 697 av[2] = pxlength; 698 n = 3; 699 break; 700 default: 701 (void) close(fi); 702 syslog(LOG_ERR, "%s: illegal format character '%c'", 703 pp->printer, format); 704 return(ERROR); 705 } 706 if (prog == NULL) { 707 (void) close(fi); 708 syslog(LOG_ERR, 709 "%s: no filter found in printcap for format character '%c'", 710 pp->printer, format); 711 return(ERROR); 712 } 713 if ((av[0] = strrchr(prog, '/')) != NULL) 714 av[0]++; 715 else 716 av[0] = prog; 717 av[n++] = "-n"; 718 av[n++] = logname; 719 av[n++] = "-h"; 720 av[n++] = fromhost; 721 av[n++] = pp->acct_file; 722 av[n] = 0; 723 fo = pfd; 724 if (ofilter > 0) { /* stop output filter */ 725 write(ofd, "\031\1", 2); 726 while ((pid = 727 wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter) 728 ; 729 if (status.w_stopval != WSTOPPED) { 730 (void) close(fi); 731 syslog(LOG_WARNING, 732 "%s: output filter died " 733 "(retcode=%d termsig=%d)", 734 pp->printer, status.w_retcode, 735 status.w_termsig); 736 return(REPRINT); 737 } 738 stopped++; 739 } 740start: 741 if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 742 dup2(fi, 0); 743 dup2(fo, 1); 744 n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0664); 745 if (n >= 0) 746 dup2(n, 2); 747 closelog(); 748 closeallfds(3); 749 execv(prog, av); 750 syslog(LOG_ERR, "cannot execv %s", prog); 751 exit(2); 752 } 753 (void) close(fi); 754 if (child < 0) 755 status.w_retcode = 100; 756 else 757 while ((pid = wait((int *)&status)) > 0 && pid != child) 758 ; 759 child = 0; 760 prchild = 0; 761 if (stopped) { /* restart output filter */ 762 if (kill(ofilter, SIGCONT) < 0) { 763 syslog(LOG_ERR, "cannot restart output filter"); 764 exit(1); 765 } 766 } 767 pp->tof = 0; 768 769 /* Copy filter output to "lf" logfile */ 770 if ((fp = fopen(tempfile, "r"))) { 771 while (fgets(buf, sizeof(buf), fp)) 772 fputs(buf, stderr); 773 fclose(fp); 774 } 775 776 if (!WIFEXITED(status)) { 777 syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 778 pp->printer, format, status.w_termsig); 779 return(ERROR); 780 } 781 switch (status.w_retcode) { 782 case 0: 783 pp->tof = 1; 784 return(OK); 785 case 1: 786 return(REPRINT); 787 case 2: 788 return(ERROR); 789 default: 790 syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 791 pp->printer, format, status.w_retcode); 792 return(FILTERERR); 793 } 794} 795 796/* 797 * Send the daemon control file (cf) and any data files. 798 * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 799 * 0 if all is well. 800 */ 801static int 802sendit(pp, file) 803 struct printer *pp; 804 char *file; 805{ 806 register int i, err = OK; 807 char *cp, last[BUFSIZ]; 808 809 /* 810 * open control file 811 */ 812 if ((cfp = fopen(file, "r")) == NULL) 813 return(OK); 814 815 /* initialize job-specific count of datafiles processed */ 816 job_dfcnt = 0; 817 818 /* 819 * read the control file for work to do 820 * 821 * file format -- first character in the line is a command 822 * rest of the line is the argument. 823 * commands of interest are: 824 * 825 * a-z -- "file name" name of file to print 826 * U -- "unlink" name of file to remove 827 * (after we print it. (Pass 2 only)). 828 */ 829 830 /* 831 * pass 1 832 */ 833 while (getline(cfp)) { 834 again: 835 if (line[0] == 'S') { 836 cp = line+1; 837 i = 0; 838 while (*cp >= '0' && *cp <= '9') 839 i = i * 10 + (*cp++ - '0'); 840 fdev = i; 841 cp++; 842 i = 0; 843 while (*cp >= '0' && *cp <= '9') 844 i = i * 10 + (*cp++ - '0'); 845 fino = i; 846 } else if (line[0] == 'H') { 847 strcpy(fromhost, line+1); 848 if (class[0] == '\0') 849 strncpy(class, line+1, sizeof(class) - 1); 850 } else if (line[0] == 'P') { 851 strncpy(logname, line+1, sizeof(logname) - 1); 852 if (pp->restricted) { /* restricted */ 853 if (getpwnam(logname) == NULL) { 854 sendmail(pp, line+1, NOACCT); 855 err = ERROR; 856 break; 857 } 858 } 859 } else if (line[0] == 'I') { 860 strncpy(indent+2, line+1, sizeof(indent) - 3); 861 } else if (line[0] >= 'a' && line[0] <= 'z') { 862 strcpy(last, line); 863 while ((i = getline(cfp)) != 0) 864 if (strcmp(last, line)) 865 break; 866 switch (sendfile(pp, '\3', last+1, *last)) { 867 case OK: 868 if (i) 869 goto again; 870 break; 871 case REPRINT: 872 (void) fclose(cfp); 873 return(REPRINT); 874 case ACCESS: 875 sendmail(pp, logname, ACCESS); 876 case ERROR: 877 err = ERROR; 878 } 879 break; 880 } 881 } 882 if (err == OK && sendfile(pp, '\2', file, '\0') > 0) { 883 (void) fclose(cfp); 884 return(REPRINT); 885 } 886 /* 887 * pass 2 888 */ 889 fseek(cfp, 0L, 0); 890 while (getline(cfp)) 891 if (line[0] == 'U' && !strchr(line+1, '/')) 892 (void) unlink(line+1); 893 /* 894 * clean-up in case another control file exists 895 */ 896 (void) fclose(cfp); 897 (void) unlink(file); 898 return(err); 899} 900 901/* 902 * Send a data file to the remote machine and spool it. 903 * Return positive if we should try resending. 904 */ 905static int 906sendfile(pp, type, file, format) 907 struct printer *pp; 908 int type; 909 char *file; 910 char format; 911{ 912 register int f, i, amt; 913 struct stat stb; 914 char buf[BUFSIZ]; 915 int sizerr, resp, closedpr; 916 917 if (lstat(file, &stb) < 0 || (f = open(file, O_RDONLY)) < 0) 918 return(ERROR); 919 /* 920 * Check to see if data file is a symbolic link. If so, it should 921 * still point to the same file or someone is trying to print something 922 * he shouldn't. 923 */ 924 if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 && 925 (stb.st_dev != fdev || stb.st_ino != fino)) 926 return(ACCESS); 927 928 job_dfcnt++; /* increment datafile counter for this job */ 929 930 /* everything seems OK, start it up */ 931 sizerr = 0; 932 closedpr = 0; 933 if (type == '\3') { 934 if (pp->filters[LPF_INPUT]) { 935 /* 936 * We're sending something with an ifilter, we have to 937 * run the ifilter and store the output as a 938 * temporary file (tfile)... the protocol requires us 939 * to send the file size 940 */ 941 char *av[15]; 942 int n; 943 int ifilter; 944 union wait status; /* XXX */ 945 946 strcpy(tfile,TFILENAME); 947 if ((tfd = mkstemp(tfile)) == -1) { 948 syslog(LOG_ERR, "mkstemp: %m"); 949 return(ERROR); 950 } 951 if ((av[0] = strrchr(pp->filters[LPF_INPUT], '/')) == NULL) 952 av[0] = pp->filters[LPF_INPUT]; 953 else 954 av[0]++; 955 if (format == 'l') 956 av[n=1] = "-c"; 957 else 958 n = 0; 959 av[++n] = width; 960 av[++n] = length; 961 av[++n] = indent; 962 av[++n] = "-n"; 963 av[++n] = logname; 964 av[++n] = "-h"; 965 av[++n] = fromhost; 966 av[++n] = pp->acct_file; 967 av[++n] = 0; 968 if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */ 969 dup2(f, 0); 970 dup2(tfd, 1); 971 n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 972 TEMP_FILE_MODE); 973 if (n >= 0) 974 dup2(n, 2); 975 closelog(); 976 closeallfds(3); 977 execv(pp->filters[LPF_INPUT], av); 978 syslog(LOG_ERR, "cannot execv %s", 979 pp->filters[LPF_INPUT]); 980 exit(2); 981 } 982 (void) close(f); 983 if (ifilter < 0) 984 status.w_retcode = 100; 985 else 986 while ((pid = wait((int *)&status)) > 0 && 987 pid != ifilter) 988 ; 989 switch (status.w_retcode) { 990 case 0: 991 break; 992 case 1: 993 unlink(tfile); 994 return(REPRINT); 995 case 2: 996 unlink(tfile); 997 return(ERROR); 998 default: 999 syslog(LOG_WARNING, "%s: filter '%c' exited" 1000 " (retcode=%d)", 1001 pp->printer, format, status.w_retcode); 1002 unlink(tfile); 1003 return(FILTERERR); 1004 } 1005 if (fstat(tfd, &stb) < 0) /* the size of tfile */ 1006 return(ERROR); 1007 f = tfd; 1008 lseek(f,0,SEEK_SET); 1009 } else if (ofilter) { 1010 /* 1011 * We're sending something with an ofilter, we have to 1012 * store the output as a temporary file (tfile)... the 1013 * protocol requires us to send the file size 1014 */ 1015 int i; 1016 for (i = 0; i < stb.st_size; i += BUFSIZ) { 1017 amt = BUFSIZ; 1018 if (i + amt > stb.st_size) 1019 amt = stb.st_size - i; 1020 if (sizerr == 0 && read(f, buf, amt) != amt) { 1021 sizerr = 1; 1022 break; 1023 } 1024 if (write(ofd, buf, amt) != amt) { 1025 (void) close(f); 1026 return(REPRINT); 1027 } 1028 } 1029 close(ofd); 1030 close(f); 1031 while ((i = wait(NULL)) > 0 && i != ofilter) 1032 ; 1033 ofilter = 0; 1034 if (fstat(tfd, &stb) < 0) { /* the size of tfile */ 1035 openpr(pp); 1036 return(ERROR); 1037 } 1038 f = tfd; 1039 lseek(f,0,SEEK_SET); 1040 closedpr = 1; 1041 } 1042 } 1043 1044 (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 1045 amt = strlen(buf); 1046 for (i = 0; ; i++) { 1047 if (write(pfd, buf, amt) != amt || 1048 (resp = response(pp)) < 0 || resp == '\1') { 1049 (void) close(f); 1050 if (tfd != -1 && type == '\3') { 1051 tfd = -1; 1052 unlink(tfile); 1053 if (closedpr) 1054 openpr(pp); 1055 } 1056 return(REPRINT); 1057 } else if (resp == '\0') 1058 break; 1059 if (i == 0) 1060 pstatus(pp, 1061 "no space on remote; waiting for queue to drain"); 1062 if (i == 10) 1063 syslog(LOG_ALERT, "%s: can't send to %s; queue full", 1064 pp->printer, pp->remote_host); 1065 sleep(5 * 60); 1066 } 1067 if (i) 1068 pstatus(pp, "sending to %s", pp->remote_host); 1069 if (type == '\3') 1070 trstat_init(pp, file, job_dfcnt); 1071 for (i = 0; i < stb.st_size; i += BUFSIZ) { 1072 amt = BUFSIZ; 1073 if (i + amt > stb.st_size) 1074 amt = stb.st_size - i; 1075 if (sizerr == 0 && read(f, buf, amt) != amt) 1076 sizerr = 1; 1077 if (write(pfd, buf, amt) != amt) { 1078 (void) close(f); 1079 if (tfd != -1 && type == '\3') { 1080 tfd = -1; 1081 unlink(tfile); 1082 if (closedpr) 1083 openpr(pp); 1084 } 1085 return(REPRINT); 1086 } 1087 } 1088 1089 (void) close(f); 1090 if (tfd != -1 && type == '\3') { 1091 tfd = -1; 1092 unlink(tfile); 1093 } 1094 if (sizerr) { 1095 syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 1096 /* tell recvjob to ignore this file */ 1097 (void) write(pfd, "\1", 1); 1098 if (closedpr) 1099 openpr(pp); 1100 return(ERROR); 1101 } 1102 if (write(pfd, "", 1) != 1 || response(pp)) { 1103 if (closedpr) 1104 openpr(pp); 1105 return(REPRINT); 1106 } 1107 if (closedpr) 1108 openpr(pp); 1109 if (type == '\3') 1110 trstat_write(pp, TR_SENDING, stb.st_size, logname, 1111 pp->remote_host, fromhost); 1112 return(OK); 1113} 1114 1115/* 1116 * Check to make sure there have been no errors and that both programs 1117 * are in sync with eachother. 1118 * Return non-zero if the connection was lost. 1119 */ 1120static char 1121response(pp) 1122 const struct printer *pp; 1123{ 1124 char resp; 1125 1126 if (read(pfd, &resp, 1) != 1) { 1127 syslog(LOG_INFO, "%s: lost connection", pp->printer); 1128 return(-1); 1129 } 1130 return(resp); 1131} 1132 1133/* 1134 * Banner printing stuff 1135 */ 1136static void 1137banner(pp, name1, name2) 1138 struct printer *pp; 1139 char *name1, *name2; 1140{ 1141 time_t tvec; 1142 1143 time(&tvec); 1144 if (!pp->no_formfeed && !pp->tof) 1145 (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 1146 if (pp->short_banner) { /* short banner only */ 1147 if (class[0]) { 1148 (void) write(ofd, class, strlen(class)); 1149 (void) write(ofd, ":", 1); 1150 } 1151 (void) write(ofd, name1, strlen(name1)); 1152 (void) write(ofd, " Job: ", 7); 1153 (void) write(ofd, name2, strlen(name2)); 1154 (void) write(ofd, " Date: ", 8); 1155 (void) write(ofd, ctime(&tvec), 24); 1156 (void) write(ofd, "\n", 1); 1157 } else { /* normal banner */ 1158 (void) write(ofd, "\n\n\n", 3); 1159 scan_out(pp, ofd, name1, '\0'); 1160 (void) write(ofd, "\n\n", 2); 1161 scan_out(pp, ofd, name2, '\0'); 1162 if (class[0]) { 1163 (void) write(ofd,"\n\n\n",3); 1164 scan_out(pp, ofd, class, '\0'); 1165 } 1166 (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 1167 (void) write(ofd, name2, strlen(name2)); 1168 (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 1169 (void) write(ofd, ctime(&tvec), 24); 1170 (void) write(ofd, "\n", 1); 1171 } 1172 if (!pp->no_formfeed) 1173 (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 1174 pp->tof = 1; 1175} 1176 1177static char * 1178scnline(key, p, c) 1179 register int key; 1180 register char *p; 1181 int c; 1182{ 1183 register int scnwidth; 1184 1185 for (scnwidth = WIDTH; --scnwidth;) { 1186 key <<= 1; 1187 *p++ = key & 0200 ? c : BACKGND; 1188 } 1189 return (p); 1190} 1191 1192#define TRC(q) (((q)-' ')&0177) 1193 1194static void 1195scan_out(pp, scfd, scsp, dlm) 1196 struct printer *pp; 1197 int scfd, dlm; 1198 char *scsp; 1199{ 1200 register char *strp; 1201 register int nchrs, j; 1202 char outbuf[LINELEN+1], *sp, c, cc; 1203 int d, scnhgt; 1204 1205 for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 1206 strp = &outbuf[0]; 1207 sp = scsp; 1208 for (nchrs = 0; ; ) { 1209 d = dropit(c = TRC(cc = *sp++)); 1210 if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 1211 for (j = WIDTH; --j;) 1212 *strp++ = BACKGND; 1213 else 1214 strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 1215 if (*sp == dlm || *sp == '\0' || 1216 nchrs++ >= pp->page_width/(WIDTH+1)-1) 1217 break; 1218 *strp++ = BACKGND; 1219 *strp++ = BACKGND; 1220 } 1221 while (*--strp == BACKGND && strp >= outbuf) 1222 ; 1223 strp++; 1224 *strp++ = '\n'; 1225 (void) write(scfd, outbuf, strp-outbuf); 1226 } 1227} 1228 1229static int 1230dropit(c) 1231 int c; 1232{ 1233 switch(c) { 1234 1235 case TRC('_'): 1236 case TRC(';'): 1237 case TRC(','): 1238 case TRC('g'): 1239 case TRC('j'): 1240 case TRC('p'): 1241 case TRC('q'): 1242 case TRC('y'): 1243 return (DROP); 1244 1245 default: 1246 return (0); 1247 } 1248} 1249 1250/* 1251 * sendmail --- 1252 * tell people about job completion 1253 */ 1254static void 1255sendmail(pp, user, bombed) 1256 struct printer *pp; 1257 char *user; 1258 int bombed; 1259{ 1260 register int i; 1261 int p[2], s; 1262 register char *cp; 1263 struct stat stb; 1264 FILE *fp; 1265 1266 pipe(p); 1267 if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 1268 dup2(p[0], 0); 1269 closelog(); 1270 closeallfds(3); 1271 if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 1272 cp++; 1273 else 1274 cp = _PATH_SENDMAIL; 1275 execl(_PATH_SENDMAIL, cp, "-t", 0); 1276 _exit(0); 1277 } else if (s > 0) { /* parent */ 1278 dup2(p[1], 1); 1279 printf("To: %s@%s\n", user, fromhost); 1280 printf("Subject: %s printer job \"%s\"\n", pp->printer, 1281 *jobname ? jobname : "<unknown>"); 1282 printf("Reply-To: root@%s\n\n", host); 1283 printf("Your printer job "); 1284 if (*jobname) 1285 printf("(%s) ", jobname); 1286 1287 cp = "XXX compiler confusion"; /* XXX shut GCC up */ 1288 switch (bombed) { 1289 case OK: 1290 printf("\ncompleted successfully\n"); 1291 cp = "OK"; 1292 break; 1293 default: 1294 case FATALERR: 1295 printf("\ncould not be printed\n"); 1296 cp = "FATALERR"; 1297 break; 1298 case NOACCT: 1299 printf("\ncould not be printed without an account on %s\n", host); 1300 cp = "NOACCT"; 1301 break; 1302 case FILTERERR: 1303 if (stat(tempfile, &stb) < 0 || stb.st_size == 0 || 1304 (fp = fopen(tempfile, "r")) == NULL) { 1305 printf("\nhad some errors and may not have printed\n"); 1306 break; 1307 } 1308 printf("\nhad the following errors and may not have printed:\n"); 1309 while ((i = getc(fp)) != EOF) 1310 putchar(i); 1311 (void) fclose(fp); 1312 cp = "FILTERERR"; 1313 break; 1314 case ACCESS: 1315 printf("\nwas not printed because it was not linked to the original file\n"); 1316 cp = "ACCESS"; 1317 } 1318 fflush(stdout); 1319 (void) close(1); 1320 } else { 1321 syslog(LOG_WARNING, "unable to send mail to %s: %m", user); 1322 return; 1323 } 1324 (void) close(p[0]); 1325 (void) close(p[1]); 1326 wait(NULL); 1327 syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 1328 user, *jobname ? jobname : "<unknown>", pp->printer, cp); 1329} 1330 1331/* 1332 * dofork - fork with retries on failure 1333 */ 1334static int 1335dofork(pp, action) 1336 const struct printer *pp; 1337 int action; 1338{ 1339 register int i, pid; 1340 struct passwd *pwd; 1341 1342 for (i = 0; i < 20; i++) { 1343 if ((pid = fork()) < 0) { 1344 sleep((unsigned)(i*i)); 1345 continue; 1346 } 1347 /* 1348 * Child should run as daemon instead of root 1349 */ 1350 if (pid == 0) { 1351 if ((pwd = getpwuid(pp->daemon_user)) == NULL) { 1352 syslog(LOG_ERR, "Can't lookup default daemon uid (%d) in password file", 1353 pp->daemon_user); 1354 break; 1355 } 1356 initgroups(pwd->pw_name, pwd->pw_gid); 1357 setgid(pwd->pw_gid); 1358 setuid(pp->daemon_user); 1359 } 1360 return(pid); 1361 } 1362 syslog(LOG_ERR, "can't fork"); 1363 1364 switch (action) { 1365 case DORETURN: 1366 return (-1); 1367 default: 1368 syslog(LOG_ERR, "bad action (%d) to dofork", action); 1369 /*FALL THRU*/ 1370 case DOABORT: 1371 exit(1); 1372 } 1373 /*NOTREACHED*/ 1374} 1375 1376/* 1377 * Kill child processes to abort current job. 1378 */ 1379static void 1380abortpr(signo) 1381 int signo; 1382{ 1383 (void) unlink(tempfile); 1384 kill(0, SIGINT); 1385 if (ofilter > 0) 1386 kill(ofilter, SIGCONT); 1387 while (wait(NULL) > 0) 1388 ; 1389 if (ofilter > 0 && tfd != -1) 1390 unlink(tfile); 1391 exit(0); 1392} 1393 1394static void 1395init(pp) 1396 struct printer *pp; 1397{ 1398 char *s; 1399 1400 sprintf(&width[2], "%ld", pp->page_width); 1401 sprintf(&length[2], "%ld", pp->page_length); 1402 sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 1403 sprintf(&pxlength[2], "%ld", pp->page_plength); 1404 if ((s = checkremote(pp)) != 0) { 1405 syslog(LOG_WARNING, "%s", s); 1406 free(s); 1407 } 1408} 1409 1410void 1411startprinting(printer) 1412 const char *printer; 1413{ 1414 struct printer myprinter, *pp = &myprinter; 1415 int status; 1416 1417 init_printer(pp); 1418 status = getprintcap(printer, pp); 1419 switch(status) { 1420 case PCAPERR_OSERR: 1421 syslog(LOG_ERR, "can't open printer description file: %m"); 1422 exit(1); 1423 case PCAPERR_NOTFOUND: 1424 syslog(LOG_ERR, "unknown printer: %s", printer); 1425 exit(1); 1426 case PCAPERR_TCLOOP: 1427 fatal(pp, "potential reference loop detected in printcap file"); 1428 default: 1429 break; 1430 } 1431 printjob(pp); 1432} 1433 1434/* 1435 * Acquire line printer or remote connection. 1436 */ 1437static void 1438openpr(pp) 1439 const struct printer *pp; 1440{ 1441 int p[2]; 1442 char *cp; 1443 1444 if (pp->remote) { 1445 openrem(pp); 1446 } else if (*pp->lp) { 1447 if ((cp = strchr(pp->lp, '@')) != NULL) 1448 opennet(pp); 1449 else 1450 opentty(pp); 1451 } else { 1452 syslog(LOG_ERR, "%s: no line printer device or host name", 1453 pp->printer); 1454 exit(1); 1455 } 1456 1457 /* 1458 * Start up an output filter, if needed. 1459 */ 1460 if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) { 1461 pipe(p); 1462 if (pp->remote) { 1463 strcpy(tfile, TFILENAME); 1464 tfd = mkstemp(tfile); 1465 } 1466 if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */ 1467 dup2(p[0], 0); /* pipe is std in */ 1468 /* tfile/printer is stdout */ 1469 dup2(pp->remote ? tfd : pfd, 1); 1470 closelog(); 1471 closeallfds(3); 1472 if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 1473 cp = pp->filters[LPF_OUTPUT]; 1474 else 1475 cp++; 1476 execl(pp->filters[LPF_OUTPUT], cp, width, length, 0); 1477 syslog(LOG_ERR, "%s: %s: %m", pp->printer, 1478 pp->filters[LPF_OUTPUT]); 1479 exit(1); 1480 } 1481 (void) close(p[0]); /* close input side */ 1482 ofd = p[1]; /* use pipe for output */ 1483 } else { 1484 ofd = pfd; 1485 ofilter = 0; 1486 } 1487} 1488 1489/* 1490 * Printer connected directly to the network 1491 * or to a terminal server on the net 1492 */ 1493static void 1494opennet(pp) 1495 const struct printer *pp; 1496{ 1497 register int i; 1498 int resp; 1499 u_long port; 1500 char *ep; 1501 void (*savealrm)(int); 1502 1503 port = strtoul(pp->lp, &ep, 0); 1504 if (*ep != '@' || port > 65535) { 1505 syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 1506 pp->lp); 1507 exit(1); 1508 } 1509 ep++; 1510 1511 for (i = 1; ; i = i < 256 ? i << 1 : i) { 1512 resp = -1; 1513 savealrm = signal(SIGALRM, alarmhandler); 1514 alarm(pp->conn_timeout); 1515 pfd = getport(pp, ep, port); 1516 alarm(0); 1517 (void)signal(SIGALRM, savealrm); 1518 if (pfd < 0 && errno == ECONNREFUSED) 1519 resp = 1; 1520 else if (pfd >= 0) { 1521 /* 1522 * need to delay a bit for rs232 lines 1523 * to stabilize in case printer is 1524 * connected via a terminal server 1525 */ 1526 delay(500); 1527 break; 1528 } 1529 if (i == 1) { 1530 if (resp < 0) 1531 pstatus(pp, "waiting for %s to come up", 1532 pp->lp); 1533 else 1534 pstatus(pp, 1535 "waiting for access to printer on %s", 1536 pp->lp); 1537 } 1538 sleep(i); 1539 } 1540 pstatus(pp, "sending to %s port %d", ep, port); 1541} 1542 1543/* 1544 * Printer is connected to an RS232 port on this host 1545 */ 1546static void 1547opentty(pp) 1548 const struct printer *pp; 1549{ 1550 register int i; 1551 1552 for (i = 1; ; i = i < 32 ? i << 1 : i) { 1553 pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 1554 if (pfd >= 0) { 1555 delay(500); 1556 break; 1557 } 1558 if (errno == ENOENT) { 1559 syslog(LOG_ERR, "%s: %m", pp->lp); 1560 exit(1); 1561 } 1562 if (i == 1) 1563 pstatus(pp, 1564 "waiting for %s to become ready (offline?)", 1565 pp->printer); 1566 sleep(i); 1567 } 1568 if (isatty(pfd)) 1569 setty(pp); 1570 pstatus(pp, "%s is ready and printing", pp->printer); 1571} 1572 1573/* 1574 * Printer is on a remote host 1575 */ 1576static void 1577openrem(pp) 1578 const struct printer *pp; 1579{ 1580 register int i; 1581 int resp; 1582 void (*savealrm)(int); 1583 1584 for (i = 1; ; i = i < 256 ? i << 1 : i) { 1585 resp = -1; 1586 savealrm = signal(SIGALRM, alarmhandler); 1587 alarm(pp->conn_timeout); 1588 pfd = getport(pp, pp->remote_host, 0); 1589 alarm(0); 1590 (void)signal(SIGALRM, savealrm); 1591 if (pfd >= 0) { 1592 if ((writel(pfd, "\2", pp->remote_queue, "\n", 1593 (char *)0) 1594 == 2 + strlen(pp->remote_queue)) 1595 && (resp = response(pp)) == 0) 1596 break; 1597 (void) close(pfd); 1598 } 1599 if (i == 1) { 1600 if (resp < 0) 1601 pstatus(pp, "waiting for %s to come up", 1602 pp->remote_host); 1603 else { 1604 pstatus(pp, 1605 "waiting for queue to be enabled on %s", 1606 pp->remote_host); 1607 i = 256; 1608 } 1609 } 1610 sleep(i); 1611 } 1612 pstatus(pp, "sending to %s", pp->remote_host); 1613} 1614 1615/* 1616 * setup tty lines. 1617 */ 1618static void 1619setty(pp) 1620 const struct printer *pp; 1621{ 1622 struct termios ttybuf; 1623 1624 if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 1625 syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 1626 exit(1); 1627 } 1628 if (tcgetattr(pfd, &ttybuf) < 0) { 1629 syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 1630 exit(1); 1631 } 1632 if (pp->baud_rate > 0) 1633 cfsetspeed(&ttybuf, pp->baud_rate); 1634 if (pp->mode_set) { 1635 char *s = strdup(pp->mode_set), *tmp; 1636 1637 while ((tmp = strsep(&s, ",")) != NULL) { 1638 (void) msearch(tmp, &ttybuf); 1639 } 1640 } 1641 if (pp->mode_set != 0 || pp->baud_rate > 0) { 1642 if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 1643 syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 1644 } 1645 } 1646} 1647 1648#ifdef __STDC__ 1649#include <stdarg.h> 1650#else 1651#include <varargs.h> 1652#endif 1653 1654static void 1655#ifdef __STDC__ 1656pstatus(const struct printer *pp, const char *msg, ...) 1657#else 1658pstatus(pp, msg, va_alist) 1659 const struct printer *pp; 1660 char *msg; 1661 va_dcl 1662#endif 1663{ 1664 int fd; 1665 char *buf; 1666 va_list ap; 1667#ifdef __STDC__ 1668 va_start(ap, msg); 1669#else 1670 va_start(ap); 1671#endif 1672 1673 umask(0); 1674 fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 1675 if (fd < 0) { 1676 syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file); 1677 exit(1); 1678 } 1679 ftruncate(fd, 0); 1680 vasprintf(&buf, msg, ap); 1681 va_end(ap); 1682 writel(fd, buf, "\n", (char *)0); 1683 close(fd); 1684 free(buf); 1685} 1686 1687void 1688alarmhandler(signo) 1689{ 1690 /* ignored */ 1691} 1692