common.c revision 99842
1284345Ssjg/*
2284345Ssjg * Copyright (c) 1983, 1993
3284345Ssjg *	The Regents of the University of California.  All rights reserved.
4284345Ssjg * (c) UNIX System Laboratories, Inc.
5284345Ssjg * All or some portions of this file are derived from material licensed
6284345Ssjg * to the University of California by American Telephone and Telegraph
7284345Ssjg * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8284345Ssjg * the permission of UNIX System Laboratories, Inc.
9284345Ssjg *
10284345Ssjg * Redistribution and use in source and binary forms, with or without
11284345Ssjg * modification, are permitted provided that the following conditions
12284345Ssjg * are met:
13284345Ssjg * 1. Redistributions of source code must retain the above copyright
14284345Ssjg *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40/*
41static char sccsid[] = "@(#)common.c	8.5 (Berkeley) 4/28/95";
42*/
43static const char rcsid[] =
44  "$FreeBSD: head/usr.sbin/lpr/common_source/common.c 99842 2002-07-12 01:22:57Z gad $";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/types.h>
51
52#include <dirent.h>
53#include <errno.h>
54#include <fcntl.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60#include "lp.h"
61#include "lp.local.h"
62#include "pathnames.h"
63
64/*
65 * Routines and data common to all the line printer functions.
66 */
67char	line[BUFSIZ];
68const char	*progname;		/* program name */
69
70extern uid_t	uid, euid;
71
72static int compar(const void *_p1, const void *_p2);
73
74/*
75 * Getline reads a line from the control file cfp, removes tabs, converts
76 *  new-line to null and leaves it in line.
77 * Returns 0 at EOF or the number of characters read.
78 */
79int
80getline(FILE *cfp)
81{
82	register int linel = 0;
83	register char *lp = line;
84	register int c;
85
86	while ((c = getc(cfp)) != '\n' && (size_t)(linel+1) < sizeof(line)) {
87		if (c == EOF)
88			return(0);
89		if (c == '\t') {
90			do {
91				*lp++ = ' ';
92				linel++;
93			} while ((linel & 07) != 0 && (size_t)(linel+1) <
94			    sizeof(line));
95			continue;
96		}
97		*lp++ = c;
98		linel++;
99	}
100	*lp++ = '\0';
101	return(linel);
102}
103
104/*
105 * Scan the current directory and make a list of daemon files sorted by
106 * creation time.
107 * Return the number of entries and a pointer to the list.
108 */
109int
110getq(const struct printer *pp, struct jobqueue *(*namelist[]))
111{
112	register struct dirent *d;
113	register struct jobqueue *q, **queue;
114	size_t arraysz, entrysz, nitems;
115	struct stat stbuf;
116	DIR *dirp;
117	int statres;
118
119	seteuid(euid);
120	if ((dirp = opendir(pp->spool_dir)) == NULL) {
121		seteuid(uid);
122		return (-1);
123	}
124	if (fstat(dirp->dd_fd, &stbuf) < 0)
125		goto errdone;
126	seteuid(uid);
127
128	/*
129	 * Estimate the array size by taking the size of the directory file
130	 * and dividing it by a multiple of the minimum size entry.
131	 */
132	arraysz = (stbuf.st_size / 24);
133	queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *));
134	if (queue == NULL)
135		goto errdone;
136
137	nitems = 0;
138	while ((d = readdir(dirp)) != NULL) {
139		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
140			continue;	/* daemon control files only */
141		seteuid(euid);
142		statres = stat(d->d_name, &stbuf);
143		seteuid(uid);
144		if (statres < 0)
145			continue;	/* Doesn't exist */
146		entrysz = sizeof(struct jobqueue) - sizeof(q->job_cfname) +
147		    strlen(d->d_name) + 1;
148		q = (struct jobqueue *)malloc(entrysz);
149		if (q == NULL)
150			goto errdone;
151		q->job_matched = 0;
152		q->job_processed = 0;
153		q->job_time = stbuf.st_mtime;
154		strcpy(q->job_cfname, d->d_name);
155		/*
156		 * Check to make sure the array has space left and
157		 * realloc the maximum size.
158		 */
159		if (++nitems > arraysz) {
160			arraysz *= 2;
161			queue = (struct jobqueue **)realloc((char *)queue,
162			    arraysz * sizeof(struct jobqueue *));
163			if (queue == NULL)
164				goto errdone;
165		}
166		queue[nitems-1] = q;
167	}
168	closedir(dirp);
169	if (nitems)
170		qsort(queue, nitems, sizeof(struct jobqueue *), compar);
171	*namelist = queue;
172	return(nitems);
173
174errdone:
175	closedir(dirp);
176	seteuid(uid);
177	return (-1);
178}
179
180/*
181 * Compare modification times.
182 */
183static int
184compar(const void *p1, const void *p2)
185{
186	const struct jobqueue *qe1, *qe2;
187
188	qe1 = *(const struct jobqueue * const *)p1;
189	qe2 = *(const struct jobqueue * const *)p2;
190
191	if (qe1->job_time < qe2->job_time)
192		return (-1);
193	if (qe1->job_time > qe2->job_time)
194		return (1);
195	/*
196	 * At this point, the two files have the same last-modification time.
197	 * return a result based on filenames, so that 'cfA001some.host' will
198	 * come before 'cfA002some.host'.  Since the jobid ('001') will wrap
199	 * around when it gets to '999', we also assume that '9xx' jobs are
200	 * older than '0xx' jobs.
201	*/
202	if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0'))
203		return (-1);
204	if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9'))
205		return (1);
206	return (strcmp(qe1->job_cfname, qe2->job_cfname));
207}
208
209/* sleep n milliseconds */
210void
211delay(int millisec)
212{
213	struct timeval tdelay;
214
215	if (millisec <= 0 || millisec > 10000)
216		fatal((struct printer *)0, /* fatal() knows how to deal */
217		    "unreasonable delay period (%d)", millisec);
218	tdelay.tv_sec = millisec / 1000;
219	tdelay.tv_usec = millisec * 1000 % 1000000;
220	(void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
221}
222
223char *
224lock_file_name(const struct printer *pp, char *buf, size_t len)
225{
226	static char staticbuf[MAXPATHLEN];
227
228	if (buf == 0)
229		buf = staticbuf;
230	if (len == 0)
231		len = MAXPATHLEN;
232
233	if (pp->lock_file[0] == '/')
234		strlcpy(buf, pp->lock_file, len);
235	else
236		snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file);
237
238	return buf;
239}
240
241char *
242status_file_name(const struct printer *pp, char *buf, size_t len)
243{
244	static char staticbuf[MAXPATHLEN];
245
246	if (buf == 0)
247		buf = staticbuf;
248	if (len == 0)
249		len = MAXPATHLEN;
250
251	if (pp->status_file[0] == '/')
252		strlcpy(buf, pp->status_file, len);
253	else
254		snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
255
256	return buf;
257}
258
259/*
260 * Routine to change operational state of a print queue.  The operational
261 * state is indicated by the access bits on the lock file for the queue.
262 * At present, this is only called from various routines in lpc/cmds.c.
263 *
264 *  XXX - Note that this works by changing access-bits on the
265 *	file, and you can only do that if you are the owner of
266 *	the file, or root.  Thus, this won't really work for
267 *	userids in the "LPR_OPER" group, unless lpc is running
268 *	setuid to root (or maybe setuid to daemon).
269 *	Generally lpc is installed setgid to daemon, but does
270 *	not run setuid.
271 */
272int
273set_qstate(int action, const char *lfname)
274{
275	struct stat stbuf;
276	mode_t chgbits, newbits, oldmask;
277	const char *failmsg, *okmsg;
278	int chres, errsav, fd, res, statres;
279
280	/*
281	 * Find what the current access-bits are.
282	 */
283	memset(&stbuf, 0, sizeof(stbuf));
284	seteuid(euid);
285	statres = stat(lfname, &stbuf);
286	errsav = errno;
287	seteuid(uid);
288	if ((statres < 0) && (errsav != ENOENT)) {
289		printf("\tcannot stat() lock file\n");
290		return (SQS_STATFAIL);
291		/* NOTREACHED */
292	}
293
294	/*
295	 * Determine which bit(s) should change for the requested action.
296	 */
297	chgbits = stbuf.st_mode;
298	newbits = LOCK_FILE_MODE;
299	okmsg = NULL;
300	failmsg = NULL;
301	if (action & SQS_DISABLEQ) {
302		chgbits |= LFM_QUEUE_DIS;
303		newbits |= LFM_QUEUE_DIS;
304		okmsg = "queuing disabled";
305		failmsg = "disable queuing";
306	}
307	if (action & SQS_STOPP) {
308		chgbits |= LFM_PRINT_DIS;
309		newbits |= LFM_PRINT_DIS;
310		okmsg = "printing disabled";
311		failmsg = "disable printing";
312		if (action & SQS_DISABLEQ) {
313			okmsg = "printer and queuing disabled";
314			failmsg = "disable queuing and printing";
315		}
316	}
317	if (action & SQS_ENABLEQ) {
318		chgbits &= ~LFM_QUEUE_DIS;
319		newbits &= ~LFM_QUEUE_DIS;
320		okmsg = "queuing enabled";
321		failmsg = "enable queuing";
322	}
323	if (action & SQS_STARTP) {
324		chgbits &= ~LFM_PRINT_DIS;
325		newbits &= ~LFM_PRINT_DIS;
326		okmsg = "printing enabled";
327		failmsg = "enable printing";
328	}
329	if (okmsg == NULL) {
330		/* This routine was called with an invalid action. */
331		printf("\t<error in set_qstate!>\n");
332		return (SQS_PARMERR);
333		/* NOTREACHED */
334	}
335
336	res = 0;
337	if (statres >= 0) {
338		/* The file already exists, so change the access. */
339		seteuid(euid);
340		chres = chmod(lfname, chgbits);
341		errsav = errno;
342		seteuid(uid);
343		res = SQS_CHGOK;
344		if (res < 0)
345			res = SQS_CHGFAIL;
346	} else if (newbits == LOCK_FILE_MODE) {
347		/*
348		 * The file does not exist, but the state requested is
349		 * the same as the default state when no file exists.
350		 * Thus, there is no need to create the file.
351		 */
352		res = SQS_SKIPCREOK;
353	} else {
354		/*
355		 * The file did not exist, so create it with the
356		 * appropriate access bits for the requested action.
357		 * Push a new umask around that create, to make sure
358		 * all the read/write bits are set as desired.
359		 */
360		oldmask = umask(S_IWOTH);
361		seteuid(euid);
362		fd = open(lfname, O_WRONLY|O_CREAT, newbits);
363		errsav = errno;
364		seteuid(uid);
365		umask(oldmask);
366		res = SQS_CREFAIL;
367		if (fd >= 0) {
368			res = SQS_CREOK;
369			close(fd);
370		}
371	}
372
373	switch (res) {
374	case SQS_CHGOK:
375	case SQS_CREOK:
376	case SQS_SKIPCREOK:
377		printf("\t%s\n", okmsg);
378		break;
379	case SQS_CREFAIL:
380		printf("\tcannot create lock file: %s\n",
381		    strerror(errsav));
382		break;
383	default:
384		printf("\tcannot %s: %s\n", failmsg, strerror(errsav));
385		break;
386	}
387
388	return (res);
389}
390
391/* routine to get a current timestamp, optionally in a standard-fmt string */
392void
393lpd_gettime(struct timespec *tsp, char *strp, size_t strsize)
394{
395	struct timespec local_ts;
396	struct timeval btime;
397	char tempstr[TIMESTR_SIZE];
398#ifdef STRFTIME_WRONG_z
399	char *destp;
400#endif
401
402	if (tsp == NULL)
403		tsp = &local_ts;
404
405	/* some platforms have a routine called clock_gettime, but the
406	 * routine does nothing but return "not implemented". */
407	memset(tsp, 0, sizeof(struct timespec));
408	if (clock_gettime(CLOCK_REALTIME, tsp)) {
409		/* nanosec-aware rtn failed, fall back to microsec-aware rtn */
410		memset(tsp, 0, sizeof(struct timespec));
411		gettimeofday(&btime, NULL);
412		tsp->tv_sec = btime.tv_sec;
413		tsp->tv_nsec = btime.tv_usec * 1000;
414	}
415
416	/* caller may not need a character-ized version */
417	if ((strp == NULL) || (strsize < 1))
418		return;
419
420	strftime(tempstr, TIMESTR_SIZE, LPD_TIMESTAMP_PATTERN,
421		 localtime(&tsp->tv_sec));
422
423	/*
424	 * This check is for implementations of strftime which treat %z
425	 * (timezone as [+-]hhmm ) like %Z (timezone as characters), or
426	 * completely ignore %z.  This section is not needed on freebsd.
427	 * I'm not sure this is completely right, but it should work OK
428	 * for EST and EDT...
429	 */
430#ifdef STRFTIME_WRONG_z
431	destp = strrchr(tempstr, ':');
432	if (destp != NULL) {
433		destp += 3;
434		if ((*destp != '+') && (*destp != '-')) {
435			char savday[6];
436			int tzmin = timezone / 60;
437			int tzhr = tzmin / 60;
438			if (daylight)
439				tzhr--;
440			strcpy(savday, destp + strlen(destp) - 4);
441			snprintf(destp, (destp - tempstr), "%+03d%02d",
442			    (-1*tzhr), tzmin % 60);
443			strcat(destp, savday);
444		}
445	}
446#endif
447
448	if (strsize > TIMESTR_SIZE) {
449		strsize = TIMESTR_SIZE;
450		strp[TIMESTR_SIZE+1] = '\0';
451	}
452	strlcpy(strp, tempstr, strsize);
453}
454
455/* routines for writing transfer-statistic records */
456void
457trstat_init(struct printer *pp, const char *fname, int filenum)
458{
459	register const char *srcp;
460	register char *destp, *endp;
461
462	/*
463	 * Figure out the job id of this file.  The filename should be
464	 * 'cf', 'df', or maybe 'tf', followed by a letter (or sometimes
465	 * two), followed by the jobnum, followed by a hostname.
466	 * The jobnum is usually 3 digits, but might be as many as 5.
467	 * Note that some care has to be taken parsing this, as the
468	 * filename could be coming from a remote-host, and thus might
469	 * not look anything like what is expected...
470	 */
471	memset(pp->jobnum, 0, sizeof(pp->jobnum));
472	pp->jobnum[0] = '0';
473	srcp = strchr(fname, '/');
474	if (srcp == NULL)
475		srcp = fname;
476	destp = &(pp->jobnum[0]);
477	endp = destp + 5;
478	while (*srcp != '\0' && (*srcp < '0' || *srcp > '9'))
479		srcp++;
480	while (*srcp >= '0' && *srcp <= '9' && destp < endp)
481		*(destp++) = *(srcp++);
482
483	/* get the starting time in both numeric and string formats, and
484	 * save those away along with the file-number */
485	pp->jobdfnum = filenum;
486	lpd_gettime(&pp->tr_start, pp->tr_timestr, (size_t)TIMESTR_SIZE);
487
488	return;
489}
490
491void
492trstat_write(struct printer *pp, tr_sendrecv sendrecv, size_t bytecnt,
493    const char *userid, const char *otherhost, const char *orighost)
494{
495#define STATLINE_SIZE 1024
496	double trtime;
497	size_t remspace;
498	int statfile;
499	char thishost[MAXHOSTNAMELEN], statline[STATLINE_SIZE];
500	char *eostat;
501	const char *lprhost, *recvdev, *recvhost, *rectype;
502	const char *sendhost, *statfname;
503#define UPD_EOSTAT(xStr) do {         \
504	eostat = strchr(xStr, '\0');  \
505	remspace = eostat - xStr;     \
506} while(0)
507
508	lpd_gettime(&pp->tr_done, NULL, (size_t)0);
509	trtime = DIFFTIME_TS(pp->tr_done, pp->tr_start);
510
511	gethostname(thishost, sizeof(thishost));
512	lprhost = sendhost = recvhost = recvdev = NULL;
513	switch (sendrecv) {
514	    case TR_SENDING:
515		rectype = "send";
516		statfname = pp->stat_send;
517		sendhost = thishost;
518		recvhost = otherhost;
519		break;
520	    case TR_RECVING:
521		rectype = "recv";
522		statfname = pp->stat_recv;
523		sendhost = otherhost;
524		recvhost = thishost;
525		break;
526	    case TR_PRINTING:
527		/*
528		 * This case is for copying to a device (presumably local,
529		 * though filters using things like 'net/CAP' can confuse
530		 * this assumption...).
531		 */
532		rectype = "prnt";
533		statfname = pp->stat_send;
534		sendhost = thishost;
535		recvdev = _PATH_DEFDEVLP;
536		if (pp->lp) recvdev = pp->lp;
537		break;
538	    default:
539		/* internal error...  should we syslog/printf an error? */
540		return;
541	}
542	if (statfname == NULL)
543		return;
544
545	/*
546	 * the original-host and userid are found out by reading thru the
547	 * cf (control-file) for the job.  Unfortunately, on incoming jobs
548	 * the df's (data-files) are sent before the matching cf, so the
549	 * orighost & userid are generally not-available for incoming jobs.
550	 *
551	 * (it would be nice to create a work-around for that..)
552	 */
553	if (orighost && (*orighost != '\0'))
554		lprhost = orighost;
555	else
556		lprhost = ".na.";
557	if (*userid == '\0')
558		userid = NULL;
559
560	/*
561	 * Format of statline.
562	 * Some of the keywords listed here are not implemented here, but
563	 * they are listed to reserve the meaning for a given keyword.
564	 * Fields are separated by a blank.  The fields in statline are:
565	 *   <tstamp>      - time the transfer started
566	 *   <ptrqueue>    - name of the printer queue (the short-name...)
567	 *   <hname>       - hostname the file originally came from (the
568	 *		     'lpr host'), if known, or  "_na_" if not known.
569	 *   <xxx>         - id of job from that host (generally three digits)
570	 *   <n>           - file count (# of file within job)
571	 *   <rectype>     - 4-byte field indicating the type of transfer
572	 *		     statistics record.  "send" means it's from the
573	 *		     host sending a datafile, "recv" means it's from
574	 *		     a host as it receives a datafile.
575	 *   user=<userid> - user who sent the job (if known)
576	 *   secs=<n>      - seconds it took to transfer the file
577	 *   bytes=<n>     - number of bytes transfered (ie, "bytecount")
578	 *   bps=<n.n>e<n> - Bytes/sec (if the transfer was "big enough"
579	 *		     for this to be useful)
580	 * ! top=<str>     - type of printer (if the type is defined in
581	 *		     printcap, and if this statline is for sending
582	 *		     a file to that ptr)
583	 * ! qls=<n>       - queue-length at start of send/print-ing a job
584	 * ! qle=<n>       - queue-length at end of send/print-ing a job
585	 *   sip=<addr>    - IP address of sending host, only included when
586	 *		     receiving a job.
587	 *   shost=<hname> - sending host (if that does != the original host)
588	 *   rhost=<hname> - hostname receiving the file (ie, "destination")
589	 *   rdev=<dev>    - device receiving the file, when the file is being
590	 *		     send to a device instead of a remote host.
591	 *
592	 * Note: A single print job may be transferred multiple times.  The
593	 * original 'lpr' occurs on one host, and that original host might
594	 * send to some interim host (or print server).  That interim host
595	 * might turn around and send the job to yet another host (most likely
596	 * the real printer).  The 'shost=' parameter is only included if the
597	 * sending host for this particular transfer is NOT the same as the
598	 * host which did the original 'lpr'.
599	 *
600	 * Many values have 'something=' tags before them, because they are
601	 * in some sense "optional", or their order may vary.  "Optional" may
602	 * mean in the sense that different SITES might choose to have other
603	 * fields in the record, or that some fields are only included under
604	 * some circumstances.  Programs processing these records should not
605	 * assume the order or existence of any of these keyword fields.
606	 */
607	snprintf(statline, STATLINE_SIZE, "%s %s %s %s %03ld %s",
608	    pp->tr_timestr, pp->printer, lprhost, pp->jobnum,
609	    pp->jobdfnum, rectype);
610	UPD_EOSTAT(statline);
611
612	if (userid != NULL) {
613		snprintf(eostat, remspace, " user=%s", userid);
614		UPD_EOSTAT(statline);
615	}
616	snprintf(eostat, remspace, " secs=%#.2f bytes=%lu", trtime,
617	    (unsigned long)bytecnt);
618	UPD_EOSTAT(statline);
619
620	/*
621	 * The bps field duplicates info from bytes and secs, so do
622	 * not bother to include it for very small files.
623	 */
624	if ((bytecnt > 25000) && (trtime > 1.1)) {
625		snprintf(eostat, remspace, " bps=%#.2e",
626		    ((double)bytecnt/trtime));
627		UPD_EOSTAT(statline);
628	}
629
630	if (sendrecv == TR_RECVING) {
631		if (remspace > 5+strlen(from_ip) ) {
632			snprintf(eostat, remspace, " sip=%s", from_ip);
633			UPD_EOSTAT(statline);
634		}
635	}
636	if (0 != strcmp(lprhost, sendhost)) {
637		if (remspace > 7+strlen(sendhost) ) {
638			snprintf(eostat, remspace, " shost=%s", sendhost);
639			UPD_EOSTAT(statline);
640		}
641	}
642	if (recvhost) {
643		if (remspace > 7+strlen(recvhost) ) {
644			snprintf(eostat, remspace, " rhost=%s", recvhost);
645			UPD_EOSTAT(statline);
646		}
647	}
648	if (recvdev) {
649		if (remspace > 6+strlen(recvdev) ) {
650			snprintf(eostat, remspace, " rdev=%s", recvdev);
651			UPD_EOSTAT(statline);
652		}
653	}
654	if (remspace > 1) {
655		strcpy(eostat, "\n");
656	} else {
657		/* probably should back up to just before the final " x=".. */
658		strcpy(statline+STATLINE_SIZE-2, "\n");
659	}
660	statfile = open(statfname, O_WRONLY|O_APPEND, 0664);
661	if (statfile < 0) {
662		/* statfile was given, but we can't open it.  should we
663		 * syslog/printf this as an error? */
664		return;
665	}
666	write(statfile, statline, strlen(statline));
667	close(statfile);
668
669	return;
670#undef UPD_EOSTAT
671}
672
673#include <stdarg.h>
674
675void
676fatal(const struct printer *pp, const char *msg, ...)
677{
678	va_list ap;
679	va_start(ap, msg);
680	/* this error message is being sent to the 'from_host' */
681	if (from_host != local_host)
682		(void)printf("%s: ", local_host);
683	(void)printf("%s: ", progname);
684	if (pp && pp->printer)
685		(void)printf("%s: ", pp->printer);
686	(void)vprintf(msg, ap);
687	va_end(ap);
688	(void)putchar('\n');
689	exit(1);
690}
691
692/*
693 * Close all file descriptors from START on up.
694 * This is a horrific kluge, since getdtablesize() might return
695 * ``infinity'', in which case we will be spending a long time
696 * closing ``files'' which were never open.  Perhaps it would
697 * be better to close the first N fds, for some small value of N.
698 */
699void
700closeallfds(int start)
701{
702	int stop = getdtablesize();
703	for (; start < stop; start++)
704		close(start);
705}
706
707