startdaemon.c revision 1.8 1 /* $NetBSD: startdaemon.c,v 1.8 1996/12/09 09:57:43 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993, 1994
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 #ifndef lint
37 static char sccsid[] = "@(#)startdaemon.c 8.2 (Berkeley) 4/17/94";
38 #endif /* not lint */
39
40
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44
45 #include <dirent.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include "lp.h"
51 #include "pathnames.h"
52
53 extern uid_t uid, euid;
54
55 static void perr __P((char *));
56
57 /*
58 * Tell the printer daemon that there are new files in the spool directory.
59 */
60
61 int
62 startdaemon(printer)
63 char *printer;
64 {
65 struct sockaddr_un un;
66 register int s, n;
67 char buf[BUFSIZ];
68
69 s = socket(AF_UNIX, SOCK_STREAM, 0);
70 if (s < 0) {
71 perr("socket");
72 return(0);
73 }
74 memset(&un, 0, sizeof(un));
75 un.sun_family = AF_UNIX;
76 strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
77 #ifndef SUN_LEN
78 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
79 #endif
80 seteuid(euid);
81 if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
82 seteuid(uid);
83 perr("connect");
84 (void)close(s);
85 return(0);
86 }
87 seteuid(uid);
88 n = snprintf(buf, sizeof(buf), "\1%s\n", printer);
89 if (write(s, buf, n) != n) {
90 perr("write");
91 (void)close(s);
92 return(0);
93 }
94 if (read(s, buf, 1) == 1) {
95 if (buf[0] == '\0') { /* everything is OK */
96 (void)close(s);
97 return(1);
98 }
99 putchar(buf[0]);
100 }
101 while ((n = read(s, buf, sizeof(buf))) > 0)
102 fwrite(buf, 1, n, stdout);
103 (void)close(s);
104 return(0);
105 }
106
107 static void
108 perr(msg)
109 char *msg;
110 {
111 extern char *name;
112
113 (void)printf("%s: %s: %s\n", name, msg, strerror(errno));
114 }
115