Branch
Hash :
87555a59
Author :
Date :
2025-05-28T10:40:21
open, openat: handle O_DIRECTORY on special files On deficient platforms where we must check for directories ourselves when opening files, check before opening as well as after. This prevents a hang when trying to open a special file like a fifo in a context where a directory is required. Although there is still a race so we could still hang in a perverse situation, it’s the best we can do and it is better than hanging in the more-common case. * lib/open.c (lstatif): New static function. (open) [REPLACE_FCHDIR]: Also inspect O_CREAT. * lib/open.c (open), lib/openat.c (rpl_openat): When checking for directories, also do this before opening. Also, respect O_NOFOLLOW when checking for directories. * lib/openat.c: Remove a few more unnecessary differences from open.c. * modules/open (Depends-on): Depend on lstat. * modules/openat (Depends-on): Add fstatat. * modules/open-tests, modules/openat-tests: (configure.ac) Check for alarm decl. * tests/test-open.c, tests/test-openat.c: Include sys/stat.h, for mkfifo. [HAVE_DECL_ALARM]: Include signal.h, for alarm. * tests/test-open.h (test_open): Fail if test takes too long because we tried to open a fifo. Test opening /dev/null, /dev/tty and a fifo, with a trailing "/" and with O_DIRECTORY.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* Test of opening a file descriptor.
Copyright (C) 2007-2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
#include <config.h>
#include <fcntl.h>
#include "signature.h"
SIGNATURE_CHECK (open, int, (char const *, int, ...));
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#if HAVE_DECL_ALARM
# include <signal.h>
#endif
#include "macros.h"
#define BASE "test-open.t"
#include "test-open.h"
int
main (void)
{
int result = test_open (open, true);
return (result ? result : test_exit_status);
}