Header And Logo

PostgreSQL
| The world's most advanced open source database.

system.c

Go to the documentation of this file.
00001 /*
00002  * src/backend/port/darwin/system.c
00003  *
00004  * only needed in OS X 10.1 and possibly early 10.2 releases */
00005 #include <AvailabilityMacros.h> /* pgrminclude ignore */
00006 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 || !defined(MAC_OS_X_VERSION_10_2)
00007 
00008 /*
00009  * Copyright (c) 1988, 1993
00010  *  The Regents of the University of California.  All rights reserved.
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions
00014  * are met:
00015  * 1. Redistributions of source code must retain the above copyright
00016  *    notice, this list of conditions and the following disclaimer.
00017  * 2. Redistributions in binary form must reproduce the above copyright
00018  *    notice, this list of conditions and the following disclaimer in the
00019  *    documentation and/or other materials provided with the distribution.
00020  * 3. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  * $FreeBSD: src/lib/libc/stdlib/system.c,v 1.6 2000/03/16 02:14:41 jasone Exp $
00037  */
00038 
00039 #if defined(LIBC_SCCS) && !defined(lint)
00040 static char sccsid[] = "@(#)system.c    8.1 (Berkeley) 6/4/93";
00041 #endif   /* LIBC_SCCS and not lint */
00042 
00043 #include <sys/wait.h>
00044 #include <signal.h>
00045 #include <unistd.h>
00046 #include <paths.h>
00047 #include <errno.h>
00048 
00049 int         system(const char *command);
00050 
00051 int
00052 system(const char *command)
00053 {
00054     pid_t       pid;
00055     int         pstat;
00056     struct sigaction ign,
00057                 intact,
00058                 quitact;
00059     sigset_t    newsigblock,
00060                 oldsigblock;
00061 
00062     if (!command)               /* just checking... */
00063         return (1);
00064 
00065     /*
00066      * Ignore SIGINT and SIGQUIT, block SIGCHLD. Remember to save existing
00067      * signal dispositions.
00068      */
00069     ign.sa_handler = SIG_IGN;
00070     (void) sigemptyset(&ign.sa_mask);
00071     ign.sa_flags = 0;
00072     (void) sigaction(SIGINT, &ign, &intact);
00073     (void) sigaction(SIGQUIT, &ign, &quitact);
00074     (void) sigemptyset(&newsigblock);
00075     (void) sigaddset(&newsigblock, SIGCHLD);
00076     (void) sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
00077     switch (pid = fork())
00078     {
00079         case -1:                /* error */
00080             break;
00081         case 0:         /* child */
00082 
00083             /*
00084              * Restore original signal dispositions and exec the command.
00085              */
00086             (void) sigaction(SIGINT, &intact, NULL);
00087             (void) sigaction(SIGQUIT, &quitact, NULL);
00088             (void) sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
00089             execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
00090             _exit(127);
00091         default:                /* parent */
00092             do
00093             {
00094                 pid = wait4(pid, &pstat, 0, (struct rusage *) 0);
00095             } while (pid == -1 && errno == EINTR);
00096             break;
00097     }
00098     (void) sigaction(SIGINT, &intact, NULL);
00099     (void) sigaction(SIGQUIT, &quitact, NULL);
00100     (void) sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
00101     return (pid == -1 ? -1 : pstat);
00102 }
00103 
00104 #endif   /* OS X < 10.3 */