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