spawn_faction_addopen.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 'open' for the given file during the 'spawn' call. */
  26. int
  27. posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *file_actions,
  28. int fd, const char *path, int oflag,
  29. mode_t mode)
  30. #undef posix_spawn_file_actions_addopen
  31. {
  32. int maxfd = __sysconf (_SC_OPEN_MAX);
  33. /* Test for the validity of the file descriptor. */
  34. if (fd < 0 || fd >= maxfd)
  35. return EBADF;
  36. #if HAVE_WORKING_POSIX_SPAWN
  37. return posix_spawn_file_actions_addopen (file_actions, fd, path, oflag, mode);
  38. #else
  39. /* Allocate more memory if needed. */
  40. if (file_actions->_used == file_actions->_allocated
  41. && __posix_spawn_file_actions_realloc (file_actions) != 0)
  42. /* This can only mean we ran out of memory. */
  43. return ENOMEM;
  44. {
  45. struct __spawn_action *rec;
  46. /* Add the new value. */
  47. rec = &file_actions->_actions[file_actions->_used];
  48. rec->tag = spawn_do_open;
  49. rec->action.open_action.fd = fd;
  50. rec->action.open_action.path = path;
  51. rec->action.open_action.oflag = oflag;
  52. rec->action.open_action.mode = mode;
  53. /* Account for the new entry. */
  54. ++file_actions->_used;
  55. return 0;
  56. }
  57. #endif
  58. }