122440Sdist /*
222440Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
534203Sbostic  * Redistribution and use in source and binary forms are permitted
634936Sbostic  * provided that the above copyright notice and this paragraph are
734936Sbostic  * duplicated in all such forms and that any documentation,
834936Sbostic  * advertising materials, and other materials related to such
934936Sbostic  * distribution and use acknowledge that the software was developed
1034936Sbostic  * by the University of California, Berkeley.  The name of the
1134936Sbostic  * University may not be used to endorse or promote products derived
1234936Sbostic  * from this software without specific prior written permission.
1334936Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434936Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534936Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622440Sdist  */
1722440Sdist 
1813955Ssam #ifndef lint
19*42424Sbostic static char sccsid[] = "@(#)startdaemon.c	5.5 (Berkeley) 05/28/90";
2034203Sbostic #endif /* not lint */
2113955Ssam 
2212114Sralph /*
2312114Sralph  * Tell the printer daemon that there are new files in the spool directory.
2412114Sralph  */
2512114Sralph 
2613443Sralph #include <sys/types.h>
2713443Sralph #include <sys/socket.h>
2813443Sralph #include <sys/un.h>
2937968Sbostic #include <stdio.h>
3013443Sralph #include "lp.local.h"
3137968Sbostic #include "pathnames.h"
3212114Sralph 
3313443Sralph startdaemon(printer)
3413443Sralph 	char *printer;
3512114Sralph {
3613443Sralph 	struct sockaddr_un sun;
3713443Sralph 	register int s, n;
3812114Sralph 	char buf[BUFSIZ];
3912114Sralph 
4013443Sralph 	s = socket(AF_UNIX, SOCK_STREAM, 0);
4113443Sralph 	if (s < 0) {
4213443Sralph 		perr("socket");
4312114Sralph 		return(0);
4412743Sralph 	}
4513443Sralph 	sun.sun_family = AF_UNIX;
4637968Sbostic 	strcpy(sun.sun_path, _PATH_SOCKETNAME);
4713443Sralph 	if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
4813443Sralph 		perr("connect");
4913443Sralph 		(void) close(s);
5013443Sralph 		return(0);
5113443Sralph 	}
5212114Sralph 	(void) sprintf(buf, "\1%s\n", printer);
5313443Sralph 	n = strlen(buf);
5413443Sralph 	if (write(s, buf, n) != n) {
5513443Sralph 		perr("write");
5613443Sralph 		(void) close(s);
5712114Sralph 		return(0);
5812114Sralph 	}
5913443Sralph 	if (read(s, buf, 1) == 1) {
6013443Sralph 		if (buf[0] == '\0') {		/* everything is OK */
6113443Sralph 			(void) close(s);
6213443Sralph 			return(1);
6313443Sralph 		}
6413443Sralph 		putchar(buf[0]);
6512114Sralph 	}
6613443Sralph 	while ((n = read(s, buf, sizeof(buf))) > 0)
6713443Sralph 		fwrite(buf, 1, n, stdout);
6813443Sralph 	(void) close(s);
6913443Sralph 	return(0);
7012114Sralph }
7112743Sralph 
7212743Sralph static
7313443Sralph perr(msg)
7413443Sralph 	char *msg;
7512743Sralph {
76*42424Sbostic 	extern int errno;
7713443Sralph 	extern char *name;
78*42424Sbostic 	char *strerror();
7912743Sralph 
80*42424Sbostic 	(void)printf("%s: %s: %s\n", name, msg, strerror(errno));
8112743Sralph }
82