blob: 49ee8f7ac4ebcf818c1772daa81076e2bd9e23d4 [file] [log] [blame]
Mårten Kongstad2503a492018-09-27 13:32:30 +02001/*
2 * Copyright (C) 2018 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#ifdef _WIN32
18// nothing to see here
19#else
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070020#include <optional>
Mårten Kongstad2503a492018-09-27 13:32:30 +020021#include <string>
22#include <vector>
23
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include "android-base/logging.h"
29
30#include "androidfw/PosixUtils.h"
31
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070032static std::optional<std::string> ReadFile(int fd) {
33 std::string str;
Mårten Kongstad2503a492018-09-27 13:32:30 +020034 char buf[1024];
35 ssize_t r;
36 while ((r = read(fd, buf, sizeof(buf))) > 0) {
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070037 str.append(buf, r);
Mårten Kongstad2503a492018-09-27 13:32:30 +020038 }
39 if (r != 0) {
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070040 return std::nullopt;
Mårten Kongstad2503a492018-09-27 13:32:30 +020041 }
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070042 return std::move(str);
Mårten Kongstad2503a492018-09-27 13:32:30 +020043}
44
45namespace android {
46namespace util {
47
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070048ProcResult ExecuteBinary(const std::vector<std::string>& argv) {
49 int stdout[2]; // [0] read, [1] write
Mårten Kongstad2503a492018-09-27 13:32:30 +020050 if (pipe(stdout) != 0) {
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070051 PLOG(ERROR) << "out pipe";
52 return ProcResult{-1};
Mårten Kongstad2503a492018-09-27 13:32:30 +020053 }
54
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070055 int stderr[2]; // [0] read, [1] write
Mårten Kongstad2503a492018-09-27 13:32:30 +020056 if (pipe(stderr) != 0) {
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070057 PLOG(ERROR) << "err pipe";
Mårten Kongstad2503a492018-09-27 13:32:30 +020058 close(stdout[0]);
59 close(stdout[1]);
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070060 return ProcResult{-1};
Mårten Kongstad2503a492018-09-27 13:32:30 +020061 }
62
Ryan Mitchella8f13662019-06-26 15:39:52 -070063 auto gid = getgid();
64 auto uid = getuid();
65
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070066 // better keep no C++ objects going into the child here
67 auto argv0 = (char const**)malloc(sizeof(char*) * (argv.size() + 1));
Mårten Kongstad2503a492018-09-27 13:32:30 +020068 for (size_t i = 0; i < argv.size(); i++) {
69 argv0[i] = argv[i].c_str();
70 }
71 argv0[argv.size()] = nullptr;
Peter Collingbourneb3982fc2021-04-15 21:42:37 -070072 int pid = fork();
73 switch (pid) {
Mårten Kongstad2503a492018-09-27 13:32:30 +020074 case -1: // error
75 free(argv0);
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070076 close(stdout[0]);
77 close(stdout[1]);
78 close(stderr[0]);
79 close(stderr[1]);
Mårten Kongstad2503a492018-09-27 13:32:30 +020080 PLOG(ERROR) << "fork";
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -070081 return ProcResult{-1};
Mårten Kongstad2503a492018-09-27 13:32:30 +020082 case 0: // child
Ryan Mitchella8f13662019-06-26 15:39:52 -070083 if (setgid(gid) != 0) {
84 PLOG(ERROR) << "setgid";
85 exit(1);
86 }
87
88 if (setuid(uid) != 0) {
89 PLOG(ERROR) << "setuid";
90 exit(1);
91 }
92
Mårten Kongstad2503a492018-09-27 13:32:30 +020093 close(stdout[0]);
94 if (dup2(stdout[1], STDOUT_FILENO) == -1) {
95 abort();
96 }
97 close(stderr[0]);
98 if (dup2(stderr[1], STDERR_FILENO) == -1) {
99 abort();
100 }
101 execvp(argv0[0], const_cast<char* const*>(argv0));
102 PLOG(ERROR) << "execv";
103 abort();
104 default: // parent
105 free(argv0);
106 close(stdout[1]);
107 close(stderr[1]);
108 int status;
Peter Collingbourneb3982fc2021-04-15 21:42:37 -0700109 waitpid(pid, &status, 0);
Mårten Kongstad2503a492018-09-27 13:32:30 +0200110 if (!WIFEXITED(status)) {
Peter Collingbourneb3982fc2021-04-15 21:42:37 -0700111 close(stdout[0]);
112 close(stderr[0]);
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -0700113 return ProcResult{-1};
Mårten Kongstad2503a492018-09-27 13:32:30 +0200114 }
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -0700115 ProcResult result(status);
116 auto out = ReadFile(stdout[0]);
117 result.stdout_str = out ? std::move(*out) : "";
Mårten Kongstad2503a492018-09-27 13:32:30 +0200118 close(stdout[0]);
Yurii Zubrytskyi1fc44eb2022-09-28 22:52:37 -0700119 auto err = ReadFile(stderr[0]);
120 result.stderr_str = err ? std::move(*err) : "";
Mårten Kongstad2503a492018-09-27 13:32:30 +0200121 close(stderr[0]);
Yi Kong95af4ca2024-08-12 07:03:29 +0800122 return result;
Mårten Kongstad2503a492018-09-27 13:32:30 +0200123 }
124}
125
126} // namespace util
127} // namespace android
128#endif