1274116Sdteske/*- 2335406Sdteske * Copyright (c) 2013-2018 Devin Teske <dteske@FreeBSD.org> 3274116Sdteske * All rights reserved. 4274116Sdteske * 5274116Sdteske * Redistribution and use in source and binary forms, with or without 6274116Sdteske * modification, are permitted provided that the following conditions 7274116Sdteske * are met: 8274116Sdteske * 1. Redistributions of source code must retain the above copyright 9274116Sdteske * notice, this list of conditions and the following disclaimer. 10274116Sdteske * 2. Redistributions in binary form must reproduce the above copyright 11274116Sdteske * notice, this list of conditions and the following disclaimer in the 12274116Sdteske * documentation and/or other materials provided with the distribution. 13274116Sdteske * 14274116Sdteske * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15274116Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16274116Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17274116Sdteske * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18274116Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19274116Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20274116Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21274116Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22274116Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23274116Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24274116Sdteske * SUCH DAMAGE. 25274116Sdteske */ 26274116Sdteske 27274116Sdteske#include <sys/cdefs.h> 28274116Sdteske__FBSDID("$FreeBSD: stable/11/lib/libdpv/util.c 335406 2018-06-20 05:45:41Z dteske $"); 29274116Sdteske 30274116Sdteske#include <err.h> 31274116Sdteske#include <limits.h> 32274116Sdteske#include <spawn.h> 33274116Sdteske#include <stdio.h> 34274116Sdteske#include <stdlib.h> 35274116Sdteske#include <unistd.h> 36274116Sdteske 37274116Sdteske#include "util.h" 38274116Sdteske 39274116Sdteskeextern char **environ; 40274116Sdteskestatic char cmdbuf[CMDBUFMAX] = ""; 41274116Sdteskestatic char shellcmd[PATH_MAX] = PATH_SHELL; 42274116Sdteskestatic char *shellcmd_argv[6] = { 43274116Sdteske shellcmd, 44274116Sdteske __DECONST(char *, "-c"), 45274116Sdteske cmdbuf, 46274116Sdteske __DECONST(char *, "--"), 47274116Sdteske shellcmd, 48274116Sdteske NULL, 49274116Sdteske}; 50274116Sdteske 51274116Sdteske/* 52274116Sdteske * Spawn a sh(1) command. Writes the resulting process ID to the pid_t pointed 53274116Sdteske * at by `pid'. Returns a file descriptor (int) suitable for writing data to 54274116Sdteske * the spawned command (data written to file descriptor is seen as standard-in 55274116Sdteske * by the spawned sh(1) command). Returns `-1' if unable to spawn command. 56274116Sdteske * 57274116Sdteske * If cmd contains a single "%s" sequence, replace it with label if non-NULL. 58274116Sdteske */ 59274116Sdteskeint 60274116Sdteskeshell_spawn_pipecmd(const char *cmd, const char *label, pid_t *pid) 61274116Sdteske{ 62274116Sdteske int error; 63274116Sdteske int len; 64274116Sdteske posix_spawn_file_actions_t action; 65274116Sdteske#if SHELL_SPAWN_DEBUG 66274116Sdteske unsigned int i; 67274116Sdteske#endif 68274116Sdteske int stdin_pipe[2] = { -1, -1 }; 69274116Sdteske 70274116Sdteske /* Populate argument array */ 71274116Sdteske if (label != NULL && fmtcheck(cmd, "%s") == cmd) 72274116Sdteske len = snprintf(cmdbuf, CMDBUFMAX, cmd, label); 73274116Sdteske else 74274116Sdteske len = snprintf(cmdbuf, CMDBUFMAX, "%s", cmd); 75274116Sdteske if (len >= CMDBUFMAX) { 76274116Sdteske warnx("%s:%d:%s: cmdbuf[%u] too small to hold cmd argument", 77274116Sdteske __FILE__, __LINE__, __func__, CMDBUFMAX); 78274116Sdteske return (-1); 79274116Sdteske } 80274116Sdteske 81274116Sdteske /* Open a pipe to communicate with [X]dialog(1) */ 82274116Sdteske if (pipe(stdin_pipe) < 0) 83274116Sdteske err(EXIT_FAILURE, "%s: pipe(2)", __func__); 84274116Sdteske 85274116Sdteske /* Fork sh(1) process */ 86274116Sdteske#if SHELL_SPAWN_DEBUG 87274116Sdteske fprintf(stderr, "%s: spawning `", __func__); 88274116Sdteske for (i = 0; shellcmd_argv[i] != NULL; i++) { 89274116Sdteske if (i == 0) 90274116Sdteske fprintf(stderr, "%s", shellcmd_argv[i]); 91274116Sdteske else if (i == 2) 92274116Sdteske fprintf(stderr, " '%s'", shellcmd_argv[i]); 93274116Sdteske else 94274116Sdteske fprintf(stderr, " %s", shellcmd_argv[i]); 95274116Sdteske } 96274116Sdteske fprintf(stderr, "'\n"); 97274116Sdteske#endif 98274116Sdteske posix_spawn_file_actions_init(&action); 99274116Sdteske posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO); 100274116Sdteske posix_spawn_file_actions_addclose(&action, stdin_pipe[1]); 101274116Sdteske error = posix_spawnp(pid, shellcmd, &action, 102274116Sdteske (const posix_spawnattr_t *)NULL, shellcmd_argv, environ); 103335406Sdteske if (error != 0) err(EXIT_FAILURE, "%s", shellcmd); 104274116Sdteske 105274116Sdteske return stdin_pipe[1]; 106274116Sdteske} 107