1 /* 2 * Copyright (C) 2014 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 "berberis/kernel_api/main_executable_real_path_emulation.h" 18 19 #include <sys/stat.h> 20 21 #include "berberis/base/config_globals.h" 22 #include "berberis/base/scoped_errno.h" 23 24 namespace berberis { 25 TryReadLinkToMainExecutableRealPath(const char * pathname)26const char* TryReadLinkToMainExecutableRealPath(const char* pathname) { 27 ScopedErrno scoped_errno; 28 29 // /proc/self/exe and /proc/<pid>/exe are the same file, as /proc/self is a link to /proc/<pid>! 30 // So we only need to check if /proc/self/exe and 'pathname' refer to the same (link) file. 31 // lstat doesn't follow symlinks (we still want program_runner to be accessible by direct path)! 32 // The st_ino and st_dev fields taken together uniquely identify the file within the system. 33 // Do not cache /proc/self/exe lstat to handle situations after fork/clone! 34 struct stat cur_stat; 35 struct stat exe_stat; 36 if (lstat(pathname, &cur_stat) == 0 && lstat("/proc/self/exe", &exe_stat) == 0 && 37 cur_stat.st_ino == exe_stat.st_ino && cur_stat.st_dev == exe_stat.st_dev) { 38 return berberis::GetMainExecutableRealPath(); 39 } 40 41 return nullptr; 42 } 43 44 } // namespace berberis 45