1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
4dea673e9SRodney W. Grimes * Copyright (c) 1983, 1993
5dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved.
6dea673e9SRodney W. Grimes * (c) UNIX System Laboratories, Inc.
7dea673e9SRodney W. Grimes * All or some portions of this file are derived from material licensed
8dea673e9SRodney W. Grimes * to the University of California by American Telephone and Telegraph
9dea673e9SRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10dea673e9SRodney W. Grimes * the permission of UNIX System Laboratories, Inc.
11dea673e9SRodney W. Grimes *
12dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
13dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions
14dea673e9SRodney W. Grimes * are met:
15dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
16dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
17dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
18dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
19dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution.
20dea673e9SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software
21dea673e9SRodney W. Grimes * must display the following acknowledgement:
22dea673e9SRodney W. Grimes * This product includes software developed by the University of
23dea673e9SRodney W. Grimes * California, Berkeley and its contributors.
24dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors
25dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software
26dea673e9SRodney W. Grimes * without specific prior written permission.
27dea673e9SRodney W. Grimes *
28dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38dea673e9SRodney W. Grimes * SUCH DAMAGE.
39dea673e9SRodney W. Grimes */
40dea673e9SRodney W. Grimes
411f589b47SGarance A Drosehn #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
42dea673e9SRodney W. Grimes #include <sys/param.h>
43dea673e9SRodney W. Grimes #include <sys/stat.h>
445458e2f4SJoerg Wunsch #include <sys/time.h>
456522ebecSGarance A Drosehn #include <sys/types.h>
46dea673e9SRodney W. Grimes
47c547dbe8SGarance A Drosehn #include <ctype.h>
48dea673e9SRodney W. Grimes #include <dirent.h>
491d1d4a47SEitan Adler #include <err.h>
50e8e715faSGarance A Drosehn #include <errno.h>
516522ebecSGarance A Drosehn #include <fcntl.h>
52dea673e9SRodney W. Grimes #include <stdio.h>
534a1a0dbeSGarrett Wollman #include <stdlib.h>
54dea673e9SRodney W. Grimes #include <string.h>
554a1a0dbeSGarrett Wollman #include <unistd.h>
564a1a0dbeSGarrett Wollman
57dea673e9SRodney W. Grimes #include "lp.h"
584a1a0dbeSGarrett Wollman #include "lp.local.h"
59dea673e9SRodney W. Grimes #include "pathnames.h"
60dea673e9SRodney W. Grimes
61dea673e9SRodney W. Grimes /*
62dea673e9SRodney W. Grimes * Routines and data common to all the line printer functions.
63dea673e9SRodney W. Grimes */
64dea673e9SRodney W. Grimes char line[BUFSIZ];
6531058a75SGarance A Drosehn const char *progname; /* program name */
66dea673e9SRodney W. Grimes
67ba7a1ad7SGarance A Drosehn static int compar(const void *_p1, const void *_p2);
68dea673e9SRodney W. Grimes
69dea673e9SRodney W. Grimes /*
70c547dbe8SGarance A Drosehn * isdigit() takes a parameter of 'int', but expect values in the range
71c547dbe8SGarance A Drosehn * of unsigned char. Define a wrapper which takes a value of type 'char',
72c547dbe8SGarance A Drosehn * whether signed or unsigned, and ensure it ends up in the right range.
73c547dbe8SGarance A Drosehn */
74c547dbe8SGarance A Drosehn #define isdigitch(Anychar) isdigit((u_char)(Anychar))
75c547dbe8SGarance A Drosehn
76c547dbe8SGarance A Drosehn /*
77a9c48188SBaptiste Daroussin * get_line reads a line from the control file cfp, removes tabs, converts
78dea673e9SRodney W. Grimes * new-line to null and leaves it in line.
79dea673e9SRodney W. Grimes * Returns 0 at EOF or the number of characters read.
80dea673e9SRodney W. Grimes */
81dea673e9SRodney W. Grimes int
get_line(FILE * cfp)82a9c48188SBaptiste Daroussin get_line(FILE *cfp)
83dea673e9SRodney W. Grimes {
84dea673e9SRodney W. Grimes register int linel = 0;
85dea673e9SRodney W. Grimes register char *lp = line;
8690cf373dSGarrett Wollman register int c;
87dea673e9SRodney W. Grimes
88011b9c79SGarance A Drosehn while ((c = getc(cfp)) != '\n' && (size_t)(linel+1) < sizeof(line)) {
89dea673e9SRodney W. Grimes if (c == EOF)
90dea673e9SRodney W. Grimes return(0);
91dea673e9SRodney W. Grimes if (c == '\t') {
92dea673e9SRodney W. Grimes do {
93dea673e9SRodney W. Grimes *lp++ = ' ';
94dea673e9SRodney W. Grimes linel++;
95011b9c79SGarance A Drosehn } while ((linel & 07) != 0 && (size_t)(linel+1) <
96011b9c79SGarance A Drosehn sizeof(line));
97dea673e9SRodney W. Grimes continue;
98dea673e9SRodney W. Grimes }
99dea673e9SRodney W. Grimes *lp++ = c;
100dea673e9SRodney W. Grimes linel++;
101dea673e9SRodney W. Grimes }
102dea673e9SRodney W. Grimes *lp++ = '\0';
103dea673e9SRodney W. Grimes return(linel);
104dea673e9SRodney W. Grimes }
105dea673e9SRodney W. Grimes
106dea673e9SRodney W. Grimes /*
107dea673e9SRodney W. Grimes * Scan the current directory and make a list of daemon files sorted by
108dea673e9SRodney W. Grimes * creation time.
109dea673e9SRodney W. Grimes * Return the number of entries and a pointer to the list.
110dea673e9SRodney W. Grimes */
111dea673e9SRodney W. Grimes int
getq(const struct printer * pp,struct jobqueue * (* namelist[]))112ba7a1ad7SGarance A Drosehn getq(const struct printer *pp, struct jobqueue *(*namelist[]))
113dea673e9SRodney W. Grimes {
114dea673e9SRodney W. Grimes register struct dirent *d;
11530b4b758SGarance A Drosehn register struct jobqueue *q, **queue;
116bae8d45cSGarance A Drosehn size_t arraysz, entrysz, nitems;
117dea673e9SRodney W. Grimes struct stat stbuf;
118dea673e9SRodney W. Grimes DIR *dirp;
119011b9c79SGarance A Drosehn int statres;
120dea673e9SRodney W. Grimes
1211d1d4a47SEitan Adler PRIV_START
122a4f87098SGarance A Drosehn if ((dirp = opendir(pp->spool_dir)) == NULL) {
1231d1d4a47SEitan Adler PRIV_END
124dea673e9SRodney W. Grimes return (-1);
125a4f87098SGarance A Drosehn }
1260bb2aabfSGleb Kurtsou if (fstat(dirfd(dirp), &stbuf) < 0)
127dea673e9SRodney W. Grimes goto errdone;
1281d1d4a47SEitan Adler PRIV_END
129dea673e9SRodney W. Grimes
130dea673e9SRodney W. Grimes /*
131dea673e9SRodney W. Grimes * Estimate the array size by taking the size of the directory file
132dea673e9SRodney W. Grimes * and dividing it by a multiple of the minimum size entry.
133dea673e9SRodney W. Grimes */
134dea673e9SRodney W. Grimes arraysz = (stbuf.st_size / 24);
135e0d20e66SJaakko Heinonen if (arraysz < 16)
136e0d20e66SJaakko Heinonen arraysz = 16;
13730b4b758SGarance A Drosehn queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *));
138dea673e9SRodney W. Grimes if (queue == NULL)
139dea673e9SRodney W. Grimes goto errdone;
140dea673e9SRodney W. Grimes
141dea673e9SRodney W. Grimes nitems = 0;
142dea673e9SRodney W. Grimes while ((d = readdir(dirp)) != NULL) {
143dea673e9SRodney W. Grimes if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
144dea673e9SRodney W. Grimes continue; /* daemon control files only */
1451d1d4a47SEitan Adler PRIV_START
146c83a68e0SGarance A Drosehn statres = stat(d->d_name, &stbuf);
1471d1d4a47SEitan Adler PRIV_END
148c83a68e0SGarance A Drosehn if (statres < 0)
149c83a68e0SGarance A Drosehn continue; /* Doesn't exist */
150bae8d45cSGarance A Drosehn entrysz = sizeof(struct jobqueue) - sizeof(q->job_cfname) +
151bae8d45cSGarance A Drosehn strlen(d->d_name) + 1;
152bae8d45cSGarance A Drosehn q = (struct jobqueue *)malloc(entrysz);
153dea673e9SRodney W. Grimes if (q == NULL)
154dea673e9SRodney W. Grimes goto errdone;
155bae8d45cSGarance A Drosehn q->job_matched = 0;
156bae8d45cSGarance A Drosehn q->job_processed = 0;
15730b4b758SGarance A Drosehn q->job_time = stbuf.st_mtime;
15830b4b758SGarance A Drosehn strcpy(q->job_cfname, d->d_name);
159dea673e9SRodney W. Grimes /*
160dea673e9SRodney W. Grimes * Check to make sure the array has space left and
161dea673e9SRodney W. Grimes * realloc the maximum size.
162dea673e9SRodney W. Grimes */
163dea673e9SRodney W. Grimes if (++nitems > arraysz) {
1644fd444b0SPedro F. Giffuni queue = (struct jobqueue **)reallocarray((char *)queue,
1654fd444b0SPedro F. Giffuni arraysz, 2 * sizeof(struct jobqueue *));
1664fd444b0SPedro F. Giffuni if (queue == NULL) {
1674fd444b0SPedro F. Giffuni free(q);
168dea673e9SRodney W. Grimes goto errdone;
169dea673e9SRodney W. Grimes }
1704fd444b0SPedro F. Giffuni arraysz *= 2;
1714fd444b0SPedro F. Giffuni }
172dea673e9SRodney W. Grimes queue[nitems-1] = q;
173dea673e9SRodney W. Grimes }
174dea673e9SRodney W. Grimes closedir(dirp);
175dea673e9SRodney W. Grimes if (nitems)
17630b4b758SGarance A Drosehn qsort(queue, nitems, sizeof(struct jobqueue *), compar);
177dea673e9SRodney W. Grimes *namelist = queue;
178dea673e9SRodney W. Grimes return(nitems);
179dea673e9SRodney W. Grimes
180dea673e9SRodney W. Grimes errdone:
181dea673e9SRodney W. Grimes closedir(dirp);
1821d1d4a47SEitan Adler PRIV_END
183dea673e9SRodney W. Grimes return (-1);
184dea673e9SRodney W. Grimes }
185dea673e9SRodney W. Grimes
186dea673e9SRodney W. Grimes /*
187dea673e9SRodney W. Grimes * Compare modification times.
188dea673e9SRodney W. Grimes */
189dea673e9SRodney W. Grimes static int
compar(const void * p1,const void * p2)190ba7a1ad7SGarance A Drosehn compar(const void *p1, const void *p2)
191dea673e9SRodney W. Grimes {
19230b4b758SGarance A Drosehn const struct jobqueue *qe1, *qe2;
193b770f354SGarance A Drosehn
194ac7d1b15SGarance A Drosehn qe1 = *(const struct jobqueue * const *)p1;
195ac7d1b15SGarance A Drosehn qe2 = *(const struct jobqueue * const *)p2;
1962b578691SGarrett Wollman
19730b4b758SGarance A Drosehn if (qe1->job_time < qe2->job_time)
198dea673e9SRodney W. Grimes return (-1);
19930b4b758SGarance A Drosehn if (qe1->job_time > qe2->job_time)
200dea673e9SRodney W. Grimes return (1);
2012b578691SGarrett Wollman /*
2022b578691SGarrett Wollman * At this point, the two files have the same last-modification time.
2032b578691SGarrett Wollman * return a result based on filenames, so that 'cfA001some.host' will
2042b578691SGarrett Wollman * come before 'cfA002some.host'. Since the jobid ('001') will wrap
2052b578691SGarrett Wollman * around when it gets to '999', we also assume that '9xx' jobs are
2062b578691SGarrett Wollman * older than '0xx' jobs.
2072b578691SGarrett Wollman */
20830b4b758SGarance A Drosehn if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0'))
2092b578691SGarrett Wollman return (-1);
21030b4b758SGarance A Drosehn if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9'))
2112b578691SGarrett Wollman return (1);
21230b4b758SGarance A Drosehn return (strcmp(qe1->job_cfname, qe2->job_cfname));
213dea673e9SRodney W. Grimes }
214dea673e9SRodney W. Grimes
215c547dbe8SGarance A Drosehn /*
216c547dbe8SGarance A Drosehn * A simple routine to determine the job number for a print job based on
217c547dbe8SGarance A Drosehn * the name of its control file. The algorithm used here may look odd, but
218c547dbe8SGarance A Drosehn * the main issue is that all parts of `lpd', `lpc', `lpq' & `lprm' must be
219c547dbe8SGarance A Drosehn * using the same algorithm, whatever that algorithm may be. If the caller
220c547dbe8SGarance A Drosehn * provides a non-null value for ''hostpp', then this returns a pointer to
221c547dbe8SGarance A Drosehn * the start of the hostname (or IP address?) as found in the filename.
222c547dbe8SGarance A Drosehn *
223c547dbe8SGarance A Drosehn * Algorithm: The standard `cf' file has the job number start in position 4,
224c547dbe8SGarance A Drosehn * but some implementations have that as an extra file-sequence letter, and
225c547dbe8SGarance A Drosehn * start the job number in position 5. The job number is usually three bytes,
226c547dbe8SGarance A Drosehn * but may be as many as five. Confusing matters still more, some Windows
227c547dbe8SGarance A Drosehn * print servers will append an IP address to the job number, instead of
228c547dbe8SGarance A Drosehn * the expected hostname. So, if the job number ends with a '.', then
229c547dbe8SGarance A Drosehn * assume the correct jobnum value is the first three digits.
230c547dbe8SGarance A Drosehn */
231c547dbe8SGarance A Drosehn int
calc_jobnum(const char * cfname,const char ** hostpp)232c547dbe8SGarance A Drosehn calc_jobnum(const char *cfname, const char **hostpp)
233c547dbe8SGarance A Drosehn {
234c547dbe8SGarance A Drosehn int jnum;
235c547dbe8SGarance A Drosehn const char *cp, *numstr, *hoststr;
236c547dbe8SGarance A Drosehn
237c547dbe8SGarance A Drosehn numstr = cfname + 3;
238c547dbe8SGarance A Drosehn if (!isdigitch(*numstr))
239c547dbe8SGarance A Drosehn numstr++;
240c547dbe8SGarance A Drosehn jnum = 0;
241c547dbe8SGarance A Drosehn for (cp = numstr; (cp < numstr + 5) && isdigitch(*cp); cp++)
242c547dbe8SGarance A Drosehn jnum = jnum * 10 + (*cp - '0');
243c547dbe8SGarance A Drosehn hoststr = cp;
244c547dbe8SGarance A Drosehn
245c547dbe8SGarance A Drosehn /*
246c547dbe8SGarance A Drosehn * If the filename was built with an IP number instead of a hostname,
247c547dbe8SGarance A Drosehn * then recalculate using only the first three digits found.
248c547dbe8SGarance A Drosehn */
249c547dbe8SGarance A Drosehn while(isdigitch(*cp))
250c547dbe8SGarance A Drosehn cp++;
251c547dbe8SGarance A Drosehn if (*cp == '.') {
252c547dbe8SGarance A Drosehn jnum = 0;
253c547dbe8SGarance A Drosehn for (cp = numstr; (cp < numstr + 3) && isdigitch(*cp); cp++)
254c547dbe8SGarance A Drosehn jnum = jnum * 10 + (*cp - '0');
255c547dbe8SGarance A Drosehn hoststr = cp;
256c547dbe8SGarance A Drosehn }
257c547dbe8SGarance A Drosehn if (hostpp != NULL)
258c547dbe8SGarance A Drosehn *hostpp = hoststr;
259c547dbe8SGarance A Drosehn return (jnum);
260c547dbe8SGarance A Drosehn }
261c547dbe8SGarance A Drosehn
2625458e2f4SJoerg Wunsch /* sleep n milliseconds */
2635458e2f4SJoerg Wunsch void
delay(int millisec)264ba7a1ad7SGarance A Drosehn delay(int millisec)
2655458e2f4SJoerg Wunsch {
2665458e2f4SJoerg Wunsch struct timeval tdelay;
2675458e2f4SJoerg Wunsch
268ba7a1ad7SGarance A Drosehn if (millisec <= 0 || millisec > 10000)
2694a1a0dbeSGarrett Wollman fatal((struct printer *)0, /* fatal() knows how to deal */
270ba7a1ad7SGarance A Drosehn "unreasonable delay period (%d)", millisec);
271ba7a1ad7SGarance A Drosehn tdelay.tv_sec = millisec / 1000;
272ba7a1ad7SGarance A Drosehn tdelay.tv_usec = millisec * 1000 % 1000000;
2735458e2f4SJoerg Wunsch (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay);
274dea673e9SRodney W. Grimes }
275dea673e9SRodney W. Grimes
2764a1a0dbeSGarrett Wollman char *
lock_file_name(const struct printer * pp,char * buf,size_t len)277ba7a1ad7SGarance A Drosehn lock_file_name(const struct printer *pp, char *buf, size_t len)
2784a1a0dbeSGarrett Wollman {
2794a1a0dbeSGarrett Wollman static char staticbuf[MAXPATHLEN];
2804a1a0dbeSGarrett Wollman
281bc17d12dSPedro F. Giffuni if (buf == NULL)
2824a1a0dbeSGarrett Wollman buf = staticbuf;
2834a1a0dbeSGarrett Wollman if (len == 0)
2844a1a0dbeSGarrett Wollman len = MAXPATHLEN;
2854a1a0dbeSGarrett Wollman
286a4ccd1dfSGarance A Drosehn if (pp->lock_file[0] == '/')
287a4ccd1dfSGarance A Drosehn strlcpy(buf, pp->lock_file, len);
288a4ccd1dfSGarance A Drosehn else
2894a1a0dbeSGarrett Wollman snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file);
290a4ccd1dfSGarance A Drosehn
2914a1a0dbeSGarrett Wollman return buf;
2924a1a0dbeSGarrett Wollman }
2934a1a0dbeSGarrett Wollman
2944a1a0dbeSGarrett Wollman char *
status_file_name(const struct printer * pp,char * buf,size_t len)295ba7a1ad7SGarance A Drosehn status_file_name(const struct printer *pp, char *buf, size_t len)
2964a1a0dbeSGarrett Wollman {
2974a1a0dbeSGarrett Wollman static char staticbuf[MAXPATHLEN];
2984a1a0dbeSGarrett Wollman
299bc17d12dSPedro F. Giffuni if (buf == NULL)
3004a1a0dbeSGarrett Wollman buf = staticbuf;
3014a1a0dbeSGarrett Wollman if (len == 0)
3024a1a0dbeSGarrett Wollman len = MAXPATHLEN;
3034a1a0dbeSGarrett Wollman
304a4ccd1dfSGarance A Drosehn if (pp->status_file[0] == '/')
305a4ccd1dfSGarance A Drosehn strlcpy(buf, pp->status_file, len);
306a4ccd1dfSGarance A Drosehn else
3074a1a0dbeSGarrett Wollman snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
308a4ccd1dfSGarance A Drosehn
3094a1a0dbeSGarrett Wollman return buf;
3104a1a0dbeSGarrett Wollman }
3114a1a0dbeSGarrett Wollman
312e8e715faSGarance A Drosehn /*
313e8e715faSGarance A Drosehn * Routine to change operational state of a print queue. The operational
3145e55dc17SGarance A Drosehn * state is indicated by the access bits on the lock file for the queue.
315e8e715faSGarance A Drosehn * At present, this is only called from various routines in lpc/cmds.c.
316e8e715faSGarance A Drosehn *
317e8e715faSGarance A Drosehn * XXX - Note that this works by changing access-bits on the
318e8e715faSGarance A Drosehn * file, and you can only do that if you are the owner of
319e8e715faSGarance A Drosehn * the file, or root. Thus, this won't really work for
320e8e715faSGarance A Drosehn * userids in the "LPR_OPER" group, unless lpc is running
321e8e715faSGarance A Drosehn * setuid to root (or maybe setuid to daemon).
322e8e715faSGarance A Drosehn * Generally lpc is installed setgid to daemon, but does
323e8e715faSGarance A Drosehn * not run setuid.
324e8e715faSGarance A Drosehn */
325e8e715faSGarance A Drosehn int
set_qstate(int action,const char * lfname)326e8e715faSGarance A Drosehn set_qstate(int action, const char *lfname)
327e8e715faSGarance A Drosehn {
328e8e715faSGarance A Drosehn struct stat stbuf;
329e8e715faSGarance A Drosehn mode_t chgbits, newbits, oldmask;
330e8e715faSGarance A Drosehn const char *failmsg, *okmsg;
3314c2a3991SGarance A Drosehn static const char *nomsg = "no state msg";
332e8e715faSGarance A Drosehn int chres, errsav, fd, res, statres;
333e8e715faSGarance A Drosehn
334e8e715faSGarance A Drosehn /*
335e8e715faSGarance A Drosehn * Find what the current access-bits are.
336e8e715faSGarance A Drosehn */
337e8e715faSGarance A Drosehn memset(&stbuf, 0, sizeof(stbuf));
3381d1d4a47SEitan Adler PRIV_START
339e8e715faSGarance A Drosehn statres = stat(lfname, &stbuf);
340e8e715faSGarance A Drosehn errsav = errno;
3411d1d4a47SEitan Adler PRIV_END
342e8e715faSGarance A Drosehn if ((statres < 0) && (errsav != ENOENT)) {
343e8e715faSGarance A Drosehn printf("\tcannot stat() lock file\n");
344e8e715faSGarance A Drosehn return (SQS_STATFAIL);
345e8e715faSGarance A Drosehn /* NOTREACHED */
346e8e715faSGarance A Drosehn }
347e8e715faSGarance A Drosehn
348e8e715faSGarance A Drosehn /*
349e8e715faSGarance A Drosehn * Determine which bit(s) should change for the requested action.
350e8e715faSGarance A Drosehn */
351e8e715faSGarance A Drosehn chgbits = stbuf.st_mode;
352e8e715faSGarance A Drosehn newbits = LOCK_FILE_MODE;
353e8e715faSGarance A Drosehn okmsg = NULL;
354e8e715faSGarance A Drosehn failmsg = NULL;
3554c2a3991SGarance A Drosehn if (action & SQS_QCHANGED) {
3564c2a3991SGarance A Drosehn chgbits |= LFM_RESET_QUE;
3574c2a3991SGarance A Drosehn newbits |= LFM_RESET_QUE;
3584c2a3991SGarance A Drosehn /* The okmsg is not actually printed for this case. */
3594c2a3991SGarance A Drosehn okmsg = nomsg;
3604c2a3991SGarance A Drosehn failmsg = "set queue-changed";
3614c2a3991SGarance A Drosehn }
362e8e715faSGarance A Drosehn if (action & SQS_DISABLEQ) {
363e8e715faSGarance A Drosehn chgbits |= LFM_QUEUE_DIS;
364e8e715faSGarance A Drosehn newbits |= LFM_QUEUE_DIS;
365e8e715faSGarance A Drosehn okmsg = "queuing disabled";
366e8e715faSGarance A Drosehn failmsg = "disable queuing";
367e8e715faSGarance A Drosehn }
368e8e715faSGarance A Drosehn if (action & SQS_STOPP) {
369e8e715faSGarance A Drosehn chgbits |= LFM_PRINT_DIS;
370e8e715faSGarance A Drosehn newbits |= LFM_PRINT_DIS;
371e8e715faSGarance A Drosehn okmsg = "printing disabled";
372e8e715faSGarance A Drosehn failmsg = "disable printing";
373e8e715faSGarance A Drosehn if (action & SQS_DISABLEQ) {
374e8e715faSGarance A Drosehn okmsg = "printer and queuing disabled";
375e8e715faSGarance A Drosehn failmsg = "disable queuing and printing";
376e8e715faSGarance A Drosehn }
377e8e715faSGarance A Drosehn }
378e8e715faSGarance A Drosehn if (action & SQS_ENABLEQ) {
379e8e715faSGarance A Drosehn chgbits &= ~LFM_QUEUE_DIS;
380e8e715faSGarance A Drosehn newbits &= ~LFM_QUEUE_DIS;
381e8e715faSGarance A Drosehn okmsg = "queuing enabled";
382e8e715faSGarance A Drosehn failmsg = "enable queuing";
383e8e715faSGarance A Drosehn }
384e8e715faSGarance A Drosehn if (action & SQS_STARTP) {
385e8e715faSGarance A Drosehn chgbits &= ~LFM_PRINT_DIS;
386e8e715faSGarance A Drosehn newbits &= ~LFM_PRINT_DIS;
387e8e715faSGarance A Drosehn okmsg = "printing enabled";
388e8e715faSGarance A Drosehn failmsg = "enable printing";
389e8e715faSGarance A Drosehn }
390e8e715faSGarance A Drosehn if (okmsg == NULL) {
391e8e715faSGarance A Drosehn /* This routine was called with an invalid action. */
392e8e715faSGarance A Drosehn printf("\t<error in set_qstate!>\n");
393e8e715faSGarance A Drosehn return (SQS_PARMERR);
394e8e715faSGarance A Drosehn /* NOTREACHED */
395e8e715faSGarance A Drosehn }
396e8e715faSGarance A Drosehn
397e8e715faSGarance A Drosehn res = 0;
398e8e715faSGarance A Drosehn if (statres >= 0) {
399e8e715faSGarance A Drosehn /* The file already exists, so change the access. */
4001d1d4a47SEitan Adler PRIV_START
401e8e715faSGarance A Drosehn chres = chmod(lfname, chgbits);
402e8e715faSGarance A Drosehn errsav = errno;
4031d1d4a47SEitan Adler PRIV_END
404e8e715faSGarance A Drosehn res = SQS_CHGOK;
405722915cdSGarance A Drosehn if (chres < 0)
406e8e715faSGarance A Drosehn res = SQS_CHGFAIL;
407e8e715faSGarance A Drosehn } else if (newbits == LOCK_FILE_MODE) {
408e8e715faSGarance A Drosehn /*
409e8e715faSGarance A Drosehn * The file does not exist, but the state requested is
410e8e715faSGarance A Drosehn * the same as the default state when no file exists.
411e8e715faSGarance A Drosehn * Thus, there is no need to create the file.
412e8e715faSGarance A Drosehn */
413e8e715faSGarance A Drosehn res = SQS_SKIPCREOK;
414e8e715faSGarance A Drosehn } else {
415e8e715faSGarance A Drosehn /*
416e8e715faSGarance A Drosehn * The file did not exist, so create it with the
417e8e715faSGarance A Drosehn * appropriate access bits for the requested action.
418e8e715faSGarance A Drosehn * Push a new umask around that create, to make sure
419e8e715faSGarance A Drosehn * all the read/write bits are set as desired.
420e8e715faSGarance A Drosehn */
421e8e715faSGarance A Drosehn oldmask = umask(S_IWOTH);
4221d1d4a47SEitan Adler PRIV_START
423e8e715faSGarance A Drosehn fd = open(lfname, O_WRONLY|O_CREAT, newbits);
424e8e715faSGarance A Drosehn errsav = errno;
4251d1d4a47SEitan Adler PRIV_END
426e8e715faSGarance A Drosehn umask(oldmask);
427e8e715faSGarance A Drosehn res = SQS_CREFAIL;
428e8e715faSGarance A Drosehn if (fd >= 0) {
429e8e715faSGarance A Drosehn res = SQS_CREOK;
430e8e715faSGarance A Drosehn close(fd);
431e8e715faSGarance A Drosehn }
432e8e715faSGarance A Drosehn }
433e8e715faSGarance A Drosehn
434e8e715faSGarance A Drosehn switch (res) {
435e8e715faSGarance A Drosehn case SQS_CHGOK:
436e8e715faSGarance A Drosehn case SQS_CREOK:
437e8e715faSGarance A Drosehn case SQS_SKIPCREOK:
4384c2a3991SGarance A Drosehn if (okmsg != nomsg)
439e8e715faSGarance A Drosehn printf("\t%s\n", okmsg);
440e8e715faSGarance A Drosehn break;
441e8e715faSGarance A Drosehn case SQS_CREFAIL:
442e8e715faSGarance A Drosehn printf("\tcannot create lock file: %s\n",
443e8e715faSGarance A Drosehn strerror(errsav));
444e8e715faSGarance A Drosehn break;
445e8e715faSGarance A Drosehn default:
446e8e715faSGarance A Drosehn printf("\tcannot %s: %s\n", failmsg, strerror(errsav));
447e8e715faSGarance A Drosehn break;
448e8e715faSGarance A Drosehn }
449e8e715faSGarance A Drosehn
450e8e715faSGarance A Drosehn return (res);
451e8e715faSGarance A Drosehn }
452e8e715faSGarance A Drosehn
4536522ebecSGarance A Drosehn /* routine to get a current timestamp, optionally in a standard-fmt string */
4546522ebecSGarance A Drosehn void
lpd_gettime(struct timespec * tsp,char * strp,size_t strsize)455a4ccd1dfSGarance A Drosehn lpd_gettime(struct timespec *tsp, char *strp, size_t strsize)
4566522ebecSGarance A Drosehn {
4576522ebecSGarance A Drosehn struct timespec local_ts;
4586522ebecSGarance A Drosehn struct timeval btime;
4596522ebecSGarance A Drosehn char tempstr[TIMESTR_SIZE];
460011b9c79SGarance A Drosehn #ifdef STRFTIME_WRONG_z
461011b9c79SGarance A Drosehn char *destp;
462011b9c79SGarance A Drosehn #endif
4636522ebecSGarance A Drosehn
4646522ebecSGarance A Drosehn if (tsp == NULL)
4656522ebecSGarance A Drosehn tsp = &local_ts;
4666522ebecSGarance A Drosehn
4676522ebecSGarance A Drosehn /* some platforms have a routine called clock_gettime, but the
4686522ebecSGarance A Drosehn * routine does nothing but return "not implemented". */
4696522ebecSGarance A Drosehn memset(tsp, 0, sizeof(struct timespec));
4706522ebecSGarance A Drosehn if (clock_gettime(CLOCK_REALTIME, tsp)) {
4716522ebecSGarance A Drosehn /* nanosec-aware rtn failed, fall back to microsec-aware rtn */
4726522ebecSGarance A Drosehn memset(tsp, 0, sizeof(struct timespec));
4736522ebecSGarance A Drosehn gettimeofday(&btime, NULL);
4746522ebecSGarance A Drosehn tsp->tv_sec = btime.tv_sec;
4756522ebecSGarance A Drosehn tsp->tv_nsec = btime.tv_usec * 1000;
4766522ebecSGarance A Drosehn }
4776522ebecSGarance A Drosehn
4786522ebecSGarance A Drosehn /* caller may not need a character-ized version */
4796522ebecSGarance A Drosehn if ((strp == NULL) || (strsize < 1))
4806522ebecSGarance A Drosehn return;
4816522ebecSGarance A Drosehn
4826522ebecSGarance A Drosehn strftime(tempstr, TIMESTR_SIZE, LPD_TIMESTAMP_PATTERN,
4836522ebecSGarance A Drosehn localtime(&tsp->tv_sec));
4846522ebecSGarance A Drosehn
4856522ebecSGarance A Drosehn /*
4866522ebecSGarance A Drosehn * This check is for implementations of strftime which treat %z
4876522ebecSGarance A Drosehn * (timezone as [+-]hhmm ) like %Z (timezone as characters), or
4886522ebecSGarance A Drosehn * completely ignore %z. This section is not needed on freebsd.
4896522ebecSGarance A Drosehn * I'm not sure this is completely right, but it should work OK
4906522ebecSGarance A Drosehn * for EST and EDT...
4916522ebecSGarance A Drosehn */
4926522ebecSGarance A Drosehn #ifdef STRFTIME_WRONG_z
4936522ebecSGarance A Drosehn destp = strrchr(tempstr, ':');
4946522ebecSGarance A Drosehn if (destp != NULL) {
4956522ebecSGarance A Drosehn destp += 3;
4966522ebecSGarance A Drosehn if ((*destp != '+') && (*destp != '-')) {
4976522ebecSGarance A Drosehn char savday[6];
4986522ebecSGarance A Drosehn int tzmin = timezone / 60;
4996522ebecSGarance A Drosehn int tzhr = tzmin / 60;
5006522ebecSGarance A Drosehn if (daylight)
5016522ebecSGarance A Drosehn tzhr--;
5026522ebecSGarance A Drosehn strcpy(savday, destp + strlen(destp) - 4);
5036522ebecSGarance A Drosehn snprintf(destp, (destp - tempstr), "%+03d%02d",
5046522ebecSGarance A Drosehn (-1*tzhr), tzmin % 60);
5056522ebecSGarance A Drosehn strcat(destp, savday);
5066522ebecSGarance A Drosehn }
5076522ebecSGarance A Drosehn }
5086522ebecSGarance A Drosehn #endif
5096522ebecSGarance A Drosehn
5106522ebecSGarance A Drosehn if (strsize > TIMESTR_SIZE) {
5116522ebecSGarance A Drosehn strsize = TIMESTR_SIZE;
5126522ebecSGarance A Drosehn strp[TIMESTR_SIZE+1] = '\0';
5136522ebecSGarance A Drosehn }
514a4ccd1dfSGarance A Drosehn strlcpy(strp, tempstr, strsize);
5156522ebecSGarance A Drosehn }
5166522ebecSGarance A Drosehn
5176522ebecSGarance A Drosehn /* routines for writing transfer-statistic records */
5186522ebecSGarance A Drosehn void
trstat_init(struct printer * pp,const char * fname,int filenum)519ba7a1ad7SGarance A Drosehn trstat_init(struct printer *pp, const char *fname, int filenum)
5206522ebecSGarance A Drosehn {
5216522ebecSGarance A Drosehn register const char *srcp;
5226522ebecSGarance A Drosehn register char *destp, *endp;
5236522ebecSGarance A Drosehn
5246522ebecSGarance A Drosehn /*
5256522ebecSGarance A Drosehn * Figure out the job id of this file. The filename should be
5266522ebecSGarance A Drosehn * 'cf', 'df', or maybe 'tf', followed by a letter (or sometimes
5276522ebecSGarance A Drosehn * two), followed by the jobnum, followed by a hostname.
5286522ebecSGarance A Drosehn * The jobnum is usually 3 digits, but might be as many as 5.
5296522ebecSGarance A Drosehn * Note that some care has to be taken parsing this, as the
5306522ebecSGarance A Drosehn * filename could be coming from a remote-host, and thus might
5316522ebecSGarance A Drosehn * not look anything like what is expected...
5326522ebecSGarance A Drosehn */
5336522ebecSGarance A Drosehn memset(pp->jobnum, 0, sizeof(pp->jobnum));
5346522ebecSGarance A Drosehn pp->jobnum[0] = '0';
5356522ebecSGarance A Drosehn srcp = strchr(fname, '/');
5366522ebecSGarance A Drosehn if (srcp == NULL)
5376522ebecSGarance A Drosehn srcp = fname;
5386522ebecSGarance A Drosehn destp = &(pp->jobnum[0]);
5396522ebecSGarance A Drosehn endp = destp + 5;
5406522ebecSGarance A Drosehn while (*srcp != '\0' && (*srcp < '0' || *srcp > '9'))
5416522ebecSGarance A Drosehn srcp++;
5426522ebecSGarance A Drosehn while (*srcp >= '0' && *srcp <= '9' && destp < endp)
5436522ebecSGarance A Drosehn *(destp++) = *(srcp++);
5446522ebecSGarance A Drosehn
5456522ebecSGarance A Drosehn /* get the starting time in both numeric and string formats, and
5466522ebecSGarance A Drosehn * save those away along with the file-number */
5476522ebecSGarance A Drosehn pp->jobdfnum = filenum;
548a4ccd1dfSGarance A Drosehn lpd_gettime(&pp->tr_start, pp->tr_timestr, (size_t)TIMESTR_SIZE);
5496522ebecSGarance A Drosehn }
5506522ebecSGarance A Drosehn
5516522ebecSGarance A Drosehn void
trstat_write(struct printer * pp,tr_sendrecv sendrecv,size_t bytecnt,const char * userid,const char * otherhost,const char * orighost)552ba7a1ad7SGarance A Drosehn trstat_write(struct printer *pp, tr_sendrecv sendrecv, size_t bytecnt,
553ba7a1ad7SGarance A Drosehn const char *userid, const char *otherhost, const char *orighost)
5546522ebecSGarance A Drosehn {
5556522ebecSGarance A Drosehn #define STATLINE_SIZE 1024
5566522ebecSGarance A Drosehn double trtime;
557011b9c79SGarance A Drosehn size_t remspace;
558011b9c79SGarance A Drosehn int statfile;
559b770f354SGarance A Drosehn char thishost[MAXHOSTNAMELEN], statline[STATLINE_SIZE];
5606522ebecSGarance A Drosehn char *eostat;
561b770f354SGarance A Drosehn const char *lprhost, *recvdev, *recvhost, *rectype;
562b770f354SGarance A Drosehn const char *sendhost, *statfname;
5636522ebecSGarance A Drosehn #define UPD_EOSTAT(xStr) do { \
5646522ebecSGarance A Drosehn eostat = strchr(xStr, '\0'); \
5656522ebecSGarance A Drosehn remspace = eostat - xStr; \
5666522ebecSGarance A Drosehn } while(0)
5676522ebecSGarance A Drosehn
568a4ccd1dfSGarance A Drosehn lpd_gettime(&pp->tr_done, NULL, (size_t)0);
5696522ebecSGarance A Drosehn trtime = DIFFTIME_TS(pp->tr_done, pp->tr_start);
5706522ebecSGarance A Drosehn
5716522ebecSGarance A Drosehn gethostname(thishost, sizeof(thishost));
5726522ebecSGarance A Drosehn lprhost = sendhost = recvhost = recvdev = NULL;
5736522ebecSGarance A Drosehn switch (sendrecv) {
5746522ebecSGarance A Drosehn case TR_SENDING:
5756522ebecSGarance A Drosehn rectype = "send";
5766522ebecSGarance A Drosehn statfname = pp->stat_send;
5776522ebecSGarance A Drosehn sendhost = thishost;
5786522ebecSGarance A Drosehn recvhost = otherhost;
5796522ebecSGarance A Drosehn break;
5806522ebecSGarance A Drosehn case TR_RECVING:
5816522ebecSGarance A Drosehn rectype = "recv";
5826522ebecSGarance A Drosehn statfname = pp->stat_recv;
5836522ebecSGarance A Drosehn sendhost = otherhost;
5846522ebecSGarance A Drosehn recvhost = thishost;
5856522ebecSGarance A Drosehn break;
5866522ebecSGarance A Drosehn case TR_PRINTING:
587b770f354SGarance A Drosehn /*
588b770f354SGarance A Drosehn * This case is for copying to a device (presumably local,
589b770f354SGarance A Drosehn * though filters using things like 'net/CAP' can confuse
590b770f354SGarance A Drosehn * this assumption...).
591b770f354SGarance A Drosehn */
5926522ebecSGarance A Drosehn rectype = "prnt";
5936522ebecSGarance A Drosehn statfname = pp->stat_send;
5946522ebecSGarance A Drosehn sendhost = thishost;
5956522ebecSGarance A Drosehn recvdev = _PATH_DEFDEVLP;
5966522ebecSGarance A Drosehn if (pp->lp) recvdev = pp->lp;
5976522ebecSGarance A Drosehn break;
5986522ebecSGarance A Drosehn default:
5996522ebecSGarance A Drosehn /* internal error... should we syslog/printf an error? */
6006522ebecSGarance A Drosehn return;
6016522ebecSGarance A Drosehn }
602b770f354SGarance A Drosehn if (statfname == NULL)
603b770f354SGarance A Drosehn return;
6046522ebecSGarance A Drosehn
6056522ebecSGarance A Drosehn /*
6066522ebecSGarance A Drosehn * the original-host and userid are found out by reading thru the
6076522ebecSGarance A Drosehn * cf (control-file) for the job. Unfortunately, on incoming jobs
6086522ebecSGarance A Drosehn * the df's (data-files) are sent before the matching cf, so the
6096522ebecSGarance A Drosehn * orighost & userid are generally not-available for incoming jobs.
6106522ebecSGarance A Drosehn *
6116522ebecSGarance A Drosehn * (it would be nice to create a work-around for that..)
6126522ebecSGarance A Drosehn */
6136522ebecSGarance A Drosehn if (orighost && (*orighost != '\0'))
6146522ebecSGarance A Drosehn lprhost = orighost;
6156522ebecSGarance A Drosehn else
6166522ebecSGarance A Drosehn lprhost = ".na.";
617b770f354SGarance A Drosehn if (*userid == '\0')
618b770f354SGarance A Drosehn userid = NULL;
6196522ebecSGarance A Drosehn
6206522ebecSGarance A Drosehn /*
6216522ebecSGarance A Drosehn * Format of statline.
6226522ebecSGarance A Drosehn * Some of the keywords listed here are not implemented here, but
6236522ebecSGarance A Drosehn * they are listed to reserve the meaning for a given keyword.
6246522ebecSGarance A Drosehn * Fields are separated by a blank. The fields in statline are:
6256522ebecSGarance A Drosehn * <tstamp> - time the transfer started
6266522ebecSGarance A Drosehn * <ptrqueue> - name of the printer queue (the short-name...)
6276522ebecSGarance A Drosehn * <hname> - hostname the file originally came from (the
6286522ebecSGarance A Drosehn * 'lpr host'), if known, or "_na_" if not known.
6296522ebecSGarance A Drosehn * <xxx> - id of job from that host (generally three digits)
6306522ebecSGarance A Drosehn * <n> - file count (# of file within job)
6316522ebecSGarance A Drosehn * <rectype> - 4-byte field indicating the type of transfer
6326522ebecSGarance A Drosehn * statistics record. "send" means it's from the
6336522ebecSGarance A Drosehn * host sending a datafile, "recv" means it's from
6346522ebecSGarance A Drosehn * a host as it receives a datafile.
6356522ebecSGarance A Drosehn * user=<userid> - user who sent the job (if known)
6366522ebecSGarance A Drosehn * secs=<n> - seconds it took to transfer the file
637b5635ba0SPedro F. Giffuni * bytes=<n> - number of bytes transferred (ie, "bytecount")
6386522ebecSGarance A Drosehn * bps=<n.n>e<n> - Bytes/sec (if the transfer was "big enough"
6396522ebecSGarance A Drosehn * for this to be useful)
6406522ebecSGarance A Drosehn * ! top=<str> - type of printer (if the type is defined in
6416522ebecSGarance A Drosehn * printcap, and if this statline is for sending
6426522ebecSGarance A Drosehn * a file to that ptr)
6436522ebecSGarance A Drosehn * ! qls=<n> - queue-length at start of send/print-ing a job
6446522ebecSGarance A Drosehn * ! qle=<n> - queue-length at end of send/print-ing a job
6456522ebecSGarance A Drosehn * sip=<addr> - IP address of sending host, only included when
6466522ebecSGarance A Drosehn * receiving a job.
6476522ebecSGarance A Drosehn * shost=<hname> - sending host (if that does != the original host)
6486522ebecSGarance A Drosehn * rhost=<hname> - hostname receiving the file (ie, "destination")
6496522ebecSGarance A Drosehn * rdev=<dev> - device receiving the file, when the file is being
6506522ebecSGarance A Drosehn * send to a device instead of a remote host.
6516522ebecSGarance A Drosehn *
6526522ebecSGarance A Drosehn * Note: A single print job may be transferred multiple times. The
6536522ebecSGarance A Drosehn * original 'lpr' occurs on one host, and that original host might
6546522ebecSGarance A Drosehn * send to some interim host (or print server). That interim host
6556522ebecSGarance A Drosehn * might turn around and send the job to yet another host (most likely
6566522ebecSGarance A Drosehn * the real printer). The 'shost=' parameter is only included if the
6576522ebecSGarance A Drosehn * sending host for this particular transfer is NOT the same as the
6586522ebecSGarance A Drosehn * host which did the original 'lpr'.
6596522ebecSGarance A Drosehn *
6606522ebecSGarance A Drosehn * Many values have 'something=' tags before them, because they are
6616522ebecSGarance A Drosehn * in some sense "optional", or their order may vary. "Optional" may
6626522ebecSGarance A Drosehn * mean in the sense that different SITES might choose to have other
6636522ebecSGarance A Drosehn * fields in the record, or that some fields are only included under
6646522ebecSGarance A Drosehn * some circumstances. Programs processing these records should not
6656522ebecSGarance A Drosehn * assume the order or existence of any of these keyword fields.
6666522ebecSGarance A Drosehn */
6676522ebecSGarance A Drosehn snprintf(statline, STATLINE_SIZE, "%s %s %s %s %03ld %s",
668b770f354SGarance A Drosehn pp->tr_timestr, pp->printer, lprhost, pp->jobnum,
669b770f354SGarance A Drosehn pp->jobdfnum, rectype);
6706522ebecSGarance A Drosehn UPD_EOSTAT(statline);
6716522ebecSGarance A Drosehn
6726522ebecSGarance A Drosehn if (userid != NULL) {
6736522ebecSGarance A Drosehn snprintf(eostat, remspace, " user=%s", userid);
6746522ebecSGarance A Drosehn UPD_EOSTAT(statline);
6756522ebecSGarance A Drosehn }
676011b9c79SGarance A Drosehn snprintf(eostat, remspace, " secs=%#.2f bytes=%lu", trtime,
677011b9c79SGarance A Drosehn (unsigned long)bytecnt);
6786522ebecSGarance A Drosehn UPD_EOSTAT(statline);
6796522ebecSGarance A Drosehn
680b770f354SGarance A Drosehn /*
681b770f354SGarance A Drosehn * The bps field duplicates info from bytes and secs, so do
682b770f354SGarance A Drosehn * not bother to include it for very small files.
683b770f354SGarance A Drosehn */
6846522ebecSGarance A Drosehn if ((bytecnt > 25000) && (trtime > 1.1)) {
6856522ebecSGarance A Drosehn snprintf(eostat, remspace, " bps=%#.2e",
6866522ebecSGarance A Drosehn ((double)bytecnt/trtime));
6876522ebecSGarance A Drosehn UPD_EOSTAT(statline);
6886522ebecSGarance A Drosehn }
6896522ebecSGarance A Drosehn
6906522ebecSGarance A Drosehn if (sendrecv == TR_RECVING) {
6916522ebecSGarance A Drosehn if (remspace > 5+strlen(from_ip) ) {
6926522ebecSGarance A Drosehn snprintf(eostat, remspace, " sip=%s", from_ip);
6936522ebecSGarance A Drosehn UPD_EOSTAT(statline);
6946522ebecSGarance A Drosehn }
6956522ebecSGarance A Drosehn }
6966522ebecSGarance A Drosehn if (0 != strcmp(lprhost, sendhost)) {
6976522ebecSGarance A Drosehn if (remspace > 7+strlen(sendhost) ) {
6986522ebecSGarance A Drosehn snprintf(eostat, remspace, " shost=%s", sendhost);
6996522ebecSGarance A Drosehn UPD_EOSTAT(statline);
7006522ebecSGarance A Drosehn }
7016522ebecSGarance A Drosehn }
7026522ebecSGarance A Drosehn if (recvhost) {
7036522ebecSGarance A Drosehn if (remspace > 7+strlen(recvhost) ) {
7046522ebecSGarance A Drosehn snprintf(eostat, remspace, " rhost=%s", recvhost);
7056522ebecSGarance A Drosehn UPD_EOSTAT(statline);
7066522ebecSGarance A Drosehn }
7076522ebecSGarance A Drosehn }
7086522ebecSGarance A Drosehn if (recvdev) {
7096522ebecSGarance A Drosehn if (remspace > 6+strlen(recvdev) ) {
7106522ebecSGarance A Drosehn snprintf(eostat, remspace, " rdev=%s", recvdev);
7116522ebecSGarance A Drosehn UPD_EOSTAT(statline);
7126522ebecSGarance A Drosehn }
7136522ebecSGarance A Drosehn }
7146522ebecSGarance A Drosehn if (remspace > 1) {
7156522ebecSGarance A Drosehn strcpy(eostat, "\n");
7166522ebecSGarance A Drosehn } else {
7176522ebecSGarance A Drosehn /* probably should back up to just before the final " x=".. */
7186522ebecSGarance A Drosehn strcpy(statline+STATLINE_SIZE-2, "\n");
7196522ebecSGarance A Drosehn }
7206522ebecSGarance A Drosehn statfile = open(statfname, O_WRONLY|O_APPEND, 0664);
7216522ebecSGarance A Drosehn if (statfile < 0) {
7226522ebecSGarance A Drosehn /* statfile was given, but we can't open it. should we
7236522ebecSGarance A Drosehn * syslog/printf this as an error? */
7246522ebecSGarance A Drosehn return;
7256522ebecSGarance A Drosehn }
7266522ebecSGarance A Drosehn write(statfile, statline, strlen(statline));
7276522ebecSGarance A Drosehn close(statfile);
7286522ebecSGarance A Drosehn
7296522ebecSGarance A Drosehn return;
7306522ebecSGarance A Drosehn #undef UPD_EOSTAT
7316522ebecSGarance A Drosehn }
7326522ebecSGarance A Drosehn
733dea673e9SRodney W. Grimes #include <stdarg.h>
734dea673e9SRodney W. Grimes
735dea673e9SRodney W. Grimes void
fatal(const struct printer * pp,const char * msg,...)7364a1a0dbeSGarrett Wollman fatal(const struct printer *pp, const char *msg, ...)
737dea673e9SRodney W. Grimes {
738dea673e9SRodney W. Grimes va_list ap;
739dea673e9SRodney W. Grimes va_start(ap, msg);
740cc3fd56fSGarance A Drosehn /* this error message is being sent to the 'from_host' */
741cc3fd56fSGarance A Drosehn if (from_host != local_host)
742cc3fd56fSGarance A Drosehn (void)printf("%s: ", local_host);
74331058a75SGarance A Drosehn (void)printf("%s: ", progname);
7444a1a0dbeSGarrett Wollman if (pp && pp->printer)
7454a1a0dbeSGarrett Wollman (void)printf("%s: ", pp->printer);
746dea673e9SRodney W. Grimes (void)vprintf(msg, ap);
747dea673e9SRodney W. Grimes va_end(ap);
748dea673e9SRodney W. Grimes (void)putchar('\n');
749dea673e9SRodney W. Grimes exit(1);
750dea673e9SRodney W. Grimes }
7514a1a0dbeSGarrett Wollman
7524a1a0dbeSGarrett Wollman /*
7534a1a0dbeSGarrett Wollman * Close all file descriptors from START on up.
7544a1a0dbeSGarrett Wollman */
7554a1a0dbeSGarrett Wollman void
closeallfds(int start)756ba7a1ad7SGarance A Drosehn closeallfds(int start)
7574a1a0dbeSGarrett Wollman {
758137076c5SGarance A Drosehn int stop;
759137076c5SGarance A Drosehn
760137076c5SGarance A Drosehn if (USE_CLOSEFROM) /* The faster, modern solution */
761137076c5SGarance A Drosehn closefrom(start);
762137076c5SGarance A Drosehn else {
763137076c5SGarance A Drosehn /* This older logic can be pretty awful on some OS's. The
764137076c5SGarance A Drosehn * getdtablesize() might return ``infinity'', and then this
765137076c5SGarance A Drosehn * will waste a lot of time closing file descriptors which
766137076c5SGarance A Drosehn * had never been open()-ed. */
767137076c5SGarance A Drosehn stop = getdtablesize();
7684a1a0dbeSGarrett Wollman for (; start < stop; start++)
7694a1a0dbeSGarrett Wollman close(start);
7704a1a0dbeSGarrett Wollman }
771137076c5SGarance A Drosehn }
7724a1a0dbeSGarrett Wollman
773