xref: /aosp_15_r20/external/libchrome/base/process/internal_aix.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/process/internal_aix.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <sys/procfs.h>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include <errno.h>
10*635a8641SAndroid Build Coastguard Worker #include <fcntl.h>
11*635a8641SAndroid Build Coastguard Worker #include <limits.h>
12*635a8641SAndroid Build Coastguard Worker #include <unistd.h>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include <map>
15*635a8641SAndroid Build Coastguard Worker #include <string>
16*635a8641SAndroid Build Coastguard Worker #include <vector>
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker #include "base/files/file_util.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_number_conversions.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_split.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
23*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
24*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker // Not defined on AIX by default.
27*635a8641SAndroid Build Coastguard Worker #define NAME_MAX 255
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker namespace base {
30*635a8641SAndroid Build Coastguard Worker namespace internalAIX {
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker const char kProcDir[] = "/proc";
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker const char kStatFile[] = "psinfo";  // AIX specific
35*635a8641SAndroid Build Coastguard Worker 
GetProcPidDir(pid_t pid)36*635a8641SAndroid Build Coastguard Worker FilePath GetProcPidDir(pid_t pid) {
37*635a8641SAndroid Build Coastguard Worker   return FilePath(kProcDir).Append(IntToString(pid));
38*635a8641SAndroid Build Coastguard Worker }
39*635a8641SAndroid Build Coastguard Worker 
ProcDirSlotToPid(const char * d_name)40*635a8641SAndroid Build Coastguard Worker pid_t ProcDirSlotToPid(const char* d_name) {
41*635a8641SAndroid Build Coastguard Worker   int i;
42*635a8641SAndroid Build Coastguard Worker   for (i = 0; i < NAME_MAX && d_name[i]; ++i) {
43*635a8641SAndroid Build Coastguard Worker     if (!IsAsciiDigit(d_name[i])) {
44*635a8641SAndroid Build Coastguard Worker       return 0;
45*635a8641SAndroid Build Coastguard Worker     }
46*635a8641SAndroid Build Coastguard Worker   }
47*635a8641SAndroid Build Coastguard Worker   if (i == NAME_MAX)
48*635a8641SAndroid Build Coastguard Worker     return 0;
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker   // Read the process's command line.
51*635a8641SAndroid Build Coastguard Worker   pid_t pid;
52*635a8641SAndroid Build Coastguard Worker   std::string pid_string(d_name);
53*635a8641SAndroid Build Coastguard Worker   if (!StringToInt(pid_string, &pid)) {
54*635a8641SAndroid Build Coastguard Worker     NOTREACHED();
55*635a8641SAndroid Build Coastguard Worker     return 0;
56*635a8641SAndroid Build Coastguard Worker   }
57*635a8641SAndroid Build Coastguard Worker   return pid;
58*635a8641SAndroid Build Coastguard Worker }
59*635a8641SAndroid Build Coastguard Worker 
ReadProcFile(const FilePath & file,struct psinfo * info)60*635a8641SAndroid Build Coastguard Worker bool ReadProcFile(const FilePath& file, struct psinfo* info) {
61*635a8641SAndroid Build Coastguard Worker   // Synchronously reading files in /proc is safe.
62*635a8641SAndroid Build Coastguard Worker   ThreadRestrictions::ScopedAllowIO allow_io;
63*635a8641SAndroid Build Coastguard Worker   int fileId;
64*635a8641SAndroid Build Coastguard Worker   if ((fileId = open(file.value().c_str(), O_RDONLY)) < 0) {
65*635a8641SAndroid Build Coastguard Worker     DLOG(WARNING) << "Failed to open " << file.MaybeAsASCII()
66*635a8641SAndroid Build Coastguard Worker                   << " errno = " << errno;
67*635a8641SAndroid Build Coastguard Worker     return false;
68*635a8641SAndroid Build Coastguard Worker   }
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker   if (read(fileId, info, sizeof(*info)) < 0) {
71*635a8641SAndroid Build Coastguard Worker     DLOG(WARNING) << "Failed to read " << file.MaybeAsASCII()
72*635a8641SAndroid Build Coastguard Worker                   << " errno = " << errno;
73*635a8641SAndroid Build Coastguard Worker     return false;
74*635a8641SAndroid Build Coastguard Worker   }
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   return true;
77*635a8641SAndroid Build Coastguard Worker }
78*635a8641SAndroid Build Coastguard Worker 
ReadProcStats(pid_t pid,struct psinfo * info)79*635a8641SAndroid Build Coastguard Worker bool ReadProcStats(pid_t pid, struct psinfo* info) {
80*635a8641SAndroid Build Coastguard Worker   FilePath stat_file = internalAIX::GetProcPidDir(pid).Append(kStatFile);
81*635a8641SAndroid Build Coastguard Worker   return ReadProcFile(stat_file, info);
82*635a8641SAndroid Build Coastguard Worker }
83*635a8641SAndroid Build Coastguard Worker 
ParseProcStats(struct psinfo & stats_data,std::vector<std::string> * proc_stats)84*635a8641SAndroid Build Coastguard Worker bool ParseProcStats(struct psinfo& stats_data,
85*635a8641SAndroid Build Coastguard Worker                     std::vector<std::string>* proc_stats) {
86*635a8641SAndroid Build Coastguard Worker   // The stat file is formatted as:
87*635a8641SAndroid Build Coastguard Worker   // struct psinfo
88*635a8641SAndroid Build Coastguard Worker   // see -
89*635a8641SAndroid Build Coastguard Worker   // https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.files/proc.htm
90*635a8641SAndroid Build Coastguard Worker   proc_stats->clear();
91*635a8641SAndroid Build Coastguard Worker   // PID.
92*635a8641SAndroid Build Coastguard Worker   proc_stats->push_back(IntToString(stats_data.pr_pid));
93*635a8641SAndroid Build Coastguard Worker   // Process name without parentheses. // 1
94*635a8641SAndroid Build Coastguard Worker   proc_stats->push_back(stats_data.pr_fname);
95*635a8641SAndroid Build Coastguard Worker   // Process State (Not available)  // 2
96*635a8641SAndroid Build Coastguard Worker   proc_stats->push_back("0");
97*635a8641SAndroid Build Coastguard Worker   // Process id of parent  // 3
98*635a8641SAndroid Build Coastguard Worker   proc_stats->push_back(IntToString(stats_data.pr_ppid));
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   // Process group id // 4
101*635a8641SAndroid Build Coastguard Worker   proc_stats->push_back(IntToString(stats_data.pr_pgid));
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker   return true;
104*635a8641SAndroid Build Coastguard Worker }
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker typedef std::map<std::string, std::string> ProcStatMap;
ParseProcStat(const std::string & contents,ProcStatMap * output)107*635a8641SAndroid Build Coastguard Worker void ParseProcStat(const std::string& contents, ProcStatMap* output) {
108*635a8641SAndroid Build Coastguard Worker   StringPairs key_value_pairs;
109*635a8641SAndroid Build Coastguard Worker   SplitStringIntoKeyValuePairs(contents, ' ', '\n', &key_value_pairs);
110*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < key_value_pairs.size(); ++i) {
111*635a8641SAndroid Build Coastguard Worker     output->insert(key_value_pairs[i]);
112*635a8641SAndroid Build Coastguard Worker   }
113*635a8641SAndroid Build Coastguard Worker }
114*635a8641SAndroid Build Coastguard Worker 
GetProcStatsFieldAsInt64(const std::vector<std::string> & proc_stats,ProcStatsFields field_num)115*635a8641SAndroid Build Coastguard Worker int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
116*635a8641SAndroid Build Coastguard Worker                                  ProcStatsFields field_num) {
117*635a8641SAndroid Build Coastguard Worker   DCHECK_GE(field_num, VM_PPID);
118*635a8641SAndroid Build Coastguard Worker   CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());
119*635a8641SAndroid Build Coastguard Worker 
120*635a8641SAndroid Build Coastguard Worker   int64_t value;
121*635a8641SAndroid Build Coastguard Worker   return StringToInt64(proc_stats[field_num], &value) ? value : 0;
122*635a8641SAndroid Build Coastguard Worker }
123*635a8641SAndroid Build Coastguard Worker 
GetProcStatsFieldAsSizeT(const std::vector<std::string> & proc_stats,ProcStatsFields field_num)124*635a8641SAndroid Build Coastguard Worker size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
125*635a8641SAndroid Build Coastguard Worker                                 ProcStatsFields field_num) {
126*635a8641SAndroid Build Coastguard Worker   DCHECK_GE(field_num, VM_PPID);
127*635a8641SAndroid Build Coastguard Worker   CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());
128*635a8641SAndroid Build Coastguard Worker 
129*635a8641SAndroid Build Coastguard Worker   size_t value;
130*635a8641SAndroid Build Coastguard Worker   return StringToSizeT(proc_stats[field_num], &value) ? value : 0;
131*635a8641SAndroid Build Coastguard Worker }
132*635a8641SAndroid Build Coastguard Worker 
ReadProcStatsAndGetFieldAsInt64(pid_t pid,ProcStatsFields field_num)133*635a8641SAndroid Build Coastguard Worker int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num) {
134*635a8641SAndroid Build Coastguard Worker   struct psinfo stats_data;
135*635a8641SAndroid Build Coastguard Worker   if (!ReadProcStats(pid, &stats_data))
136*635a8641SAndroid Build Coastguard Worker     return 0;
137*635a8641SAndroid Build Coastguard Worker   std::vector<std::string> proc_stats;
138*635a8641SAndroid Build Coastguard Worker   if (!ParseProcStats(stats_data, &proc_stats))
139*635a8641SAndroid Build Coastguard Worker     return 0;
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker   return GetProcStatsFieldAsInt64(proc_stats, field_num);
142*635a8641SAndroid Build Coastguard Worker }
143*635a8641SAndroid Build Coastguard Worker 
ReadProcStatsAndGetFieldAsSizeT(pid_t pid,ProcStatsFields field_num)144*635a8641SAndroid Build Coastguard Worker size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid, ProcStatsFields field_num) {
145*635a8641SAndroid Build Coastguard Worker   struct psinfo stats_data;
146*635a8641SAndroid Build Coastguard Worker   if (!ReadProcStats(pid, &stats_data))
147*635a8641SAndroid Build Coastguard Worker     return 0;
148*635a8641SAndroid Build Coastguard Worker   std::vector<std::string> proc_stats;
149*635a8641SAndroid Build Coastguard Worker   if (!ParseProcStats(stats_data, &proc_stats))
150*635a8641SAndroid Build Coastguard Worker     return 0;
151*635a8641SAndroid Build Coastguard Worker   return GetProcStatsFieldAsSizeT(proc_stats, field_num);
152*635a8641SAndroid Build Coastguard Worker }
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker }  // namespace internalAIX
155*635a8641SAndroid Build Coastguard Worker }  // namespace base
156