xref: /freebsd/usr.sbin/lpr/common_source/startdaemon.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4dea673e9SRodney W. Grimes  * Copyright (c) 1983, 1993, 1994
5dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dea673e9SRodney W. Grimes  *
7dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
9dea673e9SRodney W. Grimes  * are met:
10dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17dea673e9SRodney W. Grimes  *    without specific prior written permission.
18dea673e9SRodney W. Grimes  *
19dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29dea673e9SRodney W. Grimes  * SUCH DAMAGE.
30dea673e9SRodney W. Grimes  */
31dea673e9SRodney W. Grimes 
321f589b47SGarance A Drosehn #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
33dea673e9SRodney W. Grimes #include <sys/param.h>
34dea673e9SRodney W. Grimes #include <sys/socket.h>
354a1a0dbeSGarrett Wollman #include <sys/uio.h>
36dea673e9SRodney W. Grimes #include <sys/un.h>
37dea673e9SRodney W. Grimes 
38dea673e9SRodney W. Grimes #include <dirent.h>
399b3fe531SPhilippe Charnier #include <err.h>
40dea673e9SRodney W. Grimes #include <stdio.h>
41dea673e9SRodney W. Grimes #include <string.h>
429b3fe531SPhilippe Charnier #include <unistd.h>
43dea673e9SRodney W. Grimes #include "lp.h"
44dea673e9SRodney W. Grimes #include "pathnames.h"
45dea673e9SRodney W. Grimes 
46dea673e9SRodney W. Grimes /*
47dea673e9SRodney W. Grimes  * Tell the printer daemon that there are new files in the spool directory.
48dea673e9SRodney W. Grimes  */
49dea673e9SRodney W. Grimes 
50dea673e9SRodney W. Grimes int
startdaemon(const struct printer * pp)51ba7a1ad7SGarance A Drosehn startdaemon(const struct printer *pp)
52dea673e9SRodney W. Grimes {
53dea673e9SRodney W. Grimes 	struct sockaddr_un un;
54dea673e9SRodney W. Grimes 	register int s, n;
55055c1315SGarance A Drosehn 	int connectres;
564a1a0dbeSGarrett Wollman 	char c;
57dea673e9SRodney W. Grimes 
584a1a0dbeSGarrett Wollman 	s = socket(PF_LOCAL, SOCK_STREAM, 0);
59dea673e9SRodney W. Grimes 	if (s < 0) {
609b3fe531SPhilippe Charnier 		warn("socket");
61dea673e9SRodney W. Grimes 		return(0);
62dea673e9SRodney W. Grimes 	}
63dea673e9SRodney W. Grimes 	memset(&un, 0, sizeof(un));
644a1a0dbeSGarrett Wollman 	un.sun_family = AF_LOCAL;
65dea673e9SRodney W. Grimes 	strcpy(un.sun_path, _PATH_SOCKETNAME);
66dea673e9SRodney W. Grimes #ifndef SUN_LEN
67dea673e9SRodney W. Grimes #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
68dea673e9SRodney W. Grimes #endif
691d1d4a47SEitan Adler 	PRIV_START
70055c1315SGarance A Drosehn 	connectres = connect(s, (struct sockaddr *)&un, SUN_LEN(&un));
711d1d4a47SEitan Adler 	PRIV_END
72055c1315SGarance A Drosehn 	if (connectres < 0) {
73055c1315SGarance A Drosehn 		warn("Unable to connect to %s", _PATH_SOCKETNAME);
74055c1315SGarance A Drosehn 		warnx("Check to see if the master 'lpd' process is running.");
75dea673e9SRodney W. Grimes 		(void) close(s);
76dea673e9SRodney W. Grimes 		return(0);
77dea673e9SRodney W. Grimes 	}
784a1a0dbeSGarrett Wollman 
794a1a0dbeSGarrett Wollman 	/*
804a1a0dbeSGarrett Wollman 	 * Avoid overruns without putting artificial limitations on
814a1a0dbeSGarrett Wollman 	 * the length.
824a1a0dbeSGarrett Wollman 	 */
834a1a0dbeSGarrett Wollman 	if (writel(s, "\1", pp->printer, "\n", (char *)0) <= 0) {
849b3fe531SPhilippe Charnier 		warn("write");
85dea673e9SRodney W. Grimes 		(void) close(s);
86dea673e9SRodney W. Grimes 		return(0);
87dea673e9SRodney W. Grimes 	}
884a1a0dbeSGarrett Wollman 	if (read(s, &c, 1) == 1) {
894a1a0dbeSGarrett Wollman 		if (c == '\0') {		/* everything is OK */
90dea673e9SRodney W. Grimes 			(void) close(s);
91dea673e9SRodney W. Grimes 			return(1);
92dea673e9SRodney W. Grimes 		}
934a1a0dbeSGarrett Wollman 		putchar(c);
94dea673e9SRodney W. Grimes 	}
954a1a0dbeSGarrett Wollman 	while ((n = read(s, &c, 1)) > 0)
964a1a0dbeSGarrett Wollman 		putchar(c);
97dea673e9SRodney W. Grimes 	(void) close(s);
98dea673e9SRodney W. Grimes 	return(0);
99dea673e9SRodney W. Grimes }
100