1 /* Test that spawn file action functions work without file limit.
2 Copyright (C) 2016-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <sys/resource.h>
27 /* _SC_OPEN_MAX value. */
30 /* A positive but unused file descriptor, used for testing
32 static int invalid_fd
;
34 /* Indicate that errors have been encountered. */
37 static posix_spawn_file_actions_t actions
;
40 one_test (const char *name
, int (*func
) (int), int fd
,
49 printf ("error: posix_spawn_file_actions_%s (%d): %m\n", name
, fd
);
53 else if (ret
!= EBADF
)
56 printf ("error: posix_spawn_file_actions_%s (%d):"
57 " unexpected success\n", name
, fd
);
61 printf ("error: posix_spawn_file_actions_%s (%d): %m\n", name
, fd
);
68 all_tests (const char *name
, int (*func
) (int))
70 one_test (name
, func
, 0, true);
71 one_test (name
, func
, invalid_fd
, true);
72 one_test (name
, func
, -1, false);
73 one_test (name
, func
, -2, false);
75 one_test (name
, func
, maxfd
, false);
81 return posix_spawn_file_actions_addopen
82 (&actions
, fd
, "/dev/null", O_RDONLY
, 0);
88 return posix_spawn_file_actions_adddup2 (&actions
, fd
, 1);
92 adddup2_reverse (int fd
)
94 return posix_spawn_file_actions_adddup2 (&actions
, 1, fd
);
100 return posix_spawn_file_actions_addclose (&actions
, fd
);
106 all_tests ("addopen", addopen
);
107 all_tests ("adddup2", adddup2
);
108 all_tests ("adddup2", adddup2_reverse
);
109 all_tests ("adddup2", addclose
);
115 /* Try to eliminate the file descriptor limit. */
118 if (getrlimit (RLIMIT_NOFILE
, &limit
) < 0)
120 printf ("error: getrlimit: %m\n");
123 limit
.rlim_cur
= RLIM_INFINITY
;
124 if (setrlimit (RLIMIT_NOFILE
, &limit
) < 0)
125 printf ("warning: setrlimit: %m\n");
128 maxfd
= sysconf (_SC_OPEN_MAX
);
129 printf ("info: _SC_OPEN_MAX: %ld\n", maxfd
);
131 invalid_fd
= dup (0);
134 printf ("error: dup: %m\n");
137 if (close (invalid_fd
) < 0)
139 printf ("error: close: %m\n");
143 int ret
= posix_spawn_file_actions_init (&actions
);
147 printf ("error: posix_spawn_file_actions_init: %m\n");
153 ret
= posix_spawn_file_actions_destroy (&actions
);
157 printf ("error: posix_spawn_file_actions_destroy: %m\n");
164 #define TEST_FUNCTION do_test ()
165 #include "../test-skeleton.c"