spawn_faction_addclose.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (C) 2000, 2009-2023 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <spawn.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #if !_LIBC
  19. # define __sysconf(open_max) getdtablesize ()
  20. #endif
  21. #if REPLACE_POSIX_SPAWN
  22. # include "spawn_int.h"
  23. #endif
  24. /* Add an action to FILE-ACTIONS which tells the implementation to call
  25. 'close' for the given file descriptor during the 'spawn' call. */
  26. int
  27. posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
  28. int fd)
  29. #undef posix_spawn_file_actions_addclose
  30. {
  31. int maxfd = __sysconf (_SC_OPEN_MAX);
  32. /* Test for the validity of the file descriptor. */
  33. if (fd < 0 || fd >= maxfd)
  34. return EBADF;
  35. #if !REPLACE_POSIX_SPAWN
  36. return posix_spawn_file_actions_addclose (file_actions, fd);
  37. #else
  38. /* Allocate more memory if needed. */
  39. if (file_actions->_used == file_actions->_allocated
  40. && __posix_spawn_file_actions_realloc (file_actions) != 0)
  41. /* This can only mean we ran out of memory. */
  42. return ENOMEM;
  43. {
  44. struct __spawn_action *rec;
  45. /* Add the new value. */
  46. rec = &file_actions->_actions[file_actions->_used];
  47. rec->tag = spawn_do_close;
  48. rec->action.open_action.fd = fd;
  49. /* Account for the new entry. */
  50. ++file_actions->_used;
  51. return 0;
  52. }
  53. #endif
  54. }