annotate lib/spawn_faction_addclose.c @ 12246:b156d2d1b827

freopen-safer: new module * modules/freopen-safer: New module. * m4/stdio-safer.m4 (gl_FREOPEN_SAFER): New macro. * lib/freopen-safer.c (freopen_safer): New file. * lib/stdio-safer.h (freopen_safer): New declaration. * lib/stdio--.h (freopen): New override. * MODULES.html.sh (File stream based Input/Output): Mention it. * doc/posix-functions/freopen.texi (freopen): Mention pitfalls and freopen-safer module. * doc/posix-functions/stderr.texi (stderr): Likewise. * doc/posix-functions/stdin.texi (stdin): Likewise. * doc/posix-functions/stdout.texi (stdout): Likewise. * modules/freopen-safer-tests: New test. * tests/test-reopen-safer.c: New file. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Thu, 05 Nov 2009 15:13:00 -0700
parents e893ef7aef81
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10507
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 This file is part of the GNU C Library.
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <spawn.h>
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <errno.h>
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <unistd.h>
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #if !_LIBC
10584
e893ef7aef81 Use getdtablesize module.
Bruno Haible <bruno@clisp.org>
parents: 10507
diff changeset
26 # define __sysconf(open_max) getdtablesize ()
10507
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #endif
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include "spawn_int.h"
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Add an action to FILE-ACTIONS which tells the implementation to call
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 `close' for the given file descriptor during the `spawn' call. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 int
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 int fd)
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 int maxfd = __sysconf (_SC_OPEN_MAX);
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 struct __spawn_action *rec;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 /* Test for the validity of the file descriptor. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 if (fd < 0 || fd >= maxfd)
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 return EBADF;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Allocate more memory if needed. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 if (file_actions->_used == file_actions->_allocated
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 && __posix_spawn_file_actions_realloc (file_actions) != 0)
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* This can only mean we ran out of memory. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 return ENOMEM;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 /* Add the new value. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 rec = &file_actions->_actions[file_actions->_used];
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 rec->tag = spawn_do_close;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 rec->action.open_action.fd = fd;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* Account for the new entry. */
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 ++file_actions->_used;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 return 0;
e0e84ec52436 New module 'posix_spawn_file_actions_addclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 }