shim_getpid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_getpid.c
  15. *
  16. * Implementation of system call "getpid", "gettid", "getppid",
  17. * "set_tid_address", "getuid", "getgid", "setuid", "setgid", "geteuid",
  18. * "getegid", "setpgid", "getpgid", "getpgrp", "setsid" and "getsid".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <sys/syscall.h>
  26. #include <sys/mman.h>
  27. #include <asm/prctl.h>
  28. #include <errno.h>
  29. pid_t shim_do_getpid (void)
  30. {
  31. struct shim_thread * cur = get_cur_thread();
  32. return cur ? cur->tgid : 0;
  33. }
  34. pid_t shim_do_gettid (void)
  35. {
  36. struct shim_thread * cur = get_cur_thread();
  37. return cur ? cur->tid : 0;
  38. }
  39. pid_t shim_do_getppid (void)
  40. {
  41. struct shim_thread * cur = get_cur_thread();
  42. return cur ? (cur->parent ? cur->parent->tid : cur->ppid) : 0;
  43. }
  44. int shim_do_set_tid_address (int * tidptr)
  45. {
  46. /* http://man7.org/linux/man-pages/man2/set_tid_address.2.html */
  47. struct shim_thread * cur = get_cur_thread();
  48. cur->clear_child_tid = tidptr;
  49. return cur->tid;
  50. }
  51. uid_t shim_do_getuid (void)
  52. {
  53. struct shim_thread * cur = get_cur_thread();
  54. return cur ? cur->uid : 0;
  55. }
  56. gid_t shim_do_getgid (void)
  57. {
  58. struct shim_thread * cur = get_cur_thread();
  59. return cur ? cur->gid : 0;
  60. }
  61. int shim_do_setuid (uid_t uid)
  62. {
  63. struct shim_thread * cur = get_cur_thread();
  64. cur->euid = (uint16_t) uid;
  65. return 0;
  66. }
  67. int shim_do_setgid (gid_t gid)
  68. {
  69. struct shim_thread * cur = get_cur_thread();
  70. cur->egid = (uint16_t) gid;
  71. return 0;
  72. }
  73. uid_t shim_do_geteuid (void)
  74. {
  75. struct shim_thread * cur = get_cur_thread();
  76. return cur ? cur->euid : 0;
  77. }
  78. gid_t shim_do_getegid (void)
  79. {
  80. struct shim_thread * cur = get_cur_thread();
  81. return cur ? cur->egid : 0;
  82. }
  83. int shim_do_setpgid (pid_t pid, pid_t pgid)
  84. {
  85. struct shim_thread * thread =
  86. pid ? lookup_thread(pid) : get_cur_thread();
  87. if (!pid)
  88. assert(thread);
  89. if (!thread)
  90. return -ESRCH;
  91. thread->pgid = pgid ? : thread->tgid;
  92. return 0;
  93. }
  94. int shim_do_getpgid (pid_t pid)
  95. {
  96. struct shim_thread * thread =
  97. pid ? lookup_thread(pid) : get_cur_thread();
  98. if (!thread)
  99. return -ESRCH;
  100. return thread->pgid;
  101. }
  102. pid_t shim_do_getpgrp (void)
  103. {
  104. struct shim_thread * cur_thread = get_cur_thread();
  105. assert(cur_thread);
  106. return cur_thread->pgid;
  107. }
  108. int shim_do_setsid (void)
  109. {
  110. struct shim_thread * cur_thread = get_cur_thread();
  111. assert(cur_thread);
  112. if (cur_thread->pgid == cur_thread->tgid)
  113. return -EPERM;
  114. cur_thread->pgid = cur_thread->tgid;
  115. /* TODO: the calling process may haveto be detached from the
  116. tty, but there is no need to handle it for now. */
  117. return 0;
  118. }
  119. int shim_do_getsid (pid_t pid)
  120. {
  121. struct shim_thread * thread =
  122. pid ? lookup_thread(pid) : get_cur_thread();
  123. if (!thread)
  124. return -ESRCH;
  125. return thread->pgid;
  126. }