internal_aix.cc 4.2 KB

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