1 /* 2 * Copyright (C) 2020 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 #define LOG_TAG "installd" 17 18 #include "execv_helper.h" 19 20 #include <stdlib.h> 21 #include <unistd.h> 22 23 #include <string> 24 25 #include <android-base/logging.h> 26 #include <android-base/properties.h> 27 28 namespace android { 29 namespace installd { 30 31 // Store a placeholder for the binary name. ExecVHelper()32ExecVHelper::ExecVHelper() : args_(1u, std::string()) {} 33 ~ExecVHelper()34ExecVHelper::~ExecVHelper() {} 35 PrepareArgs(const std::string & bin)36void ExecVHelper::PrepareArgs(const std::string& bin) { 37 CHECK(!args_.empty()); 38 CHECK(args_[0].empty()); 39 args_[0] = bin; 40 // Write char* into array. 41 for (const std::string& arg : args_) { 42 argv_.push_back(arg.c_str()); 43 } 44 argv_.push_back(nullptr); // Add null terminator. 45 } 46 Exec(int exit_code)47void ExecVHelper::Exec(int exit_code) { 48 execv(argv_[0], (char * const *)&argv_[0]); 49 PLOG(ERROR) << "execv(" << argv_[0] << ") failed"; 50 exit(exit_code); 51 } 52 AddArg(const std::string & arg)53void ExecVHelper::AddArg(const std::string& arg) { 54 if (!arg.empty()) { 55 args_.push_back(arg); 56 } 57 } 58 AddRuntimeArg(const std::string & arg)59void ExecVHelper::AddRuntimeArg(const std::string& arg) { 60 if (!arg.empty()) { 61 args_.push_back("--runtime-arg"); 62 args_.push_back(arg); 63 } 64 } 65 66 } // namespace installd 67 } // namespace android 68