1331722Seadler/*
21553Srgrimes * Copyright (c) 1983, 1993, 1994
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
3029780Scharnier#if 0
31117592Sgad#ifndef lint
321553Srgrimesstatic char sccsid[] = "@(#)startdaemon.c	8.2 (Berkeley) 4/17/94";
33117592Sgad#endif /* not lint */
3429780Scharnier#endif
35117592Sgad
36117541Sgad#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
37117541Sgad__FBSDID("$FreeBSD$");
381553Srgrimes
391553Srgrimes#include <sys/param.h>
401553Srgrimes#include <sys/socket.h>
4131492Swollman#include <sys/uio.h>
421553Srgrimes#include <sys/un.h>
431553Srgrimes
441553Srgrimes#include <dirent.h>
4529780Scharnier#include <err.h>
461553Srgrimes#include <stdio.h>
4729780Scharnier#include <string.h>
481553Srgrimes#include <unistd.h>
491553Srgrimes#include "lp.h"
501553Srgrimes#include "pathnames.h"
511553Srgrimes
521553Srgrimes/*
531553Srgrimes * Tell the printer daemon that there are new files in the spool directory.
541553Srgrimes */
551553Srgrimes
561553Srgrimesint
5778146Sgadstartdaemon(const struct printer *pp)
581553Srgrimes{
591553Srgrimes	struct sockaddr_un un;
601553Srgrimes	register int s, n;
6195299Sgad	int connectres;
6231492Swollman	char c;
631553Srgrimes
6431492Swollman	s = socket(PF_LOCAL, SOCK_STREAM, 0);
651553Srgrimes	if (s < 0) {
6629780Scharnier		warn("socket");
671553Srgrimes		return(0);
681553Srgrimes	}
691553Srgrimes	memset(&un, 0, sizeof(un));
7031492Swollman	un.sun_family = AF_LOCAL;
711553Srgrimes	strcpy(un.sun_path, _PATH_SOCKETNAME);
721553Srgrimes#ifndef SUN_LEN
731553Srgrimes#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
741553Srgrimes#endif
75241852Seadler	PRIV_START
7695299Sgad	connectres = connect(s, (struct sockaddr *)&un, SUN_LEN(&un));
77241852Seadler	PRIV_END
7895299Sgad	if (connectres < 0) {
7995299Sgad		warn("Unable to connect to %s", _PATH_SOCKETNAME);
8095299Sgad		warnx("Check to see if the master 'lpd' process is running.");
811553Srgrimes		(void) close(s);
821553Srgrimes		return(0);
831553Srgrimes	}
8431492Swollman
8531492Swollman	/*
8631492Swollman	 * Avoid overruns without putting artificial limitations on
8731492Swollman	 * the length.
8831492Swollman	 */
8931492Swollman	if (writel(s, "\1", pp->printer, "\n", (char *)0) <= 0) {
9029780Scharnier		warn("write");
911553Srgrimes		(void) close(s);
921553Srgrimes		return(0);
931553Srgrimes	}
9431492Swollman	if (read(s, &c, 1) == 1) {
9531492Swollman		if (c == '\0') {		/* everything is OK */
961553Srgrimes			(void) close(s);
971553Srgrimes			return(1);
981553Srgrimes		}
9931492Swollman		putchar(c);
1001553Srgrimes	}
10131492Swollman	while ((n = read(s, &c, 1)) > 0)
10231492Swollman		putchar(c);
1031553Srgrimes	(void) close(s);
1041553Srgrimes	return(0);
1051553Srgrimes}
106