From 58f0484fa251c266ede97b591b499fe3dd4f578e Mon Sep 17 00:00:00 2001 From: "Rodney W. Grimes" Date: Fri, 27 May 1994 05:00:24 +0000 Subject: BSD 4.4 Lite Lib Sources --- lib/libc/stdlib/system.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lib/libc/stdlib/system.c (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c new file mode 100644 index 000000000000..c438f1710a78 --- /dev/null +++ b/lib/libc/stdlib/system.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +system(command) + const char *command; +{ + union wait pstat; + pid_t pid; + int omask; + sig_t intsave, quitsave; + + if (!command) /* just checking... */ + return(1); + + omask = sigblock(sigmask(SIGCHLD)); + switch(pid = vfork()) { + case -1: /* error */ + (void)sigsetmask(omask); + pstat.w_status = 0; + pstat.w_retcode = 127; + return(pstat.w_status); + case 0: /* child */ + (void)sigsetmask(omask); + execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); + _exit(127); + } + intsave = signal(SIGINT, SIG_IGN); + quitsave = signal(SIGQUIT, SIG_IGN); + pid = waitpid(pid, (int *)&pstat, 0); + (void)sigsetmask(omask); + (void)signal(SIGINT, intsave); + (void)signal(SIGQUIT, quitsave); + return(pid == -1 ? -1 : pstat.w_status); +} -- cgit v1.2.3 From b487e9d3567797dfaa188ae7e37e170e27f4e1f5 Mon Sep 17 00:00:00 2001 From: James Raynard Date: Wed, 5 Jun 1996 00:08:54 +0000 Subject: Submitted by: (based on code in "Advanced Programming in the Unix Environment" by W.Richard Ste vens. EINTR handling suggested by bde@freebsd.org). Code cleanup: 1. Add missing return type. 2. Replace 'union wait' by int. 3. Use Posix-style signal handling instead of signal(). 4. Use fork() instead of deprecated vfork(). 5. Block signals before fork()'ing, instead of after. 6. Return -1 if fork() fails, instead of 0. 7. Add EINTR handling for waitpid() call. Also add claim of Posix conformance to man page. --- lib/libc/stdlib/system.c | 51 +++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index c438f1710a78..3bae36ae4c62 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -42,35 +42,50 @@ static char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; #include #include #include +#include -system(command) +int system(command) const char *command; { - union wait pstat; pid_t pid; - int omask; - sig_t intsave, quitsave; + int pstat; + struct sigaction ign, intact, quitact; + sigset_t newsigblock, oldsigblock; if (!command) /* just checking... */ return(1); - omask = sigblock(sigmask(SIGCHLD)); - switch(pid = vfork()) { + /* + * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save + * existing signal dispositions. + */ + ign.sa_handler = SIG_IGN; + (void)sigemptyset(&ign.sa_mask); + ign.sa_flags = 0; + (void)sigaction(SIGINT, &ign, &intact); + (void)sigaction(SIGQUIT, &ign, &quitact); + (void)sigemptyset(&newsigblock); + sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); + switch(pid = fork()) { case -1: /* error */ - (void)sigsetmask(omask); - pstat.w_status = 0; - pstat.w_retcode = 127; - return(pstat.w_status); + break; case 0: /* child */ - (void)sigsetmask(omask); + /* + * Restore original signal dispositions and exec the command. + */ + (void)sigaction(SIGINT, &intact, NULL); + (void)sigaction(SIGQUIT, &quitact, NULL); + (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); _exit(127); + default: /* parent */ + do { + pid = waitpid(pid, &pstat, 0); + } while (pid == -1 && errno == EINTR); + break; } - intsave = signal(SIGINT, SIG_IGN); - quitsave = signal(SIGQUIT, SIG_IGN); - pid = waitpid(pid, (int *)&pstat, 0); - (void)sigsetmask(omask); - (void)signal(SIGINT, intsave); - (void)signal(SIGQUIT, quitsave); - return(pid == -1 ? -1 : pstat.w_status); + (void)sigaction(SIGINT, &intact, NULL); + (void)sigaction(SIGQUIT, &quitact, NULL); + (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); + return(pid == -1 ? -1 : pstat); } -- cgit v1.2.3 From 1638d6b6d5326b37d996ce49fb54ddfc3bc16ad0 Mon Sep 17 00:00:00 2001 From: James Raynard Date: Sat, 8 Jun 1996 15:28:11 +0000 Subject: Oops, replace a rather important line that was lost in transit 8-( --- lib/libc/stdlib/system.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index 3bae36ae4c62..00db7d6ce968 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -65,7 +65,8 @@ int system(command) (void)sigaction(SIGINT, &ign, &intact); (void)sigaction(SIGQUIT, &ign, &quitact); (void)sigemptyset(&newsigblock); - sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); + (void)sigaddset(&newsigblock, SIGCHLD); + (void)sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); switch(pid = fork()) { case -1: /* error */ break; -- cgit v1.2.3 From 929273386f6e688c008b15fd24932df2ed7e7172 Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Wed, 12 Jan 2000 09:23:48 +0000 Subject: Add three-tier symbol naming in support of POSIX thread cancellation points. For library functions, the pattern is __sleep() <-- _libc_sleep() <-- sleep(). The arrows represent weak aliases. For system calls, the pattern is _read() <-- _libc_read() <-- read(). --- lib/libc/stdlib/system.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index 00db7d6ce968..0039c078216c 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -29,6 +29,8 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD$ */ #if defined(LIBC_SCCS) && !defined(lint) @@ -44,7 +46,8 @@ static char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/4/93"; #include #include -int system(command) +int +__system(command) const char *command; { pid_t pid; @@ -81,7 +84,7 @@ int system(command) _exit(127); default: /* parent */ do { - pid = waitpid(pid, &pstat, 0); + pid = _libc_waitpid(pid, &pstat, 0); } while (pid == -1 && errno == EINTR); break; } @@ -90,3 +93,6 @@ int system(command) (void)sigprocmask(SIG_SETMASK, &oldsigblock, NULL); return(pid == -1 ? -1 : pstat); } + +__weak_reference(__system, _libc_system); +__weak_reference(_libc_system, system); -- cgit v1.2.3 From 9233c4d9426e03b28e043baeefb6d5a37dc4086e Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Thu, 27 Jan 2000 23:07:25 +0000 Subject: Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(), just use _foo() <-- foo(). In the case of a libpthread that doesn't do call conversion (such as linuxthreads and our upcoming libpthread), this is adequate. In the case of libc_r, we still need three names, which are now _thread_sys_foo() <-- _foo() <-- foo(). Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(), nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo(). Remove all internal libc usage of: creat(), pause(), sleep(), system(), tcdrain(), wait(), and waitpid(). Make thread cancellation fully POSIX-compliant. Suggested by: deischen --- lib/libc/stdlib/system.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index 0039c078216c..236a8b0cc2ee 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -84,7 +84,7 @@ __system(command) _exit(127); default: /* parent */ do { - pid = _libc_waitpid(pid, &pstat, 0); + pid = _wait4(pid, &pstat, 0, (struct rusage *)0); } while (pid == -1 && errno == EINTR); break; } @@ -94,5 +94,4 @@ __system(command) return(pid == -1 ? -1 : pstat); } -__weak_reference(__system, _libc_system); -__weak_reference(_libc_system, system); +__weak_reference(__system, system); -- cgit v1.2.3 From 058b24af1b02554e61e9f71a3d64953d2a3b64f1 Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Sat, 18 Mar 2000 23:13:29 +0000 Subject: MFC: For libc_r, do not create both weak and strong symbols of the same name. --- lib/libc/stdlib/system.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index 236a8b0cc2ee..c7e68bae69b7 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -94,4 +94,6 @@ __system(command) return(pid == -1 ? -1 : pstat); } +#ifndef _THREAD_SAFE __weak_reference(__system, system); +#endif -- cgit v1.2.3 From da2a979bccce928387e75c1f16d849d35660e1c6 Mon Sep 17 00:00:00 2001 From: Alfred Perlstein Date: Wed, 10 Oct 2001 12:50:22 +0000 Subject: MFC: 1.8 (don't get stuck if a signal interrupts us during wait4) --- lib/libc/stdlib/system.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/libc/stdlib/system.c') diff --git a/lib/libc/stdlib/system.c b/lib/libc/stdlib/system.c index c7e68bae69b7..1e1761cc8675 100644 --- a/lib/libc/stdlib/system.c +++ b/lib/libc/stdlib/system.c @@ -50,7 +50,7 @@ int __system(command) const char *command; { - pid_t pid; + pid_t pid, savedpid; int pstat; struct sigaction ign, intact, quitact; sigset_t newsigblock, oldsigblock; @@ -83,8 +83,9 @@ __system(command) execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL); _exit(127); default: /* parent */ + savedpid = pid; do { - pid = _wait4(pid, &pstat, 0, (struct rusage *)0); + pid = _wait4(savedpid, &pstat, 0, (struct rusage *)0); } while (pid == -1 && errno == EINTR); break; } -- cgit v1.2.3