shim_getpid.c 3.7 KB

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