xref: /freebsd/lib/libdpv/util.c (revision 4db78cacdee1b6f3b7880eb8c5560e8edaf91698)
1 /*-
2  * Copyright (c) 2013-2018 Devin Teske <dteske@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <err.h>
31 #include <limits.h>
32 #include <spawn.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include "util.h"
38 
39 extern char **environ;
40 static char cmdbuf[CMDBUFMAX]	= "";
41 static char shellcmd[PATH_MAX]	= PATH_SHELL;
42 static char *shellcmd_argv[6]	= {
43 	shellcmd,
44 	__DECONST(char *, "-c"),
45 	cmdbuf,
46 	__DECONST(char *, "--"),
47 	shellcmd,
48 	NULL,
49 };
50 
51 /*
52  * Spawn a sh(1) command. Writes the resulting process ID to the pid_t pointed
53  * at by `pid'. Returns a file descriptor (int) suitable for writing data to
54  * the spawned command (data written to file descriptor is seen as standard-in
55  * by the spawned sh(1) command). Returns `-1' if unable to spawn command.
56  *
57  * If cmd contains a single "%s" sequence, replace it with label if non-NULL.
58  */
59 int
60 shell_spawn_pipecmd(const char *cmd, const char *label, pid_t *pid)
61 {
62 	int error;
63 	int len;
64 	posix_spawn_file_actions_t action;
65 #if SHELL_SPAWN_DEBUG
66 	unsigned int i;
67 #endif
68 	int stdin_pipe[2] = { -1, -1 };
69 
70 	/* Populate argument array */
71 	if (label != NULL && fmtcheck(cmd, "%s") == cmd)
72 		len = snprintf(cmdbuf, CMDBUFMAX, cmd, label);
73 	else
74 		len = snprintf(cmdbuf, CMDBUFMAX, "%s", cmd);
75 	if (len >= CMDBUFMAX) {
76 		warnx("%s:%d:%s: cmdbuf[%u] too small to hold cmd argument",
77 		    __FILE__, __LINE__, __func__, CMDBUFMAX);
78 		return (-1);
79 	}
80 
81 	/* Open a pipe to communicate with [X]dialog(1) */
82 	if (pipe(stdin_pipe) < 0)
83 		err(EXIT_FAILURE, "%s: pipe(2)", __func__);
84 
85 	/* Fork sh(1) process */
86 #if SHELL_SPAWN_DEBUG
87 	fprintf(stderr, "%s: spawning `", __func__);
88 	for (i = 0; shellcmd_argv[i] != NULL; i++) {
89 		if (i == 0)
90 			fprintf(stderr, "%s", shellcmd_argv[i]);
91 		else if (i == 2)
92 			fprintf(stderr, " '%s'", shellcmd_argv[i]);
93 		else
94 			fprintf(stderr, " %s", shellcmd_argv[i]);
95 	}
96 	fprintf(stderr, "'\n");
97 #endif
98 	posix_spawn_file_actions_init(&action);
99 	posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO);
100 	posix_spawn_file_actions_addclose(&action, stdin_pipe[1]);
101 	error = posix_spawnp(pid, shellcmd, &action,
102 	    (const posix_spawnattr_t *)NULL, shellcmd_argv, environ);
103 	if (error != 0) err(EXIT_FAILURE, "%s", shellcmd);
104 
105 	return stdin_pipe[1];
106 }
107