122440Sdist /*
2*66851Sbostic * Copyright (c) 1983, 1993, 1994
361840Sbostic * The Regents of the University of California. All rights reserved.
434203Sbostic *
542801Sbostic * %sccs.include.redist.c%
622440Sdist */
722440Sdist
813955Ssam #ifndef lint
9*66851Sbostic static char sccsid[] = "@(#)startdaemon.c 8.2 (Berkeley) 04/17/94";
1034203Sbostic #endif /* not lint */
1113955Ssam
1212114Sralph
1355470Sbostic #include <sys/param.h>
1413443Sralph #include <sys/socket.h>
1513443Sralph #include <sys/un.h>
1655470Sbostic
1755470Sbostic #include <dirent.h>
1855470Sbostic #include <errno.h>
1937968Sbostic #include <stdio.h>
2055470Sbostic #include <unistd.h>
2155470Sbostic #include <string.h>
2255470Sbostic #include "lp.h"
2337968Sbostic #include "pathnames.h"
2412114Sralph
2555470Sbostic static void perr __P((char *));
2655470Sbostic
2755470Sbostic /*
2855470Sbostic * Tell the printer daemon that there are new files in the spool directory.
2955470Sbostic */
3055470Sbostic
3155470Sbostic int
startdaemon(printer)3213443Sralph startdaemon(printer)
3313443Sralph char *printer;
3412114Sralph {
3550885Storek struct sockaddr_un un;
3613443Sralph register int s, n;
3712114Sralph char buf[BUFSIZ];
3812114Sralph
3913443Sralph s = socket(AF_UNIX, SOCK_STREAM, 0);
4013443Sralph if (s < 0) {
4113443Sralph perr("socket");
4212114Sralph return(0);
4312743Sralph }
44*66851Sbostic memset(&un, 0, sizeof(un));
4550885Storek un.sun_family = AF_UNIX;
4650885Storek strcpy(un.sun_path, _PATH_SOCKETNAME);
4758239Storek #ifndef SUN_LEN
4858239Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
4958239Storek #endif
5058239Storek if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
5113443Sralph perr("connect");
5213443Sralph (void) close(s);
5313443Sralph return(0);
5413443Sralph }
5512114Sralph (void) sprintf(buf, "\1%s\n", printer);
5613443Sralph n = strlen(buf);
5713443Sralph if (write(s, buf, n) != n) {
5813443Sralph perr("write");
5913443Sralph (void) close(s);
6012114Sralph return(0);
6112114Sralph }
6213443Sralph if (read(s, buf, 1) == 1) {
6313443Sralph if (buf[0] == '\0') { /* everything is OK */
6413443Sralph (void) close(s);
6513443Sralph return(1);
6613443Sralph }
6713443Sralph putchar(buf[0]);
6812114Sralph }
6913443Sralph while ((n = read(s, buf, sizeof(buf))) > 0)
7013443Sralph fwrite(buf, 1, n, stdout);
7113443Sralph (void) close(s);
7213443Sralph return(0);
7312114Sralph }
7412743Sralph
7546910Sbostic static void
perr(msg)7613443Sralph perr(msg)
7713443Sralph char *msg;
7812743Sralph {
7913443Sralph extern char *name;
8012743Sralph
8142424Sbostic (void)printf("%s: %s: %s\n", name, msg, strerror(errno));
8212743Sralph }
83