1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/common/process_singleton_lock_posix.h" 6 7 #include "base/files/file_util.h" 8 #include "base/logging.h" 9 #include "base/strings/string_number_conversions.h" 10 11 const char kProcessSingletonLockDelimiter = '-'; 12 ParseProcessSingletonLock(const base::FilePath & path,std::string * hostname,int * pid)13bool ParseProcessSingletonLock(const base::FilePath& path, 14 std::string* hostname, 15 int* pid) { 16 base::FilePath target; 17 if (!base::ReadSymbolicLink(path, &target)) { 18 // The only errno that should occur is ENOENT. 19 if (errno != 0 && errno != ENOENT) 20 PLOG(ERROR) << "readlink(" << path.value() << ") failed"; 21 } 22 23 std::string real_path = target.value(); 24 if (real_path.empty()) 25 return false; 26 27 std::string::size_type pos = real_path.rfind(kProcessSingletonLockDelimiter); 28 29 // If the path is not a symbolic link, or doesn't contain what we expect, 30 // bail. 31 if (pos == std::string::npos) { 32 *hostname = ""; 33 *pid = -1; 34 return true; 35 } 36 37 *hostname = real_path.substr(0, pos); 38 39 const std::string& pid_str = real_path.substr(pos + 1); 40 if (!base::StringToInt(pid_str, pid)) 41 *pid = -1; 42 43 return true; 44 } 45