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