11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*	$OpenBSD: system.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1988 The Regents of the University of California.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Neither the name of the University nor the names of its contributors
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/types.h>
321e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes#include <sys/wait.h>
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <signal.h>
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <unistd.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <paths.h>
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectextern char **environ;
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectsystem(const char *command)
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
431e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	pid_t pid;
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sig_t intsave, quitsave;
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sigset_t mask, omask;
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	int pstat;
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	char *argp[] = {"sh", "-c", NULL, NULL};
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (!command)		/* just checking... */
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return(1);
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	argp[2] = (char *)command;
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sigemptyset(&mask);
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sigaddset(&mask, SIGCHLD);
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sigprocmask(SIG_BLOCK, &mask, &omask);
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	switch (pid = vfork()) {
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	case -1:			/* error */
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		sigprocmask(SIG_SETMASK, &omask, NULL);
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		return(-1);
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	case 0:				/* child */
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		sigprocmask(SIG_SETMASK, &omask, NULL);
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		execve(_PATH_BSHELL, argp, environ);
641e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes		_exit(127);
651e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	}
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
671e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	intsave = signal(SIGINT, SIG_IGN);
681e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	quitsave = signal(SIGQUIT, SIG_IGN);
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	pid = waitpid(pid, (int *)&pstat, 0);
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sigprocmask(SIG_SETMASK, &omask, NULL);
711e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	(void)signal(SIGINT, intsave);
721e4378ea06d06f57027a97714f076ad4f417a705Elliott Hughes	(void)signal(SIGQUIT, quitsave);
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (pid == -1 ? -1 : pstat);
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
75