printjob.c revision 1.13 1 1.13 mikel /* $NetBSD: printjob.c,v 1.13 1997/07/10 06:28:58 mikel Exp $ */
2 1.1 cgd /*
3 1.5 cgd * Copyright (c) 1983, 1993
4 1.5 cgd * The Regents of the University of California. All rights reserved.
5 1.5 cgd *
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.5 cgd static char copyright[] =
38 1.5 cgd "@(#) Copyright (c) 1983, 1993\n\
39 1.5 cgd The Regents of the University of California. All rights reserved.\n";
40 1.5 cgd #endif /* not lint */
41 1.5 cgd
42 1.5 cgd #ifndef lint
43 1.13 mikel #if 0
44 1.5 cgd static char sccsid[] = "@(#)printjob.c 8.2 (Berkeley) 4/16/94";
45 1.13 mikel #else
46 1.13 mikel static char rcsid[] = "$NetBSD";
47 1.13 mikel #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.5 cgd
51 1.1 cgd /*
52 1.1 cgd * printjob -- print jobs in the queue.
53 1.1 cgd *
54 1.1 cgd * NOTE: the lock file is used to pass information to lpq and lprm.
55 1.1 cgd * it does not need to be removed because file locks are dynamic.
56 1.1 cgd */
57 1.1 cgd
58 1.5 cgd #include <sys/param.h>
59 1.5 cgd #include <sys/wait.h>
60 1.5 cgd #include <sys/stat.h>
61 1.5 cgd #include <sys/types.h>
62 1.5 cgd
63 1.5 cgd #include <pwd.h>
64 1.5 cgd #include <unistd.h>
65 1.5 cgd #include <signal.h>
66 1.7 hpeyerl #include <termios.h>
67 1.5 cgd #include <syslog.h>
68 1.5 cgd #include <fcntl.h>
69 1.5 cgd #include <dirent.h>
70 1.5 cgd #include <errno.h>
71 1.5 cgd #include <stdio.h>
72 1.5 cgd #include <string.h>
73 1.5 cgd #include <stdlib.h>
74 1.1 cgd #include "lp.h"
75 1.5 cgd #include "lp.local.h"
76 1.1 cgd #include "pathnames.h"
77 1.5 cgd #include "extern.h"
78 1.1 cgd
79 1.1 cgd #define DORETURN 0 /* absorb fork error */
80 1.1 cgd #define DOABORT 1 /* abort if dofork fails */
81 1.1 cgd
82 1.1 cgd /*
83 1.1 cgd * Error tokens
84 1.1 cgd */
85 1.1 cgd #define REPRINT -2
86 1.1 cgd #define ERROR -1
87 1.1 cgd #define OK 0
88 1.1 cgd #define FATALERR 1
89 1.1 cgd #define NOACCT 2
90 1.1 cgd #define FILTERERR 3
91 1.1 cgd #define ACCESS 4
92 1.1 cgd
93 1.5 cgd static dev_t fdev; /* device of file pointed to by symlink */
94 1.5 cgd static ino_t fino; /* inode of file pointed to by symlink */
95 1.5 cgd static FILE *cfp; /* control file */
96 1.5 cgd static int child; /* id of any filters */
97 1.5 cgd static int lfd; /* lock file descriptor */
98 1.5 cgd static int ofd; /* output filter file descriptor */
99 1.5 cgd static int ofilter; /* id of output filter, if any */
100 1.5 cgd static int pfd; /* prstatic inter file descriptor */
101 1.5 cgd static int pid; /* pid of lpd process */
102 1.5 cgd static int prchild; /* id of pr process */
103 1.5 cgd static int remote; /* true if sending files to remote */
104 1.5 cgd static char title[80]; /* ``pr'' title */
105 1.5 cgd static int tof; /* true if at top of form */
106 1.5 cgd
107 1.5 cgd static char class[32]; /* classification field */
108 1.5 cgd static char fromhost[32]; /* user's host machine */
109 1.5 cgd /* indentation size in static characters */
110 1.5 cgd static char indent[10] = "-i0";
111 1.5 cgd static char jobname[100]; /* job or file name */
112 1.5 cgd static char length[10] = "-l"; /* page length in lines */
113 1.5 cgd static char logname[32]; /* user's login name */
114 1.5 cgd static char pxlength[10] = "-y"; /* page length in pixels */
115 1.5 cgd static char pxwidth[10] = "-x"; /* page width in pixels */
116 1.5 cgd static char tempfile[] = "errsXXXXXX"; /* file name for filter output */
117 1.5 cgd static char width[10] = "-w"; /* page width in static characters */
118 1.5 cgd
119 1.5 cgd static void abortpr __P((int));
120 1.5 cgd static void banner __P((char *, char *));
121 1.5 cgd static int dofork __P((int));
122 1.5 cgd static int dropit __P((int));
123 1.5 cgd static void init __P((void));
124 1.5 cgd static void openpr __P((void));
125 1.5 cgd static int print __P((int, char *));
126 1.5 cgd static int printit __P((char *));
127 1.5 cgd static void pstatus __P((const char *, ...));
128 1.5 cgd static char response __P((void));
129 1.5 cgd static void scan_out __P((int, char *, int));
130 1.5 cgd static char *scnline __P((int, char *, int));
131 1.5 cgd static int sendfile __P((int, char *));
132 1.5 cgd static int sendit __P((char *));
133 1.5 cgd static void sendmail __P((char *, int));
134 1.7 hpeyerl static void set_ttyflags __P((struct termios *));
135 1.5 cgd static void setty __P((void));
136 1.1 cgd
137 1.5 cgd void
138 1.1 cgd printjob()
139 1.1 cgd {
140 1.1 cgd struct stat stb;
141 1.1 cgd register struct queue *q, **qp;
142 1.1 cgd struct queue **queue;
143 1.1 cgd register int i, nitems;
144 1.1 cgd long pidoff;
145 1.1 cgd int count = 0;
146 1.1 cgd
147 1.1 cgd init(); /* set up capabilities */
148 1.11 mrg (void)write(1, "", 1); /* ack that daemon is started */
149 1.11 mrg (void)close(2); /* set up log file */
150 1.1 cgd if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) {
151 1.1 cgd syslog(LOG_ERR, "%s: %m", LF);
152 1.11 mrg (void)open(_PATH_DEVNULL, O_WRONLY);
153 1.1 cgd }
154 1.1 cgd setgid(getegid());
155 1.1 cgd pid = getpid(); /* for use with lprm */
156 1.1 cgd setpgrp(0, pid);
157 1.1 cgd signal(SIGHUP, abortpr);
158 1.1 cgd signal(SIGINT, abortpr);
159 1.1 cgd signal(SIGQUIT, abortpr);
160 1.1 cgd signal(SIGTERM, abortpr);
161 1.1 cgd
162 1.11 mrg (void)mktemp(tempfile);
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * uses short form file names
166 1.1 cgd */
167 1.1 cgd if (chdir(SD) < 0) {
168 1.1 cgd syslog(LOG_ERR, "%s: %m", SD);
169 1.1 cgd exit(1);
170 1.1 cgd }
171 1.1 cgd if (stat(LO, &stb) == 0 && (stb.st_mode & 0100))
172 1.1 cgd exit(0); /* printing disabled */
173 1.1 cgd lfd = open(LO, O_WRONLY|O_CREAT, 0644);
174 1.1 cgd if (lfd < 0) {
175 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, LO);
176 1.1 cgd exit(1);
177 1.1 cgd }
178 1.1 cgd if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
179 1.1 cgd if (errno == EWOULDBLOCK) /* active deamon present */
180 1.1 cgd exit(0);
181 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, LO);
182 1.1 cgd exit(1);
183 1.1 cgd }
184 1.1 cgd ftruncate(lfd, 0);
185 1.1 cgd /*
186 1.1 cgd * write process id for others to know
187 1.1 cgd */
188 1.11 mrg pidoff = i = snprintf(line, sizeof(line), "%u\n", pid);
189 1.1 cgd if (write(lfd, line, i) != i) {
190 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, LO);
191 1.1 cgd exit(1);
192 1.1 cgd }
193 1.1 cgd /*
194 1.1 cgd * search the spool directory for work and sort by queue order.
195 1.1 cgd */
196 1.1 cgd if ((nitems = getq(&queue)) < 0) {
197 1.1 cgd syslog(LOG_ERR, "%s: can't scan %s", printer, SD);
198 1.1 cgd exit(1);
199 1.1 cgd }
200 1.1 cgd if (nitems == 0) /* no work to do */
201 1.1 cgd exit(0);
202 1.1 cgd if (stb.st_mode & 01) { /* reset queue flag */
203 1.1 cgd if (fchmod(lfd, stb.st_mode & 0776) < 0)
204 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, LO);
205 1.1 cgd }
206 1.1 cgd openpr(); /* open printer or remote */
207 1.1 cgd again:
208 1.1 cgd /*
209 1.1 cgd * we found something to do now do it --
210 1.1 cgd * write the name of the current control file into the lock file
211 1.1 cgd * so the spool queue program can tell what we're working on
212 1.1 cgd */
213 1.1 cgd for (qp = queue; nitems--; free((char *) q)) {
214 1.1 cgd q = *qp++;
215 1.1 cgd if (stat(q->q_name, &stb) < 0)
216 1.1 cgd continue;
217 1.1 cgd restart:
218 1.11 mrg (void)lseek(lfd, (off_t)pidoff, 0);
219 1.11 mrg i = snprintf(line, sizeof(line), "%s\n", q->q_name);
220 1.1 cgd if (write(lfd, line, i) != i)
221 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, LO);
222 1.1 cgd if (!remote)
223 1.1 cgd i = printit(q->q_name);
224 1.1 cgd else
225 1.1 cgd i = sendit(q->q_name);
226 1.1 cgd /*
227 1.1 cgd * Check to see if we are supposed to stop printing or
228 1.1 cgd * if we are to rebuild the queue.
229 1.1 cgd */
230 1.1 cgd if (fstat(lfd, &stb) == 0) {
231 1.1 cgd /* stop printing before starting next job? */
232 1.1 cgd if (stb.st_mode & 0100)
233 1.1 cgd goto done;
234 1.1 cgd /* rebuild queue (after lpc topq) */
235 1.1 cgd if (stb.st_mode & 01) {
236 1.1 cgd for (free((char *) q); nitems--; free((char *) q))
237 1.1 cgd q = *qp++;
238 1.1 cgd if (fchmod(lfd, stb.st_mode & 0776) < 0)
239 1.1 cgd syslog(LOG_WARNING, "%s: %s: %m",
240 1.1 cgd printer, LO);
241 1.1 cgd break;
242 1.1 cgd }
243 1.1 cgd }
244 1.1 cgd if (i == OK) /* file ok and printed */
245 1.1 cgd count++;
246 1.1 cgd else if (i == REPRINT) { /* try reprinting the job */
247 1.1 cgd syslog(LOG_INFO, "restarting %s", printer);
248 1.1 cgd if (ofilter > 0) {
249 1.1 cgd kill(ofilter, SIGCONT); /* to be sure */
250 1.11 mrg (void)close(ofd);
251 1.1 cgd while ((i = wait(0)) > 0 && i != ofilter)
252 1.1 cgd ;
253 1.1 cgd ofilter = 0;
254 1.1 cgd }
255 1.11 mrg (void)close(pfd); /* close printer */
256 1.1 cgd if (ftruncate(lfd, pidoff) < 0)
257 1.1 cgd syslog(LOG_WARNING, "%s: %s: %m", printer, LO);
258 1.1 cgd openpr(); /* try to reopen printer */
259 1.1 cgd goto restart;
260 1.1 cgd }
261 1.1 cgd }
262 1.1 cgd free((char *) queue);
263 1.1 cgd /*
264 1.1 cgd * search the spool directory for more work.
265 1.1 cgd */
266 1.1 cgd if ((nitems = getq(&queue)) < 0) {
267 1.1 cgd syslog(LOG_ERR, "%s: can't scan %s", printer, SD);
268 1.1 cgd exit(1);
269 1.1 cgd }
270 1.1 cgd if (nitems == 0) { /* no more work to do */
271 1.1 cgd done:
272 1.1 cgd if (count > 0) { /* Files actually printed */
273 1.1 cgd if (!SF && !tof)
274 1.11 mrg (void)write(ofd, FF, strlen(FF));
275 1.1 cgd if (TR != NULL) /* output trailer */
276 1.11 mrg (void)write(ofd, TR, strlen(TR));
277 1.1 cgd }
278 1.11 mrg (void)unlink(tempfile);
279 1.1 cgd exit(0);
280 1.1 cgd }
281 1.1 cgd goto again;
282 1.1 cgd }
283 1.1 cgd
284 1.11 mrg #define FONTLEN 50
285 1.11 mrg char fonts[4][FONTLEN]; /* fonts for troff */
286 1.1 cgd
287 1.1 cgd char ifonts[4][40] = {
288 1.1 cgd _PATH_VFONTR,
289 1.1 cgd _PATH_VFONTI,
290 1.1 cgd _PATH_VFONTB,
291 1.1 cgd _PATH_VFONTS,
292 1.1 cgd };
293 1.1 cgd
294 1.1 cgd /*
295 1.1 cgd * The remaining part is the reading of the control file (cf)
296 1.1 cgd * and performing the various actions.
297 1.1 cgd */
298 1.5 cgd static int
299 1.1 cgd printit(file)
300 1.1 cgd char *file;
301 1.1 cgd {
302 1.1 cgd register int i;
303 1.1 cgd char *cp;
304 1.1 cgd int bombed = OK;
305 1.1 cgd
306 1.1 cgd /*
307 1.1 cgd * open control file; ignore if no longer there.
308 1.1 cgd */
309 1.1 cgd if ((cfp = fopen(file, "r")) == NULL) {
310 1.1 cgd syslog(LOG_INFO, "%s: %s: %m", printer, file);
311 1.1 cgd return(OK);
312 1.1 cgd }
313 1.1 cgd /*
314 1.1 cgd * Reset troff fonts.
315 1.1 cgd */
316 1.1 cgd for (i = 0; i < 4; i++)
317 1.11 mrg strncpy(fonts[i], ifonts[i], FONTLEN);
318 1.13 mikel (void)snprintf(&width[2], sizeof(width) - 2, "%ld", PW);
319 1.11 mrg indent[2] = '0';
320 1.11 mrg indent[3] = '\0';
321 1.1 cgd
322 1.1 cgd /*
323 1.1 cgd * read the control file for work to do
324 1.1 cgd *
325 1.1 cgd * file format -- first character in the line is a command
326 1.1 cgd * rest of the line is the argument.
327 1.1 cgd * valid commands are:
328 1.1 cgd *
329 1.1 cgd * S -- "stat info" for symbolic link protection
330 1.1 cgd * J -- "job name" on banner page
331 1.1 cgd * C -- "class name" on banner page
332 1.1 cgd * L -- "literal" user's name to print on banner
333 1.1 cgd * T -- "title" for pr
334 1.1 cgd * H -- "host name" of machine where lpr was done
335 1.1 cgd * P -- "person" user's login name
336 1.1 cgd * I -- "indent" amount to indent output
337 1.1 cgd * f -- "file name" name of text file to print
338 1.1 cgd * l -- "file name" text file with control chars
339 1.1 cgd * p -- "file name" text file to print with pr(1)
340 1.1 cgd * t -- "file name" troff(1) file to print
341 1.1 cgd * n -- "file name" ditroff(1) file to print
342 1.1 cgd * d -- "file name" dvi file to print
343 1.1 cgd * g -- "file name" plot(1G) file to print
344 1.1 cgd * v -- "file name" plain raster file to print
345 1.1 cgd * c -- "file name" cifplot file to print
346 1.1 cgd * 1 -- "R font file" for troff
347 1.1 cgd * 2 -- "I font file" for troff
348 1.1 cgd * 3 -- "B font file" for troff
349 1.1 cgd * 4 -- "S font file" for troff
350 1.1 cgd * N -- "name" of file (used by lpq)
351 1.1 cgd * U -- "unlink" name of file to remove
352 1.1 cgd * (after we print it. (Pass 2 only)).
353 1.1 cgd * M -- "mail" to user when done printing
354 1.1 cgd *
355 1.1 cgd * getline reads a line and expands tabs to blanks
356 1.1 cgd */
357 1.1 cgd
358 1.1 cgd /* pass 1 */
359 1.1 cgd
360 1.1 cgd while (getline(cfp))
361 1.1 cgd switch (line[0]) {
362 1.1 cgd case 'H':
363 1.11 mrg strncpy(fromhost, line+1, sizeof(fromhost) - 1);
364 1.1 cgd if (class[0] == '\0')
365 1.11 mrg strncpy(class, line+1, sizeof(class) - 1);
366 1.1 cgd continue;
367 1.1 cgd
368 1.1 cgd case 'P':
369 1.11 mrg strncpy(logname, line+1, sizeof(logname) - 1);
370 1.1 cgd if (RS) { /* restricted */
371 1.5 cgd if (getpwnam(logname) == NULL) {
372 1.1 cgd bombed = NOACCT;
373 1.1 cgd sendmail(line+1, bombed);
374 1.1 cgd goto pass2;
375 1.1 cgd }
376 1.1 cgd }
377 1.1 cgd continue;
378 1.1 cgd
379 1.1 cgd case 'S':
380 1.1 cgd cp = line+1;
381 1.1 cgd i = 0;
382 1.1 cgd while (*cp >= '0' && *cp <= '9')
383 1.1 cgd i = i * 10 + (*cp++ - '0');
384 1.1 cgd fdev = i;
385 1.1 cgd cp++;
386 1.1 cgd i = 0;
387 1.1 cgd while (*cp >= '0' && *cp <= '9')
388 1.1 cgd i = i * 10 + (*cp++ - '0');
389 1.1 cgd fino = i;
390 1.1 cgd continue;
391 1.1 cgd
392 1.1 cgd case 'J':
393 1.1 cgd if (line[1] != '\0')
394 1.11 mrg strncpy(jobname, line+1, sizeof(jobname) - 1);
395 1.11 mrg else {
396 1.11 mrg jobname[0] = ' ';
397 1.11 mrg jobname[1] = '\0';
398 1.11 mrg }
399 1.1 cgd continue;
400 1.1 cgd
401 1.1 cgd case 'C':
402 1.1 cgd if (line[1] != '\0')
403 1.11 mrg strncpy(class, line+1, sizeof(class) - 1);
404 1.1 cgd else if (class[0] == '\0')
405 1.1 cgd gethostname(class, sizeof(class));
406 1.1 cgd continue;
407 1.1 cgd
408 1.1 cgd case 'T': /* header title for pr */
409 1.11 mrg strncpy(title, line+1, sizeof(title) - 1);
410 1.1 cgd continue;
411 1.1 cgd
412 1.1 cgd case 'L': /* identification line */
413 1.1 cgd if (!SH && !HL)
414 1.1 cgd banner(line+1, jobname);
415 1.1 cgd continue;
416 1.1 cgd
417 1.1 cgd case '1': /* troff fonts */
418 1.1 cgd case '2':
419 1.1 cgd case '3':
420 1.1 cgd case '4':
421 1.1 cgd if (line[1] != '\0')
422 1.11 mrg strncpy(fonts[line[0]-'1'], line+1, FONTLEN - 1);
423 1.1 cgd continue;
424 1.1 cgd
425 1.1 cgd case 'W': /* page width */
426 1.11 mrg strncpy(width+2, line+1, sizeof(width) - 3);
427 1.1 cgd continue;
428 1.1 cgd
429 1.1 cgd case 'I': /* indent amount */
430 1.11 mrg strncpy(indent+2, line+1, sizeof(indent) - 3);
431 1.1 cgd continue;
432 1.1 cgd
433 1.1 cgd default: /* some file to print */
434 1.1 cgd switch (i = print(line[0], line+1)) {
435 1.1 cgd case ERROR:
436 1.1 cgd if (bombed == OK)
437 1.1 cgd bombed = FATALERR;
438 1.1 cgd break;
439 1.1 cgd case REPRINT:
440 1.11 mrg (void)fclose(cfp);
441 1.1 cgd return(REPRINT);
442 1.1 cgd case FILTERERR:
443 1.1 cgd case ACCESS:
444 1.1 cgd bombed = i;
445 1.1 cgd sendmail(logname, bombed);
446 1.1 cgd }
447 1.1 cgd title[0] = '\0';
448 1.1 cgd continue;
449 1.1 cgd
450 1.1 cgd case 'N':
451 1.1 cgd case 'U':
452 1.1 cgd case 'M':
453 1.1 cgd continue;
454 1.1 cgd }
455 1.1 cgd
456 1.1 cgd /* pass 2 */
457 1.1 cgd
458 1.1 cgd pass2:
459 1.1 cgd fseek(cfp, 0L, 0);
460 1.1 cgd while (getline(cfp))
461 1.1 cgd switch (line[0]) {
462 1.1 cgd case 'L': /* identification line */
463 1.1 cgd if (!SH && HL)
464 1.1 cgd banner(line+1, jobname);
465 1.1 cgd continue;
466 1.1 cgd
467 1.1 cgd case 'M':
468 1.1 cgd if (bombed < NOACCT) /* already sent if >= NOACCT */
469 1.1 cgd sendmail(line+1, bombed);
470 1.1 cgd continue;
471 1.1 cgd
472 1.1 cgd case 'U':
473 1.11 mrg (void)unlink(line+1);
474 1.1 cgd }
475 1.1 cgd /*
476 1.1 cgd * clean-up in case another control file exists
477 1.1 cgd */
478 1.11 mrg (void)fclose(cfp);
479 1.11 mrg (void)unlink(file);
480 1.1 cgd return(bombed == OK ? OK : ERROR);
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd /*
484 1.1 cgd * Print a file.
485 1.1 cgd * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
486 1.1 cgd * Return -1 if a non-recoverable error occured,
487 1.1 cgd * 2 if the filter detected some errors (but printed the job anyway),
488 1.1 cgd * 1 if we should try to reprint this job and
489 1.1 cgd * 0 if all is well.
490 1.1 cgd * Note: all filters take stdin as the file, stdout as the printer,
491 1.1 cgd * stderr as the log file, and must not ignore SIGINT.
492 1.1 cgd */
493 1.5 cgd static int
494 1.1 cgd print(format, file)
495 1.1 cgd int format;
496 1.1 cgd char *file;
497 1.1 cgd {
498 1.1 cgd register int n;
499 1.1 cgd register char *prog;
500 1.1 cgd int fi, fo;
501 1.1 cgd FILE *fp;
502 1.1 cgd char *av[15], buf[BUFSIZ];
503 1.1 cgd int pid, p[2], stopped = 0;
504 1.1 cgd union wait status;
505 1.1 cgd struct stat stb;
506 1.1 cgd
507 1.1 cgd if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0)
508 1.1 cgd return(ERROR);
509 1.1 cgd /*
510 1.1 cgd * Check to see if data file is a symbolic link. If so, it should
511 1.1 cgd * still point to the same file or someone is trying to print
512 1.1 cgd * something he shouldn't.
513 1.1 cgd */
514 1.1 cgd if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
515 1.1 cgd (stb.st_dev != fdev || stb.st_ino != fino))
516 1.1 cgd return(ACCESS);
517 1.1 cgd if (!SF && !tof) { /* start on a fresh page */
518 1.11 mrg (void)write(ofd, FF, strlen(FF));
519 1.1 cgd tof = 1;
520 1.1 cgd }
521 1.1 cgd if (IF == NULL && (format == 'f' || format == 'l')) {
522 1.1 cgd tof = 0;
523 1.1 cgd while ((n = read(fi, buf, BUFSIZ)) > 0)
524 1.1 cgd if (write(ofd, buf, n) != n) {
525 1.11 mrg (void)close(fi);
526 1.1 cgd return(REPRINT);
527 1.1 cgd }
528 1.11 mrg (void)close(fi);
529 1.1 cgd return(OK);
530 1.1 cgd }
531 1.1 cgd switch (format) {
532 1.1 cgd case 'p': /* print file using 'pr' */
533 1.1 cgd if (IF == NULL) { /* use output filter */
534 1.1 cgd prog = _PATH_PR;
535 1.1 cgd av[0] = "pr";
536 1.1 cgd av[1] = width;
537 1.1 cgd av[2] = length;
538 1.1 cgd av[3] = "-h";
539 1.1 cgd av[4] = *title ? title : " ";
540 1.1 cgd av[5] = 0;
541 1.1 cgd fo = ofd;
542 1.1 cgd goto start;
543 1.1 cgd }
544 1.1 cgd pipe(p);
545 1.1 cgd if ((prchild = dofork(DORETURN)) == 0) { /* child */
546 1.1 cgd dup2(fi, 0); /* file is stdin */
547 1.1 cgd dup2(p[1], 1); /* pipe is stdout */
548 1.1 cgd for (n = 3; n < NOFILE; n++)
549 1.11 mrg (void)close(n);
550 1.1 cgd execl(_PATH_PR, "pr", width, length,
551 1.1 cgd "-h", *title ? title : " ", 0);
552 1.1 cgd syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
553 1.1 cgd exit(2);
554 1.1 cgd }
555 1.11 mrg (void)close(p[1]); /* close output side */
556 1.11 mrg (void)close(fi);
557 1.1 cgd if (prchild < 0) {
558 1.1 cgd prchild = 0;
559 1.11 mrg (void)close(p[0]);
560 1.1 cgd return(ERROR);
561 1.1 cgd }
562 1.1 cgd fi = p[0]; /* use pipe for input */
563 1.1 cgd case 'f': /* print plain text file */
564 1.1 cgd prog = IF;
565 1.1 cgd av[1] = width;
566 1.1 cgd av[2] = length;
567 1.1 cgd av[3] = indent;
568 1.1 cgd n = 4;
569 1.1 cgd break;
570 1.1 cgd case 'l': /* like 'f' but pass control characters */
571 1.1 cgd prog = IF;
572 1.1 cgd av[1] = "-c";
573 1.1 cgd av[2] = width;
574 1.1 cgd av[3] = length;
575 1.1 cgd av[4] = indent;
576 1.1 cgd n = 5;
577 1.1 cgd break;
578 1.1 cgd case 'r': /* print a fortran text file */
579 1.1 cgd prog = RF;
580 1.1 cgd av[1] = width;
581 1.1 cgd av[2] = length;
582 1.1 cgd n = 3;
583 1.1 cgd break;
584 1.1 cgd case 't': /* print troff output */
585 1.1 cgd case 'n': /* print ditroff output */
586 1.1 cgd case 'd': /* print tex output */
587 1.11 mrg (void)unlink(".railmag");
588 1.1 cgd if ((fo = creat(".railmag", FILMOD)) < 0) {
589 1.1 cgd syslog(LOG_ERR, "%s: cannot create .railmag", printer);
590 1.11 mrg (void)unlink(".railmag");
591 1.1 cgd } else {
592 1.1 cgd for (n = 0; n < 4; n++) {
593 1.1 cgd if (fonts[n][0] != '/')
594 1.11 mrg (void)write(fo, _PATH_VFONT,
595 1.5 cgd sizeof(_PATH_VFONT) - 1);
596 1.11 mrg (void)write(fo, fonts[n], strlen(fonts[n]));
597 1.11 mrg (void)write(fo, "\n", 1);
598 1.1 cgd }
599 1.11 mrg (void)close(fo);
600 1.1 cgd }
601 1.1 cgd prog = (format == 't') ? TF : (format == 'n') ? NF : DF;
602 1.1 cgd av[1] = pxwidth;
603 1.1 cgd av[2] = pxlength;
604 1.1 cgd n = 3;
605 1.1 cgd break;
606 1.1 cgd case 'c': /* print cifplot output */
607 1.1 cgd prog = CF;
608 1.1 cgd av[1] = pxwidth;
609 1.1 cgd av[2] = pxlength;
610 1.1 cgd n = 3;
611 1.1 cgd break;
612 1.1 cgd case 'g': /* print plot(1G) output */
613 1.1 cgd prog = GF;
614 1.1 cgd av[1] = pxwidth;
615 1.1 cgd av[2] = pxlength;
616 1.1 cgd n = 3;
617 1.1 cgd break;
618 1.1 cgd case 'v': /* print raster output */
619 1.1 cgd prog = VF;
620 1.1 cgd av[1] = pxwidth;
621 1.1 cgd av[2] = pxlength;
622 1.1 cgd n = 3;
623 1.1 cgd break;
624 1.1 cgd default:
625 1.11 mrg (void)close(fi);
626 1.1 cgd syslog(LOG_ERR, "%s: illegal format character '%c'",
627 1.1 cgd printer, format);
628 1.1 cgd return(ERROR);
629 1.1 cgd }
630 1.1 cgd if ((av[0] = rindex(prog, '/')) != NULL)
631 1.1 cgd av[0]++;
632 1.1 cgd else
633 1.1 cgd av[0] = prog;
634 1.1 cgd av[n++] = "-n";
635 1.1 cgd av[n++] = logname;
636 1.1 cgd av[n++] = "-h";
637 1.1 cgd av[n++] = fromhost;
638 1.1 cgd av[n++] = AF;
639 1.1 cgd av[n] = 0;
640 1.1 cgd fo = pfd;
641 1.1 cgd if (ofilter > 0) { /* stop output filter */
642 1.1 cgd write(ofd, "\031\1", 2);
643 1.1 cgd while ((pid =
644 1.1 cgd wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter)
645 1.1 cgd ;
646 1.1 cgd if (status.w_stopval != WSTOPPED) {
647 1.11 mrg (void)close(fi);
648 1.1 cgd syslog(LOG_WARNING, "%s: output filter died (%d)",
649 1.1 cgd printer, status.w_retcode);
650 1.1 cgd return(REPRINT);
651 1.1 cgd }
652 1.1 cgd stopped++;
653 1.1 cgd }
654 1.1 cgd start:
655 1.1 cgd if ((child = dofork(DORETURN)) == 0) { /* child */
656 1.1 cgd dup2(fi, 0);
657 1.1 cgd dup2(fo, 1);
658 1.12 lukem unlink(tempfile);
659 1.12 lukem n = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0664);
660 1.1 cgd if (n >= 0)
661 1.1 cgd dup2(n, 2);
662 1.1 cgd for (n = 3; n < NOFILE; n++)
663 1.11 mrg (void)close(n);
664 1.1 cgd execv(prog, av);
665 1.1 cgd syslog(LOG_ERR, "cannot execv %s", prog);
666 1.1 cgd exit(2);
667 1.1 cgd }
668 1.11 mrg (void)close(fi);
669 1.1 cgd if (child < 0)
670 1.1 cgd status.w_retcode = 100;
671 1.1 cgd else
672 1.1 cgd while ((pid = wait((int *)&status)) > 0 && pid != child)
673 1.1 cgd ;
674 1.1 cgd child = 0;
675 1.1 cgd prchild = 0;
676 1.1 cgd if (stopped) { /* restart output filter */
677 1.1 cgd if (kill(ofilter, SIGCONT) < 0) {
678 1.1 cgd syslog(LOG_ERR, "cannot restart output filter");
679 1.1 cgd exit(1);
680 1.1 cgd }
681 1.1 cgd }
682 1.1 cgd tof = 0;
683 1.1 cgd
684 1.1 cgd /* Copy filter output to "lf" logfile */
685 1.13 mikel if ((fp = fopen(tempfile, "r")) != NULL) {
686 1.1 cgd while (fgets(buf, sizeof(buf), fp))
687 1.1 cgd fputs(buf, stderr);
688 1.5 cgd fclose(fp);
689 1.1 cgd }
690 1.1 cgd
691 1.1 cgd if (!WIFEXITED(status)) {
692 1.1 cgd syslog(LOG_WARNING, "%s: Daemon filter '%c' terminated (%d)",
693 1.1 cgd printer, format, status.w_termsig);
694 1.1 cgd return(ERROR);
695 1.1 cgd }
696 1.1 cgd switch (status.w_retcode) {
697 1.1 cgd case 0:
698 1.1 cgd tof = 1;
699 1.1 cgd return(OK);
700 1.1 cgd case 1:
701 1.1 cgd return(REPRINT);
702 1.1 cgd default:
703 1.1 cgd syslog(LOG_WARNING, "%s: Daemon filter '%c' exited (%d)",
704 1.1 cgd printer, format, status.w_retcode);
705 1.1 cgd case 2:
706 1.1 cgd return(ERROR);
707 1.1 cgd }
708 1.1 cgd }
709 1.1 cgd
710 1.1 cgd /*
711 1.1 cgd * Send the daemon control file (cf) and any data files.
712 1.1 cgd * Return -1 if a non-recoverable error occured, 1 if a recoverable error and
713 1.1 cgd * 0 if all is well.
714 1.1 cgd */
715 1.5 cgd static int
716 1.1 cgd sendit(file)
717 1.1 cgd char *file;
718 1.1 cgd {
719 1.1 cgd register int i, err = OK;
720 1.1 cgd char *cp, last[BUFSIZ];
721 1.1 cgd
722 1.1 cgd /*
723 1.1 cgd * open control file
724 1.1 cgd */
725 1.1 cgd if ((cfp = fopen(file, "r")) == NULL)
726 1.1 cgd return(OK);
727 1.1 cgd /*
728 1.1 cgd * read the control file for work to do
729 1.1 cgd *
730 1.1 cgd * file format -- first character in the line is a command
731 1.1 cgd * rest of the line is the argument.
732 1.1 cgd * commands of interest are:
733 1.1 cgd *
734 1.1 cgd * a-z -- "file name" name of file to print
735 1.1 cgd * U -- "unlink" name of file to remove
736 1.1 cgd * (after we print it. (Pass 2 only)).
737 1.1 cgd */
738 1.1 cgd
739 1.1 cgd /*
740 1.1 cgd * pass 1
741 1.1 cgd */
742 1.1 cgd while (getline(cfp)) {
743 1.1 cgd again:
744 1.1 cgd if (line[0] == 'S') {
745 1.1 cgd cp = line+1;
746 1.1 cgd i = 0;
747 1.1 cgd while (*cp >= '0' && *cp <= '9')
748 1.1 cgd i = i * 10 + (*cp++ - '0');
749 1.1 cgd fdev = i;
750 1.1 cgd cp++;
751 1.1 cgd i = 0;
752 1.1 cgd while (*cp >= '0' && *cp <= '9')
753 1.1 cgd i = i * 10 + (*cp++ - '0');
754 1.1 cgd fino = i;
755 1.1 cgd continue;
756 1.1 cgd }
757 1.1 cgd if (line[0] >= 'a' && line[0] <= 'z') {
758 1.11 mrg strncpy(last, line, sizeof(last) - 1);
759 1.13 mikel while ((i = getline(cfp)) != 0)
760 1.1 cgd if (strcmp(last, line))
761 1.1 cgd break;
762 1.1 cgd switch (sendfile('\3', last+1)) {
763 1.1 cgd case OK:
764 1.1 cgd if (i)
765 1.1 cgd goto again;
766 1.1 cgd break;
767 1.1 cgd case REPRINT:
768 1.11 mrg (void)fclose(cfp);
769 1.1 cgd return(REPRINT);
770 1.1 cgd case ACCESS:
771 1.1 cgd sendmail(logname, ACCESS);
772 1.1 cgd case ERROR:
773 1.1 cgd err = ERROR;
774 1.1 cgd }
775 1.1 cgd break;
776 1.1 cgd }
777 1.1 cgd }
778 1.1 cgd if (err == OK && sendfile('\2', file) > 0) {
779 1.11 mrg (void)fclose(cfp);
780 1.1 cgd return(REPRINT);
781 1.1 cgd }
782 1.1 cgd /*
783 1.1 cgd * pass 2
784 1.1 cgd */
785 1.1 cgd fseek(cfp, 0L, 0);
786 1.1 cgd while (getline(cfp))
787 1.1 cgd if (line[0] == 'U')
788 1.11 mrg (void)unlink(line+1);
789 1.1 cgd /*
790 1.1 cgd * clean-up in case another control file exists
791 1.1 cgd */
792 1.11 mrg (void)fclose(cfp);
793 1.11 mrg (void)unlink(file);
794 1.1 cgd return(err);
795 1.1 cgd }
796 1.1 cgd
797 1.1 cgd /*
798 1.1 cgd * Send a data file to the remote machine and spool it.
799 1.1 cgd * Return positive if we should try resending.
800 1.1 cgd */
801 1.5 cgd static int
802 1.1 cgd sendfile(type, file)
803 1.5 cgd int type;
804 1.5 cgd char *file;
805 1.1 cgd {
806 1.1 cgd register int f, i, amt;
807 1.1 cgd struct stat stb;
808 1.1 cgd char buf[BUFSIZ];
809 1.1 cgd int sizerr, resp;
810 1.1 cgd
811 1.1 cgd if (lstat(file, &stb) < 0 || (f = open(file, O_RDONLY)) < 0)
812 1.1 cgd return(ERROR);
813 1.1 cgd /*
814 1.1 cgd * Check to see if data file is a symbolic link. If so, it should
815 1.1 cgd * still point to the same file or someone is trying to print something
816 1.1 cgd * he shouldn't.
817 1.1 cgd */
818 1.1 cgd if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 &&
819 1.1 cgd (stb.st_dev != fdev || stb.st_ino != fino))
820 1.1 cgd return(ACCESS);
821 1.11 mrg amt = snprintf(buf, sizeof(buf), "%c%qd %s\n", type, stb.st_size, file);
822 1.1 cgd for (i = 0; ; i++) {
823 1.1 cgd if (write(pfd, buf, amt) != amt ||
824 1.1 cgd (resp = response()) < 0 || resp == '\1') {
825 1.11 mrg (void)close(f);
826 1.1 cgd return(REPRINT);
827 1.1 cgd } else if (resp == '\0')
828 1.1 cgd break;
829 1.1 cgd if (i == 0)
830 1.5 cgd pstatus("no space on remote; waiting for queue to drain");
831 1.1 cgd if (i == 10)
832 1.1 cgd syslog(LOG_ALERT, "%s: can't send to %s; queue full",
833 1.1 cgd printer, RM);
834 1.1 cgd sleep(5 * 60);
835 1.1 cgd }
836 1.1 cgd if (i)
837 1.5 cgd pstatus("sending to %s", RM);
838 1.1 cgd sizerr = 0;
839 1.1 cgd for (i = 0; i < stb.st_size; i += BUFSIZ) {
840 1.1 cgd amt = BUFSIZ;
841 1.1 cgd if (i + amt > stb.st_size)
842 1.1 cgd amt = stb.st_size - i;
843 1.1 cgd if (sizerr == 0 && read(f, buf, amt) != amt)
844 1.1 cgd sizerr = 1;
845 1.1 cgd if (write(pfd, buf, amt) != amt) {
846 1.11 mrg (void)close(f);
847 1.1 cgd return(REPRINT);
848 1.1 cgd }
849 1.1 cgd }
850 1.5 cgd
851 1.5 cgd
852 1.5 cgd
853 1.5 cgd
854 1.11 mrg (void)close(f);
855 1.1 cgd if (sizerr) {
856 1.1 cgd syslog(LOG_INFO, "%s: %s: changed size", printer, file);
857 1.1 cgd /* tell recvjob to ignore this file */
858 1.11 mrg (void)write(pfd, "\1", 1);
859 1.1 cgd return(ERROR);
860 1.1 cgd }
861 1.1 cgd if (write(pfd, "", 1) != 1 || response())
862 1.1 cgd return(REPRINT);
863 1.1 cgd return(OK);
864 1.1 cgd }
865 1.1 cgd
866 1.1 cgd /*
867 1.1 cgd * Check to make sure there have been no errors and that both programs
868 1.1 cgd * are in sync with eachother.
869 1.1 cgd * Return non-zero if the connection was lost.
870 1.1 cgd */
871 1.5 cgd static char
872 1.1 cgd response()
873 1.1 cgd {
874 1.1 cgd char resp;
875 1.1 cgd
876 1.1 cgd if (read(pfd, &resp, 1) != 1) {
877 1.1 cgd syslog(LOG_INFO, "%s: lost connection", printer);
878 1.1 cgd return(-1);
879 1.1 cgd }
880 1.1 cgd return(resp);
881 1.1 cgd }
882 1.1 cgd
883 1.1 cgd /*
884 1.1 cgd * Banner printing stuff
885 1.1 cgd */
886 1.5 cgd static void
887 1.1 cgd banner(name1, name2)
888 1.1 cgd char *name1, *name2;
889 1.1 cgd {
890 1.1 cgd time_t tvec;
891 1.1 cgd extern char *ctime();
892 1.1 cgd
893 1.1 cgd time(&tvec);
894 1.1 cgd if (!SF && !tof)
895 1.11 mrg (void)write(ofd, FF, strlen(FF));
896 1.1 cgd if (SB) { /* short banner only */
897 1.1 cgd if (class[0]) {
898 1.11 mrg (void)write(ofd, class, strlen(class));
899 1.11 mrg (void)write(ofd, ":", 1);
900 1.1 cgd }
901 1.11 mrg (void)write(ofd, name1, strlen(name1));
902 1.11 mrg (void)write(ofd, " Job: ", 7);
903 1.11 mrg (void)write(ofd, name2, strlen(name2));
904 1.11 mrg (void)write(ofd, " Date: ", 8);
905 1.11 mrg (void)write(ofd, ctime(&tvec), 24);
906 1.11 mrg (void)write(ofd, "\n", 1);
907 1.1 cgd } else { /* normal banner */
908 1.11 mrg (void)write(ofd, "\n\n\n", 3);
909 1.1 cgd scan_out(ofd, name1, '\0');
910 1.11 mrg (void)write(ofd, "\n\n", 2);
911 1.1 cgd scan_out(ofd, name2, '\0');
912 1.1 cgd if (class[0]) {
913 1.11 mrg (void)write(ofd,"\n\n\n",3);
914 1.1 cgd scan_out(ofd, class, '\0');
915 1.1 cgd }
916 1.11 mrg (void)write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15);
917 1.11 mrg (void)write(ofd, name2, strlen(name2));
918 1.11 mrg (void)write(ofd, "\n\t\t\t\t\tDate: ", 12);
919 1.11 mrg (void)write(ofd, ctime(&tvec), 24);
920 1.11 mrg (void)write(ofd, "\n", 1);
921 1.1 cgd }
922 1.1 cgd if (!SF)
923 1.11 mrg (void)write(ofd, FF, strlen(FF));
924 1.1 cgd tof = 1;
925 1.1 cgd }
926 1.1 cgd
927 1.5 cgd static char *
928 1.1 cgd scnline(key, p, c)
929 1.5 cgd register int key;
930 1.5 cgd register char *p;
931 1.5 cgd int c;
932 1.1 cgd {
933 1.1 cgd register scnwidth;
934 1.1 cgd
935 1.1 cgd for (scnwidth = WIDTH; --scnwidth;) {
936 1.1 cgd key <<= 1;
937 1.1 cgd *p++ = key & 0200 ? c : BACKGND;
938 1.1 cgd }
939 1.1 cgd return (p);
940 1.1 cgd }
941 1.1 cgd
942 1.1 cgd #define TRC(q) (((q)-' ')&0177)
943 1.1 cgd
944 1.5 cgd static void
945 1.1 cgd scan_out(scfd, scsp, dlm)
946 1.5 cgd int scfd, dlm;
947 1.5 cgd char *scsp;
948 1.1 cgd {
949 1.1 cgd register char *strp;
950 1.1 cgd register nchrs, j;
951 1.1 cgd char outbuf[LINELEN+1], *sp, c, cc;
952 1.1 cgd int d, scnhgt;
953 1.1 cgd extern char scnkey[][HEIGHT]; /* in lpdchar.c */
954 1.1 cgd
955 1.1 cgd for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
956 1.1 cgd strp = &outbuf[0];
957 1.1 cgd sp = scsp;
958 1.1 cgd for (nchrs = 0; ; ) {
959 1.1 cgd d = dropit(c = TRC(cc = *sp++));
960 1.1 cgd if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
961 1.1 cgd for (j = WIDTH; --j;)
962 1.1 cgd *strp++ = BACKGND;
963 1.1 cgd else
964 1.13 mikel strp = scnline(scnkey[(int)c][scnhgt-1-d],
965 1.13 mikel strp, cc);
966 1.13 mikel if (*sp == dlm || *sp == '\0' ||
967 1.13 mikel nchrs++ >= PW/(WIDTH+1)-1)
968 1.1 cgd break;
969 1.1 cgd *strp++ = BACKGND;
970 1.1 cgd *strp++ = BACKGND;
971 1.1 cgd }
972 1.1 cgd while (*--strp == BACKGND && strp >= outbuf)
973 1.1 cgd ;
974 1.1 cgd strp++;
975 1.1 cgd *strp++ = '\n';
976 1.11 mrg (void)write(scfd, outbuf, strp-outbuf);
977 1.1 cgd }
978 1.1 cgd }
979 1.1 cgd
980 1.5 cgd static int
981 1.1 cgd dropit(c)
982 1.5 cgd int c;
983 1.1 cgd {
984 1.1 cgd switch(c) {
985 1.1 cgd
986 1.1 cgd case TRC('_'):
987 1.1 cgd case TRC(';'):
988 1.1 cgd case TRC(','):
989 1.1 cgd case TRC('g'):
990 1.1 cgd case TRC('j'):
991 1.1 cgd case TRC('p'):
992 1.1 cgd case TRC('q'):
993 1.1 cgd case TRC('y'):
994 1.1 cgd return (DROP);
995 1.1 cgd
996 1.1 cgd default:
997 1.1 cgd return (0);
998 1.1 cgd }
999 1.1 cgd }
1000 1.1 cgd
1001 1.1 cgd /*
1002 1.1 cgd * sendmail ---
1003 1.1 cgd * tell people about job completion
1004 1.1 cgd */
1005 1.5 cgd static void
1006 1.1 cgd sendmail(user, bombed)
1007 1.1 cgd char *user;
1008 1.1 cgd int bombed;
1009 1.1 cgd {
1010 1.1 cgd register int i;
1011 1.1 cgd int p[2], s;
1012 1.1 cgd register char *cp;
1013 1.1 cgd char buf[100];
1014 1.1 cgd struct stat stb;
1015 1.1 cgd FILE *fp;
1016 1.1 cgd
1017 1.1 cgd pipe(p);
1018 1.1 cgd if ((s = dofork(DORETURN)) == 0) { /* child */
1019 1.1 cgd dup2(p[0], 0);
1020 1.1 cgd for (i = 3; i < NOFILE; i++)
1021 1.11 mrg (void)close(i);
1022 1.1 cgd if ((cp = rindex(_PATH_SENDMAIL, '/')) != NULL)
1023 1.1 cgd cp++;
1024 1.5 cgd else
1025 1.1 cgd cp = _PATH_SENDMAIL;
1026 1.11 mrg (void)snprintf(buf, sizeof(buf), "%s@%s", user, fromhost);
1027 1.1 cgd execl(_PATH_SENDMAIL, cp, buf, 0);
1028 1.1 cgd exit(0);
1029 1.1 cgd } else if (s > 0) { /* parent */
1030 1.1 cgd dup2(p[1], 1);
1031 1.1 cgd printf("To: %s@%s\n", user, fromhost);
1032 1.1 cgd printf("Subject: printer job\n\n");
1033 1.1 cgd printf("Your printer job ");
1034 1.1 cgd if (*jobname)
1035 1.1 cgd printf("(%s) ", jobname);
1036 1.1 cgd switch (bombed) {
1037 1.1 cgd case OK:
1038 1.1 cgd printf("\ncompleted successfully\n");
1039 1.1 cgd break;
1040 1.1 cgd default:
1041 1.1 cgd case FATALERR:
1042 1.1 cgd printf("\ncould not be printed\n");
1043 1.1 cgd break;
1044 1.1 cgd case NOACCT:
1045 1.1 cgd printf("\ncould not be printed without an account on %s\n", host);
1046 1.1 cgd break;
1047 1.1 cgd case FILTERERR:
1048 1.1 cgd if (stat(tempfile, &stb) < 0 || stb.st_size == 0 ||
1049 1.1 cgd (fp = fopen(tempfile, "r")) == NULL) {
1050 1.1 cgd printf("\nwas printed but had some errors\n");
1051 1.1 cgd break;
1052 1.1 cgd }
1053 1.1 cgd printf("\nwas printed but had the following errors:\n");
1054 1.1 cgd while ((i = getc(fp)) != EOF)
1055 1.1 cgd putchar(i);
1056 1.11 mrg (void)fclose(fp);
1057 1.1 cgd break;
1058 1.1 cgd case ACCESS:
1059 1.1 cgd printf("\nwas not printed because it was not linked to the original file\n");
1060 1.1 cgd }
1061 1.1 cgd fflush(stdout);
1062 1.11 mrg (void)close(1);
1063 1.1 cgd }
1064 1.11 mrg (void)close(p[0]);
1065 1.11 mrg (void)close(p[1]);
1066 1.1 cgd wait(&s);
1067 1.1 cgd }
1068 1.1 cgd
1069 1.1 cgd /*
1070 1.1 cgd * dofork - fork with retries on failure
1071 1.1 cgd */
1072 1.5 cgd static int
1073 1.1 cgd dofork(action)
1074 1.1 cgd int action;
1075 1.1 cgd {
1076 1.1 cgd register int i, pid;
1077 1.6 mycroft struct passwd *pw;
1078 1.1 cgd
1079 1.1 cgd for (i = 0; i < 20; i++) {
1080 1.1 cgd if ((pid = fork()) < 0) {
1081 1.1 cgd sleep((unsigned)(i*i));
1082 1.1 cgd continue;
1083 1.1 cgd }
1084 1.1 cgd /*
1085 1.1 cgd * Child should run as daemon instead of root
1086 1.1 cgd */
1087 1.6 mycroft if (pid == 0) {
1088 1.6 mycroft pw = getpwuid(DU);
1089 1.6 mycroft if (pw == 0) {
1090 1.13 mikel syslog(LOG_ERR, "uid %ld not in password file",
1091 1.6 mycroft DU);
1092 1.6 mycroft break;
1093 1.6 mycroft }
1094 1.6 mycroft initgroups(pw->pw_name, pw->pw_gid);
1095 1.6 mycroft setgid(pw->pw_gid);
1096 1.1 cgd setuid(DU);
1097 1.6 mycroft }
1098 1.6 mycroft return (pid);
1099 1.1 cgd }
1100 1.1 cgd syslog(LOG_ERR, "can't fork");
1101 1.1 cgd
1102 1.1 cgd switch (action) {
1103 1.1 cgd case DORETURN:
1104 1.1 cgd return (-1);
1105 1.1 cgd default:
1106 1.1 cgd syslog(LOG_ERR, "bad action (%d) to dofork", action);
1107 1.1 cgd /*FALL THRU*/
1108 1.1 cgd case DOABORT:
1109 1.1 cgd exit(1);
1110 1.1 cgd }
1111 1.1 cgd /*NOTREACHED*/
1112 1.1 cgd }
1113 1.1 cgd
1114 1.1 cgd /*
1115 1.1 cgd * Kill child processes to abort current job.
1116 1.1 cgd */
1117 1.5 cgd static void
1118 1.5 cgd abortpr(signo)
1119 1.5 cgd int signo;
1120 1.1 cgd {
1121 1.11 mrg (void)unlink(tempfile);
1122 1.1 cgd kill(0, SIGINT);
1123 1.1 cgd if (ofilter > 0)
1124 1.1 cgd kill(ofilter, SIGCONT);
1125 1.1 cgd while (wait(NULL) > 0)
1126 1.1 cgd ;
1127 1.1 cgd exit(0);
1128 1.1 cgd }
1129 1.1 cgd
1130 1.5 cgd static void
1131 1.1 cgd init()
1132 1.1 cgd {
1133 1.1 cgd int status;
1134 1.1 cgd char *s;
1135 1.1 cgd
1136 1.5 cgd if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
1137 1.1 cgd syslog(LOG_ERR, "can't open printer description file");
1138 1.1 cgd exit(1);
1139 1.5 cgd } else if (status == -1) {
1140 1.1 cgd syslog(LOG_ERR, "unknown printer: %s", printer);
1141 1.1 cgd exit(1);
1142 1.5 cgd } else if (status == -3)
1143 1.5 cgd fatal("potential reference loop detected in printcap file");
1144 1.5 cgd
1145 1.5 cgd if (cgetstr(bp, "lp", &LP) == -1)
1146 1.1 cgd LP = _PATH_DEFDEVLP;
1147 1.5 cgd if (cgetstr(bp, "rp", &RP) == -1)
1148 1.1 cgd RP = DEFLP;
1149 1.5 cgd if (cgetstr(bp, "lo", &LO) == -1)
1150 1.1 cgd LO = DEFLOCK;
1151 1.5 cgd if (cgetstr(bp, "st", &ST) == -1)
1152 1.1 cgd ST = DEFSTAT;
1153 1.5 cgd if (cgetstr(bp, "lf", &LF) == -1)
1154 1.1 cgd LF = _PATH_CONSOLE;
1155 1.5 cgd if (cgetstr(bp, "sd", &SD) == -1)
1156 1.1 cgd SD = _PATH_DEFSPOOL;
1157 1.5 cgd if (cgetnum(bp, "du", &DU) < 0)
1158 1.1 cgd DU = DEFUID;
1159 1.5 cgd if (cgetstr(bp,"ff", &FF) == -1)
1160 1.1 cgd FF = DEFFF;
1161 1.5 cgd if (cgetnum(bp, "pw", &PW) < 0)
1162 1.1 cgd PW = DEFWIDTH;
1163 1.13 mikel (void)snprintf(&width[2], sizeof(width) - 2, "%ld", PW);
1164 1.5 cgd if (cgetnum(bp, "pl", &PL) < 0)
1165 1.1 cgd PL = DEFLENGTH;
1166 1.13 mikel (void)snprintf(&length[2], sizeof(length) - 2, "%ld", PL);
1167 1.5 cgd if (cgetnum(bp,"px", &PX) < 0)
1168 1.1 cgd PX = 0;
1169 1.13 mikel (void)snprintf(&pxwidth[2], sizeof(pxwidth) - 2, "%ld", PX);
1170 1.5 cgd if (cgetnum(bp, "py", &PY) < 0)
1171 1.1 cgd PY = 0;
1172 1.13 mikel (void)snprintf(&pxlength[2], sizeof(pxlength) - 2, "%ld", PY);
1173 1.5 cgd cgetstr(bp, "rm", &RM);
1174 1.13 mikel if ((s = checkremote()) != NULL)
1175 1.1 cgd syslog(LOG_WARNING, s);
1176 1.1 cgd
1177 1.5 cgd cgetstr(bp, "af", &AF);
1178 1.5 cgd cgetstr(bp, "of", &OF);
1179 1.5 cgd cgetstr(bp, "if", &IF);
1180 1.5 cgd cgetstr(bp, "rf", &RF);
1181 1.5 cgd cgetstr(bp, "tf", &TF);
1182 1.5 cgd cgetstr(bp, "nf", &NF);
1183 1.5 cgd cgetstr(bp, "df", &DF);
1184 1.5 cgd cgetstr(bp, "gf", &GF);
1185 1.5 cgd cgetstr(bp, "vf", &VF);
1186 1.5 cgd cgetstr(bp, "cf", &CF);
1187 1.5 cgd cgetstr(bp, "tr", &TR);
1188 1.5 cgd
1189 1.5 cgd RS = (cgetcap(bp, "rs", ':') != NULL);
1190 1.5 cgd SF = (cgetcap(bp, "sf", ':') != NULL);
1191 1.5 cgd SH = (cgetcap(bp, "sh", ':') != NULL);
1192 1.5 cgd SB = (cgetcap(bp, "sb", ':') != NULL);
1193 1.5 cgd HL = (cgetcap(bp, "hl", ':') != NULL);
1194 1.5 cgd RW = (cgetcap(bp, "rw", ':') != NULL);
1195 1.5 cgd
1196 1.5 cgd cgetnum(bp, "br", &BR);
1197 1.5 cgd if (cgetnum(bp, "fc", &FC) < 0)
1198 1.1 cgd FC = 0;
1199 1.5 cgd if (cgetnum(bp, "fs", &FS) < 0)
1200 1.1 cgd FS = 0;
1201 1.5 cgd if (cgetnum(bp, "xc", &XC) < 0)
1202 1.1 cgd XC = 0;
1203 1.5 cgd if (cgetnum(bp, "xs", &XS) < 0)
1204 1.1 cgd XS = 0;
1205 1.7 hpeyerl cgetstr(bp, "ms", &MS);
1206 1.5 cgd
1207 1.5 cgd tof = (cgetcap(bp, "fo", ':') == NULL);
1208 1.1 cgd }
1209 1.1 cgd
1210 1.1 cgd /*
1211 1.1 cgd * Acquire line printer or remote connection.
1212 1.1 cgd */
1213 1.5 cgd static void
1214 1.1 cgd openpr()
1215 1.1 cgd {
1216 1.1 cgd register int i, n;
1217 1.1 cgd int resp;
1218 1.1 cgd
1219 1.1 cgd if (!sendtorem && *LP) {
1220 1.1 cgd for (i = 1; ; i = i < 32 ? i << 1 : i) {
1221 1.1 cgd pfd = open(LP, RW ? O_RDWR : O_WRONLY);
1222 1.1 cgd if (pfd >= 0)
1223 1.1 cgd break;
1224 1.1 cgd if (errno == ENOENT) {
1225 1.1 cgd syslog(LOG_ERR, "%s: %m", LP);
1226 1.1 cgd exit(1);
1227 1.1 cgd }
1228 1.1 cgd if (i == 1)
1229 1.5 cgd pstatus("waiting for %s to become ready (offline ?)", printer);
1230 1.1 cgd sleep(i);
1231 1.1 cgd }
1232 1.1 cgd if (isatty(pfd))
1233 1.1 cgd setty();
1234 1.5 cgd pstatus("%s is ready and printing", printer);
1235 1.1 cgd } else if (RM != NULL) {
1236 1.1 cgd for (i = 1; ; i = i < 256 ? i << 1 : i) {
1237 1.1 cgd resp = -1;
1238 1.1 cgd pfd = getport(RM);
1239 1.1 cgd if (pfd >= 0) {
1240 1.11 mrg n = snprintf(line, sizeof(line), "\2%s\n", RP);
1241 1.1 cgd if (write(pfd, line, n) == n &&
1242 1.1 cgd (resp = response()) == '\0')
1243 1.1 cgd break;
1244 1.11 mrg (void)close(pfd);
1245 1.1 cgd }
1246 1.1 cgd if (i == 1) {
1247 1.1 cgd if (resp < 0)
1248 1.5 cgd pstatus("waiting for %s to come up", RM);
1249 1.1 cgd else {
1250 1.5 cgd pstatus("waiting for queue to be enabled on %s", RM);
1251 1.1 cgd i = 256;
1252 1.1 cgd }
1253 1.1 cgd }
1254 1.1 cgd sleep(i);
1255 1.1 cgd }
1256 1.5 cgd pstatus("sending to %s", RM);
1257 1.1 cgd remote = 1;
1258 1.1 cgd } else {
1259 1.1 cgd syslog(LOG_ERR, "%s: no line printer device or host name",
1260 1.1 cgd printer);
1261 1.1 cgd exit(1);
1262 1.1 cgd }
1263 1.1 cgd /*
1264 1.1 cgd * Start up an output filter, if needed.
1265 1.1 cgd */
1266 1.1 cgd if (!remote && OF) {
1267 1.1 cgd int p[2];
1268 1.1 cgd char *cp;
1269 1.1 cgd
1270 1.1 cgd pipe(p);
1271 1.1 cgd if ((ofilter = dofork(DOABORT)) == 0) { /* child */
1272 1.1 cgd dup2(p[0], 0); /* pipe is std in */
1273 1.1 cgd dup2(pfd, 1); /* printer is std out */
1274 1.1 cgd for (i = 3; i < NOFILE; i++)
1275 1.11 mrg (void)close(i);
1276 1.1 cgd if ((cp = rindex(OF, '/')) == NULL)
1277 1.1 cgd cp = OF;
1278 1.1 cgd else
1279 1.1 cgd cp++;
1280 1.1 cgd execl(OF, cp, width, length, 0);
1281 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, OF);
1282 1.1 cgd exit(1);
1283 1.1 cgd }
1284 1.11 mrg (void)close(p[0]); /* close input side */
1285 1.1 cgd ofd = p[1]; /* use pipe for output */
1286 1.1 cgd } else {
1287 1.1 cgd ofd = pfd;
1288 1.1 cgd ofilter = 0;
1289 1.1 cgd }
1290 1.1 cgd }
1291 1.1 cgd
1292 1.10 jtc #if !defined(__NetBSD__)
1293 1.1 cgd struct bauds {
1294 1.1 cgd int baud;
1295 1.1 cgd int speed;
1296 1.1 cgd } bauds[] = {
1297 1.1 cgd 50, B50,
1298 1.1 cgd 75, B75,
1299 1.1 cgd 110, B110,
1300 1.1 cgd 134, B134,
1301 1.1 cgd 150, B150,
1302 1.1 cgd 200, B200,
1303 1.1 cgd 300, B300,
1304 1.1 cgd 600, B600,
1305 1.1 cgd 1200, B1200,
1306 1.1 cgd 1800, B1800,
1307 1.1 cgd 2400, B2400,
1308 1.1 cgd 4800, B4800,
1309 1.1 cgd 9600, B9600,
1310 1.7 hpeyerl 19200, B19200,
1311 1.7 hpeyerl 38400, B38400,
1312 1.1 cgd 0, 0
1313 1.1 cgd };
1314 1.10 jtc #endif
1315 1.1 cgd
1316 1.1 cgd /*
1317 1.1 cgd * setup tty lines.
1318 1.1 cgd */
1319 1.5 cgd static void
1320 1.1 cgd setty()
1321 1.1 cgd {
1322 1.7 hpeyerl struct info i;
1323 1.7 hpeyerl char **argv, **ap, *p, *val;
1324 1.1 cgd
1325 1.7 hpeyerl i.fd = pfd;
1326 1.7 hpeyerl i.set = i.wset = 0;
1327 1.7 hpeyerl if (ioctl(i.fd, TIOCEXCL, (char *)0) < 0) {
1328 1.1 cgd syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", printer);
1329 1.1 cgd exit(1);
1330 1.1 cgd }
1331 1.7 hpeyerl if (tcgetattr(i.fd, &i.t) < 0) {
1332 1.7 hpeyerl syslog(LOG_ERR, "%s: tcgetattr: %m", printer);
1333 1.1 cgd exit(1);
1334 1.1 cgd }
1335 1.1 cgd if (BR > 0) {
1336 1.10 jtc #if !defined(__NetBSD__)
1337 1.10 jtc register struct bauds *bp;
1338 1.1 cgd for (bp = bauds; bp->baud; bp++)
1339 1.1 cgd if (BR == bp->baud)
1340 1.1 cgd break;
1341 1.1 cgd if (!bp->baud) {
1342 1.1 cgd syslog(LOG_ERR, "%s: illegal baud rate %d", printer, BR);
1343 1.1 cgd exit(1);
1344 1.1 cgd }
1345 1.7 hpeyerl cfsetspeed(&i.t, bp->speed);
1346 1.10 jtc #else
1347 1.10 jtc cfsetspeed(&i.t, BR);
1348 1.10 jtc #endif
1349 1.7 hpeyerl i.set = 1;
1350 1.1 cgd }
1351 1.7 hpeyerl if (MS) {
1352 1.7 hpeyerl if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0) {
1353 1.7 hpeyerl syslog(LOG_ERR, "%s: ioctl(TIOCGETD): %m", printer);
1354 1.1 cgd exit(1);
1355 1.1 cgd }
1356 1.7 hpeyerl if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
1357 1.7 hpeyerl syslog(LOG_INFO, "%s: ioctl(TIOCGWINSZ): %m",
1358 1.7 hpeyerl printer);
1359 1.7 hpeyerl
1360 1.7 hpeyerl argv = (char **)calloc(256, sizeof(char *));
1361 1.7 hpeyerl if (argv == NULL) {
1362 1.7 hpeyerl syslog(LOG_ERR, "%s: calloc: %m", printer);
1363 1.1 cgd exit(1);
1364 1.1 cgd }
1365 1.7 hpeyerl p = strdup(MS);
1366 1.7 hpeyerl ap = argv;
1367 1.7 hpeyerl while ((val = strsep(&p, " \t,")) != NULL) {
1368 1.7 hpeyerl *ap++ = strdup(val);
1369 1.7 hpeyerl }
1370 1.7 hpeyerl
1371 1.7 hpeyerl for (; *argv; ++argv) {
1372 1.7 hpeyerl if (ksearch(&argv, &i))
1373 1.7 hpeyerl continue;
1374 1.7 hpeyerl if (msearch(&argv, &i))
1375 1.7 hpeyerl continue;
1376 1.7 hpeyerl syslog(LOG_INFO, "%s: unknown stty flag: %s",
1377 1.7 hpeyerl printer, *argv);
1378 1.7 hpeyerl }
1379 1.7 hpeyerl } else {
1380 1.7 hpeyerl if (FC) {
1381 1.7 hpeyerl sttyclearflags(&i.t, FC);
1382 1.7 hpeyerl i.set = 1;
1383 1.7 hpeyerl }
1384 1.7 hpeyerl if (FS) {
1385 1.7 hpeyerl sttysetflags(&i.t, FS);
1386 1.7 hpeyerl i.set = 1;
1387 1.7 hpeyerl }
1388 1.7 hpeyerl if (XC) {
1389 1.7 hpeyerl sttyclearlflags(&i.t, XC);
1390 1.7 hpeyerl i.set = 1;
1391 1.7 hpeyerl }
1392 1.7 hpeyerl if (XS) {
1393 1.9 jtc sttysetlflags(&i.t, XS);
1394 1.7 hpeyerl i.set = 1;
1395 1.7 hpeyerl }
1396 1.7 hpeyerl }
1397 1.7 hpeyerl
1398 1.7 hpeyerl if (i.set && tcsetattr(i.fd, TCSANOW, &i.t) < 0) {
1399 1.7 hpeyerl syslog(LOG_ERR, "%s: tcsetattr: %m", printer);
1400 1.7 hpeyerl exit(1);
1401 1.1 cgd }
1402 1.7 hpeyerl if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
1403 1.7 hpeyerl syslog(LOG_INFO, "%s: ioctl(TIOCSWINSZ): %m", printer);
1404 1.7 hpeyerl return;
1405 1.1 cgd }
1406 1.1 cgd
1407 1.5 cgd #if __STDC__
1408 1.5 cgd #include <stdarg.h>
1409 1.5 cgd #else
1410 1.5 cgd #include <varargs.h>
1411 1.5 cgd #endif
1412 1.5 cgd
1413 1.5 cgd void
1414 1.5 cgd #if __STDC__
1415 1.5 cgd pstatus(const char *msg, ...)
1416 1.5 cgd #else
1417 1.5 cgd pstatus(msg, va_alist)
1418 1.1 cgd char *msg;
1419 1.5 cgd va_dcl
1420 1.5 cgd #endif
1421 1.1 cgd {
1422 1.1 cgd register int fd;
1423 1.1 cgd char buf[BUFSIZ];
1424 1.5 cgd va_list ap;
1425 1.5 cgd #if __STDC__
1426 1.5 cgd va_start(ap, msg);
1427 1.5 cgd #else
1428 1.5 cgd va_start(ap);
1429 1.5 cgd #endif
1430 1.1 cgd
1431 1.1 cgd umask(0);
1432 1.1 cgd fd = open(ST, O_WRONLY|O_CREAT, 0664);
1433 1.1 cgd if (fd < 0 || flock(fd, LOCK_EX) < 0) {
1434 1.1 cgd syslog(LOG_ERR, "%s: %s: %m", printer, ST);
1435 1.1 cgd exit(1);
1436 1.1 cgd }
1437 1.1 cgd ftruncate(fd, 0);
1438 1.11 mrg (void)vsnprintf(buf, sizeof(buf) - 2, msg, ap);
1439 1.5 cgd va_end(ap);
1440 1.11 mrg strncat(buf, "\n", 2);
1441 1.11 mrg (void)write(fd, buf, strlen(buf));
1442 1.11 mrg (void)close(fd);
1443 1.1 cgd }
1444