11553Srgrimes/*
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
5227618Simpextern uid_t	uid, euid;
5327618Simp
541553Srgrimes/*
551553Srgrimes * Tell the printer daemon that there are new files in the spool directory.
561553Srgrimes */
571553Srgrimes
581553Srgrimesint
5978146Sgadstartdaemon(const struct printer *pp)
601553Srgrimes{
611553Srgrimes	struct sockaddr_un un;
621553Srgrimes	register int s, n;
6395299Sgad	int connectres;
6431492Swollman	char c;
651553Srgrimes
6631492Swollman	s = socket(PF_LOCAL, SOCK_STREAM, 0);
671553Srgrimes	if (s < 0) {
6829780Scharnier		warn("socket");
691553Srgrimes		return(0);
701553Srgrimes	}
711553Srgrimes	memset(&un, 0, sizeof(un));
7231492Swollman	un.sun_family = AF_LOCAL;
731553Srgrimes	strcpy(un.sun_path, _PATH_SOCKETNAME);
741553Srgrimes#ifndef SUN_LEN
751553Srgrimes#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
761553Srgrimes#endif
7727618Simp	seteuid(euid);
7895299Sgad	connectres = connect(s, (struct sockaddr *)&un, SUN_LEN(&un));
7995299Sgad	seteuid(uid);
8095299Sgad	if (connectres < 0) {
8195299Sgad		warn("Unable to connect to %s", _PATH_SOCKETNAME);
8295299Sgad		warnx("Check to see if the master 'lpd' process is running.");
831553Srgrimes		(void) close(s);
841553Srgrimes		return(0);
851553Srgrimes	}
8631492Swollman
8731492Swollman	/*
8831492Swollman	 * Avoid overruns without putting artificial limitations on
8931492Swollman	 * the length.
9031492Swollman	 */
9131492Swollman	if (writel(s, "\1", pp->printer, "\n", (char *)0) <= 0) {
9229780Scharnier		warn("write");
931553Srgrimes		(void) close(s);
941553Srgrimes		return(0);
951553Srgrimes	}
9631492Swollman	if (read(s, &c, 1) == 1) {
9731492Swollman		if (c == '\0') {		/* everything is OK */
981553Srgrimes			(void) close(s);
991553Srgrimes			return(1);
1001553Srgrimes		}
10131492Swollman		putchar(c);
1021553Srgrimes	}
10331492Swollman	while ((n = read(s, &c, 1)) > 0)
10431492Swollman		putchar(c);
1051553Srgrimes	(void) close(s);
1061553Srgrimes	return(0);
1071553Srgrimes}
108