aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/autofs/popen.c
blob: f1be8ec9a4ca80548673095882065a1667eff2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 2014 The FreeBSD Foundation
 * All rights reserved.
 *
 * This code is derived from software written by Ken Arnold and
 * published in UNIX Review, Vol. 6, No. 8.
 *
 * Portions of this software were developed by Edward Tomasz Napierala
 * under sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <paths.h>

#include "common.h"

extern char **environ;

struct pid {
	SLIST_ENTRY(pid) next;
	FILE *outfp;
	pid_t pid;
	char *command;
};
static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);

#define	ARGV_LEN	42

/*
 * Replacement for popen(3), without stdin (which we do not use), but with
 * stderr, proper logging, and improved command line arguments passing.
 * Error handling is built in - if it returns, then it succeeded.
 */
FILE *
auto_popen(const char *argv0, ...)
{
	va_list ap;
	struct pid *cur, *p;
	pid_t pid;
	int error, i, nullfd, outfds[2];
	char *arg, *argv[ARGV_LEN], *command;

	nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
	if (nullfd < 0)
		log_err(1, "cannot open %s", _PATH_DEVNULL);

	error = pipe(outfds);
	if (error != 0)
		log_err(1, "pipe");

	cur = malloc(sizeof(struct pid));
	if (cur == NULL)
		log_err(1, "malloc");

	argv[0] = checked_strdup(argv0);
	command = argv[0];

	va_start(ap, argv0);
	for (i = 1;; i++) {
		if (i >= ARGV_LEN)
			log_errx(1, "too many arguments to auto_popen");
		arg = va_arg(ap, char *);
		argv[i] = arg;
		if (arg == NULL)
			break;

		command = concat(command, ' ', arg);
	}
	va_end(ap);

	cur->command = checked_strdup(command);

	switch (pid = fork()) {
	case -1:			/* Error. */
		log_err(1, "fork");
		/* NOTREACHED */
	case 0:				/* Child. */
		dup2(nullfd, STDIN_FILENO);
		dup2(outfds[1], STDOUT_FILENO);

		close(nullfd);
		close(outfds[0]);
		close(outfds[1]);

		SLIST_FOREACH(p, &pidlist, next)
			close(fileno(p->outfp));
		execvp(argv[0], argv);
		log_err(1, "failed to execute %s", argv[0]);
		/* NOTREACHED */
	}

	log_debugx("executing \"%s\" as pid %d", command, pid);

	/* Parent; assume fdopen cannot fail. */
	cur->outfp = fdopen(outfds[0], "r");
	close(nullfd);
	close(outfds[1]);

	/* Link into list of file descriptors. */
	cur->pid = pid;
	SLIST_INSERT_HEAD(&pidlist, cur, next);

	return (cur->outfp);
}

int
auto_pclose(FILE *iop)
{
	struct pid *cur, *last = NULL;
	int status;
	pid_t pid;

	/*
	 * Find the appropriate file pointer and remove it from the list.
	 */
	SLIST_FOREACH(cur, &pidlist, next) {
		if (cur->outfp == iop)
			break;
		last = cur;
	}
	if (cur == NULL) {
		return (-1);
	}
	if (last == NULL)
		SLIST_REMOVE_HEAD(&pidlist, next);
	else
		SLIST_REMOVE_AFTER(last, next);

	fclose(cur->outfp);

	do {
		pid = wait4(cur->pid, &status, 0, NULL);
	} while (pid == -1 && errno == EINTR);

	if (WIFSIGNALED(status)) {
		log_warnx("\"%s\", pid %d, terminated with signal %d",
		    cur->command, pid, WTERMSIG(status));
		return (status);
	}

	if (WEXITSTATUS(status) != 0) {
		log_warnx("\"%s\", pid %d, terminated with exit status %d",
		    cur->command, pid, WEXITSTATUS(status));
		return (status);
	}

	log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid);

	free(cur->command);
	free(cur);

	return (pid == -1 ? -1 : status);
}