syscall.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <Kernel/API/Syscall.h>
  27. #include <errno.h>
  28. #include <getopt.h>
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #if !defined __ENUMERATE_SYSCALL
  35. # define __ENUMERATE_SYSCALL(x) SC_##x,
  36. #endif
  37. #define SC_NARG 4
  38. Syscall::Function syscall_table[] = {
  39. ENUMERATE_SYSCALLS(__ENUMERATE_SYSCALL)
  40. };
  41. FlatPtr arg[SC_NARG];
  42. char buf[BUFSIZ];
  43. FlatPtr parse(char* s);
  44. int main(int argc, char** argv)
  45. {
  46. int oflag;
  47. int opt;
  48. while ((opt = getopt(argc, argv, "olh")) != -1) {
  49. switch (opt) {
  50. case 'o':
  51. oflag = 1;
  52. break;
  53. case 'l':
  54. for (auto sc : syscall_table) {
  55. fprintf(stdout, "%s ", Syscall::to_string(sc));
  56. }
  57. return EXIT_SUCCESS;
  58. case 'h':
  59. fprintf(stderr, "usage: \tsyscall [-o] [-l] [-h] <syscall-name> <args...> [buf==BUFSIZ buffer]\n");
  60. fprintf(stderr, "\tsyscall write 1 hello 5\n");
  61. fprintf(stderr, "\tsyscall -o read 0 buf 5\n");
  62. fprintf(stderr, "\tsyscall sleep 3\n");
  63. break;
  64. default:
  65. exit(EXIT_FAILURE);
  66. }
  67. }
  68. if (optind >= argc) {
  69. fprintf(stderr, "No entry specified\n");
  70. return -1;
  71. }
  72. for (int i = 0; i < argc - optind; i++) {
  73. arg[i] = parse(argv[i + optind]);
  74. }
  75. for (auto sc : syscall_table) {
  76. if (strcmp(Syscall::to_string(sc), (char*)arg[0]) == 0) {
  77. int rc = syscall(sc, arg[1], arg[2], arg[3]);
  78. if (rc == -1) {
  79. perror("syscall");
  80. } else {
  81. if (oflag)
  82. fwrite(buf, 1, sizeof(buf), stdout);
  83. }
  84. fprintf(stderr, "Syscall return: %d\n", rc);
  85. return 0;
  86. }
  87. }
  88. fprintf(stderr, "Invalid syscall entry %s\n", (char*)arg[0]);
  89. return -1;
  90. }
  91. FlatPtr parse(char* s)
  92. {
  93. char* t;
  94. FlatPtr l;
  95. if (strcmp(s, "buf") == 0) {
  96. return (FlatPtr)buf;
  97. }
  98. l = strtoul(s, &t, 0);
  99. if (t > s && *t == 0) {
  100. return l;
  101. }
  102. return (FlatPtr)s;
  103. }