Home | History | Annotate | Line # | Download | only in common_source
startdaemon.c revision 1.7
      1 /*	$NetBSD: startdaemon.c,v 1.7 1995/11/28 19:43:32 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char sccsid[] = "@(#)startdaemon.c	8.2 (Berkeley) 4/17/94";
     38 #endif /* not lint */
     39 
     40 
     41 #include <sys/param.h>
     42 #include <sys/socket.h>
     43 #include <sys/un.h>
     44 
     45 #include <dirent.h>
     46 #include <errno.h>
     47 #include <stdio.h>
     48 #include <unistd.h>
     49 #include <string.h>
     50 #include "lp.h"
     51 #include "pathnames.h"
     52 
     53 extern uid_t	uid, euid;
     54 
     55 static void perr __P((char *));
     56 
     57 /*
     58  * Tell the printer daemon that there are new files in the spool directory.
     59  */
     60 
     61 int
     62 startdaemon(printer)
     63 	char *printer;
     64 {
     65 	struct sockaddr_un un;
     66 	register int s, n;
     67 	char buf[BUFSIZ];
     68 
     69 	s = socket(AF_UNIX, SOCK_STREAM, 0);
     70 	if (s < 0) {
     71 		perr("socket");
     72 		return(0);
     73 	}
     74 	memset(&un, 0, sizeof(un));
     75 	un.sun_family = AF_UNIX;
     76 	strcpy(un.sun_path, _PATH_SOCKETNAME);
     77 #ifndef SUN_LEN
     78 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
     79 #endif
     80 	seteuid(euid);
     81 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
     82 		seteuid(uid);
     83 		perr("connect");
     84 		(void) close(s);
     85 		return(0);
     86 	}
     87 	seteuid(uid);
     88 	(void) sprintf(buf, "\1%s\n", printer);
     89 	n = strlen(buf);
     90 	if (write(s, buf, n) != n) {
     91 		perr("write");
     92 		(void) close(s);
     93 		return(0);
     94 	}
     95 	if (read(s, buf, 1) == 1) {
     96 		if (buf[0] == '\0') {		/* everything is OK */
     97 			(void) close(s);
     98 			return(1);
     99 		}
    100 		putchar(buf[0]);
    101 	}
    102 	while ((n = read(s, buf, sizeof(buf))) > 0)
    103 		fwrite(buf, 1, n, stdout);
    104 	(void) close(s);
    105 	return(0);
    106 }
    107 
    108 static void
    109 perr(msg)
    110 	char *msg;
    111 {
    112 	extern char *name;
    113 
    114 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
    115 }
    116