1 /*
2  * Copyright (C) 2011 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 #include "exec_utils.h"
18 
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <string>
22 #include <vector>
23 
24 #include "android-base/stringprintf.h"
25 #include "android-base/strings.h"
26 
27 #include "runtime.h"
28 
29 namespace art {
30 
31 using android::base::StringPrintf;
32 
ExecAndReturnCode(std::vector<std::string> & arg_vector,std::string * error_msg)33 int ExecAndReturnCode(std::vector<std::string>& arg_vector, std::string* error_msg) {
34   const std::string command_line(android::base::Join(arg_vector, ' '));
35   CHECK_GE(arg_vector.size(), 1U) << command_line;
36 
37   // Convert the args to char pointers.
38   const char* program = arg_vector[0].c_str();
39   std::vector<char*> args;
40   for (size_t i = 0; i < arg_vector.size(); ++i) {
41     const std::string& arg = arg_vector[i];
42     char* arg_str = const_cast<char*>(arg.c_str());
43     CHECK(arg_str != nullptr) << i;
44     args.push_back(arg_str);
45   }
46   args.push_back(nullptr);
47 
48   // fork and exec
49   pid_t pid = fork();
50   if (pid == 0) {
51     // no allocation allowed between fork and exec
52 
53     // change process groups, so we don't get reaped by ProcessManager
54     setpgid(0, 0);
55 
56     // (b/30160149): protect subprocesses from modifications to LD_LIBRARY_PATH, etc.
57     // Use the snapshot of the environment from the time the runtime was created.
58     char** envp = (Runtime::Current() == nullptr) ? nullptr : Runtime::Current()->GetEnvSnapshot();
59     if (envp == nullptr) {
60       execv(program, &args[0]);
61     } else {
62       execve(program, &args[0], envp);
63     }
64     PLOG(ERROR) << "Failed to execve(" << command_line << ")";
65     // _exit to avoid atexit handlers in child.
66     _exit(1);
67   } else {
68     if (pid == -1) {
69       *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
70                                 command_line.c_str(), strerror(errno));
71       return -1;
72     }
73 
74     // wait for subprocess to finish
75     int status = -1;
76     pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
77     if (got_pid != pid) {
78       *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
79                                 "wanted %d, got %d: %s",
80                                 command_line.c_str(), pid, got_pid, strerror(errno));
81       return -1;
82     }
83     if (WIFEXITED(status)) {
84       return WEXITSTATUS(status);
85     }
86     return -1;
87   }
88 }
89 
Exec(std::vector<std::string> & arg_vector,std::string * error_msg)90 bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) {
91   int status = ExecAndReturnCode(arg_vector, error_msg);
92   if (status != 0) {
93     const std::string command_line(android::base::Join(arg_vector, ' '));
94     *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
95                               command_line.c_str());
96     return false;
97   }
98   return true;
99 }
100 
101 }  // namespace art
102