sysctl.cpp 3.9 KB

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