syscall.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <LibCore/ArgsParser.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <syscall.h>
  31. #define SC_NARG 4
  32. FlatPtr arg[SC_NARG];
  33. char buf[BUFSIZ];
  34. static FlatPtr parse(const char* s);
  35. int main(int argc, char** argv)
  36. {
  37. bool output_buffer = false;
  38. bool list_syscalls = false;
  39. Vector<const char*> arguments;
  40. Core::ArgsParser args_parser;
  41. args_parser.add_option(output_buffer, "Output the contents of the buffer (beware of stray zero bytes!)", "output-buffer", 'o');
  42. args_parser.add_option(list_syscalls, "List all existing syscalls", "list-syscalls", 'l');
  43. args_parser.add_positional_argument(arguments, "Syscall arguments (can be strings, 'buf' for the output buffer, or numbers like 1234 or 0xffffffff)", "syscall-arguments");
  44. args_parser.parse(argc, argv);
  45. for (size_t i = 0; i < arguments.size(); i++) {
  46. arg[i] = parse(arguments[i]);
  47. }
  48. for (int sc = 0; sc < Syscall::Function::__Count; ++sc) {
  49. if (strcmp(Syscall::to_string((Syscall::Function)sc), (char*)arg[0]) == 0) {
  50. int rc = syscall(sc, arg[1], arg[2], arg[3]);
  51. if (rc == -1) {
  52. perror("syscall");
  53. } else {
  54. if (output_buffer)
  55. fwrite(buf, 1, sizeof(buf), stdout);
  56. }
  57. fprintf(stderr, "Syscall return: %d\n", rc);
  58. return 0;
  59. }
  60. }
  61. fprintf(stderr, "Invalid syscall entry %s\n", (char*)arg[0]);
  62. return -1;
  63. }
  64. FlatPtr parse(const char* s)
  65. {
  66. char* t;
  67. FlatPtr l;
  68. if (strcmp(s, "buf") == 0) {
  69. return (FlatPtr)buf;
  70. }
  71. l = strtoul(s, &t, 0);
  72. if (t > s && *t == 0) {
  73. return l;
  74. }
  75. return (FlatPtr)s;
  76. }