Sync lttng-modules ABI version in internal kernel-ioctl.h
[lttng-tools.git] / src / common / compat / directory-handle.h
1 /*
2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #ifndef _COMPAT_DIRECTORY_HANDLE_H
19 #define _COMPAT_DIRECTORY_HANDLE_H
20
21 #include <common/macros.h>
22 #include <common/credentials.h>
23 #include <urcu/ref.h>
24
25 enum lttng_directory_handle_rmdir_recursive_flags {
26 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG = (1U << 0),
27 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG = (1U << 1),
28 };
29
30 /*
31 * Some platforms, such as Solaris 10, do not support directory file descriptors
32 * and their associated functions (*at(...)), which are defined in POSIX.2008.
33 *
34 * This wrapper provides a handle that is either a copy of a directory's path
35 * or a directory file descriptors, depending on the platform's capabilities.
36 */
37 #ifdef COMPAT_DIRFD
38 struct lttng_directory_handle {
39 struct urcu_ref ref;
40 int dirfd;
41 };
42
43 static inline
44 int lttng_directory_handle_get_dirfd(
45 const struct lttng_directory_handle *handle)
46 {
47 return handle->dirfd;
48 }
49
50 #else
51 struct lttng_directory_handle {
52 struct urcu_ref ref;
53 char *base_path;
54 };
55 #endif
56
57 /*
58 * Create a directory handle to the provided path. Passing a NULL path
59 * returns a handle to the current working directory.
60 *
61 * The reference to the directory handle must be released using
62 * lttng_directory_handle_put().
63 */
64 LTTNG_HIDDEN
65 struct lttng_directory_handle *lttng_directory_handle_create(
66 const char *path);
67
68 /*
69 * Create a new directory handle to a path relative to an existing handle.
70 *
71 * The provided path must already exist. Note that the creation of a
72 * subdirectory and the creation of a handle are kept as separate operations
73 * to highlight the fact that there is an inherent race between the creation of
74 * a directory and the creation of a handle to it.
75 *
76 * Passing a NULL path effectively copies the original handle.
77 *
78 * The reference to the directory handle must be released using
79 * lttng_directory_handle_put().
80 */
81 LTTNG_HIDDEN
82 struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
83 const char *path,
84 const struct lttng_directory_handle *ref_handle);
85
86 /*
87 * Create a new directory handle from an existing directory fd.
88 *
89 * The new directory handle assumes the ownership of the directory fd.
90 * Note that this method should only be used in very specific cases, such as
91 * re-creating a directory handle from a dirfd passed over a unix socket.
92 *
93 * The reference to the directory handle must be released using
94 * lttng_directory_handle_put().
95 */
96 LTTNG_HIDDEN
97 struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
98 int dirfd);
99
100 /*
101 * Copy a directory handle.
102 *
103 * The reference to the directory handle must be released using
104 * lttng_directory_handle_put().
105 */
106 LTTNG_HIDDEN
107 struct lttng_directory_handle *lttng_directory_handle_copy(
108 const struct lttng_directory_handle *handle);
109
110 /*
111 * Acquire a reference to a directory handle.
112 */
113 LTTNG_HIDDEN
114 bool lttng_directory_handle_get(struct lttng_directory_handle *handle);
115
116 /*
117 * Release a reference to a directory handle.
118 */
119 LTTNG_HIDDEN
120 void lttng_directory_handle_put(struct lttng_directory_handle *handle);
121
122 /*
123 * Create a subdirectory relative to a directory handle.
124 */
125 LTTNG_HIDDEN
126 int lttng_directory_handle_create_subdirectory(
127 const struct lttng_directory_handle *handle,
128 const char *subdirectory,
129 mode_t mode);
130
131 /*
132 * Create a subdirectory relative to a directory handle
133 * as a given user.
134 */
135 LTTNG_HIDDEN
136 int lttng_directory_handle_create_subdirectory_as_user(
137 const struct lttng_directory_handle *handle,
138 const char *subdirectory,
139 mode_t mode, const struct lttng_credentials *creds);
140
141 /*
142 * Recursively create a directory relative to a directory handle.
143 */
144 LTTNG_HIDDEN
145 int lttng_directory_handle_create_subdirectory_recursive(
146 const struct lttng_directory_handle *handle,
147 const char *subdirectory_path,
148 mode_t mode);
149
150 /*
151 * Recursively create a directory relative to a directory handle
152 * as a given user.
153 */
154 LTTNG_HIDDEN
155 int lttng_directory_handle_create_subdirectory_recursive_as_user(
156 const struct lttng_directory_handle *handle,
157 const char *subdirectory_path,
158 mode_t mode, const struct lttng_credentials *creds);
159
160 /*
161 * Open a file descriptor to a path relative to a directory handle.
162 */
163 LTTNG_HIDDEN
164 int lttng_directory_handle_open_file(
165 const struct lttng_directory_handle *handle,
166 const char *filename,
167 int flags, mode_t mode);
168
169 /*
170 * Open a file descriptor to a path relative to a directory handle
171 * as a given user.
172 */
173 LTTNG_HIDDEN
174 int lttng_directory_handle_open_file_as_user(
175 const struct lttng_directory_handle *handle,
176 const char *filename,
177 int flags, mode_t mode,
178 const struct lttng_credentials *creds);
179
180 /*
181 * Unlink a file to a path relative to a directory handle.
182 */
183 LTTNG_HIDDEN
184 int lttng_directory_handle_unlink_file(
185 const struct lttng_directory_handle *handle,
186 const char *filename);
187
188 /*
189 * Unlink a file to a path relative to a directory handle as a given user.
190 */
191 LTTNG_HIDDEN
192 int lttng_directory_handle_unlink_file_as_user(
193 const struct lttng_directory_handle *handle,
194 const char *filename,
195 const struct lttng_credentials *creds);
196
197 /*
198 * Rename a file from a path relative to a directory handle to a new
199 * name relative to another directory handle.
200 */
201 LTTNG_HIDDEN
202 int lttng_directory_handle_rename(
203 const struct lttng_directory_handle *old_handle,
204 const char *old_name,
205 const struct lttng_directory_handle *new_handle,
206 const char *new_name);
207
208 /*
209 * Rename a file from a path relative to a directory handle to a new
210 * name relative to another directory handle as a given user.
211 */
212 LTTNG_HIDDEN
213 int lttng_directory_handle_rename_as_user(
214 const struct lttng_directory_handle *old_handle,
215 const char *old_name,
216 const struct lttng_directory_handle *new_handle,
217 const char *new_name,
218 const struct lttng_credentials *creds);
219
220 /*
221 * Remove a subdirectory relative to a directory handle.
222 */
223 LTTNG_HIDDEN
224 int lttng_directory_handle_remove_subdirectory(
225 const struct lttng_directory_handle *handle,
226 const char *name);
227
228 /*
229 * Remove a subdirectory relative to a directory handle as a given user.
230 */
231 LTTNG_HIDDEN
232 int lttng_directory_handle_remove_subdirectory_as_user(
233 const struct lttng_directory_handle *handle,
234 const char *name,
235 const struct lttng_credentials *creds);
236
237 /*
238 * Remove a subdirectory and remove its contents if it only
239 * consists in empty directories.
240 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
241 */
242 LTTNG_HIDDEN
243 int lttng_directory_handle_remove_subdirectory_recursive(
244 const struct lttng_directory_handle *handle,
245 const char *name, int flags);
246
247 /*
248 * Remove a subdirectory and remove its contents if it only
249 * consists in empty directories as a given user.
250 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
251 */
252 LTTNG_HIDDEN
253 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
254 const struct lttng_directory_handle *handle,
255 const char *name,
256 const struct lttng_credentials *creds,
257 int flags);
258
259 #endif /* _COMPAT_PATH_HANDLE_H */
This page took 0.037553 seconds and 4 git commands to generate.