2005-11-28 06:26:06 +03:00
|
|
|
/* $NetBSD: lpq.c,v 1.16 2005/11/28 03:26:06 christos Exp $ */
|
1997-10-05 19:11:58 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-18 05:25:37 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:25:11 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-05 19:11:58 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
|
|
|
#if 0
|
1997-10-05 15:52:17 +04:00
|
|
|
static char sccsid[] = "@(#)lpq.c 8.3 (Berkeley) 5/10/95";
|
1997-10-05 19:11:58 +04:00
|
|
|
#else
|
2005-11-28 06:26:06 +03:00
|
|
|
__RCSID("$NetBSD: lpq.c,v 1.16 2005/11/28 03:26:06 christos Exp $");
|
1997-10-05 19:11:58 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Spool Queue examination program
|
|
|
|
*
|
1997-10-05 15:52:17 +04:00
|
|
|
* lpq [-a] [-l] [-Pprinter] [user...] [job...]
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
1997-10-05 15:52:17 +04:00
|
|
|
* -a show all non-null queues on the local machine
|
1993-03-21 12:45:37 +03:00
|
|
|
* -l long output
|
|
|
|
* -P used to identify printer as per lpr/lprm
|
|
|
|
*/
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
1997-10-05 19:11:58 +04:00
|
|
|
#include <err.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "lp.h"
|
1994-05-18 05:25:37 +04:00
|
|
|
#include "lp.local.h"
|
1997-10-05 15:52:17 +04:00
|
|
|
#include "pathnames.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
int requ[MAXREQUESTS]; /* job number of spool entries */
|
|
|
|
int requests; /* # of spool requests */
|
1993-03-21 12:45:37 +03:00
|
|
|
char *user[MAXUSERS]; /* users to process */
|
1994-05-18 05:25:37 +04:00
|
|
|
int users; /* # of users in user array */
|
1995-10-03 18:02:17 +03:00
|
|
|
uid_t uid, euid;
|
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
int
|
2002-07-14 19:27:58 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 15:52:17 +04:00
|
|
|
int ch, aflag, lflag;
|
|
|
|
char *buf, *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-11-28 06:26:06 +03:00
|
|
|
setprogname(*argv);
|
1995-10-03 18:02:17 +03:00
|
|
|
euid = geteuid();
|
|
|
|
uid = getuid();
|
|
|
|
seteuid(uid);
|
1997-10-05 19:11:58 +04:00
|
|
|
if (gethostname(host, sizeof(host)))
|
|
|
|
err(1, "lpq: gethostname");
|
1998-07-06 10:56:06 +04:00
|
|
|
host[sizeof(host) - 1] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
openlog("lpd", 0, LOG_LPR);
|
|
|
|
|
1997-10-05 15:52:17 +04:00
|
|
|
aflag = lflag = 0;
|
- add timeouts to displayq(), rmremote(), sendfile() and response(),
and use these timeout in the lpq, lpd and lprm programs.
these stop hung remote printers that accept tcp connections but do
not process jobs from hanging the whole system and letting the sysadmin
have a clue about what is going on with this rogue printer.
- add a -r flag to lpd to allow `of' filters for remote jobs.
i know there are ways around this, but i just don't care.
- add a -f flag to lpf to add missing carriage returns.
useful when printing UNIX files to an, eg, LaserWriter that wants CR's
as well as LF's in raw text. stair-stepped text is no fun.
- implement child process accounting: we just have a limit on the number
of children we can have (settable by the sysadmin), and we sleep when
this number is reached. this can reduce malicious not-so-malicious
attacks on the print server by a rogue remote client..
- use setproctitle() where appropriate so the sysadmin has a clue about
what each of the lpd's here are doing.
this was useful to help diagnose a problem (that the above child process
accounting change reduces the lossages of) where a rogue client was
attempting "lpq" operations on one stuck queue in rapid succession,
causing the lpd server to be extremely slow, due to the large number
of lpd processes running.
i have been running these changes in production for about a year.
1999-12-07 17:54:44 +03:00
|
|
|
while ((ch = getopt(argc, argv, "alP:w:")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch((char)ch) {
|
1997-10-05 15:52:17 +04:00
|
|
|
case 'a':
|
|
|
|
++aflag;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'l': /* long output */
|
|
|
|
++lflag;
|
|
|
|
break;
|
|
|
|
case 'P': /* printer name */
|
|
|
|
printer = optarg;
|
|
|
|
break;
|
- add timeouts to displayq(), rmremote(), sendfile() and response(),
and use these timeout in the lpq, lpd and lprm programs.
these stop hung remote printers that accept tcp connections but do
not process jobs from hanging the whole system and letting the sysadmin
have a clue about what is going on with this rogue printer.
- add a -r flag to lpd to allow `of' filters for remote jobs.
i know there are ways around this, but i just don't care.
- add a -f flag to lpf to add missing carriage returns.
useful when printing UNIX files to an, eg, LaserWriter that wants CR's
as well as LF's in raw text. stair-stepped text is no fun.
- implement child process accounting: we just have a limit on the number
of children we can have (settable by the sysadmin), and we sleep when
this number is reached. this can reduce malicious not-so-malicious
attacks on the print server by a rogue remote client..
- use setproctitle() where appropriate so the sysadmin has a clue about
what each of the lpd's here are doing.
this was useful to help diagnose a problem (that the above child process
accounting change reduces the lossages of) where a rogue client was
attempting "lpq" operations on one stuck queue in rapid succession,
causing the lpd server to be extremely slow, due to the large number
of lpd processes running.
i have been running these changes in production for about a year.
1999-12-07 17:54:44 +03:00
|
|
|
case 'w':
|
|
|
|
wait_time = atoi(optarg);
|
|
|
|
if (wait_time < 0)
|
|
|
|
errx(1, "wait time must be postive: %s",
|
|
|
|
optarg);
|
|
|
|
if (wait_time < 30)
|
|
|
|
warnx("warning: wait time less than 30 seconds");
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1997-10-05 15:52:17 +04:00
|
|
|
if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
printer = DEFLP;
|
|
|
|
|
|
|
|
for (argc -= optind, argv += optind; argc; --argc, ++argv)
|
2004-10-30 12:44:25 +04:00
|
|
|
if (isdigit((unsigned char)argv[0][0])) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (requests >= MAXREQUESTS)
|
|
|
|
fatal("too many requests");
|
|
|
|
requ[requests++] = atoi(*argv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (users >= MAXUSERS)
|
|
|
|
fatal("too many users");
|
|
|
|
user[users++] = *argv;
|
|
|
|
}
|
|
|
|
|
1997-10-05 15:52:17 +04:00
|
|
|
if (aflag) {
|
|
|
|
while (cgetnext(&buf, printcapdb) > 0) {
|
|
|
|
if (ckqueue(buf) <= 0) {
|
|
|
|
free(buf);
|
|
|
|
continue; /* no jobs */
|
|
|
|
}
|
|
|
|
for (cp = buf; *cp; cp++)
|
|
|
|
if (*cp == '|' || *cp == ':') {
|
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printer = buf;
|
|
|
|
printf("%s:\n", printer);
|
|
|
|
displayq(lflag);
|
|
|
|
free(buf);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
displayq(lflag);
|
2005-11-28 06:26:06 +03:00
|
|
|
return 0;
|
1997-10-05 15:52:17 +04:00
|
|
|
}
|
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
static void
|
2002-07-14 19:27:58 +04:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-11-28 06:26:06 +03:00
|
|
|
(void)fprintf(stderr, "Usage: %s [-a] [-l] [-Pprinter] [-w maxwait] [user ...] [job ...]", getprogname());
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|