xref: /openbsd-src/usr.sbin/lpr/common_source/startdaemon.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: startdaemon.c,v 1.6 2001/12/06 03:12:30 ericj 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.6 2001/12/06 03:12:30 ericj 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 /*
59  * Tell the printer daemon that there are new files in the spool directory.
60  */
61 
62 int
63 startdaemon(printer)
64 	char *printer;
65 {
66 	struct sockaddr_un un;
67 	int s, n;
68 	char buf[BUFSIZ];
69 
70 	s = socket(AF_UNIX, SOCK_STREAM, 0);
71 	if (s < 0) {
72 		perror("socket");
73 		return(0);
74 	}
75 	memset(&un, 0, sizeof(un));
76 	un.sun_family = AF_UNIX;
77 	strcpy(un.sun_path, _PATH_SOCKETNAME);
78 #ifndef SUN_LEN
79 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
80 #endif
81 	seteuid(euid);
82 	siginterrupt(SIGINT, 1);
83 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
84 		if (errno == EINTR && gotintr) {
85 			siginterrupt(SIGINT, 0);
86 			seteuid(uid);
87 			close(s);
88 			return(0);
89 		}
90 		siginterrupt(SIGINT, 0);
91 		seteuid(uid);
92 		perror("connect");
93 		(void) close(s);
94 		return(0);
95 	}
96 	siginterrupt(SIGINT, 0);
97 	seteuid(uid);
98 	if (snprintf(buf, sizeof buf, "\1%s\n", printer) > sizeof buf-1) {
99 		close(s);
100 		return (0);
101 	}
102 	n = strlen(buf);
103 
104 	/* XXX atomicio inside siginterrupt? */
105 	if (write(s, buf, n) != n) {
106 		perror("write");
107 		(void) close(s);
108 		return(0);
109 	}
110 	if (read(s, buf, 1) == 1) {
111 		if (buf[0] == '\0') {		/* everything is OK */
112 			(void) close(s);
113 			return(1);
114 		}
115 		putchar(buf[0]);
116 	}
117 	while ((n = read(s, buf, sizeof(buf))) > 0)
118 		fwrite(buf, 1, n, stdout);
119 
120 	(void) close(s);
121 	return(0);
122 }
123