xref: /aosp_15_r20/external/cronet/base/linux_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_LINUX_UTIL_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_LINUX_UTIL_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker #include <sys/types.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <string>
12*6777b538SAndroid Build Coastguard Worker #include <vector>
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker namespace base {
17*6777b538SAndroid Build Coastguard Worker 
18*6777b538SAndroid Build Coastguard Worker // This is declared here so the crash reporter can access the memory directly
19*6777b538SAndroid Build Coastguard Worker // in compromised context without going through the standard library.
20*6777b538SAndroid Build Coastguard Worker BASE_EXPORT extern char g_linux_distro[];
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker // Get the Linux Distro if we can, or return "Unknown".
23*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::string GetLinuxDistro();
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker #if defined(UNIT_TEST)
26*6777b538SAndroid Build Coastguard Worker // Get the value of given key from the given input (content of the
27*6777b538SAndroid Build Coastguard Worker // /etc/os-release file. Exposed for testing.
28*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::string GetKeyValueFromOSReleaseFileForTesting(
29*6777b538SAndroid Build Coastguard Worker     const std::string& input,
30*6777b538SAndroid Build Coastguard Worker     const char* key);
31*6777b538SAndroid Build Coastguard Worker #endif  // defined(UNIT_TEST)
32*6777b538SAndroid Build Coastguard Worker 
33*6777b538SAndroid Build Coastguard Worker // Set the Linux Distro string.
34*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void SetLinuxDistro(const std::string& distro);
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker // For a given process |pid|, get a list of all its threads. On success, returns
37*6777b538SAndroid Build Coastguard Worker // true and appends the list of threads to |tids|. Otherwise, returns false.
38*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids);
39*6777b538SAndroid Build Coastguard Worker 
40*6777b538SAndroid Build Coastguard Worker // Get a list of all threads for the current process. On success, returns true
41*6777b538SAndroid Build Coastguard Worker // and appends the list of threads to |tids|. Otherwise, returns false.
42*6777b538SAndroid Build Coastguard Worker // Unlike the function above, this function reads /proc/self/tasks, not
43*6777b538SAndroid Build Coastguard Worker // /proc/<pid>/tasks. On Android, the former should always be accessible to
44*6777b538SAndroid Build Coastguard Worker // GPU and Browser processes, while the latter may or may not be accessible
45*6777b538SAndroid Build Coastguard Worker // depending on the system and the app configuration.
46*6777b538SAndroid Build Coastguard Worker BASE_EXPORT bool GetThreadsForCurrentProcess(std::vector<pid_t>* tids);
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker // For a given process |pid|, look through all its threads and find the first
49*6777b538SAndroid Build Coastguard Worker // thread with /proc/[pid]/task/[thread_id]/syscall whose first N bytes matches
50*6777b538SAndroid Build Coastguard Worker // |expected_data|, where N is the length of |expected_data|.
51*6777b538SAndroid Build Coastguard Worker // Returns the thread id or -1 on error.  If |syscall_supported| is
52*6777b538SAndroid Build Coastguard Worker // set to false the kernel does not support syscall in procfs.
53*6777b538SAndroid Build Coastguard Worker BASE_EXPORT pid_t FindThreadIDWithSyscall(pid_t pid,
54*6777b538SAndroid Build Coastguard Worker                                           const std::string& expected_data,
55*6777b538SAndroid Build Coastguard Worker                                           bool* syscall_supported);
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker // For a given process |pid|, look through all its threads and find the first
58*6777b538SAndroid Build Coastguard Worker // thread with /proc/[pid]/task/[thread_id]/status where NSpid matches |ns_tid|.
59*6777b538SAndroid Build Coastguard Worker // Returns the thread id or -1 on error.  If |ns_pid_supported| is
60*6777b538SAndroid Build Coastguard Worker // set to false the kernel does not support NSpid in procfs.
61*6777b538SAndroid Build Coastguard Worker BASE_EXPORT pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported);
62*6777b538SAndroid Build Coastguard Worker 
63*6777b538SAndroid Build Coastguard Worker }  // namespace base
64*6777b538SAndroid Build Coastguard Worker 
65*6777b538SAndroid Build Coastguard Worker #endif  // BASE_LINUX_UTIL_H_
66