Back to index

lightning-sunbird  0.9+nobinonly
bproc.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; c-basic-offset: 8 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 #include <stdio.h>
00040 #include <signal.h>
00041 
00042 #define _PR_SIGNALED_EXITSTATUS 256
00043 
00044 PRProcess*
00045 _MD_create_process (const char *path, char *const *argv,
00046                   char *const *envp, const PRProcessAttr *attr)
00047 {
00048        PRProcess *process;
00049        int nEnv, idx;
00050        char *const *childEnvp;
00051        char **newEnvp = NULL;
00052        int flags;
00053 
00054        process = PR_NEW(PRProcess);
00055        if (!process) {
00056               PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00057               return NULL;
00058        }
00059 
00060        childEnvp = envp;
00061        if (attr && attr->fdInheritBuffer) {
00062               if (NULL == childEnvp) {
00063                      childEnvp = environ;
00064               }
00065               for (nEnv = 0; childEnvp[nEnv]; nEnv++) {
00066               }
00067               newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *));
00068               if (NULL == newEnvp) {
00069                      PR_DELETE(process);
00070                      PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00071                      return NULL;
00072               }
00073               for (idx = 0; idx < nEnv; idx++) {
00074                      newEnvp[idx] = childEnvp[idx];
00075               }
00076               newEnvp[idx++] = attr->fdInheritBuffer;
00077               newEnvp[idx] = NULL;
00078               childEnvp = newEnvp;
00079        }
00080 
00081        process->md.pid = fork();
00082 
00083        if ((pid_t) -1 == process->md.pid) {
00084               PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno);
00085               PR_DELETE(process);
00086               if (newEnvp) {
00087                      PR_DELETE(newEnvp);
00088               }
00089               return NULL;
00090        } else if (0 == process->md.pid) {  /* the child process */
00091               /*
00092                * If the child process needs to exit, it must call _exit().
00093                * Do not call exit(), because exit() will flush and close
00094                * the standard I/O file descriptors, and hence corrupt
00095                * the parent process's standard I/O data structures.
00096                */
00097 
00098               if (attr) {
00099                      /* the osfd's to redirect stdin, stdout, and stderr to */
00100                      int in_osfd = -1, out_osfd = -1, err_osfd = -1;
00101 
00102                      if (attr->stdinFd
00103                          && attr->stdinFd->secret->md.osfd != 0) {
00104                             in_osfd = attr->stdinFd->secret->md.osfd;
00105                             if (dup2(in_osfd, 0) != 0) {
00106                                    _exit(1);  /* failed */
00107                             }
00108                             flags = fcntl(0, F_GETFL, 0);
00109                             if (flags & O_NONBLOCK) {
00110                                    fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
00111                             }
00112                      }
00113                      if (attr->stdoutFd
00114                          && attr->stdoutFd->secret->md.osfd != 1) {
00115                             out_osfd = attr->stdoutFd->secret->md.osfd;
00116                             if (dup2(out_osfd, 1) != 1) {
00117                                    _exit(1);  /* failed */
00118                             }
00119                             flags = fcntl(1, F_GETFL, 0);
00120                             if (flags & O_NONBLOCK) {
00121                                    fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
00122                             }
00123                      }
00124                      if (attr->stderrFd
00125                          && attr->stderrFd->secret->md.osfd != 2) {
00126                             err_osfd = attr->stderrFd->secret->md.osfd;
00127                             if (dup2(err_osfd, 2) != 2) {
00128                                    _exit(1);  /* failed */
00129                             }
00130                             flags = fcntl(2, F_GETFL, 0);
00131                             if (flags & O_NONBLOCK) {
00132                                    fcntl(2, F_SETFL, flags & ~O_NONBLOCK);
00133                             }
00134                      }
00135                      if (in_osfd != -1) {
00136                             close(in_osfd);
00137                      }
00138                      if (out_osfd != -1 && out_osfd != in_osfd) {
00139                             close(out_osfd);
00140                      }
00141                      if (err_osfd != -1 && err_osfd != in_osfd
00142                          && err_osfd != out_osfd) {
00143                             close(err_osfd);
00144                      }
00145                      if (attr->currentDirectory) {
00146                             if (chdir(attr->currentDirectory) < 0) {
00147                                    _exit(1);  /* failed */
00148                             }
00149                      }
00150               }
00151 
00152               if (childEnvp) {
00153                      (void)execve(path, argv, childEnvp);
00154               } else {
00155                      /* Inherit the environment of the parent. */
00156                      (void)execv(path, argv);
00157               }
00158               /* Whoops! It returned. That's a bad sign. */
00159               _exit(1);
00160        }
00161 
00162        if (newEnvp) {
00163               PR_DELETE(newEnvp);
00164        }
00165 
00166        return process;
00167 }
00168 
00169 PRStatus
00170 _MD_detach_process (PRProcess *process)
00171 {
00172        /* If we kept a process table like unix does,
00173         * we'd remove the entry here.
00174         * Since we dont', just delete the process variable
00175         */
00176        PR_DELETE(process);
00177        return PR_SUCCESS;
00178 }
00179 
00180 PRStatus
00181 _MD_wait_process (PRProcess *process, PRInt32 *exitCode)
00182 {
00183        PRStatus retVal = PR_SUCCESS;
00184        int ret, status;
00185        
00186        /* Ignore interruptions */
00187        do {
00188               ret = waitpid(process->md.pid, &status, 0);
00189        } while (ret == -1 && errno == EINTR);
00190 
00191        /*
00192         * waitpid() cannot return 0 because we did not invoke it
00193         * with the WNOHANG option.
00194         */ 
00195        PR_ASSERT(0 != ret);
00196 
00197        if (ret < 0) {
00198                 PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
00199               return PR_FAILURE;
00200        }
00201 
00202        /* If child process exited normally, return child exit code */
00203        if (WIFEXITED(status)) {
00204               *exitCode = WEXITSTATUS(status);
00205        } else {
00206               PR_ASSERT(WIFSIGNALED(status));
00207               *exitCode = _PR_SIGNALED_EXITSTATUS;
00208        }             
00209 
00210        PR_DELETE(process);
00211        return PR_SUCCESS;
00212 }
00213 
00214 PRStatus
00215 _MD_kill_process (PRProcess *process)
00216 {
00217        PRErrorCode prerror;
00218        PRInt32 oserror;
00219        
00220        if (kill(process->md.pid, SIGKILL) == 0) {
00221               return PR_SUCCESS;
00222        }
00223        oserror = errno;
00224        switch (oserror) {
00225         case EPERM:
00226               prerror = PR_NO_ACCESS_RIGHTS_ERROR;
00227               break;
00228         case ESRCH:
00229               prerror = PR_INVALID_ARGUMENT_ERROR;
00230               break;
00231         default:
00232               prerror = PR_UNKNOWN_ERROR;
00233               break;
00234        }
00235        PR_SetError(prerror, oserror);
00236        return PR_FAILURE;
00237 }