displayq.c revision 1.30 1 /* $NetBSD: displayq.c,v 1.30 2006/01/20 17:30:00 christos 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.30 2006/01/20 17:30:00 christos 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
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 free(q);
206 }
207 free(queue);
208 }
209 if (!remote) {
210 if (nitems == 0)
211 puts("no entries");
212 return;
213 }
214
215 /*
216 * Print foreign queue
217 * Note that a file in transit may show up in either queue.
218 */
219 if (nitems)
220 putchar('\n');
221 (void)snprintf(line, sizeof(line), "%c%s", format + '\3', RP);
222 cp = line;
223 ecp = line + sizeof(line);
224 for (i = 0; i < requests && cp - line + 11 < sizeof(line) - 2; i++) {
225 cp += strlen(cp);
226 (void)snprintf(cp, ecp - cp, " %d", requ[i]);
227 }
228 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) <
229 sizeof(line) - 2; i++) {
230 cp += strlen(cp);
231 if (cp - line > sizeof(line) - 2)
232 break;
233 *cp++ = ' ';
234 /* truncation may happen */
235 (void)strlcpy(cp, user[i], ecp - cp);
236 }
237 (void)strlcat(line, "\n", sizeof(line));
238 fd = getport(RM);
239 if (fd < 0) {
240 if (from != host)
241 printf("%s: ", host);
242 (void)printf("connection to %s is down\n", RM);
243 }
244 else {
245 struct sigaction osa, nsa;
246
247 i = strlen(line);
248 if (write(fd, line, (size_t)i) != i)
249 fatal("Lost connection");
250 nsa.sa_handler = alarmer;
251 sigemptyset(&nsa.sa_mask);
252 sigaddset(&nsa.sa_mask, SIGALRM);
253 nsa.sa_flags = 0;
254 (void)sigaction(SIGALRM, &nsa, &osa);
255 alarm(wait_time);
256 while ((i = read(fd, line, sizeof(line))) > 0) {
257 (void)fwrite(line, 1, (size_t)i, stdout);
258 alarm(wait_time);
259 }
260 alarm(0);
261 (void)sigaction(SIGALRM, &osa, NULL);
262 (void)close(fd);
263 }
264 }
265
266 static void
267 alarmer(int s)
268 {
269 /* nothing */
270 }
271
272 /*
273 * Print a warning message if there is no daemon present.
274 */
275 void
276 nodaemon(void)
277 {
278 if (remote)
279 printf("\n%s: ", host);
280 puts("Warning: no daemon present");
281 current[0] = '\0';
282 }
283
284 /*
285 * Print the header for the short listing format
286 */
287 void
288 header(void)
289 {
290 printf(head0);
291 col = strlen(head0)+1;
292 blankfill(SIZCOL);
293 printf(head1);
294 }
295
296 void
297 inform(const char *cf)
298 {
299 int j;
300 FILE *cfp;
301
302 /*
303 * There's a chance the control file has gone away
304 * in the meantime; if this is the case just keep going
305 */
306 seteuid(euid);
307 if ((cfp = fopen(cf, "r")) == NULL)
308 return;
309 seteuid(uid);
310
311 if (rank < 0)
312 rank = 0;
313 if (remote || garbage || strcmp(cf, current))
314 rank++;
315 j = 0;
316 while (getline(cfp)) {
317 switch (line[0]) {
318 case 'P': /* Was this file specified in the user's list? */
319 if (!inlist(line+1, cf)) {
320 fclose(cfp);
321 return;
322 }
323 if (lflag) {
324 printf("\n%s: ", line+1);
325 col = strlen(line+1) + 2;
326 prank(rank);
327 blankfill(JOBCOL);
328 printf(" [job %s]\n", cf+3);
329 } else {
330 col = 0;
331 prank(rank);
332 blankfill(OWNCOL);
333 printf("%-10s %-3d ", line+1, atoi(cf+3));
334 col += 16;
335 first = 1;
336 }
337 continue;
338 default: /* some format specifer and file name? */
339 if (line[0] < 'a' || line[0] > 'z')
340 continue;
341 if (j == 0 || strcmp(fname, line+1) != 0) {
342 (void)strlcpy(fname, line+1, sizeof(fname));
343 }
344 j++;
345 continue;
346 case 'N':
347 show(line + 1, fname, j);
348 fname[0] = '\0';
349 j = 0;
350 }
351 }
352 fclose(cfp);
353 if (!lflag) {
354 blankfill(SIZCOL);
355 printf("%ld bytes\n", totsize);
356 totsize = 0;
357 }
358 }
359
360 int
361 inlist(const char *name, const char *file)
362 {
363 int *r, n;
364 char **u;
365 const char *cp;
366
367 if (users == 0 && requests == 0)
368 return(1);
369 /*
370 * Check to see if it's in the user list
371 */
372 for (u = user; u < &user[users]; u++)
373 if (!strcmp(*u, name))
374 return(1);
375 /*
376 * Check the request list
377 */
378 for (n = 0, cp = file+3; isdigit((unsigned char)*cp); )
379 n = n * 10 + (*cp++ - '0');
380 for (r = requ; r < &requ[requests]; r++)
381 if (*r == n && !strcmp(cp, from))
382 return(1);
383 return(0);
384 }
385
386 void
387 show(const char *nfile, const char *file, int copies)
388 {
389 if (strcmp(nfile, " ") == 0)
390 nfile = "(standard input)";
391 if (lflag)
392 ldump(nfile, file, copies);
393 else
394 dump(nfile, file, copies);
395 }
396
397 /*
398 * Fill the line with blanks to the specified column
399 */
400 void
401 blankfill(int n)
402 {
403 while (col++ < n)
404 putchar(' ');
405 }
406
407 /*
408 * Give the abbreviated dump of the file names
409 */
410 void
411 dump(const char *nfile, const char *file, int copies)
412 {
413 short n, fill;
414 struct stat lbuf;
415
416 /*
417 * Print as many files as will fit
418 * (leaving room for the total size)
419 */
420 fill = first ? 0 : 2; /* fill space for ``, '' */
421 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) {
422 if (col < SIZCOL) {
423 printf(" ..."), col += 4;
424 blankfill(SIZCOL);
425 }
426 } else {
427 if (first)
428 first = 0;
429 else
430 printf(", ");
431 printf("%s", nfile);
432 col += n+fill;
433 }
434 seteuid(euid);
435 if (*file && !stat(file, &lbuf))
436 totsize += copies * (long)lbuf.st_size;
437 seteuid(uid);
438 }
439
440 /*
441 * Print the long info about the file
442 */
443 void
444 ldump(const char *nfile, const char *file, int copies)
445 {
446 struct stat lbuf;
447
448 putchar('\t');
449 if (copies > 1)
450 printf("%-2d copies of %-19s", copies, nfile);
451 else
452 printf("%-32s", nfile);
453 if (*file && !stat(file, &lbuf))
454 printf(" %lld bytes", (long long)lbuf.st_size);
455 else
456 printf(" ??? bytes");
457 putchar('\n');
458 }
459
460 /*
461 * Print the job's rank in the queue,
462 * update col for screen management
463 */
464 void
465 prank(int n)
466 {
467 char rline[100];
468 static const char *r[] = {
469 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
470 };
471
472 if (n == 0) {
473 printf("active");
474 col += 6;
475 return;
476 }
477 if ((n/10)%10 == 1)
478 (void)snprintf(rline, sizeof(rline), "%dth", n);
479 else
480 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]);
481 col += strlen(rline);
482 printf("%s", rline);
483 }
484