xref: /freebsd/lib/libdpv/util.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
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 #include <err.h>
29 #include <limits.h>
30 #include <spawn.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 #include "util.h"
36 
37 extern char **environ;
38 static char cmdbuf[CMDBUFMAX]	= "";
39 static char shellcmd[PATH_MAX]	= PATH_SHELL;
40 static char *shellcmd_argv[6]	= {
41 	shellcmd,
42 	__DECONST(char *, "-c"),
43 	cmdbuf,
44 	__DECONST(char *, "--"),
45 	shellcmd,
46 	NULL,
47 };
48 
49 /*
50  * Spawn a sh(1) command. Writes the resulting process ID to the pid_t pointed
51  * at by `pid'. Returns a file descriptor (int) suitable for writing data to
52  * the spawned command (data written to file descriptor is seen as standard-in
53  * by the spawned sh(1) command). Returns `-1' if unable to spawn command.
54  *
55  * If cmd contains a single "%s" sequence, replace it with label if non-NULL.
56  */
57 int
58 shell_spawn_pipecmd(const char *cmd, const char *label, pid_t *pid)
59 {
60 	int error;
61 	int len;
62 	posix_spawn_file_actions_t action;
63 #if SHELL_SPAWN_DEBUG
64 	unsigned int i;
65 #endif
66 	int stdin_pipe[2] = { -1, -1 };
67 
68 	/* Populate argument array */
69 	if (label != NULL && fmtcheck(cmd, "%s") == cmd)
70 		len = snprintf(cmdbuf, CMDBUFMAX, cmd, label);
71 	else
72 		len = snprintf(cmdbuf, CMDBUFMAX, "%s", cmd);
73 	if (len >= CMDBUFMAX) {
74 		warnx("%s:%d:%s: cmdbuf[%u] too small to hold cmd argument",
75 		    __FILE__, __LINE__, __func__, CMDBUFMAX);
76 		return (-1);
77 	}
78 
79 	/* Open a pipe to communicate with [X]dialog(1) */
80 	if (pipe(stdin_pipe) < 0)
81 		err(EXIT_FAILURE, "%s: pipe(2)", __func__);
82 
83 	/* Fork sh(1) process */
84 #if SHELL_SPAWN_DEBUG
85 	fprintf(stderr, "%s: spawning `", __func__);
86 	for (i = 0; shellcmd_argv[i] != NULL; i++) {
87 		if (i == 0)
88 			fprintf(stderr, "%s", shellcmd_argv[i]);
89 		else if (i == 2)
90 			fprintf(stderr, " '%s'", shellcmd_argv[i]);
91 		else
92 			fprintf(stderr, " %s", shellcmd_argv[i]);
93 	}
94 	fprintf(stderr, "'\n");
95 #endif
96 	posix_spawn_file_actions_init(&action);
97 	posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO);
98 	posix_spawn_file_actions_addclose(&action, stdin_pipe[1]);
99 	error = posix_spawnp(pid, shellcmd, &action,
100 	    (const posix_spawnattr_t *)NULL, shellcmd_argv, environ);
101 	if (error != 0) err(EXIT_FAILURE, "%s", shellcmd);
102 
103 	return stdin_pipe[1];
104 }
105