xref: /openbsd-src/usr.sbin/lpr/common_source/startdaemon.c (revision 0122b1e59c997166176c6c63762011bd4a071ee2)
1 /*	$OpenBSD: startdaemon.c,v 1.5 2001/11/23 03:58:17 deraadt 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 #if 0
38 static const char sccsid[] = "@(#)startdaemon.c	8.2 (Berkeley) 4/17/94";
39 #else
40 static const char rcsid[] = "$OpenBSD: startdaemon.c,v 1.5 2001/11/23 03:58:17 deraadt Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
47 
48 #include <dirent.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include "lp.h"
54 #include "pathnames.h"
55 
56 extern uid_t	uid, euid;
57 
58 static void perr __P((char *));
59 
60 /*
61  * Tell the printer daemon that there are new files in the spool directory.
62  */
63 
64 int
65 startdaemon(printer)
66 	char *printer;
67 {
68 	struct sockaddr_un un;
69 	int s, n;
70 	char buf[BUFSIZ];
71 
72 	s = socket(AF_UNIX, SOCK_STREAM, 0);
73 	if (s < 0) {
74 		perr("socket");
75 		return(0);
76 	}
77 	memset(&un, 0, sizeof(un));
78 	un.sun_family = AF_UNIX;
79 	strcpy(un.sun_path, _PATH_SOCKETNAME);
80 #ifndef SUN_LEN
81 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
82 #endif
83 	seteuid(euid);
84 	siginterrupt(SIGINT, 1);
85 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
86 		if (errno == EINTR && gotintr) {
87 			siginterrupt(SIGINT, 0);
88 			seteuid(uid);
89 			close(s);
90 			return(0);
91 		}
92 		siginterrupt(SIGINT, 0);
93 		seteuid(uid);
94 		perr("connect");
95 		(void) close(s);
96 		return(0);
97 	}
98 	siginterrupt(SIGINT, 0);
99 	seteuid(uid);
100 	if (snprintf(buf, sizeof buf, "\1%s\n", printer) > sizeof buf-1) {
101 		close(s);
102 		return (0);
103 	}
104 	n = strlen(buf);
105 
106 	/* XXX atomicio inside siginterrupt? */
107 	if (write(s, buf, n) != n) {
108 		perr("write");
109 		(void) close(s);
110 		return(0);
111 	}
112 	if (read(s, buf, 1) == 1) {
113 		if (buf[0] == '\0') {		/* everything is OK */
114 			(void) close(s);
115 			return(1);
116 		}
117 		putchar(buf[0]);
118 	}
119 	while ((n = read(s, buf, sizeof(buf))) > 0)
120 		fwrite(buf, 1, n, stdout);
121 
122 	(void) close(s);
123 	return(0);
124 }
125 
126 static void
127 perr(msg)
128 	char *msg;
129 {
130 	extern char *name;
131 
132 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
133 }
134