2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95";
45 static const char rcsid[] =
50 * Receive printer jobs from the network, queue them and
51 * start the printer daemon.
53 #include <sys/param.h>
54 #include <sys/mount.h>
68 #include "pathnames.h"
70 #define ack() (void) write(1, sp, 1);
72 static char dfname[NAME_MAX]; /* data files */
73 static int minfree; /* keep at least minfree blocks available */
75 static char tfname[NAME_MAX]; /* tmp copy of cf before linking */
77 static int chksize __P((int));
78 static void frecverr __P((const char *, ...));
79 static int noresponse __P((void));
80 static void rcleanup __P((int));
81 static int read_number __P((char *));
82 static int readfile __P((char *, int));
83 static int readjob __P((struct printer *pp));
92 struct printer myprinter, *pp = &myprinter;
95 * Perform lookup for printer name or abbreviation
98 status = getprintcap(printer, pp);
101 frecverr("cannot open printer description file");
103 case PCAPERR_NOTFOUND:
104 frecverr("unknown printer %s", printer);
107 fatal(pp, "potential reference loop detected in printcap file");
112 (void) close(2); /* set up log file */
113 if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) {
114 syslog(LOG_ERR, "%s: %m", pp->log_file);
115 (void) open(_PATH_DEVNULL, O_WRONLY);
118 if (chdir(pp->spool_dir) < 0)
119 frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
120 if (stat(pp->lock_file, &stb) == 0) {
121 if (stb.st_mode & 010) {
122 /* queue is disabled */
123 putchar('\1'); /* return error code */
126 } else if (stat(pp->spool_dir, &stb) < 0)
127 frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
128 minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */
129 signal(SIGTERM, rcleanup);
130 signal(SIGPIPE, rcleanup);
137 * Read printer jobs sent by lpd and copy them to the spooling directory.
138 * Return the number of jobs successfully transfered.
144 register int size, nfiles;
151 * Read a command to tell us what to do
155 if ((size = read(1, cp, 1)) != 1) {
157 frecverr("%s: lost connection",
161 } while (*cp++ != '\n' && (cp - line + 1) < sizeof(line));
162 if (cp - line + 1 >= sizeof(line))
163 frecverr("readjob overflow");
167 case '\1': /* cleanup because data sent was bad */
171 case '\2': /* read cf file */
173 while (*cp >= '0' && *cp <= '9')
174 size = size * 10 + (*cp++ - '0');
178 * host name has been authenticated, we use our
179 * view of the host name since we may be passed
180 * something different than what gethostbyaddr()
183 strncpy(cp + 6, from, sizeof(line) + line - cp - 7);
184 line[sizeof(line) - 1 ] = '\0';
185 strncpy(tfname, cp, sizeof(tfname) - 1);
186 tfname[sizeof (tfname) - 1] = '\0';
188 if (strchr(tfname, '/'))
189 frecverr("readjob: %s: illegal path name",
191 if (!chksize(size)) {
192 (void) write(1, "\2", 1);
195 if (!readfile(tfname, size)) {
199 if (link(tfname, cp) < 0)
200 frecverr("%s: %m", tfname);
201 (void) unlink(tfname);
206 case '\3': /* read df file */
208 while (*cp >= '0' && *cp <= '9')
209 size = size * 10 + (*cp++ - '0');
212 if (!chksize(size)) {
213 (void) write(1, "\2", 1);
216 (void) strncpy(dfname, cp, sizeof(dfname) - 1);
217 dfname[sizeof(dfname) - 1] = '\0';
218 if (strchr(dfname, '/'))
219 frecverr("readjob: %s: illegal path name",
221 (void) readfile(dfname, size);
224 frecverr("protocol screwup: %s", line);
229 * Read files send by lpd and copy them to the spooling directory.
238 register int i, j, amt;
241 fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
243 frecverr("readfile: %s: illegal path name: %m", file);
246 for (i = 0; i < size; i += BUFSIZ) {
252 j = read(1, cp, amt);
254 frecverr("lost connection");
261 if (write(fd, buf, amt) != amt) {
268 frecverr("%s: write error", file);
269 if (noresponse()) { /* file sent had bad data in it */
270 if (strchr(file, '/') == NULL)
283 if (read(1, &resp, 1) != 1)
284 frecverr("lost connection");
291 * Check to see if there is enough space on the disk for size bytes.
292 * 1 == OK, 0 == Not OK.
301 if (statfs(".", &sfb) < 0) {
302 syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
305 spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
306 size = (size + 511) / 512;
307 if (minfree + size > spacefree)
319 if ((fp = fopen(fn, "r")) == NULL)
321 if (fgets(lin, 80, fp) == NULL) {
330 * Remove all the files associated with the current job being transfered.
336 if (tfname[0] && strchr(tfname, '/') == NULL)
337 (void) unlink(tfname);
338 if (dfname[0] && strchr(dfname, '/') == NULL) {
341 (void) unlink(dfname);
342 while (dfname[2]-- != 'A');
344 } while (dfname[0]-- != 'd');
357 frecverr(const char *msg, ...)
359 frecverr(msg, va_alist)
371 syslog(LOG_ERR, "%s", fromb);
372 vsyslog(LOG_ERR, msg, ap);
374 putchar('\1'); /* return error code */