xref: /netbsd-src/usr.sbin/lpr/common_source/displayq.c (revision 7027866a09786f44d29080aa3a48badc640ddfe2)
1 /*	$NetBSD: displayq.c,v 1.34 2009/07/13 19:05:41 roy Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)displayq.c	8.4 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: displayq.c,v 1.34 2009/07/13 19:05:41 roy Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <dirent.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <ctype.h>
53 #include "lp.h"
54 #include "lp.local.h"
55 #include "pathnames.h"
56 
57 /*
58  * Routines to display the state of the queue.
59  */
60 #define JOBCOL	40		/* column for job # in -l format */
61 #define OWNCOL	7		/* start of Owner column in normal */
62 #define SIZCOL	62		/* start of Size column in normal */
63 
64 /*
65  * Stuff for handling job specifications
66  */
67 extern int	requ[];		/* job number of spool entries */
68 extern int	requests;	/* # of spool requests */
69 extern char    *user[];	        /* users to process */
70 extern int	users;		/* # of users in user array */
71 
72 extern uid_t	uid, euid;
73 
74 static int	col;		/* column on screen */
75 static char	current[MAXPATHLEN]; /* current file being printed */
76 static char	fname[MAXPATHLEN]; /* print file name */
77 static int	first;		/* first file in ``files'' column? */
78 static int	garbage;	/* # of garbage cf files */
79 static int	lflag;		/* long output option */
80 static int	rank;		/* order to be printed (-1=none, 0=active) */
81 static long	totsize;	/* total print job size in bytes */
82 
83 static const char head0[] = "Rank   Owner      Job  Files";
84 static const char head1[] = "Total Size\n";
85 
86 static	void	alarmer(int);
87 
88 int wait_time = 300;	/* time out after 5 minutes by default */
89 
90 /*
91  * Display the current state of the queue. Format = 1 if long format.
92  */
93 void
displayq(int format)94 displayq(int format)
95 {
96 	struct queue *q;
97 	int i, nitems, fd, ret;
98 	char *cp, *ecp;
99 	struct queue **queue;
100 	struct stat statb;
101 	FILE *fp;
102 
103 	lflag = format;
104 	totsize = 0;
105 	rank = -1;
106 	getprintcap(printer);
107 
108 	/*
109 	 * Print out local queue
110 	 * Find all the control files in the spooling directory
111 	 */
112 	seteuid(euid);
113 	if (chdir(SD) < 0)
114 		fatal("cannot chdir to spooling directory");
115 	seteuid(uid);
116 	if ((nitems = getq(&queue)) < 0)
117 		fatal("cannot examine spooling area\n");
118 	seteuid(euid);
119 	ret = stat(LO, &statb);
120 	seteuid(uid);
121 	if (ret >= 0) {
122 		if (statb.st_mode & S_IXUSR) {
123 			if (remote)
124 				printf("%s: ", host);
125 			printf("Warning: %s is down: ", printer);
126 			seteuid(euid);
127 			fd = open(ST, O_RDONLY);
128 			seteuid(uid);
129 			if (fd >= 0) {
130 				(void)flock(fd, LOCK_SH);
131 				while ((i = read(fd, line, sizeof(line))) > 0)
132 					(void)fwrite(line, 1, (size_t)i, stdout);
133 				(void)close(fd);	/* unlocks as well */
134 			} else
135 				putchar('\n');
136 		}
137 		if (statb.st_mode & S_IXGRP) {
138 			if (remote)
139 				printf("%s: ", host);
140 			printf("Warning: %s queue is turned off\n", printer);
141 		}
142 	}
143 
144 	if (nitems) {
145 		seteuid(euid);
146 		fp = fopen(LO, "r");
147 		seteuid(uid);
148 		if (fp == NULL)
149 			nodaemon();
150 		else {
151 			/* get daemon pid */
152 			cp = current;
153 			ecp = cp + sizeof(current) - 1;
154 			while ((i = getc(fp)) != EOF && i != '\n') {
155 				if (cp < ecp)
156 					*cp++ = i;
157 			}
158 			*cp = '\0';
159 			i = atoi(current);
160 			if (i <= 0) {
161 				ret = -1;
162 			} else {
163 				seteuid(euid);
164 				ret = kill(i, 0);
165 				seteuid(uid);
166 			}
167 			if (ret < 0) {
168 				nodaemon();
169 			} else {
170 				/* read current file name */
171 				cp = current;
172 		    		ecp = cp + sizeof(current) - 1;
173 				while ((i = getc(fp)) != EOF && i != '\n') {
174 					if (cp < ecp)
175 						*cp++ = i;
176 				}
177 				*cp = '\0';
178 				/*
179 				 * Print the status file.
180 				 */
181 				if (remote)
182 					printf("%s: ", host);
183 				seteuid(euid);
184 				fd = open(ST, O_RDONLY);
185 				seteuid(uid);
186 				if (fd >= 0) {
187 					(void)flock(fd, LOCK_SH);
188 					while ((i = read(fd, line, sizeof(line))) > 0)
189 						(void)fwrite(line, 1, (size_t)i, stdout);
190 					(void)close(fd);	/* unlocks as well */
191 				} else
192 					putchar('\n');
193 			}
194 			(void)fclose(fp);
195 		}
196 		/*
197 		 * Now, examine the control files and print out the jobs to
198 		 * be done for each user.
199 		 */
200 		if (!lflag)
201 			header();
202 		for (i = 0; i < nitems; i++) {
203 			q = queue[i];
204 			inform(q->q_name);
205 		}
206 	}
207 	freeq(queue, nitems);
208 	if (!remote) {
209 		if (nitems == 0)
210 			puts("no entries");
211 		return;
212 	}
213 
214 	/*
215 	 * Print foreign queue
216 	 * Note that a file in transit may show up in either queue.
217 	 */
218 	if (nitems)
219 		putchar('\n');
220 	(void)snprintf(line, sizeof(line), "%c%s", format + '\3', RP);
221 	cp = line;
222 	ecp = line + sizeof(line);
223 	for (i = 0;
224 	    i < requests && (size_t)(cp - line + 11) < sizeof(line) - 2;
225 	    i++) {
226 		cp += strlen(cp);
227 		(void)snprintf(cp, ecp - cp, " %d", requ[i]);
228 	}
229 	for (i = 0;
230 	    i < users && cp - line + 1 + strlen(user[i]) < sizeof(line) - 2;
231 	    i++) {
232 		cp += strlen(cp);
233 		if ((size_t)(cp - line) > sizeof(line) - 2)
234 			break;
235 		*cp++ = ' ';
236 		/* truncation may happen */
237 		(void)strlcpy(cp, user[i], ecp - cp);
238 	}
239 	(void)strlcat(line, "\n", sizeof(line));
240 	fd = getport(RM);
241 	if (fd < 0) {
242 		if (from != host)
243 			printf("%s: ", host);
244 		(void)printf("connection to %s is down\n", RM);
245 	}
246 	else {
247 		struct sigaction osa, nsa;
248 
249 		i = strlen(line);
250 		if (write(fd, line, (size_t)i) != i)
251 			fatal("Lost connection");
252 		nsa.sa_handler = alarmer;
253 		sigemptyset(&nsa.sa_mask);
254 		sigaddset(&nsa.sa_mask, SIGALRM);
255 		nsa.sa_flags = 0;
256 		(void)sigaction(SIGALRM, &nsa, &osa);
257 		alarm(wait_time);
258 		while ((i = read(fd, line, sizeof(line))) > 0) {
259 			(void)fwrite(line, 1, (size_t)i, stdout);
260 			alarm(wait_time);
261 		}
262 		alarm(0);
263 		(void)sigaction(SIGALRM, &osa, NULL);
264 		(void)close(fd);
265 	}
266 }
267 
268 static void
alarmer(int s)269 alarmer(int s)
270 {
271 	/* nothing */
272 }
273 
274 /*
275  * Print a warning message if there is no daemon present.
276  */
277 void
nodaemon(void)278 nodaemon(void)
279 {
280 	if (remote)
281 		printf("\n%s: ", host);
282 	puts("Warning: no daemon present");
283 	current[0] = '\0';
284 }
285 
286 /*
287  * Print the header for the short listing format
288  */
289 void
header(void)290 header(void)
291 {
292 	printf(head0);
293 	col = strlen(head0)+1;
294 	blankfill(SIZCOL);
295 	printf(head1);
296 }
297 
298 void
inform(const char * cf)299 inform(const char *cf)
300 {
301 	int j;
302 	FILE *cfp;
303 
304 	/*
305 	 * There's a chance the control file has gone away
306 	 * in the meantime; if this is the case just keep going
307 	 */
308 	seteuid(euid);
309 	if ((cfp = fopen(cf, "r")) == NULL)
310 		return;
311 	seteuid(uid);
312 
313 	if (rank < 0)
314 		rank = 0;
315 	if (remote || garbage || strcmp(cf, current))
316 		rank++;
317 	j = 0;
318 	while (get_line(cfp)) {
319 		switch (line[0]) {
320 		case 'P': /* Was this file specified in the user's list? */
321 			if (!inlist(line+1, cf)) {
322 				fclose(cfp);
323 				return;
324 			}
325 			if (lflag) {
326 				printf("\n%s: ", line+1);
327 				col = strlen(line+1) + 2;
328 				prank(rank);
329 				blankfill(JOBCOL);
330 				printf(" [job %s]\n", cf+3);
331 			} else {
332 				col = 0;
333 				prank(rank);
334 				blankfill(OWNCOL);
335 				printf("%-10s %-3d  ", line+1, atoi(cf+3));
336 				col += 16;
337 				first = 1;
338 			}
339 			continue;
340 		default: /* some format specifer and file name? */
341 			if (line[0] < 'a' || line[0] > 'z')
342 				continue;
343 			if (j == 0 || strcmp(fname, line+1) != 0) {
344 				(void)strlcpy(fname, line+1, sizeof(fname));
345 			}
346 			j++;
347 			continue;
348 		case 'N':
349 			show(line + 1, fname, j);
350 			fname[0] = '\0';
351 			j = 0;
352 		}
353 	}
354 	fclose(cfp);
355 	if (!lflag) {
356 		blankfill(SIZCOL);
357 		printf("%ld bytes\n", totsize);
358 		totsize = 0;
359 	}
360 }
361 
362 int
inlist(const char * name,const char * file)363 inlist(const char *name, const char *file)
364 {
365 	int *r, n;
366 	char **u;
367 	const char *cp;
368 
369 	if (users == 0 && requests == 0)
370 		return(1);
371 	/*
372 	 * Check to see if it's in the user list
373 	 */
374 	for (u = user; u < &user[users]; u++)
375 		if (!strcmp(*u, name))
376 			return(1);
377 	/*
378 	 * Check the request list
379 	 */
380 	for (n = 0, cp = file+3; isdigit((unsigned char)*cp); )
381 		n = n * 10 + (*cp++ - '0');
382 	for (r = requ; r < &requ[requests]; r++)
383 		if (*r == n && !strcmp(cp, from))
384 			return(1);
385 	return(0);
386 }
387 
388 void
show(const char * nfile,const char * file,int copies)389 show(const char *nfile, const char *file, int copies)
390 {
391 	if (strcmp(nfile, " ") == 0)
392 		nfile = "(standard input)";
393 	if (lflag)
394 		ldump(nfile, file, copies);
395 	else
396 		dump(nfile, file, copies);
397 }
398 
399 /*
400  * Fill the line with blanks to the specified column
401  */
402 void
blankfill(int n)403 blankfill(int n)
404 {
405 	while (col++ < n)
406 		putchar(' ');
407 }
408 
409 /*
410  * Give the abbreviated dump of the file names
411  */
412 void
dump(const char * nfile,const char * file,int copies)413 dump(const char *nfile, const char *file, int copies)
414 {
415 	short n, fill;
416 	struct stat lbuf;
417 
418 	/*
419 	 * Print as many files as will fit
420 	 *  (leaving room for the total size)
421 	 */
422 	 fill = first ? 0 : 2;	/* fill space for ``, '' */
423 	 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
424 		if (col < SIZCOL) {
425 			printf(" ..."), col += 4;
426 			blankfill(SIZCOL);
427 		}
428 	} else {
429 		if (first)
430 			first = 0;
431 		else
432 			printf(", ");
433 		printf("%s", nfile);
434 		col += n+fill;
435 	}
436 	seteuid(euid);
437 	if (*file && !stat(file, &lbuf))
438 		totsize += copies * (long)lbuf.st_size;
439 	seteuid(uid);
440 }
441 
442 /*
443  * Print the long info about the file
444  */
445 void
ldump(const char * nfile,const char * file,int copies)446 ldump(const char *nfile, const char *file, int copies)
447 {
448 	struct stat lbuf;
449 
450 	putchar('\t');
451 	if (copies > 1)
452 		printf("%-2d copies of %-19s", copies, nfile);
453 	else
454 		printf("%-32s", nfile);
455 	if (*file && !stat(file, &lbuf))
456 		printf(" %lld bytes", (long long)lbuf.st_size);
457 	else
458 		printf(" ??? bytes");
459 	putchar('\n');
460 }
461 
462 /*
463  * Print the job's rank in the queue,
464  *   update col for screen management
465  */
466 void
prank(int n)467 prank(int n)
468 {
469 	char rline[100];
470 	static const char *r[] = {
471 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
472 	};
473 
474 	if (n == 0) {
475 		printf("active");
476 		col += 6;
477 		return;
478 	}
479 	if ((n/10)%10 == 1)
480 		(void)snprintf(rline, sizeof(rline), "%dth", n);
481 	else
482 		(void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
483 	col += strlen(rline);
484 	printf("%s", rline);
485 }
486