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