xref: /openbsd-src/usr.sbin/lpr/common_source/startdaemon.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: startdaemon.c,v 1.13 2009/10/27 23:59:51 deraadt Exp $	*/
2 /*	$NetBSD: startdaemon.c,v 1.10 1998/07/18 05:04:39 lukem Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 
37 #include <dirent.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <signal.h>
44 
45 #include "lp.h"
46 #include "pathnames.h"
47 
48 /*
49  * Tell the printer daemon that there are new files in the spool directory.
50  */
51 int
52 startdaemon(char *printer)
53 {
54 	struct sockaddr_un un;
55 	int s;
56 	size_t n;
57 	char buf[BUFSIZ];
58 
59 	s = socket(AF_LOCAL, SOCK_STREAM, 0);
60 	if (s < 0) {
61 		warn("socket");
62 		return(0);
63 	}
64 	memset(&un, 0, sizeof(un));
65 	un.sun_family = AF_LOCAL;
66 	strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path));
67 	siginterrupt(SIGINT, 1);
68 	PRIV_START;
69 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
70 		if (errno == EINTR && gotintr) {
71 			PRIV_END;
72 			siginterrupt(SIGINT, 0);
73 			close(s);
74 			return(0);
75 		}
76 		PRIV_END;
77 		siginterrupt(SIGINT, 0);
78 		warn("connect");
79 		(void)close(s);
80 		return(0);
81 	}
82 	PRIV_END;
83 	siginterrupt(SIGINT, 0);
84 	if ((n = snprintf(buf, sizeof(buf), "\1%s\n", printer)) >= sizeof(buf) ||
85 	    n == -1) {
86 		close(s);
87 		return (0);
88 	}
89 
90 	/* XXX atomicio inside siginterrupt? */
91 	if (write(s, buf, n) != n) {
92 		warn("write");
93 		(void)close(s);
94 		return(0);
95 	}
96 	if (read(s, buf, 1) == 1) {
97 		if (buf[0] == '\0') {		/* everything is OK */
98 			(void)close(s);
99 			return(1);
100 		}
101 		putchar(buf[0]);
102 	}
103 	while ((n = read(s, buf, sizeof(buf))) > 0)
104 		fwrite(buf, 1, n, stdout);
105 	(void)close(s);
106 	return(0);
107 }
108