syscall.cpp 3.5 KB

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