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