1 /*
2  * Copyright (c) 1983, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)startdaemon.c	8.2 (Berkeley) 04/17/94";
10 #endif /* not lint */
11 
12 
13 #include <sys/param.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 
17 #include <dirent.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include "lp.h"
23 #include "pathnames.h"
24 
25 static void perr __P((char *));
26 
27 /*
28  * Tell the printer daemon that there are new files in the spool directory.
29  */
30 
31 int
32 startdaemon(printer)
33 	char *printer;
34 {
35 	struct sockaddr_un un;
36 	register int s, n;
37 	char buf[BUFSIZ];
38 
39 	s = socket(AF_UNIX, SOCK_STREAM, 0);
40 	if (s < 0) {
41 		perr("socket");
42 		return(0);
43 	}
44 	memset(&un, 0, sizeof(un));
45 	un.sun_family = AF_UNIX;
46 	strcpy(un.sun_path, _PATH_SOCKETNAME);
47 #ifndef SUN_LEN
48 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
49 #endif
50 	if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
51 		perr("connect");
52 		(void) close(s);
53 		return(0);
54 	}
55 	(void) sprintf(buf, "\1%s\n", printer);
56 	n = strlen(buf);
57 	if (write(s, buf, n) != n) {
58 		perr("write");
59 		(void) close(s);
60 		return(0);
61 	}
62 	if (read(s, buf, 1) == 1) {
63 		if (buf[0] == '\0') {		/* everything is OK */
64 			(void) close(s);
65 			return(1);
66 		}
67 		putchar(buf[0]);
68 	}
69 	while ((n = read(s, buf, sizeof(buf))) > 0)
70 		fwrite(buf, 1, n, stdout);
71 	(void) close(s);
72 	return(0);
73 }
74 
75 static void
76 perr(msg)
77 	char *msg;
78 {
79 	extern char *name;
80 
81 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
82 }
83