Android-cuttlefish cvd tool
proc_file_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 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#pragma once
18
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <string>
23#include <unordered_map>
24#include <vector>
25
27
33namespace cuttlefish {
34
35static constexpr char kProcDir[] = "/proc";
36
37struct ProcInfo {
38 pid_t pid_;
41 std::string actual_exec_path_;
42 std::unordered_map<std::string, std::string> envs_;
43 std::vector<std::string> args_;
44};
46
47// collects all pids whose owner is uid
48Result<std::vector<pid_t>> CollectPids(uid_t uid = getuid());
49
50/* collects all pids that meet the following:
51 *
52 * 1. Belongs to the uid
53 * 2. Basename(readlink(/proc/<pid>/exe)) == exec_name
54 *
55 */
56Result<std::vector<pid_t>> CollectPidsByExecName(const std::string& exec_name,
57 uid_t uid = getuid());
58
59/* collects all pids that meet the following:
60 *
61 * 1. Belongs to the uid
62 * 2. readlink(/proc/<pid>/exe) == exec_name
63 *
64 */
65Result<std::vector<pid_t>> CollectPidsByExecPath(const std::string& exec_path,
66 uid_t uid = getuid());
67
72Result<std::vector<pid_t>> CollectPidsByArgv0(const std::string& expected_argv0,
73 uid_t uid = getuid());
74
75Result<uid_t> OwnerUid(pid_t pid);
76
77// retrieves command line args for the pid
79
80// retrieves the path to the executable file used for the pid
81// this does not work for the defunct processes
83
84// retrieves the environment variables of the process, pid
86
87Result<pid_t> Ppid(pid_t pid);
88
89} // namespace cuttlefish
Definition: expected.h:86
Definition: alloc_utils.cpp:23
Result< pid_t > Ppid(const pid_t pid)
Definition: proc_file_utils.cpp:308
static constexpr char kProcDir[]
Definition: proc_file_utils.h:35
Result< std::vector< pid_t > > CollectPidsByArgv0(const std::string &expected_argv0, const uid_t uid)
Definition: proc_file_utils.cpp:247
Result< std::vector< pid_t > > CollectPidsByExecName(const std::string &exec_name, const uid_t uid)
Definition: proc_file_utils.cpp:213
Result< std::vector< std::string > > GetCmdArgs(const pid_t pid)
Definition: proc_file_utils.cpp:168
Result< std::vector< pid_t > > CollectPidsByExecPath(const std::string &exec_path, const uid_t uid)
Definition: proc_file_utils.cpp:231
Result< std::unordered_map< std::string, std::string > > GetEnvs(const pid_t pid)
Definition: proc_file_utils.cpp:277
Result< std::vector< pid_t > > CollectPids(const uid_t uid)
Definition: proc_file_utils.cpp:147
Result< uid_t > OwnerUid(const pid_t pid)
Definition: proc_file_utils.cpp:266
Result< std::string > GetExecutablePath(const pid_t pid)
Definition: proc_file_utils.cpp:176
Result< ProcInfo > ExtractProcInfo(const pid_t pid)
Definition: proc_file_utils.cpp:298
Definition: proc_file_utils.h:37
std::string actual_exec_path_
Definition: proc_file_utils.h:41
std::vector< std::string > args_
Definition: proc_file_utils.h:43
std::unordered_map< std::string, std::string > envs_
Definition: proc_file_utils.h:42
uid_t real_owner_
Definition: proc_file_utils.h:39
uid_t effective_owner_
Definition: proc_file_utils.h:40
pid_t pid_
Definition: proc_file_utils.h:38