GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/lpr/lpr/../common_source/startdaemon.c Lines: 0 42 0.0 %
Date: 2017-11-07 Branches: 0 20 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: startdaemon.c,v 1.17 2015/10/27 15:23:28 millert Exp $	*/
2
/*	$NetBSD: startdaemon.c,v 1.10 1998/07/18 05:04:39 lukem Exp $	*/
3
4
/*
5
 * Copyright (c) 1983, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <sys/socket.h>
34
#include <sys/un.h>
35
36
#include <dirent.h>
37
#include <err.h>
38
#include <errno.h>
39
#include <stdio.h>
40
#include <unistd.h>
41
#include <limits.h>
42
#include <string.h>
43
#include <signal.h>
44
45
#include "lp.h"
46
#include "pathnames.h"
47
48
/*
49
 * Tell the printer daemon that there are new files in the spool directory.
50
 */
51
int
52
startdaemon(char *printer)
53
{
54
	struct sockaddr_un un;
55
	int s;
56
	size_t n;
57
	char buf[BUFSIZ];
58
59
	s = socket(AF_UNIX, SOCK_STREAM, 0);
60
	if (s < 0) {
61
		warn("socket");
62
		return(0);
63
	}
64
	memset(&un, 0, sizeof(un));
65
	un.sun_family = AF_UNIX;
66
	strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path));
67
	siginterrupt(SIGINT, 1);
68
	PRIV_START;
69
	if (connect(s, (struct sockaddr *)&un, sizeof(un)) < 0) {
70
		int saved_errno = errno;
71
		if (errno == EINTR && gotintr) {
72
			PRIV_END;
73
			siginterrupt(SIGINT, 0);
74
			close(s);
75
			return(0);
76
		}
77
		PRIV_END;
78
		siginterrupt(SIGINT, 0);
79
		warnc(saved_errno, "connect");
80
		(void)close(s);
81
		return(0);
82
	}
83
	PRIV_END;
84
	siginterrupt(SIGINT, 0);
85
	if ((n = snprintf(buf, sizeof(buf), "\1%s\n", printer)) >= sizeof(buf) ||
86
	    n == -1) {
87
		close(s);
88
		return (0);
89
	}
90
91
	/* XXX atomicio inside siginterrupt? */
92
	if (write(s, buf, n) != n) {
93
		warn("write");
94
		(void)close(s);
95
		return(0);
96
	}
97
	if (read(s, buf, 1) == 1) {
98
		if (buf[0] == '\0') {		/* everything is OK */
99
			(void)close(s);
100
			return(1);
101
		}
102
		putchar(buf[0]);
103
	}
104
	while ((n = read(s, buf, sizeof(buf))) > 0)
105
		fwrite(buf, 1, n, stdout);
106
	(void)close(s);
107
	return(0);
108
}