1 /* $OpenBSD: startdaemon.c,v 1.9 2002/07/27 22:30:00 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static const char sccsid[] = "@(#)startdaemon.c 8.2 (Berkeley) 4/17/94"; 40 #else 41 static const char rcsid[] = "$OpenBSD: startdaemon.c,v 1.9 2002/07/27 22:30:00 deraadt Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/socket.h> 47 #include <sys/un.h> 48 49 #include <dirent.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <stdio.h> 53 #include <unistd.h> 54 #include <string.h> 55 #include <signal.h> 56 57 #include "lp.h" 58 #include "pathnames.h" 59 60 /* 61 * Tell the printer daemon that there are new files in the spool directory. 62 */ 63 int 64 startdaemon(char *printer) 65 { 66 struct sockaddr_un un; 67 int s; 68 size_t n; 69 char buf[BUFSIZ]; 70 71 s = socket(AF_LOCAL, SOCK_STREAM, 0); 72 if (s < 0) { 73 warn("socket"); 74 return(0); 75 } 76 memset(&un, 0, sizeof(un)); 77 un.sun_family = AF_LOCAL; 78 strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path)); 79 #ifndef SUN_LEN 80 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 81 #endif 82 siginterrupt(SIGINT, 1); 83 PRIV_START; 84 if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 85 if (errno == EINTR && gotintr) { 86 PRIV_END; 87 siginterrupt(SIGINT, 0); 88 close(s); 89 return(0); 90 } 91 PRIV_END; 92 siginterrupt(SIGINT, 0); 93 perror("connect"); 94 (void)close(s); 95 return(0); 96 } 97 PRIV_END; 98 siginterrupt(SIGINT, 0); 99 if ((n = snprintf(buf, sizeof(buf), "\1%s\n", printer)) >= sizeof(buf) || 100 n == -1) { 101 close(s); 102 return (0); 103 } 104 105 /* XXX atomicio inside siginterrupt? */ 106 if (write(s, buf, n) != n) { 107 warn("write"); 108 (void)close(s); 109 return(0); 110 } 111 if (read(s, buf, 1) == 1) { 112 if (buf[0] == '\0') { /* everything is OK */ 113 (void)close(s); 114 return(1); 115 } 116 putchar(buf[0]); 117 } 118 while ((n = read(s, buf, sizeof(buf))) > 0) 119 fwrite(buf, 1, n, stdout); 120 (void)close(s); 121 return(0); 122 } 123