1 /* $OpenBSD: startdaemon.c,v 1.12 2007/05/01 16:32:06 stevesk 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 #ifndef lint 34 #if 0 35 static const char sccsid[] = "@(#)startdaemon.c 8.2 (Berkeley) 4/17/94"; 36 #else 37 static const char rcsid[] = "$OpenBSD: startdaemon.c,v 1.12 2007/05/01 16:32:06 stevesk Exp $"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/socket.h> 43 #include <sys/un.h> 44 45 #include <dirent.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <stdio.h> 49 #include <unistd.h> 50 #include <string.h> 51 #include <signal.h> 52 53 #include "lp.h" 54 #include "pathnames.h" 55 56 /* 57 * Tell the printer daemon that there are new files in the spool directory. 58 */ 59 int 60 startdaemon(char *printer) 61 { 62 struct sockaddr_un un; 63 int s; 64 size_t n; 65 char buf[BUFSIZ]; 66 67 s = socket(AF_LOCAL, SOCK_STREAM, 0); 68 if (s < 0) { 69 warn("socket"); 70 return(0); 71 } 72 memset(&un, 0, sizeof(un)); 73 un.sun_family = AF_LOCAL; 74 strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path)); 75 siginterrupt(SIGINT, 1); 76 PRIV_START; 77 if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 78 if (errno == EINTR && gotintr) { 79 PRIV_END; 80 siginterrupt(SIGINT, 0); 81 close(s); 82 return(0); 83 } 84 PRIV_END; 85 siginterrupt(SIGINT, 0); 86 warn("connect"); 87 (void)close(s); 88 return(0); 89 } 90 PRIV_END; 91 siginterrupt(SIGINT, 0); 92 if ((n = snprintf(buf, sizeof(buf), "\1%s\n", printer)) >= sizeof(buf) || 93 n == -1) { 94 close(s); 95 return (0); 96 } 97 98 /* XXX atomicio inside siginterrupt? */ 99 if (write(s, buf, n) != n) { 100 warn("write"); 101 (void)close(s); 102 return(0); 103 } 104 if (read(s, buf, 1) == 1) { 105 if (buf[0] == '\0') { /* everything is OK */ 106 (void)close(s); 107 return(1); 108 } 109 putchar(buf[0]); 110 } 111 while ((n = read(s, buf, sizeof(buf))) > 0) 112 fwrite(buf, 1, n, stdout); 113 (void)close(s); 114 return(0); 115 } 116