1*0eeca5dfSchristos /* $NetBSD: startdaemon.c,v 1.15 2007/05/16 20:45:45 christos Exp $ */
221908ddbSjtc
361f28255Scgd /*
42847add2Scgd * Copyright (c) 1983, 1993, 1994
52847add2Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32fe7ed7ceSmrg #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34fe7ed7ceSmrg #if 0
352847add2Scgd static char sccsid[] = "@(#)startdaemon.c 8.2 (Berkeley) 4/17/94";
36fe7ed7ceSmrg #else
37*0eeca5dfSchristos __RCSID("$NetBSD: startdaemon.c,v 1.15 2007/05/16 20:45:45 christos Exp $");
38fe7ed7ceSmrg #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
412847add2Scgd
422847add2Scgd #include <sys/param.h>
432847add2Scgd #include <sys/socket.h>
442847add2Scgd #include <sys/un.h>
452847add2Scgd
462847add2Scgd #include <dirent.h>
472847add2Scgd #include <errno.h>
482847add2Scgd #include <stdio.h>
492847add2Scgd #include <unistd.h>
502847add2Scgd #include <string.h>
51fe7ed7ceSmrg #include <err.h>
522847add2Scgd #include "lp.h"
532847add2Scgd #include "pathnames.h"
542847add2Scgd
558e41ca80Shpeyerl extern uid_t uid, euid;
568e41ca80Shpeyerl
5761f28255Scgd /*
5861f28255Scgd * Tell the printer daemon that there are new files in the spool directory.
5961f28255Scgd */
6061f28255Scgd
612847add2Scgd int
startdaemon(const char * pname)6204723c3fSchristos startdaemon(const char *pname)
6361f28255Scgd {
642847add2Scgd struct sockaddr_un un;
6523b9fac0Smrg int s;
66*0eeca5dfSchristos int n;
6761f28255Scgd char buf[BUFSIZ];
6861f28255Scgd
69786b86d7Slukem s = socket(AF_LOCAL, SOCK_STREAM, 0);
7061f28255Scgd if (s < 0) {
71fe7ed7ceSmrg warn("socket");
7261f28255Scgd return(0);
7361f28255Scgd }
742847add2Scgd memset(&un, 0, sizeof(un));
75786b86d7Slukem un.sun_family = AF_LOCAL;
76077acf50Smrg strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
772847add2Scgd #ifndef SUN_LEN
782847add2Scgd #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
792847add2Scgd #endif
808e41ca80Shpeyerl seteuid(euid);
8123b9fac0Smrg if (connect(s, (const struct sockaddr *)&un,
8223b9fac0Smrg (int)SUN_LEN(&un)) < 0) {
838e41ca80Shpeyerl seteuid(uid);
84fe7ed7ceSmrg warn("connect");
8561f28255Scgd (void)close(s);
8661f28255Scgd return(0);
8761f28255Scgd }
888e41ca80Shpeyerl seteuid(uid);
8904723c3fSchristos n = snprintf(buf, sizeof(buf), "\1%s\n", pname);
9061f28255Scgd if (write(s, buf, n) != n) {
91fe7ed7ceSmrg warn("write");
9261f28255Scgd (void)close(s);
9361f28255Scgd return(0);
9461f28255Scgd }
9561f28255Scgd if (read(s, buf, 1) == 1) {
9661f28255Scgd if (buf[0] == '\0') { /* everything is OK */
9761f28255Scgd (void)close(s);
9861f28255Scgd return(1);
9961f28255Scgd }
10061f28255Scgd putchar(buf[0]);
10161f28255Scgd }
10261f28255Scgd while ((n = read(s, buf, sizeof(buf))) > 0)
10361f28255Scgd fwrite(buf, 1, n, stdout);
10461f28255Scgd (void)close(s);
10561f28255Scgd return(0);
10661f28255Scgd }
107