spawn_faction_adddup2.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (C) 2000, 2009-2013 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://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 !HAVE_WORKING_POSIX_SPAWN
  22. # include "spawn_int.h"
  23. #endif
  24. /* Add an action to FILE-ACTIONS which tells the implementation to call
  25. 'dup2' for the given file descriptors during the 'spawn' call. */
  26. int
  27. posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *file_actions,
  28. int fd, int newfd)
  29. #undef posix_spawn_file_actions_adddup2
  30. {
  31. int maxfd = __sysconf (_SC_OPEN_MAX);
  32. /* Test for the validity of the file descriptor. */
  33. if (fd < 0 || newfd < 0 || fd >= maxfd || newfd >= maxfd)
  34. return EBADF;
  35. #if HAVE_WORKING_POSIX_SPAWN
  36. return posix_spawn_file_actions_adddup2 (file_actions, fd, newfd);
  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_dup2;
  48. rec->action.dup2_action.fd = fd;
  49. rec->action.dup2_action.newfd = newfd;
  50. /* Account for the new entry. */
  51. ++file_actions->_used;
  52. return 0;
  53. }
  54. #endif
  55. }