The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 24 | #include <sys/resource.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 29 | #include "cutils/log.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | #include "jni.h" |
Elliott Hughes | ad9208a | 2013-02-05 18:01:08 -0800 | [diff] [blame] | 31 | #include "ExecStrings.h" |
| 32 | #include "JNIHelp.h" |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 33 | #include "JniConstants.h" |
Elliott Hughes | 6c1e5f4 | 2013-01-25 15:10:58 -0800 | [diff] [blame] | 34 | #include "Portability.h" |
Elliott Hughes | e9f1204 | 2011-05-11 11:48:46 -0700 | [diff] [blame] | 35 | #include "ScopedLocalRef.h" |
Nick Kralevich | 798f855 | 2013-02-05 13:02:13 -0800 | [diff] [blame] | 36 | #include "toStringArray.h" |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 38 | static 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | } |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 65 | } |
| 66 | closedir(d); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 69 | #define PIPE_COUNT 4 // Number of pipes used to communicate with child. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 71 | static 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | } |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | static 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** Executes a command in a child process. */ |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 88 | static pid_t ExecuteProcess(JNIEnv* env, char** commands, char** environment, |
| 89 | const char* workingDirectory, jobject inDescriptor, |
| 90 | jobject outDescriptor, jobject errDescriptor, |
| 91 | jboolean redirectErrorStream) { |
Elliott Hughes | a5fb706 | 2011-04-28 15:36:47 -0700 | [diff] [blame] | 92 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 93 | // 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 Hughes | a5fb706 | 2011-04-28 15:36:47 -0700 | [diff] [blame] | 133 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 135 | // Close all but statusOut. This saves some work in the next step. |
| 136 | ClosePipes(pipes, statusOut); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 137 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 138 | // Make statusOut automatically close if execvp() succeeds. |
| 139 | fcntl(statusOut, F_SETFD, FD_CLOEXEC); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 141 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 151 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 157 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 162 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 163 | // This is the parent process. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 165 | // Close child's pipe ends. |
| 166 | close(stdinIn); |
| 167 | close(stdoutOut); |
| 168 | close(stderrOut); |
| 169 | close(statusOut); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 170 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 171 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 181 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 182 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 193 | return -1; |
| 194 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 196 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 202 | } |
| 203 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 204 | /** |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 205 | * Converts Java String[] to char** and delegates to ExecuteProcess(). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 206 | */ |
Elliott Hughes | a5fb706 | 2011-04-28 15:36:47 -0700 | [diff] [blame] | 207 | static pid_t ProcessManager_exec(JNIEnv* env, jclass, jobjectArray javaCommands, |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 208 | jobjectArray javaEnvironment, jstring javaWorkingDirectory, |
| 209 | jobject inDescriptor, jobject outDescriptor, jobject errDescriptor, |
| 210 | jboolean redirectErrorStream) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 211 | |
Elliott Hughes | ad9208a | 2013-02-05 18:01:08 -0800 | [diff] [blame] | 212 | ExecStrings commands(env, javaCommands); |
| 213 | ExecStrings environment(env, javaEnvironment); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 214 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 215 | // Extract working directory string. |
| 216 | const char* workingDirectory = NULL; |
| 217 | if (javaWorkingDirectory != NULL) { |
| 218 | workingDirectory = env->GetStringUTFChars(javaWorkingDirectory, NULL); |
| 219 | } |
| 220 | |
Elliott Hughes | ad9208a | 2013-02-05 18:01:08 -0800 | [diff] [blame] | 221 | pid_t result = ExecuteProcess(env, commands.get(), environment.get(), workingDirectory, |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 222 | inDescriptor, outDescriptor, errDescriptor, redirectErrorStream); |
| 223 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 224 | // Clean up working directory string. |
| 225 | if (javaWorkingDirectory != NULL) { |
| 226 | env->ReleaseStringUTFChars(javaWorkingDirectory, workingDirectory); |
| 227 | } |
| 228 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 229 | return result; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 232 | static 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 234 | }; |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 235 | void register_java_lang_ProcessManager(JNIEnv* env) { |
Elliott Hughes | fd6fca4 | 2013-05-07 14:47:39 -0700 | [diff] [blame] | 236 | jniRegisterNativeMethods(env, "java/lang/ProcessManager", gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 237 | } |