w.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/ByteBuffer.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <LibCore/DateTime.h>
  30. #include <LibCore/File.h>
  31. #include <LibCore/ProcessStatisticsReader.h>
  32. #include <pwd.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. #include <time.h>
  36. int main()
  37. {
  38. if (pledge("stdio rpath", nullptr) < 0) {
  39. perror("pledge");
  40. return 1;
  41. }
  42. if (unveil("/dev", "r") < 0) {
  43. perror("unveil");
  44. return 1;
  45. }
  46. if (unveil("/etc/passwd", "r") < 0) {
  47. perror("unveil");
  48. return 1;
  49. }
  50. if (unveil("/var/run/utmp", "r") < 0) {
  51. perror("unveil");
  52. return 1;
  53. }
  54. if (unveil("/proc", "r") < 0) {
  55. perror("unveil");
  56. return 1;
  57. }
  58. unveil(nullptr, nullptr);
  59. auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadOnly);
  60. if (file_or_error.is_error()) {
  61. warnln("Error: {}", file_or_error.error());
  62. return 1;
  63. }
  64. auto& file = *file_or_error.value();
  65. auto json = JsonValue::from_string(file.read_all());
  66. if (!json.has_value() || !json.value().is_object()) {
  67. warnln("Error: Could not parse /var/run/utmp");
  68. return 1;
  69. }
  70. auto process_statistics = Core::ProcessStatisticsReader::get_all();
  71. auto now = time(nullptr);
  72. printf("\033[1m%-10s %-12s %-16s %-6s %s\033[0m\n",
  73. "USER", "TTY", "LOGIN@", "IDLE", "WHAT");
  74. json.value().as_object().for_each_member([&](auto& tty, auto& value) {
  75. const JsonObject& entry = value.as_object();
  76. auto uid = entry.get("uid").to_u32();
  77. auto pid = entry.get("pid").to_i32();
  78. (void)pid;
  79. auto login_time = Core::DateTime::from_timestamp(entry.get("login_at").to_number<time_t>());
  80. auto login_at = login_time.to_string("%b%d %H:%M:%S");
  81. auto* pw = getpwuid(uid);
  82. String username;
  83. if (pw)
  84. username = pw->pw_name;
  85. else
  86. username = String::number(uid);
  87. StringBuilder builder;
  88. String idle_string = "n/a";
  89. struct stat st;
  90. if (stat(tty.characters(), &st) == 0) {
  91. auto idle_time = now - st.st_mtime;
  92. if (idle_time >= 0) {
  93. builder.appendf("%ds", idle_time);
  94. idle_string = builder.to_string();
  95. }
  96. }
  97. String what = "n/a";
  98. for (auto& it : process_statistics) {
  99. if (it.value.tty == tty && it.value.pid == it.value.pgid)
  100. what = it.value.name;
  101. }
  102. printf("%-10s %-12s %-16s %-6s %s\n",
  103. username.characters(),
  104. tty.characters(),
  105. login_at.characters(),
  106. idle_string.characters(),
  107. what.characters());
  108. });
  109. return 0;
  110. }