summaryrefslogtreecommitdiff
path: root/lib/save-cwd.c
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2009-09-19 07:12:15 -0600
committerEric Blake <ebb9@byu.net>2009-09-19 08:18:06 -0600
commit996f76cd71a98365044457d94b0d87454bcd4deb (patch)
treee3be327949e601c9a9b153f58272d38c09c7496a /lib/save-cwd.c
parent112523aea20cc9254b36e0644ca2029b634bfae6 (diff)
downloadgnulib-996f76cd71a98365044457d94b0d87454bcd4deb.tar.gz
openat: allow return of fd 0
Partially reverts patch fc33350 from 2009-09-02. * modules/chdir-long (Depends-on): Relax openat-safer to openat. * modules/save-cwd (Depends-on): Replace fcntl-safer with unistd-safer. * lib/chdir-long.c (includes): Replace "fcntl--.h" with <fcntl.h>; this module does not leak fds. * lib/openat.c (includes): Do not use "fcntl_safer"; plain openat must be allowed to return 0, leaving openat_safer to add the safety. (openat_permissive): Avoid writing to just-opened fd 2 if restoring the current directory fails. * lib/openat-die.c (openat_restore_fail): Add comment. * lib/save-cwd.c (includes): Make "fcntl--.h" conditional. (save_cwd): Guarantee safe fd, but without use of open_safer. * tests/test-openat.c: New test. * modules/openat-tests (Files, Makefile.am): Distribute and build new file. Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'lib/save-cwd.c')
-rw-r--r--lib/save-cwd.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/save-cwd.c b/lib/save-cwd.c
index e158e8b6b2..7c2eb6d94f 100644
--- a/lib/save-cwd.c
+++ b/lib/save-cwd.c
@@ -1,6 +1,6 @@
/* save-cwd.c -- Save and restore current working directory.
- Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005, 2006 Free
+ Copyright (C) 1995, 1997, 1998, 2003, 2004, 2005, 2006, 2009 Free
Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
@@ -23,15 +23,21 @@
#include "save-cwd.h"
#include <errno.h>
+#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include "chdir-long.h"
-#include "fcntl--.h"
+#include "unistd--.h"
#include "xgetcwd.h"
+#if GNULIB_FCNTL_SAFER
+# include "fcntl--.h"
+#else
+# define GNULIB_FCNTL_SAFER 0
+#endif
+
/* On systems without the fchdir function (WOE), pretend that open
always returns -1 so that save_cwd resorts to using xgetcwd.
Since chdir_long requires fchdir, use chdir instead. */
@@ -70,6 +76,8 @@ save_cwd (struct saved_cwd *cwd)
cwd->name = NULL;
cwd->desc = open (".", O_RDONLY);
+ if (!GNULIB_FCNTL_SAFER)
+ cwd->desc = fd_safer (cwd->desc);
if (cwd->desc < 0)
{
cwd->name = xgetcwd ();