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