aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/lpr/lpd/printjob.c
blob: ff16f8f298c1e7896f69a4d9445f05dbeaddf7b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1983, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#if 0
#ifndef lint
static char sccsid[] = "@(#)printjob.c	8.7 (Berkeley) 5/10/95";
#endif /* not lint */
#endif

#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
__FBSDID("$FreeBSD$");

/*
 * printjob -- print jobs in the queue.
 *
 *	NOTE: the lock file is used to pass information to lpq and lprm.
 *	it does not need to be removed because file locks are dynamic.
 */

#include <sys/param.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <pwd.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
#include "lp.h"
#include "lp.local.h"
#include "pathnames.h"
#include "extern.h"

#define DORETURN	0	/* dofork should return "can't fork" error */
#define DOABORT		1	/* dofork should just die if fork() fails */

/*
 * The buffer size to use when reading/writing spool files.
 */
#define	SPL_BUFSIZ	BUFSIZ

/*
 * Error tokens
 */
#define REPRINT		-2
#define ERROR		-1
#define	OK		0
#define	FATALERR	1
#define	NOACCT		2
#define	FILTERERR	3
#define	ACCESS		4

static dev_t	 fdev;		/* device of file pointed to by symlink */
static ino_t	 fino;		/* inode of file pointed to by symlink */
static FILE	*cfp;		/* control file */
static pid_t	 of_pid;	/* process id of output filter, if any */
static int	 child;		/* id of any filters */
static int	 job_dfcnt;	/* count of datafiles in current user job */
static int	 lfd;		/* lock file descriptor */
static int	 ofd;		/* output filter file descriptor */
static int	 tfd = -1;	/* output filter temp file output */
static int	 pfd;		/* prstatic inter file descriptor */
static int	 prchild;	/* id of pr process */
static char	 title[80];	/* ``pr'' title */
static char      locale[80];    /* ``pr'' locale */

/* these two are set from pp->daemon_user, but only if they are needed */
static char	*daemon_uname;	/* set from pwd->pw_name */
static int	 daemon_defgid;

static char	class[32];		/* classification field */
static char	origin_host[MAXHOSTNAMELEN];	/* user's host machine */
				/* indentation size in static characters */
static char	indent[10] = "-i0";
static char	jobname[100];		/* job or file name */
static char	length[10] = "-l";	/* page length in lines */
static char	logname[32];		/* user's login name */
static char	pxlength[10] = "-y";	/* page length in pixels */
static char	pxwidth[10] = "-x";	/* page width in pixels */
/* tempstderr is the filename used to catch stderr from exec-ing filters */
static char	tempstderr[] = "errs.XXXXXXX";
static char	width[10] = "-w";	/* page width in static characters */
#define TFILENAME "fltXXXXXX"
static char	tfile[] = TFILENAME;	/* file name for filter output */

static void	 abortpr(int _signo);
static void	 alarmhandler(int _signo);
static void	 banner(struct printer *_pp, char *_name1, char *_name2);
static int	 dofork(const struct printer *_pp, int _action);
static int	 dropit(int _c);
static int	 execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
		    int _infd, int _outfd);
static void	 init(struct printer *_pp);
static void	 openpr(const struct printer *_pp);
static void	 opennet(const struct printer *_pp);
static void	 opentty(const struct printer *_pp);
static void	 openrem(const struct printer *pp);
static int	 print(struct printer *_pp, int _format, char *_file);
static int	 printit(struct printer *_pp, char *_file);
static void	 pstatus(const struct printer *_pp, const char *_msg, ...)
		    __printflike(2, 3);
static char	 response(const struct printer *_pp);
static void	 scan_out(struct printer *_pp, int _scfd, char *_scsp, 
		    int _dlm);
static char	*scnline(int _key, char *_p, int _c);
static int	 sendfile(struct printer *_pp, int _type, char *_file, 
		    char _format, int _copyreq);
static int	 sendit(struct printer *_pp, char *_file);
static void	 sendmail(struct printer *_pp, char *_userid, int _bombed);
static void	 setty(const struct printer *_pp);
static void	 wait4data(struct printer *_pp, const char *_dfile);

void
printjob(struct printer *pp)
{
	struct stat stb;
	register struct jobqueue *q, **qp;
	struct jobqueue **queue;
	register int i, nitems;
	off_t pidoff;
	pid_t printpid;
	int errcnt, jobcount, statok, tempfd;

	jobcount = 0;
	init(pp); /* set up capabilities */
	(void) write(STDOUT_FILENO, "", 1);	/* ack that daemon is started */
	(void) close(STDERR_FILENO);			/* set up log file */
	if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
		syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
		    pp->log_file);
		(void) open(_PATH_DEVNULL, O_WRONLY);
	}
	if(setgid(getegid()) != 0) err(1, "setgid() failed");
	printpid = getpid();			/* for use with lprm */
	setpgid((pid_t)0, printpid);

	/*
	 * At initial lpd startup, printjob may be called with various
	 * signal handlers in effect.  After that initial startup, any
	 * calls to printjob will have a *different* set of signal-handlers
	 * in effect.  Make sure all handlers are the ones we want.
	 */
	signal(SIGCHLD, SIG_DFL);
	signal(SIGHUP, abortpr);
	signal(SIGINT, abortpr);
	signal(SIGQUIT, abortpr);
	signal(SIGTERM, abortpr);

	/*
	 * uses short form file names
	 */
	if (chdir(pp->spool_dir) < 0) {
		syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer,
		    pp->spool_dir);
		exit(1);
	}
	statok = stat(pp->lock_file, &stb);
	if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS))
		exit(0);		/* printing disabled */
	umask(S_IWOTH);
	lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 
		   LOCK_FILE_MODE);
	if (lfd < 0) {
		if (errno == EWOULDBLOCK)	/* active daemon present */
			exit(0);
		syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
		    pp->lock_file);
		exit(1);
	}
	/*
	 * If the initial call to stat() failed, then lock_file will have
	 * been created by open().  Update &stb to match that new file.
	 */
	if (statok != 0)
		statok = stat(pp->lock_file, &stb);
	/* turn off non-blocking mode (was turned on for lock effects only) */
	if (fcntl(lfd, F_SETFL, 0) < 0) {
		syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer,
		    pp->lock_file);
		exit(1);
	}
	ftruncate(lfd, 0);
	/*
	 * write process id for others to know
	 */
	sprintf(line, "%u\n", printpid);
	pidoff = i = strlen(line);
	if (write(lfd, line, i) != i) {
		syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
		    pp->lock_file);
		exit(1);
	}
	/*
	 * search the spool directory for work and sort by queue order.
	 */
	if ((nitems = getq(pp, &queue)) < 0) {
		syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 
		    pp->spool_dir);
		exit(1);
	}
	if (nitems == 0)		/* no work to do */
		exit(0);
	if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
		if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
			syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
			    pp->lock_file);
	}

	/* create a file which will be used to hold stderr from filters */
	if ((tempfd = mkstemp(tempstderr)) == -1) {
		syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
		    tempstderr);
		exit(1);
	}
	if ((i = fchmod(tempfd, 0664)) == -1) {
		syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
		    tempstderr);
		exit(1);
	}
	/* lpd doesn't need it to be open, it just needs it to exist */
	close(tempfd);

	openpr(pp);			/* open printer or remote */
again:
	/*
	 * we found something to do now do it --
	 *    write the name of the current control file into the lock file
	 *    so the spool queue program can tell what we're working on
	 */
	for (qp = queue; nitems--; free((char *) q)) {
		q = *qp++;
		if (stat(q->job_cfname, &stb) < 0)
			continue;
		errcnt = 0;
	restart:
		(void) lseek(lfd, pidoff, 0);
		(void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
		i = strlen(line);
		if (write(lfd, line, i) != i)
			syslog(LOG_ERR, "%s: write(%s): %m", pp->printer,
			    pp->lock_file);
		if (!pp->remote)
			i = printit(pp, q->job_cfname);
		else
			i = sendit(pp, q->job_cfname);
		/*
		 * Check to see if we are supposed to stop printing or
		 * if we are to rebuild the queue.
		 */
		if (fstat(lfd, &stb) == 0) {
			/* stop printing before starting next job? */
			if (stb.st_mode & LFM_PRINT_DIS)
				goto done;
			/* rebuild queue (after lpc topq) */
			if (stb.st_mode & LFM_RESET_QUE) {
				for (free(q); nitems--; free(q))
					q = *qp++;
				if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
				    < 0)
					syslog(LOG_WARNING,
					    "%s: fchmod(%s): %m",
					    pp->printer, pp->lock_file);
				break;
			}
		}
		if (i == OK)		/* all files of this job printed */
			jobcount++;
		else if (i == REPRINT && ++errcnt < 5) {
			/* try reprinting the job */
			syslog(LOG_INFO, "restarting %s", pp->printer);
			if (of_pid > 0) {
				kill(of_pid, SIGCONT); /* to be sure */
				(void) close(ofd);
				while ((i = wait(NULL)) > 0 && i != of_pid)
					;
				if (i < 0)
					syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
					    pp->printer, of_pid);
				of_pid = 0;
			}
			(void) close(pfd);	/* close printer */
			if (ftruncate(lfd, pidoff) < 0)
				syslog(LOG_WARNING, "%s: ftruncate(%s): %m", 
				    pp->printer, pp->lock_file);
			openpr(pp);		/* try to reopen printer */
			goto restart;
		} else {
			syslog(LOG_WARNING, "%s: job could not be %s (%s)", 
			    pp->printer,
			    pp->remote ? "sent to remote host" : "printed",
			    q->job_cfname);
			if (i == REPRINT) {
				/* ensure we don't attempt this job again */
				(void) unlink(q->job_cfname);
				q->job_cfname[0] = 'd';
				(void) unlink(q->job_cfname);
				if (logname[0])
					sendmail(pp, logname, FATALERR);
			}
		}
	}
	free(queue);
	/*
	 * search the spool directory for more work.
	 */
	if ((nitems = getq(pp, &queue)) < 0) {
		syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
		    pp->spool_dir);
		exit(1);
	}
	if (nitems == 0) {		/* no more work to do */
	done:
		if (jobcount > 0) {	/* jobs actually printed */
			if (!pp->no_formfeed && !pp->tof)
				(void) write(ofd, pp->form_feed,
					     strlen(pp->form_feed));
			if (pp->trailer != NULL) /* output trailer */
				(void) write(ofd, pp->trailer,
					     strlen(pp->trailer));
		}
		(void) close(ofd);
		(void) wait(NULL);
		(void) unlink(tempstderr);
		exit(0);
	}
	goto again;
}

char	fonts[4][50];	/* fonts for troff */

char ifonts[4][40] = {
	_PATH_VFONTR,
	_PATH_VFONTI,
	_PATH_VFONTB,
	_PATH_VFONTS,
};

/*
 * The remaining part is the reading of the control file (cf)
 * and performing the various actions.
 */
static int
printit(struct printer *pp, char *file)
{
	register int i;
	char *cp;
	int bombed, didignorehdr;

	bombed = OK;
	didignorehdr = 0;
	/*
	 * open control file; ignore if no longer there.
	 */
	if ((cfp = fopen(file, "r")) == NULL) {
		syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file);
		return (OK);
	}
	/*
	 * Reset troff fonts.
	 */
	for (i = 0; i < 4; i++)
		strcpy(fonts[i], ifonts[i]);
	sprintf(&width[2], "%ld", pp->page_width);
	strcpy(indent+2, "0");

	/* initialize job-specific count of datafiles processed */
	job_dfcnt = 0;
	
	/*
	 *      read the control file for work to do
	 *
	 *      file format -- first character in the line is a command
	 *      rest of the line is the argument.
	 *      valid commands are:
	 *
	 *		S -- "stat info" for symbolic link protection
	 *		J -- "job name" on banner page
	 *		C -- "class name" on banner page
	 *              L -- "literal" user's name to print on banner
	 *		T -- "title" for pr
	 *		H -- "host name" of machine where lpr was done
	 *              P -- "person" user's login name
	 *              I -- "indent" amount to indent output
	 *		R -- laser dpi "resolution"
	 *              f -- "file name" name of text file to print
	 *		l -- "file name" text file with control chars
	 *		o -- "file name" postscript file, according to
	 *		     the RFC.  Here it is treated like an 'f'.
	 *		p -- "file name" text file to print with pr(1)
	 *		t -- "file name" troff(1) file to print
	 *		n -- "file name" ditroff(1) file to print
	 *		d -- "file name" dvi file to print
	 *		g -- "file name" plot(1G) file to print
	 *		v -- "file name" plain raster file to print
	 *		c -- "file name" cifplot file to print
	 *		1 -- "R font file" for troff
	 *		2 -- "I font file" for troff
	 *		3 -- "B font file" for troff
	 *		4 -- "S font file" for troff
	 *		N -- "name" of file (used by lpq)
	 *              U -- "unlink" name of file to remove
	 *                    (after we print it. (Pass 2 only)).
	 *		M -- "mail" to user when done printing
	 *              Z -- "locale" for pr
	 *
	 *      get_line reads a line and expands tabs to blanks
	 */

	/* pass 1 */

	while (get_line(cfp))
		switch (line[0]) {
		case 'H':
			strlcpy(origin_host, line + 1, sizeof(origin_host));
			if (class[0] == '\0') {
				strlcpy(class, line+1, sizeof(class));
			}
			continue;

		case 'P':
			strlcpy(logname, line + 1, sizeof(logname));
			if (pp->restricted) { /* restricted */
				if (getpwnam(logname) == NULL) {
					bombed = NOACCT;
					sendmail(pp, line+1, bombed);
					goto pass2;
				}
			}
			continue;

		case 'S':
			cp = line+1;
			i = 0;
			while (*cp >= '0' && *cp <= '9')
				i = i * 10 + (*cp++ - '0');
			fdev = i;
			cp++;
			i = 0;
			while (*cp >= '0' && *cp <= '9')
				i = i * 10 + (*cp++ - '0');
			fino = i;
			continue;

		case 'J':
			if (line[1] != '\0') {
				strlcpy(jobname, line + 1, sizeof(jobname));
			} else
				strcpy(jobname, " ");
			continue;

		case 'C':
			if (line[1] != '\0')
				strlcpy(class, line + 1, sizeof(class));
			else if (class[0] == '\0') {
				/* XXX - why call gethostname instead of
				 *       just strlcpy'ing local_host? */
				gethostname(class, sizeof(class));
				class[sizeof(class) - 1] = '\0';
			}
			continue;

		case 'T':	/* header title for pr */
			strlcpy(title, line + 1, sizeof(title));
			continue;

		case 'L':	/* identification line */
			if (!pp->no_header && !pp->header_last)
				banner(pp, line+1, jobname);
			continue;

		case '1':	/* troff fonts */
		case '2':
		case '3':
		case '4':
			if (line[1] != '\0') {
				strlcpy(fonts[line[0]-'1'], line + 1,
				    (size_t)50);
			}
			continue;

		case 'W':	/* page width */
			strlcpy(width+2, line + 1, sizeof(width) - 2);
			continue;

		case 'I':	/* indent amount */
			strlcpy(indent+2, line + 1, sizeof(indent) - 2);
			continue;

		case 'Z':       /* locale for pr */
			strlcpy(locale, line + 1, sizeof(locale));
			continue;

		default:	/* some file to print */
			/* only lowercase cmd-codes include a file-to-print */
			if ((line[0] < 'a') || (line[0] > 'z')) {
				/* ignore any other lines */
				if (lflag <= 1)
					continue;
				if (!didignorehdr) {
					syslog(LOG_INFO, "%s: in %s :",
					    pp->printer, file);
					didignorehdr = 1;
				}
				syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
				    pp->printer, line[0], &line[1]);
				continue;
			}
			i = print(pp, line[0], line+1);
			switch (i) {
			case ERROR:
				if (bombed == OK)
					bombed = FATALERR;
				break;
			case REPRINT:
				(void) fclose(cfp);
				return (REPRINT);
			case FILTERERR:
			case ACCESS:
				bombed = i;
				sendmail(pp, logname, bombed);
			}
			title[0] = '\0';
			continue;

		case 'N':
		case 'U':
		case 'M':
		case 'R':
			continue;
		}

	/* pass 2 */

pass2:
	fseek(cfp, 0L, 0);
	while (get_line(cfp))
		switch (line[0]) {
		case 'L':	/* identification line */
			if (!pp->no_header && pp->header_last)
				banner(pp, line+1, jobname);
			continue;

		case 'M':
			if (bombed < NOACCT)	/* already sent if >= NOACCT */
				sendmail(pp, line+1, bombed);
			continue;

		case 'U':
			if (strchr(line+1, '/'))
				continue;
			(void) unlink(line+1);
		}
	/*
	 * clean-up in case another control file exists
	 */
	(void) fclose(cfp);
	(void) unlink(file);
	return (bombed == OK ? OK : ERROR);
}

/*
 * Print a file.
 * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
 * Return -1 if a non-recoverable error occurred,
 * 2 if the filter detected some errors (but printed the job anyway),
 * 1 if we should try to reprint this job and
 * 0 if all is well.
 * Note: all filters take stdin as the file, stdout as the printer,
 * stderr as the log file, and must not ignore SIGINT.
 */
static int
print(struct printer *pp, int format, char *file)
{
	register int n, i;
	register char *prog;
	int fi, fo;
	FILE *fp;
	char *av[15], buf[SPL_BUFSIZ];
	pid_t wpid;
	int p[2], retcode, stopped, wstatus, wstatus_set;
	struct stat stb;

	/* Make sure the entire data file has arrived. */
	wait4data(pp, file);

	if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
		syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
		    pp->printer, file, format);
		return (ERROR);
	}
	/*
	 * Check to see if data file is a symbolic link. If so, it should
	 * still point to the same file or someone is trying to print
	 * something he shouldn't.
	 */
	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
	    (stb.st_dev != fdev || stb.st_ino != fino))
		return (ACCESS);

	job_dfcnt++;		/* increment datafile counter for this job */
	stopped = 0;		/* output filter is not stopped */

	/* everything seems OK, start it up */
	if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
		pp->tof = 1;
	}
	if (pp->filters[LPF_INPUT] == NULL
	    && (format == 'f' || format == 'l' || format == 'o')) {
		pp->tof = 0;
		while ((n = read(fi, buf, SPL_BUFSIZ)) > 0)
			if (write(ofd, buf, n) != n) {
				(void) close(fi);
				return (REPRINT);
			}
		(void) close(fi);
		return (OK);
	}
	switch (format) {
	case 'p':	/* print file using 'pr' */
		if (pp->filters[LPF_INPUT] == NULL) {	/* use output filter */
			prog = _PATH_PR;
			i = 0;
			av[i++] = "pr";
			av[i++] = width;
			av[i++] = length;
			av[i++] = "-h";
			av[i++] = *title ? title : " ";
			av[i++] = "-L";
			av[i++] = *locale ? locale : "C";
			av[i++] = "-F";
			av[i] = NULL;
			fo = ofd;
			goto start;
		}
		pipe(p);
		if ((prchild = dofork(pp, DORETURN)) == 0) {	/* child */
			dup2(fi, STDIN_FILENO);		/* file is stdin */
			dup2(p[1], STDOUT_FILENO);	/* pipe is stdout */
			closelog();
			closeallfds(3);
			execl(_PATH_PR, "pr", width, length,
			    "-h", *title ? title : " ",
			    "-L", *locale ? locale : "C",
			    "-F", (char *)0);
			syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
			exit(2);
		}
		(void) close(p[1]);		/* close output side */
		(void) close(fi);
		if (prchild < 0) {
			prchild = 0;
			(void) close(p[0]);
			return (ERROR);
		}
		fi = p[0];			/* use pipe for input */
	case 'f':	/* print plain text file */
		prog = pp->filters[LPF_INPUT];
		av[1] = width;
		av[2] = length;
		av[3] = indent;
		n = 4;
		break;
	case 'o':	/* print postscript file */
		/*
		 * Treat this as a "plain file with control characters", and
		 * assume the standard LPF_INPUT filter will recognize that
		 * the data is postscript and know what to do with it.  These
		 * 'o'-file requests could come from MacOS 10.1 systems.
		 * (later versions of MacOS 10 will explicitly use 'l')
		 * A postscript file can contain binary data, which is why 'l'
		 * is somewhat more appropriate than 'f'.
		 */
		/* FALLTHROUGH */
	case 'l':	/* like 'f' but pass control characters */
		prog = pp->filters[LPF_INPUT];
		av[1] = "-c";
		av[2] = width;
		av[3] = length;
		av[4] = indent;
		n = 5;
		break;
	case 'r':	/* print a fortran text file */
		prog = pp->filters[LPF_FORTRAN];
		av[1] = width;
		av[2] = length;
		n = 3;
		break;
	case 't':	/* print troff output */
	case 'n':	/* print ditroff output */
	case 'd':	/* print tex output */
		(void) unlink(".railmag");
		if ((fo = creat(".railmag", FILMOD)) < 0) {
			syslog(LOG_ERR, "%s: cannot create .railmag", 
			    pp->printer);
			(void) unlink(".railmag");
		} else {
			for (n = 0; n < 4; n++) {
				if (fonts[n][0] != '/')
					(void) write(fo, _PATH_VFONT,
					    sizeof(_PATH_VFONT) - 1);
				(void) write(fo, fonts[n], strlen(fonts[n]));
				(void) write(fo, "\n", 1);
			}
			(void) close(fo);
		}
		prog = (format == 't') ? pp->filters[LPF_TROFF] 
			: ((format == 'n') ? pp->filters[LPF_DITROFF]
			   : pp->filters[LPF_DVI]);
		av[1] = pxwidth;
		av[2] = pxlength;
		n = 3;
		break;
	case 'c':	/* print cifplot output */
		prog = pp->filters[LPF_CIFPLOT];
		av[1] = pxwidth;
		av[2] = pxlength;
		n = 3;
		break;
	case 'g':	/* print plot(1G) output */
		prog = pp->filters[LPF_GRAPH];
		av[1] = pxwidth;
		av[2] = pxlength;
		n = 3;
		break;
	case 'v':	/* print raster output */
		prog = pp->filters[LPF_RASTER];
		av[1] = pxwidth;
		av[2] = pxlength;
		n = 3;
		break;
	default:
		(void) close(fi);
		syslog(LOG_ERR, "%s: illegal format character '%c'",
		    pp->printer, format);
		return (ERROR);
	}
	if (prog == NULL) {
		(void) close(fi);
		syslog(LOG_ERR,
		   "%s: no filter found in printcap for format character '%c'",
		   pp->printer, format);
		return (ERROR);
	}
	if ((av[0] = strrchr(prog, '/')) != NULL)
		av[0]++;
	else
		av[0] = prog;
	av[n++] = "-n";
	av[n++] = logname;
	av[n++] = "-h";
	av[n++] = origin_host;
	av[n++] = pp->acct_file;
	av[n] = NULL;
	fo = pfd;
	if (of_pid > 0) {		/* stop output filter */
		write(ofd, "\031\1", 2);
		while ((wpid =
		    wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid)
			;
		if (wpid < 0)
			syslog(LOG_WARNING,
			    "%s: after stopping 'of', wait3() returned: %m",
			    pp->printer);
		else if (!WIFSTOPPED(wstatus)) {
			(void) close(fi);
			syslog(LOG_WARNING, "%s: output filter died "
			    "(pid=%d retcode=%d termsig=%d)",
			    pp->printer, of_pid, WEXITSTATUS(wstatus),
			    WTERMSIG(wstatus));
			return (REPRINT);
		}
		stopped++;
	}
start:
	if ((child = dofork(pp, DORETURN)) == 0) { /* child */
		dup2(fi, STDIN_FILENO);
		dup2(fo, STDOUT_FILENO);
		/* setup stderr for the filter (child process)
		 * so it goes to our temporary errors file */
		n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
		if (n >= 0)
			dup2(n, STDERR_FILENO);
		closelog();
		closeallfds(3);
		execv(prog, av);
		syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
		    prog);
		exit(2);
	}
	(void) close(fi);
	wstatus_set = 0;
	if (child < 0)
		retcode = 100;
	else {
		while ((wpid = wait(&wstatus)) > 0 && wpid != child)
			;
		if (wpid < 0) {
			retcode = 100;
			syslog(LOG_WARNING,
			    "%s: after execv(%s), wait() returned: %m",
			    pp->printer, prog);
		} else {
			wstatus_set = 1;
			retcode = WEXITSTATUS(wstatus);
		}
	}
	child = 0;
	prchild = 0;
	if (stopped) {		/* restart output filter */
		if (kill(of_pid, SIGCONT) < 0) {
			syslog(LOG_ERR, "cannot restart output filter");
			exit(1);
		}
	}
	pp->tof = 0;

	/* Copy the filter's output to "lf" logfile */
	if ((fp = fopen(tempstderr, "r"))) {
		while (fgets(buf, sizeof(buf), fp))
			fputs(buf, stderr);
		fclose(fp);
	}

	if (wstatus_set && !WIFEXITED(wstatus)) {
		syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
		    pp->printer, format, WTERMSIG(wstatus));
		return (ERROR);
	}
	switch (retcode) {
	case 0:
		pp->tof = 1;
		return (OK);
	case 1:
		return (REPRINT);
	case 2:
		return (ERROR);
	default:
		syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
		    pp->printer, format, retcode);
		return (FILTERERR);
	}
}

/*
 * Send the daemon control file (cf) and any data files.
 * Return -1 if a non-recoverable error occurred, 1 if a recoverable error and
 * 0 if all is well.
 */
static int
sendit(struct printer *pp, char *file)
{
	int dfcopies, err, i;
	char *cp, last[sizeof(line)];

	/*
	 * open control file
	 */
	if ((cfp = fopen(file, "r")) == NULL)
		return (OK);

	/* initialize job-specific count of datafiles processed */
	job_dfcnt = 0;

	/*
	 *      read the control file for work to do
	 *
	 *      file format -- first character in the line is a command
	 *      rest of the line is the argument.
	 *      commands of interest are:
	 *
	 *            a-z -- "file name" name of file to print
	 *              U -- "unlink" name of file to remove
	 *                    (after we print it. (Pass 2 only)).
	 */

	/*
	 * pass 1
	 */
	err = OK;
	while (get_line(cfp)) {
	again:
		if (line[0] == 'S') {
			cp = line+1;
			i = 0;
			while (*cp >= '0' && *cp <= '9')
				i = i * 10 + (*cp++ - '0');
			fdev = i;
			cp++;
			i = 0;
			while (*cp >= '0' && *cp <= '9')
				i = i * 10 + (*cp++ - '0');
			fino = i;
		} else if (line[0] == 'H') {
			strlcpy(origin_host, line + 1, sizeof(origin_host));
			if (class[0] == '\0') {
				strlcpy(class, line + 1, sizeof(class));
			}
		} else if (line[0] == 'P') {
			strlcpy(logname, line + 1, sizeof(logname));
			if (pp->restricted) { /* restricted */
				if (getpwnam(logname) == NULL) {
					sendmail(pp, line+1, NOACCT);
					err = ERROR;
					break;
				}
			}
		} else if (line[0] == 'I') {
			strlcpy(indent+2, line + 1, sizeof(indent) - 2);
		} else if (line[0] >= 'a' && line[0] <= 'z') {
			dfcopies = 1;
			strcpy(last, line);
			while ((i = get_line(cfp)) != 0) {
				if (strcmp(last, line) != 0)
					break;
				dfcopies++;
			}
			switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
			case OK:
				if (i)
					goto again;
				break;
			case REPRINT:
				(void) fclose(cfp);
				return (REPRINT);
			case ACCESS:
				sendmail(pp, logname, ACCESS);
			case ERROR:
				err = ERROR;
			}
			break;
		}
	}
	if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
		(void) fclose(cfp);
		return (REPRINT);
	}
	/*
	 * pass 2
	 */
	fseek(cfp, 0L, 0);
	while (get_line(cfp))
		if (line[0] == 'U' && !strchr(line+1, '/'))
			(void) unlink(line+1);
	/*
	 * clean-up in case another control file exists
	 */
	(void) fclose(cfp);
	(void) unlink(file);
	return (err);
}

/*
 * Send a data file to the remote machine and spool it.
 * Return positive if we should try resending.
 */
static int
sendfile(struct printer *pp, int type, char *file, char format, int copyreq)
{
	int i, amt;
	struct stat stb;
	char *av[15], *filtcmd;
	char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
	int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;

	/* Make sure the entire data file has arrived. */
	wait4data(pp, file);

	statrc = lstat(file, &stb);
	if (statrc < 0) {
		syslog(LOG_ERR, "%s: error from lstat(%s): %m",
		    pp->printer, file);
		return (ERROR);
	}
	sfd = open(file, O_RDONLY);
	if (sfd < 0) {
		syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
		    pp->printer, file);
		return (ERROR);
	}
	/*
	 * Check to see if data file is a symbolic link. If so, it should
	 * still point to the same file or someone is trying to print something
	 * he shouldn't.
	 */
	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
	    (stb.st_dev != fdev || stb.st_ino != fino)) {
		close(sfd);
		return (ACCESS);
	}

	/* Everything seems OK for reading the file, now to send it */
	filtcmd = NULL;
	sizerr = 0;
	tfd = -1;
	if (type == '\3') {
		/*
		 * Type == 3 means this is a datafile, not a control file.
		 * Increment the counter of data-files in this job, and
		 * then check for input or output filters (which are only
		 * applied to datafiles, not control files).
		 */
		job_dfcnt++;

		/*
		 * Note that here we are filtering datafiles, one at a time,
		 * as they are sent to the remote machine.  Here, the *only*
		 * difference between an input filter (`if=') and an output
		 * filter (`of=') is the argument list that the filter is
		 * started up with.  Here, the output filter is executed
		 * for each individual file as it is sent.  This is not the
		 * same as local print queues, where the output filter is
		 * started up once, and then all jobs are passed thru that
		 * single invocation of the output filter.
		 *
		 * Also note that a queue for a remote-machine can have an
		 * input filter or an output filter, but not both.
		 */
		if (pp->filters[LPF_INPUT]) {
			filtcmd = pp->filters[LPF_INPUT];
			av[0] = filtcmd;
			narg = 0;
			strcpy(opt_c, "-c");
			strcpy(opt_h, "-h");
			strcpy(opt_n, "-n");
			if (format == 'l')
				av[++narg] = opt_c;
			av[++narg] = width;
			av[++narg] = length;
			av[++narg] = indent;
			av[++narg] = opt_n;
			av[++narg] = logname;
			av[++narg] = opt_h;
			av[++narg] = origin_host;
			av[++narg] = pp->acct_file;
			av[++narg] = NULL;
		} else if (pp->filters[LPF_OUTPUT]) {
			filtcmd = pp->filters[LPF_OUTPUT];
			av[0] = filtcmd;
			narg = 0;
			av[++narg] = width;
			av[++narg] = length;
			av[++narg] = NULL;
		}
	}
	if (filtcmd) {
		/*
		 * If there is an input or output filter, we have to run
		 * the datafile thru that filter and store the result as
		 * a temporary spool file, because the protocol requires
		 * that we send the remote host the file-size before we
		 * start to send any of the data.
		 */
		strcpy(tfile, TFILENAME);
		tfd = mkstemp(tfile);
		if (tfd == -1) {
			syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
			    TFILENAME);
			sfres = ERROR;
			goto return_sfres;
		}
		filtstat = execfilter(pp, filtcmd, av, sfd, tfd);

		/* process the return-code from the filter */
		switch (filtstat) {
		case 0:
			break;
		case 1:
			sfres = REPRINT;
			goto return_sfres;
		case 2:
			sfres = ERROR;
			goto return_sfres;
		default:
			syslog(LOG_WARNING,
			    "%s: filter '%c' exited (retcode=%d)",
			    pp->printer, format, filtstat);
			sfres = FILTERERR;
			goto return_sfres;
		}
		statrc = fstat(tfd, &stb);   /* to find size of tfile */
		if (statrc < 0)	{
			syslog(LOG_ERR,
			    "%s: error processing 'if', fstat(%s): %m",
			    pp->printer, tfile);
			sfres = ERROR;
			goto return_sfres;
		}
		close(sfd);
		sfd = tfd;
		lseek(sfd, 0, SEEK_SET);
	}

	copycnt = 0;
sendagain:
	copycnt++;

	if (copycnt < 2)
		(void) sprintf(buf, "%c%" PRId64 " %s\n", type, stb.st_size,
		    file);
	else
		(void) sprintf(buf, "%c%" PRId64 " %s_c%d\n", type, stb.st_size,
		    file, copycnt);
	amt = strlen(buf);
	for (i = 0;  ; i++) {
		if (write(pfd, buf, amt) != amt ||
		    (resp = response(pp)) < 0 || resp == '\1') {
			sfres = REPRINT;
			goto return_sfres;
		} else if (resp == '\0')
			break;
		if (i == 0)
			pstatus(pp,
				"no space on remote; waiting for queue to drain");
		if (i == 10)
			syslog(LOG_ALERT, "%s: can't send to %s; queue full",
			    pp->printer, pp->remote_host);
		sleep(5 * 60);
	}
	if (i)
		pstatus(pp, "sending to %s", pp->remote_host);
	/*
	 * XXX - we should change trstat_init()/trstat_write() to include
	 *	 the copycnt in the statistics record it may write.
	 */
	if (type == '\3')
		trstat_init(pp, file, job_dfcnt);
	for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) {
		amt = SPL_BUFSIZ;
		if (i + amt > stb.st_size)
			amt = stb.st_size - i;
		if (sizerr == 0 && read(sfd, buf, amt) != amt)
			sizerr = 1;
		if (write(pfd, buf, amt) != amt) {
			sfres = REPRINT;
			goto return_sfres;
		}
	}

	if (sizerr) {
		syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
		/* tell recvjob to ignore this file */
		(void) write(pfd, "\1", 1);
		sfres = ERROR;
		goto return_sfres;
	}
	if (write(pfd, "", 1) != 1 || response(pp)) {
		sfres = REPRINT;
		goto return_sfres;
	}
	if (type == '\3') {
		trstat_write(pp, TR_SENDING, stb.st_size, logname,
		    pp->remote_host, origin_host);
		/*
		 * Usually we only need to send one copy of a datafile,
		 * because the control-file will simply print the same
		 * file multiple times.  However, some printers ignore
		 * the control file, and simply print each data file as
		 * it arrives.  For such "remote hosts", we need to
		 * transfer the same data file multiple times.  Such a
		 * a host is indicated by adding 'rc' to the printcap
		 * entry.
		 * XXX - Right now this ONLY works for remote hosts which
		 *	do ignore the name of the data file, because
		 *	this sends the file multiple times with slight
		 *	changes to the filename.  To do this right would
		 *	require that we also rewrite the control file
		 *	to match those filenames.
		 */
		if (pp->resend_copies && (copycnt < copyreq)) {
			lseek(sfd, 0, SEEK_SET);
			goto sendagain;
		}
	}
	sfres = OK;

return_sfres:
	(void)close(sfd);
	if (tfd != -1) {
		/*
		 * If tfd is set, then it is the same value as sfd, and
		 * therefore it is already closed at this point.  All
		 * we need to do is remove the temporary file.
		 */
		tfd = -1;
		unlink(tfile);
	}
	return (sfres);
}

/*
 * Some print servers send the control-file first, and then start sending the
 * matching data file(s).  That is not the correct order.  If some queue is
 * already printing an active job, then when that job is finished the queue
 * may proceed to the control file of any incoming print job.  This turns
 * into a race between the process which is receiving the data file, and the
 * process which is actively printing the very same file.  When the remote
 * server sends files in the wrong order, it is even possible that a queue
 * will start to print a data file before the file has been created!
 *
 * So before we start to print() or send() a data file, we call this routine
 * to make sure the data file is not still changing in size.  Note that this
 * problem will only happen for jobs arriving from a remote host, and that
 * the process which has decided to print this job (and is thus making this
 * check) is *not* the process which is receiving the job.
 *
 * A second benefit of this is that any incoming job is guaranteed to appear
 * in a queue listing for at least a few seconds after it has arrived.  Some
 * lpr implementations get confused if they send a job and it disappears
 * from the queue before they can check on it.
 */
#define	MAXWAIT_ARRIVE	16	    /* max to wait for the file to *exist* */
#define	MAXWAIT_4DATA	(20*60)	    /* max to wait for it to stop changing */
#define	MINWAIT_4DATA	4	    /* This value must be >= 1 */
#define	DEBUG_MINWAIT	1
static void
wait4data(struct printer *pp, const char *dfile)
{
	const char *cp;
	int statres;
	u_int sleepreq;
	size_t dlen, hlen;
	time_t amtslept, cur_time, prev_mtime;
	struct stat statdf;

	/* Skip these checks if the print job is from the local host. */
	dlen = strlen(dfile);
	hlen = strlen(local_host);
	if (dlen > hlen) {
		cp = dfile + dlen - hlen;
		if (strcmp(cp, local_host) == 0)
			return;
	}

	/*
	 * If this data file does not exist, then wait up to MAXWAIT_ARRIVE
	 * seconds for it to arrive.
	 */
	amtslept = 0;
	statres = stat(dfile, &statdf);
	while (statres < 0 && amtslept < MAXWAIT_ARRIVE) {
		if (amtslept == 0)
			pstatus(pp, "Waiting for data file from remote host");
		amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA);
		statres = stat(dfile, &statdf);
	}
	if (statres < 0) {
		/* The file still does not exist, so just give up on it. */
		syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s",
		    pp->printer, dfile);
		return;
	}

	/*
	 * The file exists, so keep waiting until the data file has not
	 * changed for some reasonable amount of time.  Extra care is
	 * taken when computing wait-times, just in case there are data
	 * files with a last-modify time in the future.  While that is
	 * very unlikely to happen, it can happen when the system has
	 * a flakey time-of-day clock.
	 */
	prev_mtime = statdf.st_mtime;
	cur_time = time(NULL);
	if (statdf.st_mtime >= cur_time - MINWAIT_4DATA) {
		if (statdf.st_mtime >= cur_time)	/* some TOD oddity */
			sleepreq = MINWAIT_4DATA;
		else
			sleepreq = cur_time - statdf.st_mtime;
		if (amtslept == 0)
			pstatus(pp, "Waiting for data file from remote host");
		amtslept += sleepreq - sleep(sleepreq);
		statres = stat(dfile, &statdf);
	}
	sleepreq = MINWAIT_4DATA;
	while (statres == 0 && amtslept < MAXWAIT_4DATA) {
		if (statdf.st_mtime == prev_mtime)
			break;
		prev_mtime = statdf.st_mtime;
		amtslept += sleepreq - sleep(sleepreq);
		statres = stat(dfile, &statdf);
	}

	if (statres != 0)
		syslog(LOG_WARNING, "%s: %s disappeared during wait4data()",
		    pp->printer, dfile);
	else if (amtslept > MAXWAIT_4DATA)
		syslog(LOG_WARNING,
		    "%s: %s still changing after %lu secs in wait4data()",
		    pp->printer, dfile, (unsigned long)amtslept);
#if DEBUG_MINWAIT
	else if (amtslept > MINWAIT_4DATA)
		syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)",
		    pp->printer, (unsigned long)amtslept, dfile);
#endif
}
#undef	MAXWAIT_ARRIVE
#undef	MAXWAIT_4DATA
#undef	MINWAIT_4DATA

/*
 *  This routine is called to execute one of the filters as was
 *  specified in a printcap entry.  While the child-process will read
 *  all of 'infd', it is up to the caller to close that file descriptor
 *  in the parent process.
 */
static int
execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
{
	pid_t fpid, wpid;
	int errfd, retcode, wstatus;
	FILE *errfp;
	char buf[BUFSIZ], *slash;

	fpid = dofork(pp, DORETURN);
	if (fpid != 0) {
		/*
		 * This is the parent process, which just waits for the child
		 * to complete and then returns the result.  Note that it is
		 * the child process which reads the input stream.
		 */
		if (fpid < 0)
			retcode = 100;
		else {
			while ((wpid = wait(&wstatus)) > 0 &&
			    wpid != fpid)
				;
			if (wpid < 0) {
				retcode = 100;
				syslog(LOG_WARNING,
				    "%s: after execv(%s), wait() returned: %m",
				    pp->printer, f_cmd);
			} else
				retcode = WEXITSTATUS(wstatus);
		}

		/*
		 * Copy everything the filter wrote to stderr from our
		 * temporary errors file to the "lf=" logfile.
		 */
		errfp = fopen(tempstderr, "r");
		if (errfp) {
			while (fgets(buf, sizeof(buf), errfp))
				fputs(buf, stderr);
			fclose(errfp);
		}

		return (retcode);
	}

	/*
	 * This is the child process, which is the one that executes the
	 * given filter.
	 */
	/*
	 * If the first parameter has any slashes in it, then change it
	 * to point to the first character after the last slash.
	 */
	slash = strrchr(f_av[0], '/');
	if (slash != NULL)
		f_av[0] = slash + 1;
	/*
	 * XXX - in the future, this should setup an explicit list of
	 *       environment variables and use execve()!
	 */

	/*
	 * Setup stdin, stdout, and stderr as we want them when the filter
	 * is running.  Stderr is setup so it points to a temporary errors
	 * file, and the parent process will copy that temporary file to
	 * the real logfile after the filter completes.
	 */
	dup2(infd, STDIN_FILENO);
	dup2(outfd, STDOUT_FILENO);
	errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
	if (errfd >= 0)
		dup2(errfd, STDERR_FILENO);
	closelog();
	closeallfds(3);
	execv(f_cmd, f_av);
	syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
	exit(2);
	/* NOTREACHED */
}

/*
 * Check to make sure there have been no errors and that both programs
 * are in sync with eachother.
 * Return non-zero if the connection was lost.
 */
static char
response(const struct printer *pp)
{
	char resp;

	if (read(pfd, &resp, 1) != 1) {
		syslog(LOG_INFO, "%s: lost connection", pp->printer);
		return (-1);
	}
	return (resp);
}

/*
 * Banner printing stuff
 */
static void
banner(struct printer *pp, char *name1, char *name2)
{
	time_t tvec;

	time(&tvec);
	if (!pp->no_formfeed && !pp->tof)
		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
	if (pp->short_banner) {	/* short banner only */
		if (class[0]) {
			(void) write(ofd, class, strlen(class));
			(void) write(ofd, ":", 1);
		}
		(void) write(ofd, name1, strlen(name1));
		(void) write(ofd, "  Job: ", 7);
		(void) write(ofd, name2, strlen(name2));
		(void) write(ofd, "  Date: ", 8);
		(void) write(ofd, ctime(&tvec), 24);
		(void) write(ofd, "\n", 1);
	} else {	/* normal banner */
		(void) write(ofd, "\n\n\n", 3);
		scan_out(pp, ofd, name1, '\0');
		(void) write(ofd, "\n\n", 2);
		scan_out(pp, ofd, name2, '\0');
		if (class[0]) {
			(void) write(ofd,"\n\n\n",3);
			scan_out(pp, ofd, class, '\0');
		}
		(void) write(ofd, "\n\n\n\n\t\t\t\t\tJob:  ", 15);
		(void) write(ofd, name2, strlen(name2));
		(void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
		(void) write(ofd, ctime(&tvec), 24);
		(void) write(ofd, "\n", 1);
	}
	if (!pp->no_formfeed)
		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
	pp->tof = 1;
}

static char *
scnline(int key, char *p, int c)
{
	register int scnwidth;

	for (scnwidth = WIDTH; --scnwidth;) {
		key <<= 1;
		*p++ = key & 0200 ? c : BACKGND;
	}
	return (p);
}

#define TRC(q)	(((q)-' ')&0177)

static void
scan_out(struct printer *pp, int scfd, char *scsp, int dlm)
{
	register char *strp;
	register int nchrs, j;
	char outbuf[LINELEN+1], *sp, c, cc;
	int d, scnhgt;

	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
		strp = &outbuf[0];
		sp = scsp;
		for (nchrs = 0; ; ) {
			d = dropit(c = TRC(cc = *sp++));
			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
				for (j = WIDTH; --j;)
					*strp++ = BACKGND;
			else
				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
			if (*sp == dlm || *sp == '\0' || 
			    nchrs++ >= pp->page_width/(WIDTH+1)-1)
				break;
			*strp++ = BACKGND;
			*strp++ = BACKGND;
		}
		while (*--strp == BACKGND && strp >= outbuf)
			;
		strp++;
		*strp++ = '\n';
		(void) write(scfd, outbuf, strp-outbuf);
	}
}

static int
dropit(int c)
{
	switch(c) {

	case TRC('_'):
	case TRC(';'):
	case TRC(','):
	case TRC('g'):
	case TRC('j'):
	case TRC('p'):
	case TRC('q'):
	case TRC('y'):
		return (DROP);

	default:
		return (0);
	}
}

/*
 * sendmail ---
 *   tell people about job completion
 */
static void
sendmail(struct printer *pp, char *userid, int bombed)
{
	register int i;
	int p[2], s;
	register const char *cp;
	struct stat stb;
	FILE *fp;

	pipe(p);
	if ((s = dofork(pp, DORETURN)) == 0) {		/* child */
		dup2(p[0], STDIN_FILENO);
		closelog();
		closeallfds(3);
		if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
			cp++;
		else
			cp = _PATH_SENDMAIL;
		execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
		_exit(0);
	} else if (s > 0) {				/* parent */
		dup2(p[1], STDOUT_FILENO);
		printf("To: %s@%s\n", userid, origin_host);
		printf("Subject: %s printer job \"%s\"\n", pp->printer,
			*jobname ? jobname : "<unknown>");
		printf("Reply-To: root@%s\n\n", local_host);
		printf("Your printer job ");
		if (*jobname)
			printf("(%s) ", jobname);

		switch (bombed) {
		case OK:
			cp = "OK";
			printf("\ncompleted successfully\n");
			break;
		default:
		case FATALERR:
			cp = "FATALERR";
			printf("\ncould not be printed\n");
			break;
		case NOACCT:
			cp = "NOACCT";
			printf("\ncould not be printed without an account on %s\n",
			    local_host);
			break;
		case FILTERERR:
			cp = "FILTERERR";
			if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
			    || (fp = fopen(tempstderr, "r")) == NULL) {
				printf("\nhad some errors and may not have printed\n");
				break;
			}
			printf("\nhad the following errors and may not have printed:\n");
			while ((i = getc(fp)) != EOF)
				putchar(i);
			(void) fclose(fp);
			break;
		case ACCESS:
			cp = "ACCESS";
			printf("\nwas not printed because it was not linked to the original file\n");
		}
		fflush(stdout);
		(void) close(STDOUT_FILENO);
	} else {
		syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
		return;
	}
	(void) close(p[0]);
	(void) close(p[1]);
	wait(NULL);
	syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
	    userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
}

/*
 * dofork - fork with retries on failure
 */
static int
dofork(const struct printer *pp, int action)
{
	pid_t forkpid;
	int i, fail;
	struct passwd *pwd;

	forkpid = -1;
	if (daemon_uname == NULL) {
		pwd = getpwuid(pp->daemon_user);
		if (pwd == NULL) {
			syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
			    pp->printer, pp->daemon_user);
			goto error_ret;
		}
		daemon_uname = strdup(pwd->pw_name);
		daemon_defgid = pwd->pw_gid;
	}

	for (i = 0; i < 20; i++) {
		forkpid = fork();
		if (forkpid < 0) {
			sleep((unsigned)(i*i));
			continue;
		}
		/*
		 * Child should run as daemon instead of root
		 */
		if (forkpid == 0) {
			errno = 0;
			fail = initgroups(daemon_uname, daemon_defgid);
			if (fail) {
				syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
				    pp->printer, daemon_uname, daemon_defgid);
				break;
			}
			fail = setgid(daemon_defgid);
			if (fail) {
				syslog(LOG_ERR, "%s: setgid(%u): %m",
				    pp->printer, daemon_defgid);
				break;
			}
			fail = setuid(pp->daemon_user);
			if (fail) {
				syslog(LOG_ERR, "%s: setuid(%ld): %m",
				    pp->printer, pp->daemon_user);
				break;
			}
		}
		return (forkpid);
	}

	/*
	 * An error occurred.  If the error is in the child process, then
	 * this routine MUST always exit().  DORETURN only effects how
	 * errors should be handled in the parent process.
	 */
error_ret:
	if (forkpid == 0) {
		syslog(LOG_ERR, "%s: dofork(): aborting child process...",
		    pp->printer);
		exit(1);
	}
	syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);

	sleep(1);		/* throttle errors, as a safety measure */
	switch (action) {
	case DORETURN:
		return (-1);
	default:
		syslog(LOG_ERR, "bad action (%d) to dofork", action);
		/* FALLTHROUGH */
	case DOABORT:
		exit(1);
	}
	/*NOTREACHED*/
}

/*
 * Kill child processes to abort current job.
 */
static void
abortpr(int signo __unused)
{

	(void) unlink(tempstderr);
	kill(0, SIGINT);
	if (of_pid > 0)
		kill(of_pid, SIGCONT);
	while (wait(NULL) > 0)
		;
	if (of_pid > 0 && tfd != -1)
		unlink(tfile);
	exit(0);
}

static void
init(struct printer *pp)
{
	char *s;

	sprintf(&width[2], "%ld", pp->page_width);
	sprintf(&length[2], "%ld", pp->page_length);
	sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
	sprintf(&pxlength[2], "%ld", pp->page_plength);
	if ((s = checkremote(pp)) != NULL) {
		syslog(LOG_WARNING, "%s", s);
		free(s);
	}
}

void
startprinting(const char *printer)
{
	struct printer myprinter, *pp = &myprinter;
	int status;

	init_printer(pp);
	status = getprintcap(printer, pp);
	switch(status) {
	case PCAPERR_OSERR:
		syslog(LOG_ERR, "can't open printer description file: %m");
		exit(1);
	case PCAPERR_NOTFOUND:
		syslog(LOG_ERR, "unknown printer: %s", printer);
		exit(1);
	case PCAPERR_TCLOOP:
		fatal(pp, "potential reference loop detected in printcap file");
	default:
		break;
	}
	printjob(pp);
}

/*
 * Acquire line printer or remote connection.
 */
static void
openpr(const struct printer *pp)
{
	int p[2];
	char *cp;

	if (pp->remote) {
		openrem(pp);
		/*
		 * Lpd does support the setting of 'of=' filters for
		 * jobs going to remote machines, but that does not
		 * have the same meaning as 'of=' does when handling
		 * local print queues.  For remote machines, all 'of='
		 * filter processing is handled in sendfile(), and that
		 * does not use these global "output filter" variables.
		 */ 
		ofd = -1;
		of_pid = 0;
		return;
	} else if (*pp->lp) {
		if (strchr(pp->lp, '@') != NULL)
			opennet(pp);
		else
			opentty(pp);
	} else {
		syslog(LOG_ERR, "%s: no line printer device or host name",
		    pp->printer);
		exit(1);
	}

	/*
	 * Start up an output filter, if needed.
	 */
	if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) {
		pipe(p);
		if (pp->remote) {
			strcpy(tfile, TFILENAME);
			tfd = mkstemp(tfile);
		}
		if ((of_pid = dofork(pp, DOABORT)) == 0) {	/* child */
			dup2(p[0], STDIN_FILENO);	/* pipe is std in */
			/* tfile/printer is stdout */
			dup2(pp->remote ? tfd : pfd, STDOUT_FILENO);
			closelog();
			closeallfds(3);
			if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
				cp = pp->filters[LPF_OUTPUT];
			else
				cp++;
			execl(pp->filters[LPF_OUTPUT], cp, width, length,
			      (char *)0);
			syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer,
			    pp->filters[LPF_OUTPUT]);
			exit(1);
		}
		(void) close(p[0]);		/* close input side */
		ofd = p[1];			/* use pipe for output */
	} else {
		ofd = pfd;
		of_pid = 0;
	}
}

/*
 * Printer connected directly to the network
 * or to a terminal server on the net
 */
static void
opennet(const struct printer *pp)
{
	register int i;
	int resp;
	u_long port;
	char *ep;
	void (*savealrm)(int);

	port = strtoul(pp->lp, &ep, 0);
	if (*ep != '@' || port > 65535) {
		syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
		    pp->lp);
		exit(1);
	}
	ep++;

	for (i = 1; ; i = i < 256 ? i << 1 : i) {
		resp = -1;
		savealrm = signal(SIGALRM, alarmhandler);
		alarm(pp->conn_timeout);
		pfd = getport(pp, ep, port);
		alarm(0);
		(void)signal(SIGALRM, savealrm);
		if (pfd < 0 && errno == ECONNREFUSED)
			resp = 1;
		else if (pfd >= 0) {
			/*
			 * need to delay a bit for rs232 lines
			 * to stabilize in case printer is
			 * connected via a terminal server
			 */
			delay(500);
			break;
		}
		if (i == 1) {
			if (resp < 0)
				pstatus(pp, "waiting for %s to come up",
					pp->lp);
			else
				pstatus(pp, 
					"waiting for access to printer on %s",
					pp->lp);
		}
		sleep(i);
	}
	pstatus(pp, "sending to %s port %lu", ep, port);
}

/*
 * Printer is connected to an RS232 port on this host
 */
static void
opentty(const struct printer *pp)
{
	register int i;

	for (i = 1; ; i = i < 32 ? i << 1 : i) {
		pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
		if (pfd >= 0) {
			delay(500);
			break;
		}
		if (errno == ENOENT) {
			syslog(LOG_ERR, "%s: %m", pp->lp);
			exit(1);
		}
		if (i == 1)
			pstatus(pp, 
				"waiting for %s to become ready (offline?)",
				pp->printer);
		sleep(i);
	}
	if (isatty(pfd))
		setty(pp);
	pstatus(pp, "%s is ready and printing", pp->printer);
}

/*
 * Printer is on a remote host
 */
static void
openrem(const struct printer *pp)
{
	register int i;
	int resp;
	void (*savealrm)(int);

	for (i = 1; ; i = i < 256 ? i << 1 : i) {
		resp = -1;
		savealrm = signal(SIGALRM, alarmhandler);
		alarm(pp->conn_timeout);
		pfd = getport(pp, pp->remote_host, 0);
		alarm(0);
		(void)signal(SIGALRM, savealrm);
		if (pfd >= 0) {
			if ((writel(pfd, "\2", pp->remote_queue, "\n", 
				    (char *)0)
			     == 2 + strlen(pp->remote_queue))
			    && (resp = response(pp)) == 0)
				break;
			(void) close(pfd);
		}
		if (i == 1) {
			if (resp < 0)
				pstatus(pp, "waiting for %s to come up", 
					pp->remote_host);
			else {
				pstatus(pp,
					"waiting for queue to be enabled on %s",
					pp->remote_host);
				i = 256;
			}
		}
		sleep(i);
	}
	pstatus(pp, "sending to %s", pp->remote_host);
}

/*
 * setup tty lines.
 */
static void
setty(const struct printer *pp)
{
	struct termios ttybuf;

	if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
		syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
		exit(1);
	}
	if (tcgetattr(pfd, &ttybuf) < 0) {
		syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
		exit(1);
	}
	if (pp->baud_rate > 0)
		cfsetspeed(&ttybuf, pp->baud_rate);
	if (pp->mode_set) {
		char *s = strdup(pp->mode_set), *tmp;

		while ((tmp = strsep(&s, ",")) != NULL) {
			(void) msearch(tmp, &ttybuf);
		}
	}
	if (pp->mode_set != 0 || pp->baud_rate > 0) {
		if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
			syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
		}
	}
}

#include <stdarg.h>

static void
pstatus(const struct printer *pp, const char *msg, ...)
{
	int fd;
	char *buf;
	va_list ap;
	va_start(ap, msg);

	umask(S_IWOTH);
	fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
	if (fd < 0) {
		syslog(LOG_ERR, "%s: open(%s): %m", pp->printer,
		    pp->status_file);
		exit(1);
	}
	ftruncate(fd, 0);
	vasprintf(&buf, msg, ap);
	va_end(ap);
	writel(fd, buf, "\n", (char *)0);
	close(fd);
	free(buf);
}

void
alarmhandler(int signo __unused)
{
	/* the signal is ignored */
	/* (the '__unused' is just to avoid a compile-time warning) */
}