1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #ifndef CHILDPROC_MD_H
  25 #define CHILDPROC_MD_H
  26 
  27 #include <sys/types.h>
  28 
  29 #ifdef __APPLE__
  30 #include <crt_externs.h>
  31 #define environ (*_NSGetEnviron())
  32 #else
  33 /* This is one of the rare times it's more portable to declare an
  34  * external symbol explicitly, rather than via a system header.
  35  * The declaration is standardized as part of UNIX98, but there is
  36  * no standard (not even de-facto) header file where the
  37  * declaration is to be found.  See:
  38  * http://www.opengroup.org/onlinepubs/009695399/functions/environ.html
  39  * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html
  40  *
  41  * "All identifiers in this volume of IEEE Std 1003.1-2001, except
  42  * environ, are defined in at least one of the headers" (!)
  43  */
  44 extern char **environ;
  45 #endif
  46 
  47 #ifdef __linux__
  48 #include <sched.h>
  49 #endif
  50 
  51 #ifndef STDIN_FILENO
  52 #define STDIN_FILENO 0
  53 #endif
  54 
  55 #ifndef STDOUT_FILENO
  56 #define STDOUT_FILENO 1
  57 #endif
  58 
  59 #ifndef STDERR_FILENO
  60 #define STDERR_FILENO 2
  61 #endif
  62 
  63 #ifndef SA_NOCLDSTOP
  64 #define SA_NOCLDSTOP 0
  65 #endif
  66 
  67 #ifndef SA_RESTART
  68 #define SA_RESTART 0
  69 #endif
  70 
  71 #define FAIL_FILENO (STDERR_FILENO + 1)
  72 
  73 /* TODO: Refactor. */
  74 #define RESTARTABLE(_cmd, _result) do { \
  75   do { \
  76     _result = _cmd; \
  77   } while((_result == -1) && (errno == EINTR)); \
  78 } while(0)
  79 
  80 /* These numbers must be the same as the Enum in UNIXProcess.java
  81  * Must be a better way of doing this.
  82  */
  83 #define MODE_FORK 1
  84 #define MODE_POSIX_SPAWN 2
  85 #define MODE_VFORK 3
  86 #define MODE_CLONE 4
  87 
  88 typedef struct _ChildStuff
  89 {
  90     int in[2];
  91     int out[2];
  92     int err[2];
  93     int fail[2];
  94     int childenv[2];
  95     int fds[3];
  96     int mode;
  97     const char **argv;
  98     int argc;
  99     const char **envv;
 100     const char *pdir;
 101     int redirectErrorStream;
 102     void *clone_stack;
 103 } ChildStuff;
 104 
 105 /* following used in addition when mode is SPAWN */
 106 typedef struct _SpawnInfo {
 107     int nargv; /* number of argv array elements  */
 108     int argvBytes; /* total number of bytes in argv array */
 109     int nenvv; /* number of envv array elements  */
 110     int envvBytes; /* total number of bytes in envv array */
 111     int dirlen; /* length of home directory string */
 112     int nparentPathv; /* number of elements in parentPathv array */
 113     int parentPathvBytes; /* total number of bytes in parentPathv array */
 114 } SpawnInfo;
 115 
 116 /**
 117  * The cached and split version of the JDK's effective PATH.
 118  * (We don't support putenv("PATH=...") in native code)
 119  */
 120 const char * const *parentPathv;
 121 
 122 ssize_t restartableWrite(int fd, const void *buf, size_t count);
 123 int restartableDup2(int fd_from, int fd_to);
 124 int restartableClose(int fd);
 125 int closeSafely(int fd);
 126 int isAsciiDigit(char c);
 127 int closeDescriptors(void);
 128 int moveDescriptor(int fd_from, int fd_to);
 129 
 130 int magicNumber();
 131 ssize_t readFully(int fd, void *buf, size_t nbyte);
 132 void initVectorFromBlock(const char**vector, const char* block, int count);
 133 void execve_as_traditional_shell_script(const char *file,
 134                                         const char *argv[],
 135                                         const char *const envp[]);
 136 void execve_with_shell_fallback(int mode, const char *file,
 137                                 const char *argv[],
 138                                 const char *const envp[]);
 139 void JDK_execvpe(int mode, const char *file,
 140                  const char *argv[],
 141                  const char *const envp[]);
 142 int childProcess(void *arg);
 143 
 144 #endif