sysctl.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-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/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/Vector.h>
  30. #include <LibCore/ArgsParser.h>
  31. #include <LibCore/DirIterator.h>
  32. #include <LibCore/File.h>
  33. #include <dirent.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. static String read_var(const String& name)
  40. {
  41. StringBuilder builder;
  42. builder.append("/proc/sys/");
  43. builder.append(name);
  44. auto path = builder.to_string();
  45. auto f = Core::File::construct(path);
  46. if (!f->open(Core::IODevice::ReadOnly)) {
  47. fprintf(stderr, "open: %s\n", f->error_string());
  48. exit(1);
  49. }
  50. const auto& b = f->read_all();
  51. if (f->error() < 0) {
  52. fprintf(stderr, "read: %s\n", f->error_string());
  53. exit(1);
  54. }
  55. return String((const char*)b.data(), b.size(), Chomp);
  56. }
  57. static void write_var(const String& name, const String& value)
  58. {
  59. StringBuilder builder;
  60. builder.append("/proc/sys/");
  61. builder.append(name);
  62. auto path = builder.to_string();
  63. auto f = Core::File::construct(path);
  64. if (!f->open(Core::IODevice::WriteOnly)) {
  65. fprintf(stderr, "open: %s\n", f->error_string());
  66. exit(1);
  67. }
  68. f->write(value);
  69. if (f->error() < 0) {
  70. fprintf(stderr, "write: %s\n", f->error_string());
  71. exit(1);
  72. }
  73. }
  74. static int handle_show_all()
  75. {
  76. Core::DirIterator di("/proc/sys", Core::DirIterator::SkipDots);
  77. if (di.has_error()) {
  78. fprintf(stderr, "DirIterator: %s\n", di.error_string());
  79. return 1;
  80. }
  81. while (di.has_next()) {
  82. String variable_name = di.next_path();
  83. printf("%s = %s\n", variable_name.characters(), read_var(variable_name).characters());
  84. }
  85. return 0;
  86. }
  87. static int handle_var(const String& var)
  88. {
  89. String spec(var.characters(), Chomp);
  90. auto parts = spec.split('=');
  91. String variable_name = parts[0];
  92. bool is_write = parts.size() > 1;
  93. if (!is_write) {
  94. printf("%s = %s\n", variable_name.characters(), read_var(variable_name).characters());
  95. return 0;
  96. }
  97. printf("%s = %s", variable_name.characters(), read_var(variable_name).characters());
  98. write_var(variable_name, parts[1]);
  99. printf(" -> %s\n", read_var(variable_name).characters());
  100. return 0;
  101. }
  102. int main(int argc, char** argv)
  103. {
  104. bool show_all = false;
  105. const char* var = nullptr;
  106. Core::ArgsParser args_parser;
  107. args_parser.add_option(show_all, "Show all variables", nullptr, 'a');
  108. args_parser.add_positional_argument(var, "Command (var[=value])", "command", Core::ArgsParser::Required::No);
  109. args_parser.parse(argc, argv);
  110. if (var == nullptr) {
  111. // Not supplied; assume `-a`.
  112. show_all = true;
  113. }
  114. if (show_all) {
  115. // Ignore `var`, even if it was supplied. Just like the real procps does.
  116. return handle_show_all();
  117. }
  118. return handle_var(var);
  119. }