1 /* Copyright (C) 2018-2021 Free Software Foundation, Inc.
2
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
8 This file is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>. */
15
16 #include <config.h>
17
18 /* Specification. */
19 #include <spawn.h>
20
21 #include <errno.h>
22 #include <unistd.h>
23
24 #if !_LIBC
25 # define __sysconf(open_max) getdtablesize ()
26 #endif
27
28 #if REPLACE_POSIX_SPAWN
29 # include "spawn_int.h"
30 #endif
31
32 /* Add an action to FILE-ACTIONS which tells the implementation to call
33 'fchdir' to the given directory during the 'spawn' call. */
34 int
35 posix_spawn_file_actions_addfchdir (posix_spawn_file_actions_t *file_actions,
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
36 int fd)
37 #undef posix_spawn_file_actions_addfchdir
38 {
39 int maxfd = __sysconf (_SC_OPEN_MAX);
40
41 /* Test for the validity of the file descriptor. */
42 if (fd < 0 || fd >= maxfd)
43 return EBADF;
44
45 #if !REPLACE_POSIX_SPAWN
46 return posix_spawn_file_actions_addfchdir_np (file_actions, fd);
47 #else
48 /* Allocate more memory if needed. */
49 if (file_actions->_used == file_actions->_allocated
50 && __posix_spawn_file_actions_realloc (file_actions) != 0)
51 /* This can only mean we ran out of memory. */
52 return ENOMEM;
53
54 {
55 struct __spawn_action *rec;
56
57 /* Add the new value. */
58 rec = &file_actions->_actions[file_actions->_used];
59 rec->tag = spawn_do_fchdir;
60 rec->action.fchdir_action.fd = fd;
61
62 /* Account for the new entry. */
63 ++file_actions->_used;
64
65 return 0;
66 }
67 #endif
68 }