w.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonObject.h>
  27. #include <AK/JsonValue.h>
  28. #include <LibCore/DateTime.h>
  29. #include <LibCore/File.h>
  30. #include <LibCore/ProcessStatisticsReader.h>
  31. #include <pwd.h>
  32. #include <stdio.h>
  33. #include <sys/stat.h>
  34. #include <time.h>
  35. int main()
  36. {
  37. if (pledge("stdio rpath", nullptr) < 0) {
  38. perror("pledge");
  39. return 1;
  40. }
  41. if (unveil("/dev", "r") < 0) {
  42. perror("unveil");
  43. return 1;
  44. }
  45. if (unveil("/etc/passwd", "r") < 0) {
  46. perror("unveil");
  47. return 1;
  48. }
  49. if (unveil("/var/run/utmp", "r") < 0) {
  50. perror("unveil");
  51. return 1;
  52. }
  53. if (unveil("/proc", "r") < 0) {
  54. perror("unveil");
  55. return 1;
  56. }
  57. unveil(nullptr, nullptr);
  58. auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadOnly);
  59. if (file_or_error.is_error()) {
  60. warn() << "Error: " << file_or_error.error();
  61. return 1;
  62. }
  63. auto& file = *file_or_error.value();
  64. auto json = JsonValue::from_string(file.read_all());
  65. if (!json.has_value() || !json.value().is_object()) {
  66. warn() << "Error: Could not parse /var/run/utmp";
  67. return 1;
  68. }
  69. auto process_statistics = Core::ProcessStatisticsReader::get_all();
  70. auto now = time(nullptr);
  71. printf("\033[1m%-10s %-12s %-16s %-6s %s\033[0m\n",
  72. "USER", "TTY", "LOGIN@", "IDLE", "WHAT");
  73. json.value().as_object().for_each_member([&](auto& tty, auto& value) {
  74. const JsonObject& entry = value.as_object();
  75. auto uid = entry.get("uid").to_u32();
  76. auto pid = entry.get("pid").to_i32();
  77. (void)pid;
  78. auto login_time = Core::DateTime::from_timestamp(entry.get("login_at").to_number<time_t>());
  79. auto login_at = login_time.to_string("%b%d %H:%M:%S");
  80. auto* pw = getpwuid(uid);
  81. String username;
  82. if (pw)
  83. username = pw->pw_name;
  84. else
  85. username = String::number(uid);
  86. StringBuilder builder;
  87. String idle_string = "n/a";
  88. struct stat st;
  89. if (stat(tty.characters(), &st) == 0) {
  90. auto idle_time = now - st.st_mtime;
  91. if (idle_time >= 0) {
  92. builder.appendf("%ds", idle_time);
  93. idle_string = builder.to_string();
  94. }
  95. }
  96. String what = "n/a";
  97. for (auto& it : process_statistics) {
  98. if (it.value.tty == tty && it.value.pid == it.value.pgid)
  99. what = it.value.name;
  100. }
  101. printf("%-10s %-12s %-16s %-6s %s\n",
  102. username.characters(),
  103. tty.characters(),
  104. login_at.characters(),
  105. idle_string.characters(),
  106. what.characters());
  107. });
  108. return 0;
  109. }