openat: avoid using wrong fd
[gnulib.git] / lib / at-func.c
1 /* Define an at-style functions like fstatat, unlinkat, fchownat, etc.
2    Copyright (C) 2006, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* written by Jim Meyering */
18
19 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
20 #include "openat.h"
21 #include "openat-priv.h"
22 #include "save-cwd.h"
23
24 #ifdef AT_FUNC_USE_F1_COND
25 # define CALL_FUNC(F)                           \
26   (flag == AT_FUNC_USE_F1_COND                  \
27     ? AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS)     \
28     : AT_FUNC_F2 (F AT_FUNC_POST_FILE_ARGS))
29 # define VALIDATE_FLAG(F)                       \
30   if (flag & ~AT_FUNC_USE_F1_COND)              \
31     {                                           \
32       errno = EINVAL;                           \
33       return -1;                                \
34     }
35 #else
36 # define CALL_FUNC(F) (AT_FUNC_F1 (F AT_FUNC_POST_FILE_ARGS))
37 # define VALIDATE_FLAG(F) /* empty */
38 #endif
39
40 #ifdef AT_FUNC_RESULT
41 # define FUNC_RESULT AT_FUNC_RESULT
42 #else
43 # define FUNC_RESULT int
44 #endif
45
46 /* Call AT_FUNC_F1 to operate on FILE, which is in the directory
47    open on descriptor FD.  If AT_FUNC_USE_F1_COND is defined to a value,
48    AT_FUNC_POST_FILE_PARAM_DECLS must inlude a parameter named flag;
49    call AT_FUNC_F2 if FLAG is 0 or fail if FLAG contains more bits than
50    AT_FUNC_USE_F1_COND.  If possible, do it without changing the
51    working directory.  Otherwise, resort to using save_cwd/fchdir,
52    then AT_FUNC_F?/restore_cwd.  If either the save_cwd or the restore_cwd
53    fails, then give a diagnostic and exit nonzero.  */
54 FUNC_RESULT
55 AT_FUNC_NAME (int fd, char const *file AT_FUNC_POST_FILE_PARAM_DECLS)
56 {
57   /* Be careful to choose names unlikely to conflict with
58      AT_FUNC_POST_FILE_PARAM_DECLS.  */
59   struct saved_cwd saved_cwd;
60   int saved_errno;
61   FUNC_RESULT err;
62
63   VALIDATE_FLAG (flag);
64
65   if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
66     return CALL_FUNC (file);
67
68   {
69     char proc_buf[OPENAT_BUFFER_SIZE];
70     char *proc_file = openat_proc_name (proc_buf, fd, file);
71     if (proc_file)
72       {
73         FUNC_RESULT proc_result = CALL_FUNC (proc_file);
74         int proc_errno = errno;
75         if (proc_file != proc_buf)
76           free (proc_file);
77         /* If the syscall succeeds, or if it fails with an unexpected
78            errno value, then return right away.  Otherwise, fall through
79            and resort to using save_cwd/restore_cwd.  */
80         if (0 <= proc_result)
81           return proc_result;
82         if (! EXPECTED_ERRNO (proc_errno))
83           {
84             errno = proc_errno;
85             return proc_result;
86           }
87       }
88   }
89
90   if (save_cwd (&saved_cwd) != 0)
91     openat_save_fail (errno);
92   if (0 <= fd && fd == saved_cwd.desc)
93     {
94       /* If saving the working directory collides with the user's
95          requested fd, then the user's fd must have been closed to
96          begin with.  */
97       free_cwd (&saved_cwd);
98       errno = EBADF;
99       return -1;
100     }
101
102   if (fchdir (fd) != 0)
103     {
104       saved_errno = errno;
105       free_cwd (&saved_cwd);
106       errno = saved_errno;
107       return -1;
108     }
109
110   err = CALL_FUNC (file);
111   saved_errno = (err < 0 ? errno : 0);
112
113   if (restore_cwd (&saved_cwd) != 0)
114     openat_restore_fail (errno);
115
116   free_cwd (&saved_cwd);
117
118   if (saved_errno)
119     errno = saved_errno;
120   return err;
121 }
122 #undef CALL_FUNC
123 #undef FUNC_RESULT