1 2 // Copyright (C) 2022 Google Inc. 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 #include "aemu/base/Process.h" 17 18 #if defined(__linux__) 19 #include <fstream> 20 #endif 21 #include <string> 22 23 namespace android { 24 namespace base { 25 namespace guest { 26 getProcessName()27std::string getProcessName() { 28 // Support for "getprogname" function in bionic was introduced in L (API level 21) 29 std::string processName; 30 #if defined(__ANDROID__) && __ANDROID_API__ >= 21 31 processName = std::string(getprogname()); 32 #elif defined(__linux__) 33 { 34 std::ifstream stream("/proc/self/cmdline"); 35 if (stream.is_open()) { 36 processName = std::string(std::istreambuf_iterator<char>(stream), 37 std::istreambuf_iterator<char>()); 38 } 39 } 40 #endif 41 return processName; 42 } 43 44 } // namespace guest 45 } // namespace base 46 } // namespace android