blob: a74ba8b13ba61de17a07ba7f00c817a294caf365 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ProcessManager"
18
Elliott Hughesfd6fca42013-05-07 14:47:39 -070019#include <dirent.h>
20#include <errno.h>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080021#include <fcntl.h>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080022#include <stdlib.h>
23#include <string.h>
Elliott Hughesfd6fca42013-05-07 14:47:39 -070024#include <sys/resource.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <unistd.h>
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080028
Elliott Hughesfd6fca42013-05-07 14:47:39 -070029#include "cutils/log.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080030#include "jni.h"
Elliott Hughesad9208a2013-02-05 18:01:08 -080031#include "ExecStrings.h"
32#include "JNIHelp.h"
Elliott Hughesa9f5c162010-06-16 16:32:18 -070033#include "JniConstants.h"
Elliott Hughes6c1e5f42013-01-25 15:10:58 -080034#include "Portability.h"
Elliott Hughese9f12042011-05-11 11:48:46 -070035#include "ScopedLocalRef.h"
Nick Kralevich798f8552013-02-05 13:02:13 -080036#include "toStringArray.h"
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080037
Elliott Hughesfd6fca42013-05-07 14:47:39 -070038static void CloseNonStandardFds(int status_pipe_fd) {
39 // On Cygwin, Linux, and Solaris, the best way to close iterates over "/proc/self/fd/".
40 const char* fd_path = "/proc/self/fd";
41#ifdef __APPLE__
42 // On Mac OS, there's "/dev/fd/" which Linux seems to link to "/proc/self/fd/",
43 // but which on Solaris appears to be something quite different.
44 fd_path = "/dev/fd";
45#endif
46
47 // Keep track of the system properties fd so we don't close it.
48 int properties_fd = -1;
49 char* properties_fd_string = getenv("ANDROID_PROPERTY_WORKSPACE");
50 if (properties_fd_string != NULL) {
51 properties_fd = atoi(properties_fd_string);
52 }
53
54 DIR* d = opendir(fd_path);
55 int dir_fd = dirfd(d);
56 dirent* e;
57 while ((e = readdir(d)) != NULL) {
58 char* end;
59 int fd = strtol(e->d_name, &end, 10);
60 if (!*end) {
61 if (fd > STDERR_FILENO && fd != dir_fd && fd != status_pipe_fd && fd != properties_fd) {
62 close(fd);
63 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080064 }
Elliott Hughesfd6fca42013-05-07 14:47:39 -070065 }
66 closedir(d);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080067}
68
Elliott Hughesfd6fca42013-05-07 14:47:39 -070069#define PIPE_COUNT 4 // Number of pipes used to communicate with child.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080070
Elliott Hughesfd6fca42013-05-07 14:47:39 -070071static void ClosePipes(int pipes[], int skip_fd) {
72 for (int i = 0; i < PIPE_COUNT * 2; i++) {
73 int fd = pipes[i];
74 if (fd != -1 && fd != skip_fd) {
75 close(pipes[i]);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080076 }
Elliott Hughesfd6fca42013-05-07 14:47:39 -070077 }
78}
79
80static void AbortChild(int status_pipe_fd) {
81 int error = errno;
82 TEMP_FAILURE_RETRY(write(status_pipe_fd, &error, sizeof(int)));
83 close(status_pipe_fd);
84 _exit(127);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080085}
86
87/** Executes a command in a child process. */
Elliott Hughesfd6fca42013-05-07 14:47:39 -070088static pid_t ExecuteProcess(JNIEnv* env, char** commands, char** environment,
89 const char* workingDirectory, jobject inDescriptor,
90 jobject outDescriptor, jobject errDescriptor,
91 jboolean redirectErrorStream) {
Elliott Hughesa5fb7062011-04-28 15:36:47 -070092
Elliott Hughesfd6fca42013-05-07 14:47:39 -070093 // Create 4 pipes: stdin, stdout, stderr, and an exec() status pipe.
94 int pipes[PIPE_COUNT * 2] = { -1, -1, -1, -1, -1, -1, -1, -1 };
95 for (int i = 0; i < PIPE_COUNT; i++) {
96 if (pipe(pipes + i * 2) == -1) {
97 jniThrowIOException(env, errno);
98 ClosePipes(pipes, -1);
99 return -1;
100 }
101 }
102 int stdinIn = pipes[0];
103 int stdinOut = pipes[1];
104 int stdoutIn = pipes[2];
105 int stdoutOut = pipes[3];
106 int stderrIn = pipes[4];
107 int stderrOut = pipes[5];
108 int statusIn = pipes[6];
109 int statusOut = pipes[7];
110
111 pid_t childPid = fork();
112
113 // If fork() failed...
114 if (childPid == -1) {
115 jniThrowIOException(env, errno);
116 ClosePipes(pipes, -1);
117 return -1;
118 }
119
120 // If this is the child process...
121 if (childPid == 0) {
122 // Note: We cannot malloc(3) or free(3) after this point!
123 // A thread in the parent that no longer exists in the child may have held the heap lock
124 // when we forked, so an attempt to malloc(3) or free(3) would result in deadlock.
125
126 // Replace stdin, out, and err with pipes.
127 dup2(stdinIn, 0);
128 dup2(stdoutOut, 1);
129 if (redirectErrorStream) {
130 dup2(stdoutOut, 2);
131 } else {
132 dup2(stderrOut, 2);
Elliott Hughesa5fb7062011-04-28 15:36:47 -0700133 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800134
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700135 // Close all but statusOut. This saves some work in the next step.
136 ClosePipes(pipes, statusOut);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800137
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700138 // Make statusOut automatically close if execvp() succeeds.
139 fcntl(statusOut, F_SETFD, FD_CLOEXEC);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800140
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700141 // Close remaining unwanted open fds.
142 CloseNonStandardFds(statusOut);
143
144 // Switch to working directory.
145 if (workingDirectory != NULL) {
146 if (chdir(workingDirectory) == -1) {
147 AbortChild(statusOut);
148 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800149 }
150
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700151 // Set up environment.
152 if (environment != NULL) {
153 extern char** environ; // Standard, but not in any header file.
154 environ = environment;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800155 }
156
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700157 // Execute process. By convention, the first argument in the arg array
158 // should be the command itself.
159 execvp(commands[0], commands);
160 AbortChild(statusOut);
161 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800162
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700163 // This is the parent process.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800164
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700165 // Close child's pipe ends.
166 close(stdinIn);
167 close(stdoutOut);
168 close(stderrOut);
169 close(statusOut);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800170
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700171 // Check status pipe for an error code. If execvp(2) succeeds, the other
172 // end of the pipe should automatically close, in which case, we'll read
173 // nothing.
174 int child_errno;
175 ssize_t count = TEMP_FAILURE_RETRY(read(statusIn, &child_errno, sizeof(int)));
176 close(statusIn);
177 if (count > 0) {
178 // chdir(2) or execvp(2) in the child failed.
179 // TODO: track which so we can be more specific in the detail message.
180 jniThrowIOException(env, child_errno);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800181
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700182 close(stdoutIn);
183 close(stdinOut);
184 close(stderrIn);
185
186 // Reap our zombie child right away.
187 int status;
188 int rc = TEMP_FAILURE_RETRY(waitpid(childPid, &status, 0));
189 if (rc == -1) {
190 ALOGW("waitpid on failed exec failed: %s", strerror(errno));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800191 }
192
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700193 return -1;
194 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800195
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700196 // Fill in file descriptor wrappers.
197 jniSetFileDescriptorOfFD(env, inDescriptor, stdoutIn);
198 jniSetFileDescriptorOfFD(env, outDescriptor, stdinOut);
199 jniSetFileDescriptorOfFD(env, errDescriptor, stderrIn);
200
201 return childPid;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800202}
203
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800204/**
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700205 * Converts Java String[] to char** and delegates to ExecuteProcess().
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800206 */
Elliott Hughesa5fb7062011-04-28 15:36:47 -0700207static pid_t ProcessManager_exec(JNIEnv* env, jclass, jobjectArray javaCommands,
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700208 jobjectArray javaEnvironment, jstring javaWorkingDirectory,
209 jobject inDescriptor, jobject outDescriptor, jobject errDescriptor,
210 jboolean redirectErrorStream) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800211
Elliott Hughesad9208a2013-02-05 18:01:08 -0800212 ExecStrings commands(env, javaCommands);
213 ExecStrings environment(env, javaEnvironment);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800214
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700215 // Extract working directory string.
216 const char* workingDirectory = NULL;
217 if (javaWorkingDirectory != NULL) {
218 workingDirectory = env->GetStringUTFChars(javaWorkingDirectory, NULL);
219 }
220
Elliott Hughesad9208a2013-02-05 18:01:08 -0800221 pid_t result = ExecuteProcess(env, commands.get(), environment.get(), workingDirectory,
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700222 inDescriptor, outDescriptor, errDescriptor, redirectErrorStream);
223
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700224 // Clean up working directory string.
225 if (javaWorkingDirectory != NULL) {
226 env->ReleaseStringUTFChars(javaWorkingDirectory, workingDirectory);
227 }
228
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700229 return result;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800230}
231
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700232static JNINativeMethod gMethods[] = {
233 NATIVE_METHOD(ProcessManager, exec, "([Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;Z)I"),
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800234};
Elliott Hughes7cd67602012-05-03 17:21:04 -0700235void register_java_lang_ProcessManager(JNIEnv* env) {
Elliott Hughesfd6fca42013-05-07 14:47:39 -0700236 jniRegisterNativeMethods(env, "java/lang/ProcessManager", gMethods, NELEM(gMethods));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800237}