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