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*37968Sbostic static char sccsid[] = "@(#)startdaemon.c 5.4 (Berkeley) 05/11/89"; 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> 29*37968Sbostic #include <stdio.h> 3013443Sralph #include "lp.local.h" 31*37968Sbostic #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; 46*37968Sbostic 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 { 7613443Sralph extern char *name; 7712743Sralph extern int sys_nerr; 7812743Sralph extern char *sys_errlist[]; 7913443Sralph extern int errno; 8012743Sralph 8113443Sralph printf("%s: %s: ", name, msg); 8212743Sralph fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout); 8312743Sralph putchar('\n'); 8412743Sralph } 85