122440Sdist /*
222440Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
542801Sbostic  * %sccs.include.redist.c%
622440Sdist  */
722440Sdist 
813955Ssam #ifndef lint
9*46910Sbostic static char sccsid[] = "@(#)startdaemon.c	5.7 (Berkeley) 03/02/91";
1034203Sbostic #endif /* not lint */
1113955Ssam 
1212114Sralph /*
1312114Sralph  * Tell the printer daemon that there are new files in the spool directory.
1412114Sralph  */
1512114Sralph 
1613443Sralph #include <sys/types.h>
1713443Sralph #include <sys/socket.h>
1813443Sralph #include <sys/un.h>
1937968Sbostic #include <stdio.h>
2013443Sralph #include "lp.local.h"
2137968Sbostic #include "pathnames.h"
2212114Sralph 
2313443Sralph startdaemon(printer)
2413443Sralph 	char *printer;
2512114Sralph {
2613443Sralph 	struct sockaddr_un sun;
2713443Sralph 	register int s, n;
2812114Sralph 	char buf[BUFSIZ];
29*46910Sbostic 	static void perr();
3012114Sralph 
3113443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
3213443Sralph 	if (s < 0) {
3313443Sralph 		perr("socket");
3412114Sralph 		return(0);
3512743Sralph 	}
3613443Sralph 	sun.sun_family = AF_UNIX;
3737968Sbostic 	strcpy(sun.sun_path, _PATH_SOCKETNAME);
38*46910Sbostic 	if (connect(s, (struct sockaddr *)&sun, strlen(sun.sun_path) + 2) < 0) {
3913443Sralph 		perr("connect");
4013443Sralph 		(void) close(s);
4113443Sralph 		return(0);
4213443Sralph 	}
4312114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
4413443Sralph 	n = strlen(buf);
4513443Sralph 	if (write(s, buf, n) != n) {
4613443Sralph 		perr("write");
4713443Sralph 		(void) close(s);
4812114Sralph 		return(0);
4912114Sralph 	}
5013443Sralph 	if (read(s, buf, 1) == 1) {
5113443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
5213443Sralph 			(void) close(s);
5313443Sralph 			return(1);
5413443Sralph 		}
5513443Sralph 		putchar(buf[0]);
5612114Sralph 	}
5713443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
5813443Sralph 		fwrite(buf, 1, n, stdout);
5913443Sralph 	(void) close(s);
6013443Sralph 	return(0);
6112114Sralph }
6212743Sralph 
63*46910Sbostic static void
6413443Sralph perr(msg)
6513443Sralph 	char *msg;
6612743Sralph {
6742424Sbostic 	extern int errno;
6813443Sralph 	extern char *name;
6942424Sbostic 	char *strerror();
7012743Sralph 
7142424Sbostic 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
7212743Sralph }
73