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 int sendAlivePing;
105} ChildStuff;
106
107/* following used in addition when mode is SPAWN */
108typedef struct _SpawnInfo {
109 int nargv; /* number of argv array elements */
110 int argvBytes; /* total number of bytes in argv array */
111 int nenvv; /* number of envv array elements */
112 int envvBytes; /* total number of bytes in envv array */
113 int dirlen; /* length of home directory string */
114 int nparentPathv; /* number of elements in parentPathv array */
115 int parentPathvBytes; /* total number of bytes in parentPathv array */
116} SpawnInfo;
117
118/* If ChildStuff.sendAlivePing is true, child shall signal aliveness to
119 * the parent the moment it gains consciousness, before any subsequent
120 * pre-exec errors could happen.
121 * This code must fit into an int and not be a valid errno value on any of
122 * our platforms. */
123#define CHILD_IS_ALIVE 65535
124
125/**
126 * The cached and split version of the JDK's effective PATH.
127 * (We don't support putenv("PATH=...") in native code)
128 */
129const char * const *parentPathv;
130
131ssize_t restartableWrite(int fd, const void *buf, size_t count);
132int restartableDup2(int fd_from, int fd_to);
133int closeSafely(int fd);
134int isAsciiDigit(char c);
135int closeDescriptors(void);
136int moveDescriptor(int fd_from, int fd_to);
137
138int magicNumber();
139ssize_t readFully(int fd, void *buf, size_t nbyte);
140void initVectorFromBlock(const char**vector, const char* block, int count);
141void execve_as_traditional_shell_script(const char *file,
142 const char *argv[],
143 const char *const envp[]);
144void execve_with_shell_fallback(int mode, const char *file,
145 const char *argv[],
146 const char *const envp[]);
147void JDK_execvpe(int mode, const char *file,
148 const char *argv[],
149 const char *const envp[]);
150int childProcess(void *arg);
151
152#endif
153