dtable.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <hurd.h>
  15. #include <hurd/term.h>
  16. #include <hurd/fd.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <limits.h>
  21. #include <lock-intern.h> /* For `struct mutex'. */
  22. #include "set-hooks.h"
  23. #include "hurdmalloc.h" /* XXX */
  24. struct mutex _hurd_dtable_lock = MUTEX_INITIALIZER; /* XXX ld bug; must init */
  25. struct hurd_fd **_hurd_dtable;
  26. int _hurd_dtablesize;
  27. DEFINE_HOOK (_hurd_fd_subinit, (void));
  28. /* Initialize the file descriptor table at startup. */
  29. static void attribute_used_retain
  30. init_dtable (void)
  31. {
  32. int i;
  33. __mutex_init (&_hurd_dtable_lock);
  34. /* The initial size of the descriptor table is that of the passed-in
  35. table. It will be expanded as necessary up to _hurd_dtable_rlimit. */
  36. _hurd_dtablesize = _hurd_init_dtablesize;
  37. /* Allocate the vector of pointers. */
  38. _hurd_dtable = malloc (_hurd_dtablesize * sizeof (*_hurd_dtable));
  39. if (_hurd_dtablesize != 0 && _hurd_dtable == NULL)
  40. __libc_fatal ("hurd: Can't allocate file descriptor table\n");
  41. /* Initialize the descriptor table. */
  42. for (i = 0; (unsigned int) i < _hurd_init_dtablesize; ++i)
  43. {
  44. if (_hurd_init_dtable[i] == MACH_PORT_NULL)
  45. /* An unused descriptor is marked by a null pointer. */
  46. _hurd_dtable[i] = NULL;
  47. else
  48. {
  49. int copy;
  50. /* Allocate a new file descriptor structure. */
  51. struct hurd_fd *new = malloc (sizeof (struct hurd_fd));
  52. if (new == NULL)
  53. __libc_fatal ("hurd: Can't allocate initial file descriptors\n");
  54. /* See if this file descriptor is the same as a previous one we have
  55. already installed. In this case, we can just copy over the same
  56. ctty port without making any more RPCs. We only check the the
  57. immediately preceding fd and fd 0 -- this should be enough to
  58. handle the common cases while not requiring quadratic
  59. complexity. */
  60. if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[i - 1])
  61. copy = i - 1;
  62. else if (i > 0 && _hurd_init_dtable[i] == _hurd_init_dtable[0])
  63. copy = 0;
  64. else
  65. copy = -1;
  66. if (copy < 0)
  67. {
  68. /* Initialize the port cells. */
  69. _hurd_port_init (&new->port, MACH_PORT_NULL);
  70. _hurd_port_init (&new->ctty, MACH_PORT_NULL);
  71. /* Install the port in the descriptor.
  72. This sets up all the ctty magic. */
  73. _hurd_port2fd (new, _hurd_init_dtable[i], 0);
  74. }
  75. else
  76. {
  77. /* Copy over ctty from the already set up file descriptor that
  78. contains the same port. We can access the contents of the
  79. cell without any locking since no one could have seen it
  80. yet. */
  81. mach_port_t ctty = _hurd_dtable[copy]->ctty.port;
  82. if (MACH_PORT_VALID (ctty))
  83. __mach_port_mod_refs (__mach_task_self (), ctty,
  84. MACH_PORT_RIGHT_SEND, +1);
  85. _hurd_port_init (&new->port, _hurd_init_dtable[i]);
  86. _hurd_port_init (&new->ctty, ctty);
  87. }
  88. _hurd_dtable[i] = new;
  89. }
  90. }
  91. /* Clear out the initial descriptor table.
  92. Everything must use _hurd_dtable now. */
  93. __vm_deallocate (__mach_task_self (),
  94. (vm_address_t) _hurd_init_dtable,
  95. _hurd_init_dtablesize * sizeof (_hurd_init_dtable[0]));
  96. _hurd_init_dtable = NULL;
  97. _hurd_init_dtablesize = 0;
  98. /* Initialize the remaining empty slots in the table. */
  99. for (; i < _hurd_dtablesize; ++i)
  100. _hurd_dtable[i] = NULL;
  101. /* Run things that want to run after the file descriptor table
  102. is initialized. */
  103. RUN_RELHOOK (_hurd_fd_subinit, ());
  104. }
  105. SET_RELHOOK (_hurd_subinit, init_dtable);
  106. /* XXX when the linker supports it, the following functions should all be
  107. elsewhere and just have text_set_elements here. */
  108. /* Called by `getdport' to do its work. */
  109. static file_t
  110. get_dtable_port (int fd)
  111. {
  112. struct hurd_fd *d = _hurd_fd_get (fd);
  113. file_t dport;
  114. if (!d)
  115. return __hurd_fail (EBADF), MACH_PORT_NULL;
  116. HURD_CRITICAL_BEGIN;
  117. dport = HURD_PORT_USE (&d->port,
  118. ({
  119. error_t err;
  120. mach_port_t outport;
  121. err = __mach_port_mod_refs (__mach_task_self (),
  122. port,
  123. MACH_PORT_RIGHT_SEND,
  124. 1);
  125. if (err)
  126. {
  127. errno = err;
  128. outport = MACH_PORT_NULL;
  129. }
  130. else
  131. outport = port;
  132. outport;
  133. }));
  134. HURD_CRITICAL_END;
  135. return dport;
  136. }
  137. file_t (*_hurd_getdport_fn) (int fd) = get_dtable_port;
  138. #include <hurd/signal.h>
  139. /* We are in the child fork; the dtable lock is still held.
  140. The parent has inserted send rights for all the normal io ports,
  141. but we must recover ctty-special ports for ourselves. */
  142. static error_t
  143. fork_child_dtable (void)
  144. {
  145. error_t err;
  146. int i;
  147. err = 0;
  148. for (i = 0; !err && i < _hurd_dtablesize; ++i)
  149. {
  150. struct hurd_fd *d = _hurd_dtable[i];
  151. if (d == NULL)
  152. continue;
  153. /* No other thread is using the send rights in the child task. */
  154. d->port.users = d->ctty.users = NULL;
  155. if (d->ctty.port != MACH_PORT_NULL)
  156. {
  157. /* There was a ctty-special port in the parent.
  158. We need to get one for ourselves too. */
  159. __mach_port_deallocate (__mach_task_self (), d->ctty.port);
  160. err = __term_open_ctty (d->port.port, _hurd_pid, _hurd_pgrp,
  161. &d->ctty.port);
  162. if (err)
  163. d->ctty.port = MACH_PORT_NULL;
  164. }
  165. /* XXX for each fd with a cntlmap, reauth and re-map_cntl. */
  166. }
  167. return err;
  168. (void) &fork_child_dtable; /* Avoid "defined but not used" warning. */
  169. }
  170. data_set_element (_hurd_fork_locks, _hurd_dtable_lock); /* XXX ld bug: bss */
  171. text_set_element (_hurd_fork_child_hook, fork_child_dtable);
  172. /* Called when our process group has changed. */
  173. static void
  174. ctty_new_pgrp (void)
  175. {
  176. int i;
  177. retry:
  178. HURD_CRITICAL_BEGIN;
  179. __mutex_lock (&_hurd_dtable_lock);
  180. if (__USEPORT (CTTYID, port == MACH_PORT_NULL))
  181. {
  182. /* We have no controlling terminal. If we haven't had one recently,
  183. but our pgrp is being pointlessly diddled anyway, then we will
  184. have nothing to do in the loop below because no fd will have a
  185. ctty port at all.
  186. More likely, a setsid call is responsible both for the change
  187. in pgrp and for clearing the cttyid port. In that case, setsid
  188. held the dtable lock while updating the dtable to clear all the
  189. ctty ports, and ergo must have finished doing so before we run here.
  190. So we can be sure, again, that the loop below has no work to do. */
  191. }
  192. else
  193. for (i = 0; i < _hurd_dtablesize; ++i)
  194. {
  195. struct hurd_fd *const d = _hurd_dtable[i];
  196. struct hurd_userlink ulink, ctty_ulink;
  197. io_t port, ctty;
  198. if (d == NULL)
  199. /* Nothing to do for an unused descriptor cell. */
  200. continue;
  201. port = _hurd_port_get (&d->port, &ulink);
  202. ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
  203. if (ctty != MACH_PORT_NULL)
  204. {
  205. /* This fd has a ctty-special port. We need a new one, to tell
  206. the io server of our different process group. */
  207. io_t new;
  208. error_t err;
  209. if ((err = __term_open_ctty (port, _hurd_pid, _hurd_pgrp, &new)))
  210. {
  211. if (err == EINTR)
  212. {
  213. /* Got a signal while inside an RPC of the critical section, retry again */
  214. __mutex_unlock (&_hurd_dtable_lock);
  215. HURD_CRITICAL_UNLOCK;
  216. goto retry;
  217. }
  218. new = MACH_PORT_NULL;
  219. }
  220. _hurd_port_set (&d->ctty, new);
  221. }
  222. _hurd_port_free (&d->port, &ulink, port);
  223. _hurd_port_free (&d->ctty, &ctty_ulink, ctty);
  224. }
  225. __mutex_unlock (&_hurd_dtable_lock);
  226. HURD_CRITICAL_END;
  227. (void) &ctty_new_pgrp; /* Avoid "defined but not used" warning. */
  228. }
  229. text_set_element (_hurd_pgrp_changed_hook, ctty_new_pgrp);
  230. /* Called to reauthenticate the dtable when the auth port changes. */
  231. static void
  232. reauth_dtable (void)
  233. {
  234. int i;
  235. retry:
  236. HURD_CRITICAL_BEGIN;
  237. __mutex_lock (&_hurd_dtable_lock);
  238. for (i = 0; i < _hurd_dtablesize; ++i)
  239. {
  240. struct hurd_fd *const d = _hurd_dtable[i];
  241. mach_port_t new, newctty, ref;
  242. error_t err = 0;
  243. if (d == NULL)
  244. /* Nothing to do for an unused descriptor cell. */
  245. continue;
  246. ref = __mach_reply_port ();
  247. /* Take the descriptor cell's lock. */
  248. __spin_lock (&d->port.lock);
  249. /* Reauthenticate the descriptor's port. */
  250. if (d->port.port != MACH_PORT_NULL
  251. && ! (err = __io_reauthenticate (d->port.port,
  252. ref, MACH_MSG_TYPE_MAKE_SEND))
  253. && ! (err = __USEPORT (AUTH, __auth_user_authenticate
  254. (port,
  255. ref, MACH_MSG_TYPE_MAKE_SEND,
  256. &new))))
  257. {
  258. /* Replace the port in the descriptor cell
  259. with the newly reauthenticated port. */
  260. if (d->ctty.port != MACH_PORT_NULL
  261. && ! (err = __io_reauthenticate (d->ctty.port,
  262. ref, MACH_MSG_TYPE_MAKE_SEND))
  263. && ! (err = __USEPORT (AUTH, __auth_user_authenticate
  264. (port,
  265. ref, MACH_MSG_TYPE_MAKE_SEND,
  266. &newctty))))
  267. _hurd_port_set (&d->ctty, newctty);
  268. _hurd_port_locked_set (&d->port, new);
  269. }
  270. else
  271. /* Lost. Leave this descriptor cell alone. */
  272. __spin_unlock (&d->port.lock);
  273. __mach_port_destroy (__mach_task_self (), ref);
  274. if (err == EINTR)
  275. {
  276. /* Got a signal while inside an RPC of the critical section,
  277. retry again */
  278. __mutex_unlock (&_hurd_dtable_lock);
  279. HURD_CRITICAL_UNLOCK;
  280. goto retry;
  281. }
  282. }
  283. __mutex_unlock (&_hurd_dtable_lock);
  284. HURD_CRITICAL_END;
  285. (void) &reauth_dtable; /* Avoid "defined but not used" warning. */
  286. }
  287. text_set_element (_hurd_reauth_hook, reauth_dtable);