diff --git a/init/firmware_handler.cpp b/init/firmware_handler.cpp index 01957eff0..dcfda52d6 100644 --- a/init/firmware_handler.cpp +++ b/init/firmware_handler.cpp @@ -38,6 +38,8 @@ #include #include +#include "exthandler/exthandler.h" + using android::base::ReadFdToString; using android::base::Socketpair; using android::base::Split; @@ -136,100 +138,6 @@ FirmwareHandler::FirmwareHandler(std::vector firmware_directories, : firmware_directories_(std::move(firmware_directories)), external_firmware_handlers_(std::move(external_firmware_handlers)) {} -Result FirmwareHandler::RunExternalHandler(const std::string& handler, uid_t uid, - gid_t gid, const Uevent& uevent) const { - unique_fd child_stdout; - unique_fd parent_stdout; - if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stdout, &parent_stdout)) { - return ErrnoError() << "Socketpair() for stdout failed"; - } - - unique_fd child_stderr; - unique_fd parent_stderr; - if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stderr, &parent_stderr)) { - return ErrnoError() << "Socketpair() for stderr failed"; - } - - signal(SIGCHLD, SIG_DFL); - - auto pid = fork(); - if (pid < 0) { - return ErrnoError() << "fork() failed"; - } - - if (pid == 0) { - setenv("FIRMWARE", uevent.firmware.c_str(), 1); - setenv("DEVPATH", uevent.path.c_str(), 1); - parent_stdout.reset(); - parent_stderr.reset(); - close(STDOUT_FILENO); - close(STDERR_FILENO); - dup2(child_stdout.get(), STDOUT_FILENO); - dup2(child_stderr.get(), STDERR_FILENO); - - auto args = Split(handler, " "); - std::vector c_args; - for (auto& arg : args) { - c_args.emplace_back(arg.data()); - } - c_args.emplace_back(nullptr); - - if (gid != 0) { - if (setgid(gid) != 0) { - fprintf(stderr, "setgid() failed: %s", strerror(errno)); - _exit(EXIT_FAILURE); - } - } - - if (setuid(uid) != 0) { - fprintf(stderr, "setuid() failed: %s", strerror(errno)); - _exit(EXIT_FAILURE); - } - - execv(c_args[0], c_args.data()); - fprintf(stderr, "exec() failed: %s", strerror(errno)); - _exit(EXIT_FAILURE); - } - - child_stdout.reset(); - child_stderr.reset(); - - int status; - pid_t waited_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); - if (waited_pid == -1) { - return ErrnoError() << "waitpid() failed"; - } - - std::string stdout_content; - if (!ReadFdToString(parent_stdout.get(), &stdout_content)) { - return ErrnoError() << "ReadFdToString() for stdout failed"; - } - - std::string stderr_content; - if (ReadFdToString(parent_stderr.get(), &stderr_content)) { - auto messages = Split(stderr_content, "\n"); - for (const auto& message : messages) { - if (!message.empty()) { - LOG(ERROR) << "External Firmware Handler: " << message; - } - } - } else { - LOG(ERROR) << "ReadFdToString() for stderr failed"; - } - - if (WIFEXITED(status)) { - if (WEXITSTATUS(status) == EXIT_SUCCESS) { - return Trim(stdout_content); - } else { - return Error() << "exited with status " << WEXITSTATUS(status); - } - } else if (WIFSIGNALED(status)) { - return Error() << "killed by signal " << WTERMSIG(status); - } - - return Error() << "unexpected exit status " << status; -} - std::string FirmwareHandler::GetFirmwarePath(const Uevent& uevent) const { for (const auto& external_handler : external_firmware_handlers_) { if (external_handler.match(uevent.path)) { @@ -237,11 +145,15 @@ std::string FirmwareHandler::GetFirmwarePath(const Uevent& uevent) const { << "' for devpath: '" << uevent.path << "' firmware: '" << uevent.firmware << "'"; + std::unordered_map envs_map; + envs_map["FIRMWARE"] = uevent.firmware; + envs_map["DEVPATH"] = uevent.path; + auto result = RunExternalHandler(external_handler.handler_path, external_handler.uid, - external_handler.gid, uevent); + external_handler.gid, envs_map); if (!result.ok() && NeedsRerunExternalHandler()) { auto res = RunExternalHandler(external_handler.handler_path, external_handler.uid, - external_handler.gid, uevent); + external_handler.gid, envs_map); result = std::move(res); } if (!result.ok()) { diff --git a/init/firmware_handler.h b/init/firmware_handler.h index fceb392db..e5d353809 100644 --- a/init/firmware_handler.h +++ b/init/firmware_handler.h @@ -54,8 +54,6 @@ class FirmwareHandler : public UeventHandler { friend void FirmwareTestWithExternalHandler(const std::string& test_name, bool expect_new_firmware); - Result RunExternalHandler(const std::string& handler, uid_t uid, gid_t gid, - const Uevent& uevent) const; std::string GetFirmwarePath(const Uevent& uevent) const; void ProcessFirmwareEvent(const std::string& path, const std::string& firmware) const; bool ForEachFirmwareDirectory(std::function handler) const; diff --git a/libmodprobe/Android.bp b/libmodprobe/Android.bp index 12906cc39..78b4c83e3 100644 --- a/libmodprobe/Android.bp +++ b/libmodprobe/Android.bp @@ -13,6 +13,7 @@ cc_library_static { vendor_ramdisk_available: true, host_supported: true, srcs: [ + "exthandler.cpp", "libmodprobe.cpp", "libmodprobe_ext.cpp", ], @@ -30,6 +31,7 @@ cc_test { ], local_include_dirs: ["include/"], srcs: [ + "exthandler.cpp", "libmodprobe_test.cpp", "libmodprobe.cpp", "libmodprobe_ext_test.cpp", diff --git a/libmodprobe/exthandler.cpp b/libmodprobe/exthandler.cpp new file mode 100644 index 000000000..f48c25976 --- /dev/null +++ b/libmodprobe/exthandler.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using android::base::ErrnoError; +using android::base::Error; +using android::base::ReadFdToString; +using android::base::Result; +using android::base::Split; +using android::base::Trim; +using android::base::unique_fd; + +Result RunExternalHandler(const std::string& handler, uid_t uid, gid_t gid, + std::unordered_map& envs_map) { + unique_fd child_stdout; + unique_fd parent_stdout; + if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stdout, &parent_stdout)) { + return ErrnoError() << "Socketpair() for stdout failed"; + } + + unique_fd child_stderr; + unique_fd parent_stderr; + if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stderr, &parent_stderr)) { + return ErrnoError() << "Socketpair() for stderr failed"; + } + + signal(SIGCHLD, SIG_DFL); + + auto pid = fork(); + if (pid < 0) { + return ErrnoError() << "fork() failed"; + } + + if (pid == 0) { + for (auto it = envs_map.begin(); it != envs_map.end(); ++it) { + setenv(it->first.c_str(), it->second.c_str(), 1); + } + parent_stdout.reset(); + parent_stderr.reset(); + close(STDOUT_FILENO); + close(STDERR_FILENO); + dup2(child_stdout.get(), STDOUT_FILENO); + dup2(child_stderr.get(), STDERR_FILENO); + + auto args = Split(handler, " "); + std::vector c_args; + for (auto& arg : args) { + c_args.emplace_back(arg.data()); + } + c_args.emplace_back(nullptr); + + if (gid != 0) { + if (setgid(gid) != 0) { + fprintf(stderr, "setgid() failed: %s", strerror(errno)); + _exit(EXIT_FAILURE); + } + } + + if (setuid(uid) != 0) { + fprintf(stderr, "setuid() failed: %s", strerror(errno)); + _exit(EXIT_FAILURE); + } + + execv(c_args[0], c_args.data()); + fprintf(stderr, "exec() failed: %s", strerror(errno)); + _exit(EXIT_FAILURE); + } + + child_stdout.reset(); + child_stderr.reset(); + + int status; + pid_t waited_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); + if (waited_pid == -1) { + return ErrnoError() << "waitpid() failed"; + } + + std::string stdout_content; + if (!ReadFdToString(parent_stdout.get(), &stdout_content)) { + return ErrnoError() << "ReadFdToString() for stdout failed"; + } + + std::string stderr_content; + if (ReadFdToString(parent_stderr.get(), &stderr_content)) { + auto messages = Split(stderr_content, "\n"); + for (const auto& message : messages) { + if (!message.empty()) { + LOG(ERROR) << "External Handler: " << message; + } + } + } else { + LOG(ERROR) << "ReadFdToString() for stderr failed"; + } + + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == EXIT_SUCCESS) { + return Trim(stdout_content); + } else { + return Error() << "exited with status " << WEXITSTATUS(status); + } + } else if (WIFSIGNALED(status)) { + return Error() << "killed by signal " << WTERMSIG(status); + } + + return Error() << "unexpected exit status " << status; +} diff --git a/libmodprobe/include/exthandler/exthandler.h b/libmodprobe/include/exthandler/exthandler.h new file mode 100644 index 000000000..232aa95a4 --- /dev/null +++ b/libmodprobe/include/exthandler/exthandler.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include + +android::base::Result RunExternalHandler( + const std::string& handler, uid_t uid, gid_t gid, + std::unordered_map& envs_map);