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