cmds.c revision 1.18 1 1.18 christos /* $NetBSD: cmds.c,v 1.18 2005/11/28 03:26:06 christos Exp $ */
2 1.1 cgd /*
3 1.4 cgd * Copyright (c) 1983, 1993
4 1.4 cgd * The Regents of the University of California. All rights reserved.
5 1.4 cgd *
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.16 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.12 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.12 mrg __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 1.12 mrg The Regents of the University of California. All rights reserved.\n");
36 1.10 mikel #if 0
37 1.11 mrg static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
38 1.10 mikel #else
39 1.18 christos __RCSID("$NetBSD: cmds.c,v 1.18 2005/11/28 03:26:06 christos Exp $");
40 1.10 mikel #endif
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd /*
44 1.1 cgd * lpc -- line printer control program -- commands:
45 1.1 cgd */
46 1.1 cgd
47 1.4 cgd #include <sys/param.h>
48 1.4 cgd #include <sys/time.h>
49 1.4 cgd #include <sys/stat.h>
50 1.11 mrg #include <sys/file.h>
51 1.4 cgd
52 1.4 cgd #include <signal.h>
53 1.4 cgd #include <fcntl.h>
54 1.4 cgd #include <errno.h>
55 1.4 cgd #include <dirent.h>
56 1.4 cgd #include <unistd.h>
57 1.4 cgd #include <stdlib.h>
58 1.4 cgd #include <stdio.h>
59 1.4 cgd #include <ctype.h>
60 1.4 cgd #include <string.h>
61 1.1 cgd #include "lp.h"
62 1.4 cgd #include "lp.local.h"
63 1.4 cgd #include "lpc.h"
64 1.4 cgd #include "extern.h"
65 1.1 cgd #include "pathnames.h"
66 1.1 cgd
67 1.5 hpeyerl extern uid_t uid, euid;
68 1.5 hpeyerl
69 1.14 wiz static void abortpr(int);
70 1.14 wiz static void cleanpr(void);
71 1.14 wiz static void disablepr(void);
72 1.18 christos static int doarg(const char *);
73 1.14 wiz static int doselect(const struct dirent *);
74 1.14 wiz static void enablepr(void);
75 1.14 wiz static void prstat(void);
76 1.14 wiz static void putmsg(int, char **);
77 1.14 wiz static int sortq(const void *, const void *);
78 1.14 wiz static void startpr(int);
79 1.14 wiz static void stoppr(void);
80 1.14 wiz static int touch(struct queue *);
81 1.18 christos static void unlinkf(const char *);
82 1.18 christos static void upstat(const char *);
83 1.18 christos static int getcapdesc(void);
84 1.18 christos static void getcaps(void);
85 1.4 cgd
86 1.1 cgd /*
87 1.1 cgd * kill an existing daemon and disable printing.
88 1.1 cgd */
89 1.4 cgd void
90 1.14 wiz doabort(int argc, char *argv[])
91 1.1 cgd {
92 1.18 christos int c;
93 1.12 mrg char *cp1, *cp2;
94 1.1 cgd char prbuf[100];
95 1.1 cgd
96 1.1 cgd if (argc == 1) {
97 1.1 cgd printf("Usage: abort {all | printer ...}\n");
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
101 1.1 cgd printer = prbuf;
102 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
103 1.1 cgd cp1 = prbuf;
104 1.4 cgd cp2 = bp;
105 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
106 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
107 1.1 cgd *cp1++ = c;
108 1.1 cgd *cp1 = '\0';
109 1.1 cgd abortpr(1);
110 1.1 cgd }
111 1.1 cgd return;
112 1.1 cgd }
113 1.1 cgd while (--argc) {
114 1.1 cgd printer = *++argv;
115 1.18 christos if (!getcapdesc())
116 1.1 cgd continue;
117 1.1 cgd abortpr(1);
118 1.1 cgd }
119 1.1 cgd }
120 1.1 cgd
121 1.4 cgd static void
122 1.14 wiz abortpr(int dis)
123 1.1 cgd {
124 1.12 mrg FILE *fp;
125 1.1 cgd struct stat stbuf;
126 1.12 mrg int pid, fd;
127 1.1 cgd
128 1.18 christos getcaps();
129 1.1 cgd printf("%s:\n", printer);
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * Turn on the owner execute bit of the lock file to disable printing.
133 1.1 cgd */
134 1.1 cgd if (dis) {
135 1.5 hpeyerl seteuid(euid);
136 1.1 cgd if (stat(line, &stbuf) >= 0) {
137 1.1 cgd if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
138 1.1 cgd printf("\tcannot disable printing\n");
139 1.1 cgd else {
140 1.1 cgd upstat("printing disabled\n");
141 1.1 cgd printf("\tprinting disabled\n");
142 1.1 cgd }
143 1.1 cgd } else if (errno == ENOENT) {
144 1.1 cgd if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
145 1.1 cgd printf("\tcannot create lock file\n");
146 1.1 cgd else {
147 1.8 mrg (void)close(fd);
148 1.1 cgd upstat("printing disabled\n");
149 1.1 cgd printf("\tprinting disabled\n");
150 1.1 cgd printf("\tno daemon to abort\n");
151 1.1 cgd }
152 1.5 hpeyerl goto out;
153 1.1 cgd } else {
154 1.1 cgd printf("\tcannot stat lock file\n");
155 1.5 hpeyerl goto out;
156 1.1 cgd }
157 1.1 cgd }
158 1.1 cgd /*
159 1.1 cgd * Kill the current daemon to stop printing now.
160 1.1 cgd */
161 1.1 cgd if ((fp = fopen(line, "r")) == NULL) {
162 1.1 cgd printf("\tcannot open lock file\n");
163 1.5 hpeyerl goto out;
164 1.1 cgd }
165 1.1 cgd if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
166 1.8 mrg (void)fclose(fp); /* unlocks as well */
167 1.1 cgd printf("\tno daemon to abort\n");
168 1.5 hpeyerl goto out;
169 1.1 cgd }
170 1.8 mrg (void)fclose(fp);
171 1.12 mrg if (kill(pid = atoi(line), SIGTERM) < 0) {
172 1.12 mrg if (errno == ESRCH)
173 1.12 mrg printf("\tno daemon to abort\n");
174 1.12 mrg else
175 1.12 mrg printf("\tWarning: daemon (pid %d) not killed\n", pid);
176 1.12 mrg } else
177 1.1 cgd printf("\tdaemon (pid %d) killed\n", pid);
178 1.5 hpeyerl out:
179 1.5 hpeyerl seteuid(uid);
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd /*
183 1.1 cgd * Write a message into the status file.
184 1.1 cgd */
185 1.4 cgd static void
186 1.18 christos upstat(const char *msg)
187 1.1 cgd {
188 1.12 mrg int fd;
189 1.12 mrg char statfile[MAXPATHLEN];
190 1.1 cgd
191 1.18 christos getcaps();
192 1.8 mrg (void)snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
193 1.1 cgd umask(0);
194 1.1 cgd fd = open(statfile, O_WRONLY|O_CREAT, 0664);
195 1.1 cgd if (fd < 0 || flock(fd, LOCK_EX) < 0) {
196 1.1 cgd printf("\tcannot create status file\n");
197 1.1 cgd return;
198 1.1 cgd }
199 1.8 mrg (void)ftruncate(fd, 0);
200 1.1 cgd if (msg == (char *)NULL)
201 1.8 mrg (void)write(fd, "\n", 1);
202 1.1 cgd else
203 1.8 mrg (void)write(fd, msg, strlen(msg));
204 1.8 mrg (void)close(fd);
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd /*
208 1.1 cgd * Remove all spool files and temporaries from the spooling area.
209 1.1 cgd */
210 1.4 cgd void
211 1.14 wiz clean(int argc, char *argv[])
212 1.1 cgd {
213 1.18 christos int c;
214 1.12 mrg char *cp1, *cp2;
215 1.1 cgd char prbuf[100];
216 1.1 cgd
217 1.1 cgd if (argc == 1) {
218 1.1 cgd printf("Usage: clean {all | printer ...}\n");
219 1.1 cgd return;
220 1.1 cgd }
221 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
222 1.1 cgd printer = prbuf;
223 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
224 1.1 cgd cp1 = prbuf;
225 1.4 cgd cp2 = bp;
226 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
227 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
228 1.1 cgd *cp1++ = c;
229 1.1 cgd *cp1 = '\0';
230 1.1 cgd cleanpr();
231 1.1 cgd }
232 1.1 cgd return;
233 1.1 cgd }
234 1.1 cgd while (--argc) {
235 1.1 cgd printer = *++argv;
236 1.18 christos if (!getcapdesc())
237 1.1 cgd continue;
238 1.1 cgd cleanpr();
239 1.1 cgd }
240 1.1 cgd }
241 1.1 cgd
242 1.4 cgd static int
243 1.14 wiz doselect(const struct dirent *d)
244 1.1 cgd {
245 1.1 cgd int c = d->d_name[0];
246 1.1 cgd
247 1.1 cgd if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
248 1.1 cgd return(1);
249 1.1 cgd return(0);
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd /*
253 1.1 cgd * Comparison routine for scandir. Sort by job number and machine, then
254 1.1 cgd * by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
255 1.1 cgd */
256 1.4 cgd static int
257 1.14 wiz sortq(const void *a, const void *b)
258 1.1 cgd {
259 1.18 christos const struct dirent *const *d1, *const *d2;
260 1.1 cgd int c1, c2;
261 1.1 cgd
262 1.18 christos d1 = (const struct dirent *const *)a;
263 1.18 christos d2 = (const struct dirent *const *)b;
264 1.9 mikel if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)) != 0)
265 1.1 cgd return(c1);
266 1.1 cgd c1 = (*d1)->d_name[0];
267 1.1 cgd c2 = (*d2)->d_name[0];
268 1.1 cgd if (c1 == c2)
269 1.1 cgd return((*d1)->d_name[2] - (*d2)->d_name[2]);
270 1.1 cgd if (c1 == 'c')
271 1.1 cgd return(-1);
272 1.1 cgd if (c1 == 'd' || c2 == 'c')
273 1.1 cgd return(1);
274 1.1 cgd return(-1);
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd /*
278 1.1 cgd * Remove incomplete jobs from spooling area.
279 1.1 cgd */
280 1.4 cgd static void
281 1.14 wiz cleanpr(void)
282 1.1 cgd {
283 1.12 mrg int i, n;
284 1.18 christos char *cp1, *lp, *ep;
285 1.18 christos const char *cp;
286 1.3 jtc struct dirent **queue;
287 1.1 cgd int nitems;
288 1.1 cgd
289 1.18 christos getcaps();
290 1.1 cgd printf("%s:\n", printer);
291 1.1 cgd
292 1.8 mrg /* XXX depends on SD being non nul */
293 1.15 itojun ep = line + sizeof(line);
294 1.12 mrg for (lp = line, cp = SD; (lp - line) < sizeof(line) &&
295 1.12 mrg (*lp++ = *cp++) != '\0'; )
296 1.1 cgd ;
297 1.1 cgd lp[-1] = '/';
298 1.1 cgd
299 1.5 hpeyerl seteuid(euid);
300 1.4 cgd nitems = scandir(SD, &queue, doselect, sortq);
301 1.5 hpeyerl seteuid(uid);
302 1.1 cgd if (nitems < 0) {
303 1.1 cgd printf("\tcannot examine spool directory\n");
304 1.1 cgd return;
305 1.1 cgd }
306 1.1 cgd if (nitems == 0)
307 1.1 cgd return;
308 1.1 cgd i = 0;
309 1.1 cgd do {
310 1.1 cgd cp = queue[i]->d_name;
311 1.1 cgd if (*cp == 'c') {
312 1.1 cgd n = 0;
313 1.1 cgd while (i + 1 < nitems) {
314 1.1 cgd cp1 = queue[i + 1]->d_name;
315 1.1 cgd if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
316 1.1 cgd break;
317 1.1 cgd i++;
318 1.1 cgd n++;
319 1.1 cgd }
320 1.1 cgd if (n == 0) {
321 1.15 itojun strlcpy(lp, cp, ep - lp);
322 1.1 cgd unlinkf(line);
323 1.1 cgd }
324 1.1 cgd } else {
325 1.1 cgd /*
326 1.1 cgd * Must be a df with no cf (otherwise, it would have
327 1.1 cgd * been skipped above) or a tf file (which can always
328 1.1 cgd * be removed).
329 1.1 cgd */
330 1.15 itojun strlcpy(lp, cp, ep - lp);
331 1.1 cgd unlinkf(line);
332 1.1 cgd }
333 1.1 cgd } while (++i < nitems);
334 1.1 cgd }
335 1.1 cgd
336 1.4 cgd static void
337 1.18 christos unlinkf(const char *name)
338 1.1 cgd {
339 1.5 hpeyerl seteuid(euid);
340 1.1 cgd if (unlink(name) < 0)
341 1.1 cgd printf("\tcannot remove %s\n", name);
342 1.1 cgd else
343 1.1 cgd printf("\tremoved %s\n", name);
344 1.5 hpeyerl seteuid(uid);
345 1.1 cgd }
346 1.1 cgd
347 1.1 cgd /*
348 1.1 cgd * Enable queuing to the printer (allow lpr's).
349 1.1 cgd */
350 1.4 cgd void
351 1.14 wiz enable(int argc, char *argv[])
352 1.1 cgd {
353 1.18 christos int c;
354 1.12 mrg char *cp1, *cp2;
355 1.1 cgd char prbuf[100];
356 1.1 cgd
357 1.1 cgd if (argc == 1) {
358 1.1 cgd printf("Usage: enable {all | printer ...}\n");
359 1.1 cgd return;
360 1.1 cgd }
361 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
362 1.1 cgd printer = prbuf;
363 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
364 1.1 cgd cp1 = prbuf;
365 1.4 cgd cp2 = bp;
366 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
367 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
368 1.1 cgd *cp1++ = c;
369 1.1 cgd *cp1 = '\0';
370 1.1 cgd enablepr();
371 1.1 cgd }
372 1.1 cgd return;
373 1.1 cgd }
374 1.1 cgd while (--argc) {
375 1.1 cgd printer = *++argv;
376 1.18 christos if (!getcapdesc())
377 1.1 cgd continue;
378 1.1 cgd enablepr();
379 1.1 cgd }
380 1.1 cgd }
381 1.1 cgd
382 1.18 christos static int
383 1.18 christos getcapdesc(void)
384 1.18 christos {
385 1.18 christos int st;
386 1.18 christos if ((st = cgetent(&bp, printcapdb, printer)) == -2) {
387 1.18 christos printf("cannot open printer description file\n");
388 1.18 christos return 0;
389 1.18 christos } else if (st == -1) {
390 1.18 christos printf("unknown printer %s\n", printer);
391 1.18 christos return 0;
392 1.18 christos } else if (st == -3)
393 1.18 christos fatal("potential reference loop detected in printcap file");
394 1.18 christos return 1;
395 1.18 christos }
396 1.18 christos
397 1.4 cgd static void
398 1.14 wiz enablepr(void)
399 1.1 cgd {
400 1.1 cgd struct stat stbuf;
401 1.1 cgd
402 1.18 christos getcaps();
403 1.1 cgd printf("%s:\n", printer);
404 1.1 cgd
405 1.1 cgd /*
406 1.1 cgd * Turn off the group execute bit of the lock file to enable queuing.
407 1.1 cgd */
408 1.5 hpeyerl seteuid(euid);
409 1.1 cgd if (stat(line, &stbuf) >= 0) {
410 1.1 cgd if (chmod(line, stbuf.st_mode & 0767) < 0)
411 1.1 cgd printf("\tcannot enable queuing\n");
412 1.1 cgd else
413 1.1 cgd printf("\tqueuing enabled\n");
414 1.1 cgd }
415 1.5 hpeyerl seteuid(uid);
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd /*
419 1.1 cgd * Disable queuing.
420 1.1 cgd */
421 1.4 cgd void
422 1.14 wiz disable(int argc, char *argv[])
423 1.1 cgd {
424 1.18 christos int c;
425 1.12 mrg char *cp1, *cp2;
426 1.1 cgd char prbuf[100];
427 1.1 cgd
428 1.1 cgd if (argc == 1) {
429 1.1 cgd printf("Usage: disable {all | printer ...}\n");
430 1.1 cgd return;
431 1.1 cgd }
432 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
433 1.1 cgd printer = prbuf;
434 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
435 1.1 cgd cp1 = prbuf;
436 1.4 cgd cp2 = bp;
437 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
438 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
439 1.1 cgd *cp1++ = c;
440 1.1 cgd *cp1 = '\0';
441 1.1 cgd disablepr();
442 1.1 cgd }
443 1.1 cgd return;
444 1.1 cgd }
445 1.1 cgd while (--argc) {
446 1.1 cgd printer = *++argv;
447 1.18 christos if (!getcapdesc())
448 1.1 cgd continue;
449 1.1 cgd disablepr();
450 1.1 cgd }
451 1.1 cgd }
452 1.1 cgd
453 1.4 cgd static void
454 1.14 wiz disablepr(void)
455 1.1 cgd {
456 1.12 mrg int fd;
457 1.1 cgd struct stat stbuf;
458 1.1 cgd
459 1.18 christos getcaps();
460 1.1 cgd printf("%s:\n", printer);
461 1.1 cgd /*
462 1.1 cgd * Turn on the group execute bit of the lock file to disable queuing.
463 1.1 cgd */
464 1.5 hpeyerl seteuid(euid);
465 1.1 cgd if (stat(line, &stbuf) >= 0) {
466 1.1 cgd if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
467 1.1 cgd printf("\tcannot disable queuing\n");
468 1.1 cgd else
469 1.1 cgd printf("\tqueuing disabled\n");
470 1.1 cgd } else if (errno == ENOENT) {
471 1.1 cgd if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
472 1.1 cgd printf("\tcannot create lock file\n");
473 1.1 cgd else {
474 1.8 mrg (void)close(fd);
475 1.1 cgd printf("\tqueuing disabled\n");
476 1.1 cgd }
477 1.1 cgd } else
478 1.1 cgd printf("\tcannot stat lock file\n");
479 1.5 hpeyerl seteuid(uid);
480 1.1 cgd }
481 1.1 cgd
482 1.1 cgd /*
483 1.1 cgd * Disable queuing and printing and put a message into the status file
484 1.1 cgd * (reason for being down).
485 1.1 cgd */
486 1.4 cgd void
487 1.14 wiz down(int argc, char *argv[])
488 1.1 cgd {
489 1.18 christos int c;
490 1.12 mrg char *cp1, *cp2;
491 1.1 cgd char prbuf[100];
492 1.1 cgd
493 1.1 cgd if (argc == 1) {
494 1.1 cgd printf("Usage: down {all | printer} [message ...]\n");
495 1.1 cgd return;
496 1.1 cgd }
497 1.1 cgd if (!strcmp(argv[1], "all")) {
498 1.1 cgd printer = prbuf;
499 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
500 1.1 cgd cp1 = prbuf;
501 1.4 cgd cp2 = bp;
502 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
503 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
504 1.1 cgd *cp1++ = c;
505 1.1 cgd *cp1 = '\0';
506 1.1 cgd putmsg(argc - 2, argv + 2);
507 1.1 cgd }
508 1.1 cgd return;
509 1.1 cgd }
510 1.1 cgd printer = argv[1];
511 1.18 christos if (!getcapdesc())
512 1.1 cgd return;
513 1.18 christos putmsg(argc - 2, argv + 2);
514 1.18 christos }
515 1.4 cgd
516 1.18 christos static void
517 1.18 christos getcaps(void)
518 1.18 christos {
519 1.18 christos char *cp;
520 1.18 christos SD = cgetstr(bp, "sd", &cp) == -1 ? _PATH_DEFSPOOL : cp;
521 1.18 christos LO = cgetstr(bp, "lo", &cp) == -1 ? DEFLOCK : cp;
522 1.18 christos ST = cgetstr(bp, "st", &cp) == -1 ? DEFSTAT : cp;
523 1.18 christos (void)snprintf(line, sizeof(line), "%s/%s", SD, LO);
524 1.1 cgd }
525 1.1 cgd
526 1.4 cgd static void
527 1.14 wiz putmsg(int argc, char **argv)
528 1.1 cgd {
529 1.12 mrg int fd;
530 1.12 mrg char *cp1, *cp2;
531 1.1 cgd char buf[1024];
532 1.1 cgd struct stat stbuf;
533 1.1 cgd
534 1.1 cgd printf("%s:\n", printer);
535 1.1 cgd /*
536 1.1 cgd * Turn on the group execute bit of the lock file to disable queuing and
537 1.1 cgd * turn on the owner execute bit of the lock file to disable printing.
538 1.1 cgd */
539 1.5 hpeyerl seteuid(euid);
540 1.1 cgd if (stat(line, &stbuf) >= 0) {
541 1.1 cgd if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
542 1.1 cgd printf("\tcannot disable queuing\n");
543 1.1 cgd else
544 1.1 cgd printf("\tprinter and queuing disabled\n");
545 1.1 cgd } else if (errno == ENOENT) {
546 1.1 cgd if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
547 1.1 cgd printf("\tcannot create lock file\n");
548 1.1 cgd else {
549 1.8 mrg (void)close(fd);
550 1.1 cgd printf("\tprinter and queuing disabled\n");
551 1.1 cgd }
552 1.5 hpeyerl seteuid(uid);
553 1.1 cgd return;
554 1.1 cgd } else
555 1.1 cgd printf("\tcannot stat lock file\n");
556 1.1 cgd /*
557 1.1 cgd * Write the message into the status file.
558 1.1 cgd */
559 1.8 mrg (void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
560 1.1 cgd fd = open(line, O_WRONLY|O_CREAT, 0664);
561 1.1 cgd if (fd < 0 || flock(fd, LOCK_EX) < 0) {
562 1.1 cgd printf("\tcannot create status file\n");
563 1.5 hpeyerl seteuid(uid);
564 1.1 cgd return;
565 1.1 cgd }
566 1.5 hpeyerl seteuid(uid);
567 1.8 mrg (void)ftruncate(fd, 0);
568 1.1 cgd if (argc <= 0) {
569 1.8 mrg (void)write(fd, "\n", 1);
570 1.8 mrg (void)close(fd);
571 1.1 cgd return;
572 1.1 cgd }
573 1.1 cgd cp1 = buf;
574 1.1 cgd while (--argc >= 0) {
575 1.1 cgd cp2 = *argv++;
576 1.12 mrg while ((cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++))
577 1.1 cgd ;
578 1.1 cgd cp1[-1] = ' ';
579 1.1 cgd }
580 1.1 cgd cp1[-1] = '\n';
581 1.1 cgd *cp1 = '\0';
582 1.8 mrg (void)write(fd, buf, strlen(buf));
583 1.8 mrg (void)close(fd);
584 1.1 cgd }
585 1.1 cgd
586 1.1 cgd /*
587 1.1 cgd * Exit lpc
588 1.1 cgd */
589 1.4 cgd void
590 1.14 wiz quit(int argc, char *argv[])
591 1.1 cgd {
592 1.1 cgd exit(0);
593 1.1 cgd }
594 1.1 cgd
595 1.1 cgd /*
596 1.1 cgd * Kill and restart the daemon.
597 1.1 cgd */
598 1.4 cgd void
599 1.14 wiz restart(int argc, char *argv[])
600 1.1 cgd {
601 1.18 christos int c;
602 1.12 mrg char *cp1, *cp2;
603 1.1 cgd char prbuf[100];
604 1.1 cgd
605 1.1 cgd if (argc == 1) {
606 1.1 cgd printf("Usage: restart {all | printer ...}\n");
607 1.1 cgd return;
608 1.1 cgd }
609 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
610 1.1 cgd printer = prbuf;
611 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
612 1.1 cgd cp1 = prbuf;
613 1.4 cgd cp2 = bp;
614 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
615 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
616 1.1 cgd *cp1++ = c;
617 1.1 cgd *cp1 = '\0';
618 1.1 cgd abortpr(0);
619 1.1 cgd startpr(0);
620 1.1 cgd }
621 1.1 cgd return;
622 1.1 cgd }
623 1.1 cgd while (--argc) {
624 1.1 cgd printer = *++argv;
625 1.18 christos if (!getcapdesc())
626 1.1 cgd continue;
627 1.1 cgd abortpr(0);
628 1.1 cgd startpr(0);
629 1.1 cgd }
630 1.1 cgd }
631 1.1 cgd
632 1.1 cgd /*
633 1.1 cgd * Enable printing on the specified printer and startup the daemon.
634 1.1 cgd */
635 1.4 cgd void
636 1.14 wiz startcmd(int argc, char *argv[])
637 1.1 cgd {
638 1.18 christos int c;
639 1.12 mrg char *cp1, *cp2;
640 1.1 cgd char prbuf[100];
641 1.1 cgd
642 1.1 cgd if (argc == 1) {
643 1.1 cgd printf("Usage: start {all | printer ...}\n");
644 1.1 cgd return;
645 1.1 cgd }
646 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
647 1.1 cgd printer = prbuf;
648 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
649 1.1 cgd cp1 = prbuf;
650 1.4 cgd cp2 = bp;
651 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
652 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
653 1.1 cgd *cp1++ = c;
654 1.1 cgd *cp1 = '\0';
655 1.1 cgd startpr(1);
656 1.1 cgd }
657 1.1 cgd return;
658 1.1 cgd }
659 1.1 cgd while (--argc) {
660 1.1 cgd printer = *++argv;
661 1.18 christos if (!getcapdesc())
662 1.1 cgd continue;
663 1.1 cgd startpr(1);
664 1.1 cgd }
665 1.1 cgd }
666 1.1 cgd
667 1.4 cgd static void
668 1.18 christos startpr(int ena)
669 1.1 cgd {
670 1.1 cgd struct stat stbuf;
671 1.1 cgd
672 1.18 christos getcaps();
673 1.1 cgd printf("%s:\n", printer);
674 1.1 cgd
675 1.1 cgd /*
676 1.1 cgd * Turn off the owner execute bit of the lock file to enable printing.
677 1.1 cgd */
678 1.5 hpeyerl seteuid(euid);
679 1.18 christos if (ena && stat(line, &stbuf) >= 0) {
680 1.18 christos if (chmod(line, stbuf.st_mode & (ena == 2 ? 0666 : 0677)) < 0)
681 1.1 cgd printf("\tcannot enable printing\n");
682 1.1 cgd else
683 1.1 cgd printf("\tprinting enabled\n");
684 1.1 cgd }
685 1.1 cgd if (!startdaemon(printer))
686 1.1 cgd printf("\tcouldn't start daemon\n");
687 1.1 cgd else
688 1.1 cgd printf("\tdaemon started\n");
689 1.5 hpeyerl seteuid(uid);
690 1.1 cgd }
691 1.1 cgd
692 1.1 cgd /*
693 1.1 cgd * Print the status of each queue listed or all the queues.
694 1.1 cgd */
695 1.4 cgd void
696 1.14 wiz status(int argc, char *argv[])
697 1.1 cgd {
698 1.18 christos int c;
699 1.12 mrg char *cp1, *cp2;
700 1.1 cgd char prbuf[100];
701 1.1 cgd
702 1.12 mrg if (argc == 1 || (argc == 2 && strcmp(argv[1], "all") == 0)) {
703 1.1 cgd printer = prbuf;
704 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
705 1.1 cgd cp1 = prbuf;
706 1.4 cgd cp2 = bp;
707 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
708 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
709 1.1 cgd *cp1++ = c;
710 1.1 cgd *cp1 = '\0';
711 1.1 cgd prstat();
712 1.1 cgd }
713 1.1 cgd return;
714 1.1 cgd }
715 1.1 cgd while (--argc) {
716 1.1 cgd printer = *++argv;
717 1.18 christos if (!getcapdesc())
718 1.1 cgd continue;
719 1.1 cgd prstat();
720 1.1 cgd }
721 1.1 cgd }
722 1.1 cgd
723 1.1 cgd /*
724 1.1 cgd * Print the status of the printer queue.
725 1.1 cgd */
726 1.4 cgd static void
727 1.14 wiz prstat(void)
728 1.1 cgd {
729 1.1 cgd struct stat stbuf;
730 1.12 mrg int fd, i;
731 1.12 mrg struct dirent *dp;
732 1.1 cgd DIR *dirp;
733 1.1 cgd
734 1.18 christos getcaps();
735 1.1 cgd printf("%s:\n", printer);
736 1.1 cgd if (stat(line, &stbuf) >= 0) {
737 1.1 cgd printf("\tqueuing is %s\n",
738 1.1 cgd (stbuf.st_mode & 010) ? "disabled" : "enabled");
739 1.1 cgd printf("\tprinting is %s\n",
740 1.1 cgd (stbuf.st_mode & 0100) ? "disabled" : "enabled");
741 1.1 cgd } else {
742 1.1 cgd printf("\tqueuing is enabled\n");
743 1.1 cgd printf("\tprinting is enabled\n");
744 1.1 cgd }
745 1.1 cgd if ((dirp = opendir(SD)) == NULL) {
746 1.1 cgd printf("\tcannot examine spool directory\n");
747 1.1 cgd return;
748 1.1 cgd }
749 1.1 cgd i = 0;
750 1.1 cgd while ((dp = readdir(dirp)) != NULL) {
751 1.1 cgd if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
752 1.1 cgd i++;
753 1.1 cgd }
754 1.1 cgd closedir(dirp);
755 1.1 cgd if (i == 0)
756 1.1 cgd printf("\tno entries\n");
757 1.1 cgd else if (i == 1)
758 1.1 cgd printf("\t1 entry in spool area\n");
759 1.1 cgd else
760 1.1 cgd printf("\t%d entries in spool area\n", i);
761 1.1 cgd fd = open(line, O_RDONLY);
762 1.1 cgd if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
763 1.8 mrg (void)close(fd); /* unlocks as well */
764 1.7 hpeyerl printf("\tprinter idle\n");
765 1.1 cgd return;
766 1.1 cgd }
767 1.8 mrg (void)close(fd);
768 1.8 mrg (void)snprintf(line, sizeof(line), "%s/%s", SD, ST);
769 1.1 cgd fd = open(line, O_RDONLY);
770 1.1 cgd if (fd >= 0) {
771 1.8 mrg (void)flock(fd, LOCK_SH);
772 1.12 mrg (void)fstat(fd, &stbuf);
773 1.12 mrg if (stbuf.st_size > 0) {
774 1.12 mrg putchar('\t');
775 1.12 mrg while ((i = read(fd, line, sizeof(line))) > 0)
776 1.12 mrg (void)fwrite(line, 1, i, stdout);
777 1.12 mrg }
778 1.8 mrg (void)close(fd); /* unlocks as well */
779 1.1 cgd }
780 1.1 cgd }
781 1.1 cgd
782 1.1 cgd /*
783 1.1 cgd * Stop the specified daemon after completing the current job and disable
784 1.1 cgd * printing.
785 1.1 cgd */
786 1.4 cgd void
787 1.14 wiz stop(int argc, char *argv[])
788 1.1 cgd {
789 1.18 christos int c;
790 1.12 mrg char *cp1, *cp2;
791 1.1 cgd char prbuf[100];
792 1.1 cgd
793 1.1 cgd if (argc == 1) {
794 1.1 cgd printf("Usage: stop {all | printer ...}\n");
795 1.1 cgd return;
796 1.1 cgd }
797 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
798 1.1 cgd printer = prbuf;
799 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
800 1.1 cgd cp1 = prbuf;
801 1.4 cgd cp2 = bp;
802 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
803 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
804 1.1 cgd *cp1++ = c;
805 1.1 cgd *cp1 = '\0';
806 1.1 cgd stoppr();
807 1.1 cgd }
808 1.1 cgd return;
809 1.1 cgd }
810 1.1 cgd while (--argc) {
811 1.1 cgd printer = *++argv;
812 1.18 christos if (!getcapdesc())
813 1.1 cgd continue;
814 1.1 cgd stoppr();
815 1.1 cgd }
816 1.1 cgd }
817 1.1 cgd
818 1.4 cgd static void
819 1.14 wiz stoppr(void)
820 1.1 cgd {
821 1.12 mrg int fd;
822 1.1 cgd struct stat stbuf;
823 1.1 cgd
824 1.18 christos getcaps();
825 1.1 cgd printf("%s:\n", printer);
826 1.1 cgd
827 1.1 cgd /*
828 1.1 cgd * Turn on the owner execute bit of the lock file to disable printing.
829 1.1 cgd */
830 1.5 hpeyerl seteuid(euid);
831 1.1 cgd if (stat(line, &stbuf) >= 0) {
832 1.1 cgd if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
833 1.1 cgd printf("\tcannot disable printing\n");
834 1.1 cgd else {
835 1.1 cgd upstat("printing disabled\n");
836 1.1 cgd printf("\tprinting disabled\n");
837 1.1 cgd }
838 1.1 cgd } else if (errno == ENOENT) {
839 1.1 cgd if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
840 1.1 cgd printf("\tcannot create lock file\n");
841 1.1 cgd else {
842 1.8 mrg (void)close(fd);
843 1.1 cgd upstat("printing disabled\n");
844 1.1 cgd printf("\tprinting disabled\n");
845 1.1 cgd }
846 1.1 cgd } else
847 1.1 cgd printf("\tcannot stat lock file\n");
848 1.5 hpeyerl seteuid(uid);
849 1.1 cgd }
850 1.1 cgd
851 1.1 cgd struct queue **queue;
852 1.1 cgd int nitems;
853 1.1 cgd time_t mtime;
854 1.1 cgd
855 1.1 cgd /*
856 1.1 cgd * Put the specified jobs at the top of printer queue.
857 1.1 cgd */
858 1.4 cgd void
859 1.14 wiz topq(int argc, char *argv[])
860 1.1 cgd {
861 1.12 mrg int i;
862 1.1 cgd struct stat stbuf;
863 1.18 christos int changed;
864 1.1 cgd
865 1.1 cgd if (argc < 3) {
866 1.1 cgd printf("Usage: topq printer [jobnum ...] [user ...]\n");
867 1.1 cgd return;
868 1.1 cgd }
869 1.1 cgd
870 1.1 cgd --argc;
871 1.1 cgd printer = *++argv;
872 1.18 christos if (!getcapdesc())
873 1.1 cgd return;
874 1.4 cgd
875 1.18 christos getcaps();
876 1.1 cgd printf("%s:\n", printer);
877 1.1 cgd
878 1.5 hpeyerl seteuid(euid);
879 1.1 cgd if (chdir(SD) < 0) {
880 1.1 cgd printf("\tcannot chdir to %s\n", SD);
881 1.5 hpeyerl goto out;
882 1.1 cgd }
883 1.5 hpeyerl seteuid(uid);
884 1.1 cgd nitems = getq(&queue);
885 1.1 cgd if (nitems == 0)
886 1.1 cgd return;
887 1.1 cgd changed = 0;
888 1.1 cgd mtime = queue[0]->q_time;
889 1.1 cgd for (i = argc; --i; ) {
890 1.1 cgd if (doarg(argv[i]) == 0) {
891 1.1 cgd printf("\tjob %s is not in the queue\n", argv[i]);
892 1.1 cgd continue;
893 1.1 cgd } else
894 1.1 cgd changed++;
895 1.1 cgd }
896 1.1 cgd for (i = 0; i < nitems; i++)
897 1.1 cgd free(queue[i]);
898 1.1 cgd free(queue);
899 1.1 cgd if (!changed) {
900 1.1 cgd printf("\tqueue order unchanged\n");
901 1.1 cgd return;
902 1.1 cgd }
903 1.1 cgd /*
904 1.1 cgd * Turn on the public execute bit of the lock file to
905 1.1 cgd * get lpd to rebuild the queue after the current job.
906 1.1 cgd */
907 1.5 hpeyerl seteuid(euid);
908 1.1 cgd if (changed && stat(LO, &stbuf) >= 0)
909 1.8 mrg (void)chmod(LO, (stbuf.st_mode & 0777) | 01);
910 1.5 hpeyerl
911 1.5 hpeyerl out:
912 1.5 hpeyerl seteuid(uid);
913 1.1 cgd }
914 1.1 cgd
915 1.1 cgd /*
916 1.1 cgd * Reposition the job by changing the modification time of
917 1.1 cgd * the control file.
918 1.1 cgd */
919 1.4 cgd static int
920 1.14 wiz touch(struct queue *q)
921 1.1 cgd {
922 1.1 cgd struct timeval tvp[2];
923 1.5 hpeyerl int ret;
924 1.1 cgd
925 1.1 cgd tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
926 1.1 cgd tvp[0].tv_usec = tvp[1].tv_usec = 0;
927 1.5 hpeyerl seteuid(euid);
928 1.5 hpeyerl ret = utimes(q->q_name, tvp);
929 1.5 hpeyerl seteuid(uid);
930 1.5 hpeyerl return (ret);
931 1.1 cgd }
932 1.1 cgd
933 1.1 cgd /*
934 1.1 cgd * Checks if specified job name is in the printer's queue.
935 1.1 cgd * Returns: negative (-1) if argument name is not in the queue.
936 1.1 cgd */
937 1.4 cgd static int
938 1.18 christos doarg(const char *job)
939 1.1 cgd {
940 1.12 mrg struct queue **qq;
941 1.12 mrg int jobnum, n;
942 1.18 christos char *cp;
943 1.18 christos const char *machine;
944 1.1 cgd int cnt = 0;
945 1.1 cgd FILE *fp;
946 1.1 cgd
947 1.1 cgd /*
948 1.1 cgd * Look for a job item consisting of system name, colon, number
949 1.1 cgd * (example: ucbarpa:114)
950 1.1 cgd */
951 1.12 mrg if ((cp = strchr(job, ':')) != NULL) {
952 1.1 cgd machine = job;
953 1.1 cgd *cp++ = '\0';
954 1.1 cgd job = cp;
955 1.1 cgd } else
956 1.1 cgd machine = NULL;
957 1.1 cgd
958 1.1 cgd /*
959 1.1 cgd * Check for job specified by number (example: 112 or 235ucbarpa).
960 1.1 cgd */
961 1.17 dsl if (isdigit((unsigned char)*job)) {
962 1.1 cgd jobnum = 0;
963 1.1 cgd do
964 1.1 cgd jobnum = jobnum * 10 + (*job++ - '0');
965 1.17 dsl while (isdigit((unsigned char)*job));
966 1.1 cgd for (qq = queue + nitems; --qq >= queue; ) {
967 1.1 cgd n = 0;
968 1.17 dsl for (cp = (*qq)->q_name+3; isdigit((unsigned char)*cp); )
969 1.1 cgd n = n * 10 + (*cp++ - '0');
970 1.1 cgd if (jobnum != n)
971 1.1 cgd continue;
972 1.1 cgd if (*job && strcmp(job, cp) != 0)
973 1.1 cgd continue;
974 1.1 cgd if (machine != NULL && strcmp(machine, cp) != 0)
975 1.1 cgd continue;
976 1.1 cgd if (touch(*qq) == 0) {
977 1.1 cgd printf("\tmoved %s\n", (*qq)->q_name);
978 1.1 cgd cnt++;
979 1.1 cgd }
980 1.1 cgd }
981 1.1 cgd return(cnt);
982 1.1 cgd }
983 1.1 cgd /*
984 1.1 cgd * Process item consisting of owner's name (example: henry).
985 1.1 cgd */
986 1.1 cgd for (qq = queue + nitems; --qq >= queue; ) {
987 1.5 hpeyerl seteuid(euid);
988 1.5 hpeyerl fp = fopen((*qq)->q_name, "r");
989 1.5 hpeyerl seteuid(uid);
990 1.5 hpeyerl if (fp == NULL)
991 1.1 cgd continue;
992 1.1 cgd while (getline(fp) > 0)
993 1.1 cgd if (line[0] == 'P')
994 1.1 cgd break;
995 1.8 mrg (void)fclose(fp);
996 1.1 cgd if (line[0] != 'P' || strcmp(job, line+1) != 0)
997 1.1 cgd continue;
998 1.1 cgd if (touch(*qq) == 0) {
999 1.1 cgd printf("\tmoved %s\n", (*qq)->q_name);
1000 1.1 cgd cnt++;
1001 1.1 cgd }
1002 1.1 cgd }
1003 1.1 cgd return(cnt);
1004 1.1 cgd }
1005 1.1 cgd
1006 1.1 cgd /*
1007 1.1 cgd * Enable everything and start printer (undo `down').
1008 1.1 cgd */
1009 1.4 cgd void
1010 1.14 wiz up(int argc, char *argv[])
1011 1.1 cgd {
1012 1.18 christos int c;
1013 1.12 mrg char *cp1, *cp2;
1014 1.1 cgd char prbuf[100];
1015 1.1 cgd
1016 1.1 cgd if (argc == 1) {
1017 1.1 cgd printf("Usage: up {all | printer ...}\n");
1018 1.1 cgd return;
1019 1.1 cgd }
1020 1.1 cgd if (argc == 2 && !strcmp(argv[1], "all")) {
1021 1.1 cgd printer = prbuf;
1022 1.4 cgd while (cgetnext(&bp, printcapdb) > 0) {
1023 1.1 cgd cp1 = prbuf;
1024 1.4 cgd cp2 = bp;
1025 1.12 mrg while ((c = *cp2++) && c != '|' && c != ':' &&
1026 1.12 mrg (cp1 - prbuf) < sizeof(prbuf))
1027 1.1 cgd *cp1++ = c;
1028 1.1 cgd *cp1 = '\0';
1029 1.1 cgd startpr(2);
1030 1.1 cgd }
1031 1.1 cgd return;
1032 1.1 cgd }
1033 1.1 cgd while (--argc) {
1034 1.1 cgd printer = *++argv;
1035 1.18 christos if (!getcapdesc())
1036 1.1 cgd continue;
1037 1.1 cgd startpr(2);
1038 1.1 cgd }
1039 1.1 cgd }
1040