2 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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.
18 #include <common/compat/directory-handle.h>
19 #include <common/error.h>
20 #include <common/macros.h>
21 #include <common/runas.h>
22 #include <common/credentials.h>
23 #include <lttng/constant.h>
24 #include <common/dynamic-array.h>
27 #include <sys/types.h>
34 * This compatibility layer shares a common "base" that is implemented
35 * in terms of an internal API. This file contains two implementations
36 * of the internal API below.
39 int lttng_directory_handle_mkdir(
40 const struct lttng_directory_handle
*handle
,
41 const char *path
, mode_t mode
);
43 int _run_as_mkdir(const struct lttng_directory_handle
*handle
, const char *path
,
44 mode_t mode
, uid_t uid
, gid_t gid
);
46 int _run_as_mkdir_recursive(const struct lttng_directory_handle
*handle
,
47 const char *path
, mode_t mode
, uid_t uid
, gid_t gid
);
49 int lttng_directory_handle_open(const struct lttng_directory_handle
*handle
,
50 const char *filename
, int flags
, mode_t mode
);
52 int _run_as_open(const struct lttng_directory_handle
*handle
,
54 int flags
, mode_t mode
, uid_t uid
, gid_t gid
);
56 int lttng_directory_handle_unlink(
57 const struct lttng_directory_handle
*handle
,
58 const char *filename
);
60 int _run_as_unlink(const struct lttng_directory_handle
*handle
,
61 const char *filename
, uid_t uid
, gid_t gid
);
63 int _lttng_directory_handle_rename(
64 const struct lttng_directory_handle
*old_handle
,
66 const struct lttng_directory_handle
*new_handle
,
67 const char *new_name
);
69 int _run_as_rename(const struct lttng_directory_handle
*old_handle
,
71 const struct lttng_directory_handle
*new_handle
,
72 const char *new_name
, uid_t uid
, gid_t gid
);
74 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle
*handle
,
77 int lttng_directory_handle_rmdir(
78 const struct lttng_directory_handle
*handle
, const char *name
);
80 int _run_as_rmdir(const struct lttng_directory_handle
*handle
,
81 const char *name
, uid_t uid
, gid_t gid
);
83 int _run_as_rmdir_recursive(
84 const struct lttng_directory_handle
*handle
, const char *name
,
85 uid_t uid
, gid_t gid
, int flags
);
87 void lttng_directory_handle_invalidate(struct lttng_directory_handle
*handle
);
89 void lttng_directory_handle_release(struct urcu_ref
*ref
);
94 * Special inode number reserved to represent the "current working directory".
95 * ino_t is spec'ed as being an unsigned integral type.
97 #define RESERVED_AT_FDCWD_INO \
99 uint64_t reserved_val; \
100 switch (sizeof(ino_t)) { \
102 reserved_val = UINT32_MAX; \
105 reserved_val = UINT64_MAX; \
110 (ino_t) reserved_val; \
114 struct lttng_directory_handle
*lttng_directory_handle_create(const char *path
)
116 const struct lttng_directory_handle cwd_handle
= {
120 /* Open a handle to the CWD if NULL is passed. */
121 return lttng_directory_handle_create_from_handle(path
, &cwd_handle
);
125 struct lttng_directory_handle
*lttng_directory_handle_create_from_handle(
127 const struct lttng_directory_handle
*ref_handle
)
130 struct lttng_directory_handle
*handle
= NULL
;
133 handle
= lttng_directory_handle_copy(ref_handle
);
137 ERR("Failed to initialize directory handle: provided path is an empty string");
141 dirfd
= openat(ref_handle
->dirfd
, path
, O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
143 PERROR("Failed to initialize directory handle to \"%s\"", path
);
147 handle
= lttng_directory_handle_create_from_dirfd(dirfd
);
155 PERROR("Failed to close directory file descriptor");
161 struct lttng_directory_handle
*lttng_directory_handle_create_from_dirfd(
165 struct lttng_directory_handle
*handle
= zmalloc(sizeof(*handle
));
166 struct stat stat_buf
;
172 if (dirfd
!= AT_FDCWD
) {
173 ret
= fstat(dirfd
, &stat_buf
);
175 PERROR("Failed to fstat directory file descriptor %i", dirfd
);
176 lttng_directory_handle_release(&handle
->ref
);
179 handle
->directory_inode
= RESERVED_AT_FDCWD_INO
;
181 handle
->dirfd
= dirfd
;
182 urcu_ref_init(&handle
->ref
);
188 void lttng_directory_handle_release(struct urcu_ref
*ref
)
191 struct lttng_directory_handle
*handle
=
192 container_of(ref
, struct lttng_directory_handle
, ref
);
194 if (handle
->dirfd
== AT_FDCWD
|| handle
->dirfd
== -1) {
197 ret
= close(handle
->dirfd
);
199 PERROR("Failed to close directory file descriptor of directory handle");
202 lttng_directory_handle_invalidate(handle
);
207 struct lttng_directory_handle
*lttng_directory_handle_copy(
208 const struct lttng_directory_handle
*handle
)
210 struct lttng_directory_handle
*new_handle
= NULL
;
212 if (handle
->dirfd
== AT_FDCWD
) {
213 new_handle
= lttng_directory_handle_create_from_dirfd(AT_FDCWD
);
215 const int new_dirfd
= dup(handle
->dirfd
);
217 if (new_dirfd
== -1) {
218 PERROR("Failed to duplicate directory file descriptor of directory handle");
221 new_handle
= lttng_directory_handle_create_from_dirfd(
223 if (!new_handle
&& close(new_dirfd
)) {
224 PERROR("Failed to close directory file descriptor of directory handle");
232 bool lttng_directory_handle_equals(const struct lttng_directory_handle
*lhs
,
233 const struct lttng_directory_handle
*rhs
)
235 return lhs
->directory_inode
== rhs
->directory_inode
;
239 void lttng_directory_handle_invalidate(struct lttng_directory_handle
*handle
)
245 int lttng_directory_handle_stat(const struct lttng_directory_handle
*handle
,
246 const char *path
, struct stat
*st
)
248 return fstatat(handle
->dirfd
, path
, st
, 0);
252 bool lttng_directory_handle_uses_fd(
253 const struct lttng_directory_handle
*handle
)
255 return handle
->dirfd
!= AT_FDCWD
;
259 int lttng_directory_handle_mkdir(
260 const struct lttng_directory_handle
*handle
,
261 const char *path
, mode_t mode
)
263 return mkdirat(handle
->dirfd
, path
, mode
);
267 int lttng_directory_handle_open(const struct lttng_directory_handle
*handle
,
268 const char *filename
, int flags
, mode_t mode
)
270 return openat(handle
->dirfd
, filename
, flags
, mode
);
274 int _run_as_open(const struct lttng_directory_handle
*handle
,
275 const char *filename
,
276 int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
278 return run_as_openat(handle
->dirfd
, filename
, flags
, mode
, uid
, gid
);
282 int _run_as_unlink(const struct lttng_directory_handle
*handle
,
283 const char *filename
, uid_t uid
, gid_t gid
)
285 return run_as_unlinkat(handle
->dirfd
, filename
, uid
, gid
);
289 int lttng_directory_handle_unlink(
290 const struct lttng_directory_handle
*handle
,
291 const char *filename
)
293 return unlinkat(handle
->dirfd
, filename
, 0);
297 int _run_as_mkdir(const struct lttng_directory_handle
*handle
,
298 const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
300 return run_as_mkdirat(handle
->dirfd
, path
, mode
, uid
, gid
);
304 int _run_as_mkdir_recursive(const struct lttng_directory_handle
*handle
,
305 const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
307 return run_as_mkdirat_recursive(handle
->dirfd
, path
, mode
, uid
, gid
);
311 int _lttng_directory_handle_rename(
312 const struct lttng_directory_handle
*old_handle
,
313 const char *old_name
,
314 const struct lttng_directory_handle
*new_handle
,
315 const char *new_name
)
317 return renameat(old_handle
->dirfd
, old_name
,
318 new_handle
->dirfd
, new_name
);
322 int _run_as_rename(const struct lttng_directory_handle
*old_handle
,
323 const char *old_name
,
324 const struct lttng_directory_handle
*new_handle
,
325 const char *new_name
, uid_t uid
, gid_t gid
)
327 return run_as_renameat(old_handle
->dirfd
, old_name
, new_handle
->dirfd
,
332 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle
*handle
,
335 DIR *dir_stream
= NULL
;
336 int fd
= openat(handle
->dirfd
, path
, O_RDONLY
);
342 dir_stream
= fdopendir(fd
);
346 PERROR("Failed to open directory stream");
349 PERROR("Failed to close file descriptor to %s", path
);
359 int lttng_directory_handle_rmdir(
360 const struct lttng_directory_handle
*handle
, const char *name
)
362 return unlinkat(handle
->dirfd
, name
, AT_REMOVEDIR
);
366 int _run_as_rmdir(const struct lttng_directory_handle
*handle
,
367 const char *name
, uid_t uid
, gid_t gid
)
369 return run_as_rmdirat(handle
->dirfd
, name
, uid
, gid
);
373 int _run_as_rmdir_recursive(
374 const struct lttng_directory_handle
*handle
, const char *name
,
375 uid_t uid
, gid_t gid
, int flags
)
377 return run_as_rmdirat_recursive(handle
->dirfd
, name
, uid
, gid
, flags
);
380 #else /* COMPAT_DIRFD */
383 int get_full_path(const struct lttng_directory_handle
*handle
,
384 const char *subdirectory
, char *fullpath
, size_t size
)
387 const bool subdirectory_is_absolute
=
388 subdirectory
&& *subdirectory
== '/';
389 const char * const base
= subdirectory_is_absolute
?
390 subdirectory
: handle
->base_path
;
391 const char * const end
= subdirectory
&& !subdirectory_is_absolute
?
393 const size_t base_len
= strlen(base
);
394 const size_t end_len
= end
? strlen(end
) : 0;
395 const bool add_separator_slash
= end
&& base
[base_len
- 1] != '/';
396 const bool add_trailing_slash
= end
&& end
[end_len
- 1] != '/';
398 ret
= snprintf(fullpath
, size
, "%s%s%s%s",
400 add_separator_slash
? "/" : "",
402 add_trailing_slash
? "/" : "");
403 if (ret
== -1 || ret
>= size
) {
404 ERR("Failed to format subdirectory from directory handle");
414 struct lttng_directory_handle
*_lttng_directory_handle_create(char *path
)
416 struct lttng_directory_handle
*handle
= zmalloc(sizeof(*handle
));
421 urcu_ref_init(&handle
->ref
);
422 handle
->base_path
= path
;
428 struct lttng_directory_handle
*lttng_directory_handle_create(
432 const char *cwd
= "";
433 size_t cwd_len
, path_len
;
434 char cwd_buf
[LTTNG_PATH_MAX
] = {};
435 char handle_buf
[LTTNG_PATH_MAX
] = {};
436 struct lttng_directory_handle
*new_handle
= NULL
;
437 bool add_cwd_slash
= false, add_trailing_slash
= false;
438 const struct lttng_directory_handle cwd_handle
= {
439 .base_path
= handle_buf
,
442 path_len
= path
? strlen(path
) : 0;
443 add_trailing_slash
= path
&& path
[path_len
- 1] != '/';
444 if (!path
|| (path
&& *path
!= '/')) {
445 cwd
= getcwd(cwd_buf
, sizeof(cwd_buf
));
447 PERROR("Failed to initialize directory handle, can't get current working directory");
451 cwd_len
= strlen(cwd
);
453 ERR("Failed to initialize directory handle, current working directory path has a length of 0");
457 add_cwd_slash
= cwd
[cwd_len
- 1] != '/';
460 ret
= snprintf(handle_buf
, sizeof(handle_buf
), "%s%s%s%s",
462 add_cwd_slash
? "/" : "",
464 add_trailing_slash
? "/" : "");
465 if (ret
== -1 || ret
>= LTTNG_PATH_MAX
) {
466 ERR("Failed to initialize directory handle, failed to format directory path");
470 new_handle
= lttng_directory_handle_create_from_handle(path
, &cwd_handle
);
476 struct lttng_directory_handle
*lttng_directory_handle_create_from_handle(
478 const struct lttng_directory_handle
*ref_handle
)
481 size_t path_len
, handle_path_len
;
482 bool add_trailing_slash
;
483 struct stat stat_buf
;
484 struct lttng_directory_handle
*new_handle
= NULL
;
485 char *new_path
= NULL
;
487 assert(ref_handle
&& ref_handle
->base_path
);
489 ret
= lttng_directory_handle_stat(ref_handle
, path
, &stat_buf
);
491 PERROR("Failed to create directory handle");
493 } else if (!S_ISDIR(stat_buf
.st_mode
)) {
494 char full_path
[LTTNG_PATH_MAX
];
496 /* Best effort for logging purposes. */
497 ret
= get_full_path(ref_handle
, path
, full_path
,
503 ERR("Failed to initialize directory handle to \"%s\": not a directory",
508 new_handle
= lttng_directory_handle_copy(ref_handle
);
512 path_len
= strlen(path
);
514 ERR("Failed to initialize directory handle: provided path is an empty string");
519 new_path
= strdup(path
);
523 /* Takes ownership of new_path. */
524 new_handle
= _lttng_directory_handle_create(new_path
);
529 add_trailing_slash
= path
[path_len
- 1] != '/';
531 handle_path_len
= strlen(ref_handle
->base_path
) + path_len
+
532 !!add_trailing_slash
;
533 if (handle_path_len
>= LTTNG_PATH_MAX
) {
534 ERR("Failed to initialize directory handle as the resulting path's length (%zu bytes) exceeds the maximal allowed length (%d bytes)",
535 handle_path_len
, LTTNG_PATH_MAX
);
538 new_path
= zmalloc(handle_path_len
);
540 PERROR("Failed to initialize directory handle");
544 ret
= sprintf(new_handle
->base_path
, "%s%s%s",
545 ref_handle
->base_path
,
547 add_trailing_slash
? "/" : "");
548 if (ret
== -1 || ret
>= handle_path_len
) {
549 ERR("Failed to initialize directory handle: path formatting failed");
552 new_handle
= _lttng_directory_handle_create(new_path
);
560 struct lttng_directory_handle
*lttng_directory_handle_create_from_dirfd(
563 assert(dirfd
== AT_FDCWD
);
564 return lttng_directory_handle_create(NULL
);
568 void lttng_directory_handle_release(struct urcu_ref
*ref
)
570 struct lttng_directory_handle
*handle
=
571 container_of(ref
, struct lttng_directory_handle
, ref
);
573 free(handle
->base_path
);
574 lttng_directory_handle_invalidate(handle
);
579 struct lttng_directory_handle
*lttng_directory_handle_copy(
580 const struct lttng_directory_handle
*handle
)
582 struct lttng_directory_handle
*new_handle
= NULL
;
583 char *new_path
= NULL
;
585 if (handle
->base_path
) {
586 new_path
= strdup(handle
->base_path
);
591 new_handle
= _lttng_directory_handle_create(new_path
);
597 bool lttng_directory_handle_equals(const struct lttng_directory_handle
*lhs
,
598 const struct lttng_directory_handle
*rhs
)
600 return strcmp(lhs
->path
, rhs
->path
) == 0;
604 void lttng_directory_handle_invalidate(struct lttng_directory_handle
*handle
)
606 handle
->base_path
= NULL
;
610 int lttng_directory_handle_stat(const struct lttng_directory_handle
*handle
,
611 const char *subdirectory
, struct stat
*st
)
614 char fullpath
[LTTNG_PATH_MAX
];
616 ret
= get_full_path(handle
, subdirectory
, fullpath
, sizeof(fullpath
));
622 ret
= stat(fullpath
, st
);
628 bool lttng_directory_handle_uses_fd(
629 const struct lttng_directory_handle
*handle
)
635 int lttng_directory_handle_mkdir(const struct lttng_directory_handle
*handle
,
636 const char *subdirectory
, mode_t mode
)
639 char fullpath
[LTTNG_PATH_MAX
];
641 ret
= get_full_path(handle
, subdirectory
, fullpath
, sizeof(fullpath
));
647 ret
= mkdir(fullpath
, mode
);
653 int lttng_directory_handle_open(const struct lttng_directory_handle
*handle
,
654 const char *filename
, int flags
, mode_t mode
)
657 char fullpath
[LTTNG_PATH_MAX
];
659 ret
= get_full_path(handle
, filename
, fullpath
, sizeof(fullpath
));
665 ret
= open(fullpath
, flags
, mode
);
671 int lttng_directory_handle_unlink(
672 const struct lttng_directory_handle
*handle
,
673 const char *filename
)
676 char fullpath
[LTTNG_PATH_MAX
];
678 ret
= get_full_path(handle
, filename
, fullpath
, sizeof(fullpath
));
684 ret
= unlink(fullpath
);
690 int _run_as_mkdir(const struct lttng_directory_handle
*handle
, const char *path
,
691 mode_t mode
, uid_t uid
, gid_t gid
)
694 char fullpath
[LTTNG_PATH_MAX
];
696 ret
= get_full_path(handle
, path
, fullpath
, sizeof(fullpath
));
702 ret
= run_as_mkdir(fullpath
, mode
, uid
, gid
);
708 int _run_as_open(const struct lttng_directory_handle
*handle
,
709 const char *filename
,
710 int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
713 char fullpath
[LTTNG_PATH_MAX
];
715 ret
= get_full_path(handle
, filename
, fullpath
, sizeof(fullpath
));
721 ret
= run_as_open(fullpath
, flags
, mode
, uid
, gid
);
727 int _run_as_unlink(const struct lttng_directory_handle
*handle
,
728 const char *filename
, uid_t uid
, gid_t gid
)
731 char fullpath
[LTTNG_PATH_MAX
];
733 ret
= get_full_path(handle
, filename
, fullpath
, sizeof(fullpath
));
739 ret
= run_as_unlink(fullpath
, uid
, gid
);
745 int _run_as_mkdir_recursive(const struct lttng_directory_handle
*handle
,
746 const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
749 char fullpath
[LTTNG_PATH_MAX
];
751 ret
= get_full_path(handle
, path
, fullpath
, sizeof(fullpath
));
757 ret
= run_as_mkdir_recursive(fullpath
, mode
, uid
, gid
);
763 int _lttng_directory_handle_rename(
764 const struct lttng_directory_handle
*old_handle
,
765 const char *old_name
,
766 const struct lttng_directory_handle
*new_handle
,
767 const char *new_name
)
770 char old_fullpath
[LTTNG_PATH_MAX
];
771 char new_fullpath
[LTTNG_PATH_MAX
];
773 ret
= get_full_path(old_handle
, old_name
, old_fullpath
,
774 sizeof(old_fullpath
));
779 ret
= get_full_path(new_handle
, new_name
, new_fullpath
,
780 sizeof(new_fullpath
));
786 ret
= rename(old_fullpath
, new_fullpath
);
792 int _run_as_rename(const struct lttng_directory_handle
*old_handle
,
793 const char *old_name
,
794 const struct lttng_directory_handle
*new_handle
,
795 const char *new_name
, uid_t uid
, gid_t gid
)
798 char old_fullpath
[LTTNG_PATH_MAX
];
799 char new_fullpath
[LTTNG_PATH_MAX
];
801 ret
= get_full_path(old_handle
, old_name
, old_fullpath
,
802 sizeof(old_fullpath
));
807 ret
= get_full_path(new_handle
, new_name
, new_fullpath
,
808 sizeof(new_fullpath
));
814 ret
= run_as_rename(old_fullpath
, new_fullpath
, uid
, gid
);
820 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle
*handle
,
824 DIR *dir_stream
= NULL
;
825 char fullpath
[LTTNG_PATH_MAX
];
827 ret
= get_full_path(handle
, path
, fullpath
, sizeof(fullpath
));
833 dir_stream
= opendir(fullpath
);
839 int lttng_directory_handle_rmdir(
840 const struct lttng_directory_handle
*handle
, const char *name
)
843 char fullpath
[LTTNG_PATH_MAX
];
845 ret
= get_full_path(handle
, name
, fullpath
, sizeof(fullpath
));
851 ret
= rmdir(fullpath
);
857 int _run_as_rmdir(const struct lttng_directory_handle
*handle
,
858 const char *name
, uid_t uid
, gid_t gid
)
861 char fullpath
[LTTNG_PATH_MAX
];
863 ret
= get_full_path(handle
, name
, fullpath
, sizeof(fullpath
));
869 ret
= run_as_rmdir(fullpath
, uid
, gid
);
875 int _run_as_rmdir_recursive(
876 const struct lttng_directory_handle
*handle
, const char *name
,
877 uid_t uid
, gid_t gid
, int flags
)
880 char fullpath
[LTTNG_PATH_MAX
];
882 ret
= get_full_path(handle
, name
, fullpath
, sizeof(fullpath
));
888 ret
= run_as_rmdir_recursive(fullpath
, uid
, gid
, flags
);
893 #endif /* COMPAT_DIRFD */
895 /* Common implementation. */
898 * On some filesystems (e.g. nfs), mkdir will validate access rights before
899 * checking for the existence of the path element. This means that on a setup
900 * where "/home/" is a mounted NFS share, and running as an unpriviledged user,
901 * recursively creating a path of the form "/home/my_user/trace/" will fail with
902 * EACCES on mkdir("/home", ...).
904 * Checking the path for existence allows us to work around this behaviour.
907 int create_directory_check_exists(const struct lttng_directory_handle
*handle
,
908 const char *path
, mode_t mode
)
913 ret
= lttng_directory_handle_stat(handle
, path
, &st
);
915 if (S_ISDIR(st
.st_mode
)) {
916 /* Directory exists, skip. */
919 /* Exists, but is not a directory. */
924 } else if (errno
!= ENOENT
) {
929 * Let mkdir handle other errors as the caller expects mkdir
932 ret
= lttng_directory_handle_mkdir(handle
, path
, mode
);
938 int create_directory_recursive(const struct lttng_directory_handle
*handle
,
939 const char *path
, mode_t mode
)
941 char *p
, tmp
[LTTNG_PATH_MAX
];
947 ret
= lttng_strncpy(tmp
, path
, sizeof(tmp
));
949 ERR("Failed to create directory: provided path's length (%zu bytes) exceeds the maximal allowed length (%zu bytes)",
950 strlen(path
) + 1, sizeof(tmp
));
955 if (tmp
[len
- 1] == '/') {
959 for (p
= tmp
+ 1; *p
; p
++) {
962 if (tmp
[strlen(tmp
) - 1] == '.' &&
963 tmp
[strlen(tmp
) - 2] == '.' &&
964 tmp
[strlen(tmp
) - 3] == '/') {
965 ERR("Using '/../' is not permitted in the trace path (%s)",
970 ret
= create_directory_check_exists(handle
, tmp
, mode
);
972 if (errno
!= EACCES
) {
973 PERROR("Failed to create directory \"%s\"",
983 ret
= create_directory_check_exists(handle
, tmp
, mode
);
985 PERROR("mkdirat recursive last element");
993 bool lttng_directory_handle_get(struct lttng_directory_handle
*handle
)
995 return urcu_ref_get_unless_zero(&handle
->ref
);
999 void lttng_directory_handle_put(struct lttng_directory_handle
*handle
)
1004 assert(handle
->ref
.refcount
);
1005 urcu_ref_put(&handle
->ref
, lttng_directory_handle_release
);
1009 int lttng_directory_handle_create_subdirectory_as_user(
1010 const struct lttng_directory_handle
*handle
,
1011 const char *subdirectory
,
1012 mode_t mode
, const struct lttng_credentials
*creds
)
1017 /* Run as current user. */
1018 ret
= create_directory_check_exists(handle
,
1019 subdirectory
, mode
);
1021 ret
= _run_as_mkdir(handle
, subdirectory
,
1022 mode
, creds
->uid
, creds
->gid
);
1029 int lttng_directory_handle_create_subdirectory_recursive_as_user(
1030 const struct lttng_directory_handle
*handle
,
1031 const char *subdirectory_path
,
1032 mode_t mode
, const struct lttng_credentials
*creds
)
1037 /* Run as current user. */
1038 ret
= create_directory_recursive(handle
,
1039 subdirectory_path
, mode
);
1041 ret
= _run_as_mkdir_recursive(handle
, subdirectory_path
,
1042 mode
, creds
->uid
, creds
->gid
);
1049 int lttng_directory_handle_create_subdirectory(
1050 const struct lttng_directory_handle
*handle
,
1051 const char *subdirectory
,
1054 return lttng_directory_handle_create_subdirectory_as_user(
1055 handle
, subdirectory
, mode
, NULL
);
1059 int lttng_directory_handle_create_subdirectory_recursive(
1060 const struct lttng_directory_handle
*handle
,
1061 const char *subdirectory_path
,
1064 return lttng_directory_handle_create_subdirectory_recursive_as_user(
1065 handle
, subdirectory_path
, mode
, NULL
);
1069 int lttng_directory_handle_open_file_as_user(
1070 const struct lttng_directory_handle
*handle
,
1071 const char *filename
,
1072 int flags
, mode_t mode
,
1073 const struct lttng_credentials
*creds
)
1078 /* Run as current user. */
1079 ret
= lttng_directory_handle_open(handle
, filename
, flags
,
1082 ret
= _run_as_open(handle
, filename
, flags
, mode
,
1083 creds
->uid
, creds
->gid
);
1089 int lttng_directory_handle_open_file(
1090 const struct lttng_directory_handle
*handle
,
1091 const char *filename
,
1092 int flags
, mode_t mode
)
1094 return lttng_directory_handle_open_file_as_user(handle
, filename
, flags
,
1099 int lttng_directory_handle_unlink_file_as_user(
1100 const struct lttng_directory_handle
*handle
,
1101 const char *filename
,
1102 const struct lttng_credentials
*creds
)
1107 /* Run as current user. */
1108 ret
= lttng_directory_handle_unlink(handle
, filename
);
1110 ret
= _run_as_unlink(handle
, filename
, creds
->uid
, creds
->gid
);
1116 int lttng_directory_handle_unlink_file(
1117 const struct lttng_directory_handle
*handle
,
1118 const char *filename
)
1120 return lttng_directory_handle_unlink_file_as_user(handle
,
1125 int lttng_directory_handle_rename(
1126 const struct lttng_directory_handle
*old_handle
,
1127 const char *old_name
,
1128 const struct lttng_directory_handle
*new_handle
,
1129 const char *new_name
)
1131 return lttng_directory_handle_rename_as_user(old_handle
, old_name
,
1132 new_handle
, new_name
, NULL
);
1136 int lttng_directory_handle_rename_as_user(
1137 const struct lttng_directory_handle
*old_handle
,
1138 const char *old_name
,
1139 const struct lttng_directory_handle
*new_handle
,
1140 const char *new_name
,
1141 const struct lttng_credentials
*creds
)
1146 /* Run as current user. */
1147 ret
= _lttng_directory_handle_rename(old_handle
,
1148 old_name
, new_handle
, new_name
);
1150 ret
= _run_as_rename(old_handle
, old_name
, new_handle
,
1151 new_name
, creds
->uid
, creds
->gid
);
1157 int lttng_directory_handle_remove_subdirectory(
1158 const struct lttng_directory_handle
*handle
,
1161 return lttng_directory_handle_remove_subdirectory_as_user(handle
, name
,
1166 int lttng_directory_handle_remove_subdirectory_as_user(
1167 const struct lttng_directory_handle
*handle
,
1169 const struct lttng_credentials
*creds
)
1174 /* Run as current user. */
1175 ret
= lttng_directory_handle_rmdir(handle
, name
);
1177 ret
= _run_as_rmdir(handle
, name
, creds
->uid
, creds
->gid
);
1182 struct rmdir_frame
{
1183 ssize_t parent_frame_idx
;
1186 /* Size including '\0'. */
1191 void rmdir_frame_fini(void *data
)
1194 struct rmdir_frame
*frame
= data
;
1196 ret
= closedir(frame
->dir
);
1198 PERROR("Failed to close directory stream");
1203 int remove_directory_recursive(const struct lttng_directory_handle
*handle
,
1204 const char *path
, int flags
)
1207 struct lttng_dynamic_array frames
;
1208 size_t current_frame_idx
= 0;
1209 struct rmdir_frame initial_frame
= {
1210 .parent_frame_idx
= -1,
1211 .dir
= lttng_directory_handle_opendir(handle
, path
),
1213 .path_size
= strlen(path
) + 1,
1215 struct lttng_dynamic_buffer current_path
;
1216 const char separator
= '/';
1218 lttng_dynamic_buffer_init(¤t_path
);
1219 lttng_dynamic_array_init(&frames
, sizeof(struct rmdir_frame
),
1222 if (flags
& ~(LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
|
1223 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG
)) {
1224 ERR("Unknown flags %d", flags
);
1229 if (!initial_frame
.dir
) {
1230 if (flags
& LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
&&
1232 DBG("Cannot rmdir \"%s\": root does not exist", path
);
1236 PERROR("Failed to rmdir \"%s\"", path
);
1242 ret
= lttng_dynamic_array_add_element(&frames
, &initial_frame
);
1244 ERR("Failed to push context frame during recursive directory removal");
1245 rmdir_frame_fini(&initial_frame
);
1249 ret
= lttng_dynamic_buffer_append(
1250 ¤t_path
, path
, initial_frame
.path_size
);
1252 ERR("Failed to set initial path during recursive directory removal");
1257 while (lttng_dynamic_array_get_count(&frames
) > 0) {
1258 struct dirent
*entry
;
1259 struct rmdir_frame
*current_frame
=
1260 lttng_dynamic_array_get_element(
1261 &frames
, current_frame_idx
);
1263 assert(current_frame
->dir
);
1264 ret
= lttng_dynamic_buffer_set_size(
1265 ¤t_path
, current_frame
->path_size
);
1267 current_path
.data
[current_path
.size
- 1] = '\0';
1269 while ((entry
= readdir(current_frame
->dir
))) {
1272 if (!strcmp(entry
->d_name
, ".") ||
1273 !strcmp(entry
->d_name
, "..")) {
1277 /* Set current_path to the entry's path. */
1278 ret
= lttng_dynamic_buffer_set_size(
1279 ¤t_path
, current_path
.size
- 1);
1281 ret
= lttng_dynamic_buffer_append(¤t_path
,
1282 &separator
, sizeof(separator
));
1286 ret
= lttng_dynamic_buffer_append(¤t_path
,
1288 strlen(entry
->d_name
) + 1);
1293 if (lttng_directory_handle_stat(
1294 handle
, current_path
.data
, &st
)) {
1295 if ((flags
& LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
) &&
1299 PERROR("Failed to stat \"%s\"",
1305 if (!S_ISDIR(st
.st_mode
)) {
1306 if (flags
& LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
) {
1307 current_frame
->empty
= false;
1310 /* Not empty, abort. */
1311 DBG("Directory \"%s\" is not empty; refusing to remove directory",
1317 struct rmdir_frame new_frame
= {
1318 .path_size
= current_path
.size
,
1319 .dir
= lttng_directory_handle_opendir(
1323 .parent_frame_idx
= current_frame_idx
,
1326 if (!new_frame
.dir
) {
1327 if (flags
& LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
&&
1329 DBG("Non-existing directory stream during recursive directory removal");
1332 PERROR("Failed to open directory stream during recursive directory removal");
1337 ret
= lttng_dynamic_array_add_element(
1338 &frames
, &new_frame
);
1340 ERR("Failed to push context frame during recursive directory removal");
1341 rmdir_frame_fini(&new_frame
);
1344 current_frame_idx
++;
1345 /* We break iteration on readdir. */
1353 /* Pop rmdir frame. */
1354 if (current_frame
->empty
) {
1355 ret
= lttng_directory_handle_rmdir(
1356 handle
, current_path
.data
);
1358 if ((flags
& LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG
) ||
1360 PERROR("Failed to remove \"%s\" during recursive directory removal",
1364 DBG("Non-existing directory stream during recursive directory removal");
1366 } else if (current_frame
->parent_frame_idx
>= 0) {
1367 struct rmdir_frame
*parent_frame
;
1369 parent_frame
= lttng_dynamic_array_get_element(&frames
,
1370 current_frame
->parent_frame_idx
);
1371 assert(parent_frame
);
1372 parent_frame
->empty
= false;
1374 ret
= lttng_dynamic_array_remove_element(
1375 &frames
, current_frame_idx
);
1377 ERR("Failed to pop context frame during recursive directory removal");
1380 current_frame_idx
--;
1383 lttng_dynamic_array_reset(&frames
);
1384 lttng_dynamic_buffer_reset(¤t_path
);
1389 int lttng_directory_handle_remove_subdirectory_recursive(
1390 const struct lttng_directory_handle
*handle
,
1394 return lttng_directory_handle_remove_subdirectory_recursive_as_user(
1395 handle
, name
, NULL
, flags
);
1399 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
1400 const struct lttng_directory_handle
*handle
,
1402 const struct lttng_credentials
*creds
,
1408 /* Run as current user. */
1409 ret
= remove_directory_recursive(handle
, name
, flags
);
1411 ret
= _run_as_rmdir_recursive(handle
, name
, creds
->uid
,