193139Sru/* $NetBSD: popen.c,v 1.1 2018/01/09 03:31:15 christos Exp $ */ 2146515Sru 393139Sru/* 4100513Sru * Copyright (c) 1988, 1993 593139Sru * The Regents of the University of California. All rights reserved. 693139Sru * Copyright (c) 2017 The NetBSD Foundation, Inc. 793139Sru * Copyright (c) 2016 The DragonFly Project 893139Sru * Copyright (c) 2014 The FreeBSD Foundation 993139Sru * All rights reserved. 1093139Sru * 1193139Sru * This code is derived from software contributed to The NetBSD Foundation 1293139Sru * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>. 1393139Sru * 1493139Sru * This code is derived from software written by Ken Arnold and 1593139Sru * published in UNIX Review, Vol. 6, No. 8. 1693139Sru * 1793139Sru * Portions of this software were developed by Edward Tomasz Napierala 1893139Sru * under sponsorship from the FreeBSD Foundation. 1993139Sru * 2093139Sru * Redistribution and use in source and binary forms, with or without 2193139Sru * modification, are permitted provided that the following conditions 2293139Sru * are met: 2393139Sru * 1. Redistributions of source code must retain the above copyright 2493139Sru * notice, this list of conditions and the following disclaimer. 2593139Sru * 2. Redistributions in binary form must reproduce the above copyright 2693139Sru * notice, this list of conditions and the following disclaimer in the 2793139Sru * documentation and/or other materials provided with the distribution. 2893139Sru * 3. Neither the name of the University nor the names of its contributors 2993139Sru * may be used to endorse or promote products derived from this software 3093139Sru * without specific prior written permission. 3193139Sru * 3293139Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 3393139Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3493139Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3593139Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3693139Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3793139Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3893139Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3993139Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4093139Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 4193139Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 4293139Sru * SUCH DAMAGE. 4393139Sru * 4493139Sru */ 4593139Sru#include <sys/cdefs.h> 4693139Sru__RCSID("$NetBSD: popen.c,v 1.1 2018/01/09 03:31:15 christos Exp $"); 4793139Sru 4893139Sru#include <sys/types.h> 4993139Sru#include <sys/queue.h> 5093139Sru#include <sys/wait.h> 5193139Sru#include <errno.h> 5293139Sru#include <fcntl.h> 5393139Sru#include <paths.h> 5493139Sru#include <stdarg.h> 5593139Sru#include <stdio.h> 5693139Sru#include <stdlib.h> 5793139Sru#include <string.h> 5893139Sru#include <unistd.h> 5993139Sru 6093139Sru#include "common.h" 6193139Sru 6293139Sruextern char **environ; 6393139Sru 6493139Srustruct pid { 6593139Sru SLIST_ENTRY(pid) next; 6693139Sru FILE *outfp; 6793139Sru pid_t pid; 6893139Sru char *command; 6993139Sru}; 7093139Srustatic SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist); 7193139Sru 7293139Sru#define ARGV_LEN 42 7393139Sru 7493139Sru/* 7593139Sru * Replacement for popen(3), without stdin (which we do not use), but with 7693139Sru * stderr, proper logging, and improved command line arguments passing. 7793139Sru * Error handling is built in - if it returns, then it succeeded. 7893139Sru */ 7993139SruFILE * 8093139Sruauto_popen(const char *argv0, ...) 8193139Sru{ 8293139Sru va_list ap; 8393139Sru struct pid *cur, *p; 8493139Sru pid_t pid; 8593139Sru int error, i, nullfd, outfds[2]; 8693139Sru char *arg, *argv[ARGV_LEN], *command; 8793139Sru 8893139Sru nullfd = open(_PATH_DEVNULL, O_RDWR, 0); 8993139Sru if (nullfd < 0) 9093139Sru log_err(1, "cannot open %s", _PATH_DEVNULL); 9193139Sru 9293139Sru error = pipe(outfds); 9393139Sru if (error != 0) 9493139Sru log_err(1, "pipe"); 9593139Sru 9693139Sru cur = malloc(sizeof(struct pid)); 9793139Sru if (cur == NULL) 9893139Sru log_err(1, "malloc"); 9993139Sru 10093139Sru argv[0] = checked_strdup(argv0); 10193139Sru command = argv[0]; 10293139Sru 10393139Sru va_start(ap, argv0); 10493139Sru for (i = 1;; i++) { 10593139Sru if (i >= ARGV_LEN) 10693139Sru log_errx(1, "too many arguments to auto_popen"); 10793139Sru arg = va_arg(ap, char *); 10893139Sru argv[i] = arg; 10993139Sru if (arg == NULL) 11093139Sru break; 11193139Sru 11293139Sru command = concat(command, ' ', arg); 11393139Sru } 11493139Sru va_end(ap); 11593139Sru 11693139Sru cur->command = checked_strdup(command); 11793139Sru 11893139Sru switch (pid = fork()) { 11993139Sru case -1: /* Error. */ 12093139Sru log_err(1, "fork"); 12193139Sru /* NOTREACHED */ 12293139Sru case 0: /* Child. */ 12393139Sru dup2(nullfd, STDIN_FILENO); 12493139Sru dup2(outfds[1], STDOUT_FILENO); 12593139Sru 12693139Sru close(nullfd); 12793139Sru close(outfds[0]); 12893139Sru close(outfds[1]); 129 130 SLIST_FOREACH(p, &pidlist, next) 131 close(fileno(p->outfp)); 132 execvp(argv[0], argv); 133 log_err(1, "failed to execute %s", argv[0]); 134 /* NOTREACHED */ 135 } 136 137 log_debugx("executing \"%s\" as pid %d", command, pid); 138 139 /* Parent; assume fdopen cannot fail. */ 140 cur->outfp = fdopen(outfds[0], "r"); 141 close(nullfd); 142 close(outfds[1]); 143 144 /* Link into list of file descriptors. */ 145 cur->pid = pid; 146 SLIST_INSERT_HEAD(&pidlist, cur, next); 147 148 return cur->outfp; 149} 150 151int 152auto_pclose(FILE *iop) 153{ 154 struct pid *cur, *last = NULL; 155 int status; 156 pid_t pid; 157 158 /* 159 * Find the appropriate file pointer and remove it from the list. 160 */ 161 SLIST_FOREACH(cur, &pidlist, next) { 162 if (cur->outfp == iop) 163 break; 164 last = cur; 165 } 166 if (cur == NULL) { 167 return -1; 168 } 169 if (last == NULL) 170 SLIST_REMOVE_HEAD(&pidlist, next); 171 else 172 SLIST_REMOVE_AFTER(last, next); 173 174 fclose(cur->outfp); 175 176 do { 177 pid = wait4(cur->pid, &status, 0, NULL); 178 } while (pid == -1 && errno == EINTR); 179 180 if (WIFSIGNALED(status)) { 181 log_warnx("\"%s\", pid %d, terminated with signal %d", 182 cur->command, pid, WTERMSIG(status)); 183 return status; 184 } 185 186 if (WEXITSTATUS(status) != 0) { 187 log_warnx("\"%s\", pid %d, terminated with exit status %d", 188 cur->command, pid, WEXITSTATUS(status)); 189 return status; 190 } 191 192 log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid); 193 194 free(cur->command); 195 free(cur); 196 197 return pid == -1 ? -1 : status; 198} 199