Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / compat / directory-handle.c
1 /*
2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/compat/directory-handle.h>
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <common/runas.h>
12 #include <common/credentials.h>
13 #include <lttng/constant.h>
14 #include <common/dynamic-array.h>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <dirent.h>
21
22 /*
23 * This compatibility layer shares a common "base" that is implemented
24 * in terms of an internal API. This file contains two implementations
25 * of the internal API below.
26 */
27 static
28 int lttng_directory_handle_mkdir(
29 const struct lttng_directory_handle *handle,
30 const char *path, mode_t mode);
31 static
32 int _run_as_mkdir(const struct lttng_directory_handle *handle, const char *path,
33 mode_t mode, uid_t uid, gid_t gid);
34 static
35 int _run_as_mkdir_recursive(const struct lttng_directory_handle *handle,
36 const char *path, mode_t mode, uid_t uid, gid_t gid);
37 static
38 int lttng_directory_handle_open(const struct lttng_directory_handle *handle,
39 const char *filename, int flags, mode_t mode);
40 static
41 int _run_as_open(const struct lttng_directory_handle *handle,
42 const char *filename,
43 int flags, mode_t mode, uid_t uid, gid_t gid);
44 static
45 int lttng_directory_handle_unlink(
46 const struct lttng_directory_handle *handle,
47 const char *filename);
48 static
49 int _run_as_unlink(const struct lttng_directory_handle *handle,
50 const char *filename, uid_t uid, gid_t gid);
51 static
52 int _lttng_directory_handle_rename(
53 const struct lttng_directory_handle *old_handle,
54 const char *old_name,
55 const struct lttng_directory_handle *new_handle,
56 const char *new_name);
57 static
58 int _run_as_rename(const struct lttng_directory_handle *old_handle,
59 const char *old_name,
60 const struct lttng_directory_handle *new_handle,
61 const char *new_name, uid_t uid, gid_t gid);
62 static
63 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle *handle,
64 const char *path);
65 static
66 int lttng_directory_handle_rmdir(
67 const struct lttng_directory_handle *handle, const char *name);
68 static
69 int _run_as_rmdir(const struct lttng_directory_handle *handle,
70 const char *name, uid_t uid, gid_t gid);
71 static
72 int _run_as_rmdir_recursive(
73 const struct lttng_directory_handle *handle, const char *name,
74 uid_t uid, gid_t gid, int flags);
75 static
76 void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle);
77 static
78 void lttng_directory_handle_release(struct urcu_ref *ref);
79
80 #ifdef HAVE_DIRFD
81
82 /*
83 * Special inode number reserved to represent the "current working directory".
84 * ino_t is spec'ed as being an unsigned integral type.
85 */
86 #define RESERVED_AT_FDCWD_INO \
87 ({ \
88 uint64_t reserved_val; \
89 switch (sizeof(ino_t)) { \
90 case 4: \
91 reserved_val = UINT32_MAX; \
92 break; \
93 case 8: \
94 reserved_val = UINT64_MAX; \
95 break; \
96 default: \
97 abort(); \
98 } \
99 (ino_t) reserved_val; \
100 })
101
102 LTTNG_HIDDEN
103 struct lttng_directory_handle *lttng_directory_handle_create(const char *path)
104 {
105 const struct lttng_directory_handle cwd_handle = {
106 .dirfd = AT_FDCWD,
107 };
108
109 /* Open a handle to the CWD if NULL is passed. */
110 return lttng_directory_handle_create_from_handle(path, &cwd_handle);
111 }
112
113 LTTNG_HIDDEN
114 struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
115 const char *path,
116 const struct lttng_directory_handle *ref_handle)
117 {
118 int dirfd;
119 struct lttng_directory_handle *handle = NULL;
120
121 if (!path) {
122 handle = lttng_directory_handle_copy(ref_handle);
123 goto end;
124 }
125 if (!*path) {
126 ERR("Failed to initialize directory handle: provided path is an empty string");
127 goto end;
128 }
129
130 dirfd = openat(ref_handle->dirfd, path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
131 if (dirfd == -1) {
132 PERROR("Failed to initialize directory handle to \"%s\"", path);
133 goto end;
134 }
135
136 handle = lttng_directory_handle_create_from_dirfd(dirfd);
137 if (!handle) {
138 goto error_close;
139 }
140 end:
141 return handle;
142 error_close:
143 if (close(dirfd)) {
144 PERROR("Failed to close directory file descriptor");
145 }
146 return NULL;
147 }
148
149 LTTNG_HIDDEN
150 struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
151 int dirfd)
152 {
153 int ret;
154 struct lttng_directory_handle *handle = zmalloc(sizeof(*handle));
155 struct stat stat_buf;
156
157 if (!handle) {
158 goto end;
159 }
160
161 if (dirfd != AT_FDCWD) {
162 ret = fstat(dirfd, &stat_buf);
163 if (ret) {
164 PERROR("Failed to fstat directory file descriptor %i", dirfd);
165 lttng_directory_handle_release(&handle->ref);
166 handle = NULL;
167 goto end;
168 }
169 } else {
170 handle->directory_inode = RESERVED_AT_FDCWD_INO;
171 }
172 handle->dirfd = dirfd;
173 urcu_ref_init(&handle->ref);
174 end:
175 return handle;
176 }
177
178 static
179 void lttng_directory_handle_release(struct urcu_ref *ref)
180 {
181 int ret;
182 struct lttng_directory_handle *handle =
183 container_of(ref, struct lttng_directory_handle, ref);
184
185 if (handle->destroy_cb) {
186 handle->destroy_cb(handle, handle->destroy_cb_data);
187 }
188
189 if (handle->dirfd == AT_FDCWD || handle->dirfd == -1) {
190 goto end;
191 }
192 ret = close(handle->dirfd);
193 if (ret == -1) {
194 PERROR("Failed to close directory file descriptor of directory handle");
195 }
196 end:
197 lttng_directory_handle_invalidate(handle);
198 free(handle);
199 }
200
201 LTTNG_HIDDEN
202 struct lttng_directory_handle *lttng_directory_handle_copy(
203 const struct lttng_directory_handle *handle)
204 {
205 struct lttng_directory_handle *new_handle = NULL;
206
207 if (handle->dirfd == AT_FDCWD) {
208 new_handle = lttng_directory_handle_create_from_dirfd(AT_FDCWD);
209 } else {
210 const int new_dirfd = dup(handle->dirfd);
211
212 if (new_dirfd == -1) {
213 PERROR("Failed to duplicate directory file descriptor of directory handle");
214 goto end;
215 }
216 new_handle = lttng_directory_handle_create_from_dirfd(
217 new_dirfd);
218 if (!new_handle && close(new_dirfd)) {
219 PERROR("Failed to close directory file descriptor of directory handle");
220 }
221 }
222 end:
223 return new_handle;
224 }
225
226 LTTNG_HIDDEN
227 bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
228 const struct lttng_directory_handle *rhs)
229 {
230 return lhs->directory_inode == rhs->directory_inode;
231 }
232
233 static
234 void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle)
235 {
236 handle->dirfd = -1;
237 }
238
239 LTTNG_HIDDEN
240 int lttng_directory_handle_stat(const struct lttng_directory_handle *handle,
241 const char *path, struct stat *st)
242 {
243 return fstatat(handle->dirfd, path, st, 0);
244 }
245
246 LTTNG_HIDDEN
247 bool lttng_directory_handle_uses_fd(
248 const struct lttng_directory_handle *handle)
249 {
250 return handle->dirfd != AT_FDCWD;
251 }
252
253 static
254 int lttng_directory_handle_mkdir(
255 const struct lttng_directory_handle *handle,
256 const char *path, mode_t mode)
257 {
258 return mkdirat(handle->dirfd, path, mode);
259 }
260
261 static
262 int lttng_directory_handle_open(const struct lttng_directory_handle *handle,
263 const char *filename, int flags, mode_t mode)
264 {
265 return openat(handle->dirfd, filename, flags, mode);
266 }
267
268 static
269 int _run_as_open(const struct lttng_directory_handle *handle,
270 const char *filename,
271 int flags, mode_t mode, uid_t uid, gid_t gid)
272 {
273 return run_as_openat(handle->dirfd, filename, flags, mode, uid, gid);
274 }
275
276 static
277 int _run_as_unlink(const struct lttng_directory_handle *handle,
278 const char *filename, uid_t uid, gid_t gid)
279 {
280 return run_as_unlinkat(handle->dirfd, filename, uid, gid);
281 }
282
283 static
284 int lttng_directory_handle_unlink(
285 const struct lttng_directory_handle *handle,
286 const char *filename)
287 {
288 return unlinkat(handle->dirfd, filename, 0);
289 }
290
291 static
292 int _run_as_mkdir(const struct lttng_directory_handle *handle,
293 const char *path, mode_t mode, uid_t uid, gid_t gid)
294 {
295 return run_as_mkdirat(handle->dirfd, path, mode, uid, gid);
296 }
297
298 static
299 int _run_as_mkdir_recursive(const struct lttng_directory_handle *handle,
300 const char *path, mode_t mode, uid_t uid, gid_t gid)
301 {
302 return run_as_mkdirat_recursive(handle->dirfd, path, mode, uid, gid);
303 }
304
305 static
306 int _lttng_directory_handle_rename(
307 const struct lttng_directory_handle *old_handle,
308 const char *old_name,
309 const struct lttng_directory_handle *new_handle,
310 const char *new_name)
311 {
312 return renameat(old_handle->dirfd, old_name,
313 new_handle->dirfd, new_name);
314 }
315
316 static
317 int _run_as_rename(const struct lttng_directory_handle *old_handle,
318 const char *old_name,
319 const struct lttng_directory_handle *new_handle,
320 const char *new_name, uid_t uid, gid_t gid)
321 {
322 return run_as_renameat(old_handle->dirfd, old_name, new_handle->dirfd,
323 new_name, uid, gid);
324 }
325
326 static
327 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle *handle,
328 const char *path)
329 {
330 DIR *dir_stream = NULL;
331 int fd = openat(handle->dirfd, path, O_RDONLY);
332
333 if (fd < 0) {
334 goto end;
335 }
336
337 dir_stream = fdopendir(fd);
338 if (!dir_stream) {
339 int ret;
340
341 PERROR("Failed to open directory stream");
342 ret = close(fd);
343 if (ret) {
344 PERROR("Failed to close file descriptor to %s", path);
345 }
346 goto end;
347 }
348
349 end:
350 return dir_stream;
351 }
352
353 static
354 int lttng_directory_handle_rmdir(
355 const struct lttng_directory_handle *handle, const char *name)
356 {
357 int ret = unlinkat(handle->dirfd, name, AT_REMOVEDIR);
358 if (ret) {
359 PERROR("Failed to remove directory `%s`", name);
360 }
361
362 return ret;
363 }
364
365 static
366 int _run_as_rmdir(const struct lttng_directory_handle *handle,
367 const char *name, uid_t uid, gid_t gid)
368 {
369 return run_as_rmdirat(handle->dirfd, name, uid, gid);
370 }
371
372 static
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)
376 {
377 return run_as_rmdirat_recursive(handle->dirfd, name, uid, gid, flags);
378 }
379
380 #else /* HAVE_DIRFD */
381
382 static
383 int get_full_path(const struct lttng_directory_handle *handle,
384 const char *subdirectory, char *fullpath, size_t size)
385 {
386 int ret;
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 ?
392 subdirectory : NULL;
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] != '/';
397
398 ret = snprintf(fullpath, size, "%s%s%s%s",
399 base,
400 add_separator_slash ? "/" : "",
401 end ? end : "",
402 add_trailing_slash ? "/" : "");
403 if (ret == -1 || ret >= size) {
404 ERR("Failed to format subdirectory from directory handle");
405 ret = -1;
406 goto end;
407 }
408 ret = 0;
409 end:
410 return ret;
411 }
412
413 static
414 struct lttng_directory_handle *_lttng_directory_handle_create(char *path)
415 {
416 struct lttng_directory_handle *handle = zmalloc(sizeof(*handle));
417
418 if (!handle) {
419 goto end;
420 }
421 urcu_ref_init(&handle->ref);
422 handle->base_path = path;
423 end:
424 return handle;
425 }
426
427 LTTNG_HIDDEN
428 struct lttng_directory_handle *lttng_directory_handle_create(
429 const char *path)
430 {
431 int ret;
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,
440 };
441
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));
446 if (!cwd) {
447 PERROR("Failed to initialize directory handle, can't get current working directory");
448 ret = -1;
449 goto end;
450 }
451 cwd_len = strlen(cwd);
452 if (cwd_len == 0) {
453 ERR("Failed to initialize directory handle, current working directory path has a length of 0");
454 ret = -1;
455 goto end;
456 }
457 add_cwd_slash = cwd[cwd_len - 1] != '/';
458 }
459
460 ret = snprintf(handle_buf, sizeof(handle_buf), "%s%s%s%s",
461 cwd,
462 add_cwd_slash ? "/" : "",
463 path ? : "",
464 add_trailing_slash ? "/" : "");
465 if (ret == -1 || ret >= LTTNG_PATH_MAX) {
466 ERR("Failed to initialize directory handle, failed to format directory path");
467 goto end;
468 }
469
470 new_handle = lttng_directory_handle_create_from_handle(path, &cwd_handle);
471 end:
472 return new_handle;
473 }
474
475 LTTNG_HIDDEN
476 struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
477 const char *path,
478 const struct lttng_directory_handle *ref_handle)
479 {
480 int ret;
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;
486
487 LTTNG_ASSERT(ref_handle && ref_handle->base_path);
488
489 ret = lttng_directory_handle_stat(ref_handle, path, &stat_buf);
490 if (ret == -1) {
491 PERROR("Failed to create directory handle");
492 goto end;
493 } else if (!S_ISDIR(stat_buf.st_mode)) {
494 char full_path[LTTNG_PATH_MAX];
495
496 /* Best effort for logging purposes. */
497 ret = get_full_path(ref_handle, path, full_path,
498 sizeof(full_path));
499 if (ret) {
500 full_path[0] = '\0';
501 }
502
503 ERR("Failed to initialize directory handle to \"%s\": not a directory",
504 full_path);
505 goto end;
506 }
507 if (!path) {
508 new_handle = lttng_directory_handle_copy(ref_handle);
509 goto end;
510 }
511
512 path_len = strlen(path);
513 if (path_len == 0) {
514 ERR("Failed to initialize directory handle: provided path is an empty string");
515 ret = -1;
516 goto end;
517 }
518 if (*path == '/') {
519 new_path = strdup(path);
520 if (!new_path) {
521 goto end;
522 }
523 /* Takes ownership of new_path. */
524 new_handle = _lttng_directory_handle_create(new_path);
525 new_path = NULL;
526 goto end;
527 }
528
529 add_trailing_slash = path[path_len - 1] != '/';
530
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);
536 goto end;
537 }
538 new_path = zmalloc(handle_path_len);
539 if (!new_path) {
540 PERROR("Failed to initialize directory handle");
541 goto end;
542 }
543
544 ret = sprintf(new_handle->base_path, "%s%s%s",
545 ref_handle->base_path,
546 path,
547 add_trailing_slash ? "/" : "");
548 if (ret == -1 || ret >= handle_path_len) {
549 ERR("Failed to initialize directory handle: path formatting failed");
550 goto end;
551 }
552 new_handle = _lttng_directory_handle_create(new_path);
553 new_path = NULL;
554 end:
555 free(new_path);
556 return new_handle;
557 }
558
559 LTTNG_HIDDEN
560 struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
561 int dirfd)
562 {
563 LTTNG_ASSERT(dirfd == AT_FDCWD);
564 return lttng_directory_handle_create(NULL);
565 }
566
567 static
568 void lttng_directory_handle_release(struct urcu_ref *ref)
569 {
570 struct lttng_directory_handle *handle =
571 container_of(ref, struct lttng_directory_handle, ref);
572
573 free(handle->base_path);
574 lttng_directory_handle_invalidate(handle);
575 free(handle);
576 }
577
578 LTTNG_HIDDEN
579 struct lttng_directory_handle *lttng_directory_handle_copy(
580 const struct lttng_directory_handle *handle)
581 {
582 struct lttng_directory_handle *new_handle = NULL;
583 char *new_path = NULL;
584
585 if (handle->base_path) {
586 new_path = strdup(handle->base_path);
587 if (!new_path) {
588 goto end;
589 }
590 }
591 new_handle = _lttng_directory_handle_create(new_path);
592 end:
593 return new_handle;
594 }
595
596 LTTNG_HIDDEN
597 bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
598 const struct lttng_directory_handle *rhs)
599 {
600 return strcmp(lhs->base_path, rhs->base_path) == 0;
601 }
602
603 static
604 void lttng_directory_handle_invalidate(struct lttng_directory_handle *handle)
605 {
606 handle->base_path = NULL;
607 }
608
609 LTTNG_HIDDEN
610 int lttng_directory_handle_stat(const struct lttng_directory_handle *handle,
611 const char *subdirectory, struct stat *st)
612 {
613 int ret;
614 char fullpath[LTTNG_PATH_MAX];
615
616 ret = get_full_path(handle, subdirectory, fullpath, sizeof(fullpath));
617 if (ret) {
618 errno = ENOMEM;
619 goto end;
620 }
621
622 ret = stat(fullpath, st);
623 end:
624 return ret;
625 }
626
627 LTTNG_HIDDEN
628 bool lttng_directory_handle_uses_fd(
629 const struct lttng_directory_handle *handle)
630 {
631 return false;
632 }
633
634 static
635 int lttng_directory_handle_mkdir(const struct lttng_directory_handle *handle,
636 const char *subdirectory, mode_t mode)
637 {
638 int ret;
639 char fullpath[LTTNG_PATH_MAX];
640
641 ret = get_full_path(handle, subdirectory, fullpath, sizeof(fullpath));
642 if (ret) {
643 errno = ENOMEM;
644 goto end;
645 }
646
647 ret = mkdir(fullpath, mode);
648 end:
649 return ret;
650 }
651
652 static
653 int lttng_directory_handle_open(const struct lttng_directory_handle *handle,
654 const char *filename, int flags, mode_t mode)
655 {
656 int ret;
657 char fullpath[LTTNG_PATH_MAX];
658
659 ret = get_full_path(handle, filename, fullpath, sizeof(fullpath));
660 if (ret) {
661 errno = ENOMEM;
662 goto end;
663 }
664
665 ret = open(fullpath, flags, mode);
666 end:
667 return ret;
668 }
669
670 static
671 int lttng_directory_handle_unlink(
672 const struct lttng_directory_handle *handle,
673 const char *filename)
674 {
675 int ret;
676 char fullpath[LTTNG_PATH_MAX];
677
678 ret = get_full_path(handle, filename, fullpath, sizeof(fullpath));
679 if (ret) {
680 errno = ENOMEM;
681 goto end;
682 }
683
684 ret = unlink(fullpath);
685 end:
686 return ret;
687 }
688
689 static
690 int _run_as_mkdir(const struct lttng_directory_handle *handle, const char *path,
691 mode_t mode, uid_t uid, gid_t gid)
692 {
693 int ret;
694 char fullpath[LTTNG_PATH_MAX];
695
696 ret = get_full_path(handle, path, fullpath, sizeof(fullpath));
697 if (ret) {
698 errno = ENOMEM;
699 goto end;
700 }
701
702 ret = run_as_mkdir(fullpath, mode, uid, gid);
703 end:
704 return ret;
705 }
706
707 static
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)
711 {
712 int ret;
713 char fullpath[LTTNG_PATH_MAX];
714
715 ret = get_full_path(handle, filename, fullpath, sizeof(fullpath));
716 if (ret) {
717 errno = ENOMEM;
718 goto end;
719 }
720
721 ret = run_as_open(fullpath, flags, mode, uid, gid);
722 end:
723 return ret;
724 }
725
726 static
727 int _run_as_unlink(const struct lttng_directory_handle *handle,
728 const char *filename, uid_t uid, gid_t gid)
729 {
730 int ret;
731 char fullpath[LTTNG_PATH_MAX];
732
733 ret = get_full_path(handle, filename, fullpath, sizeof(fullpath));
734 if (ret) {
735 errno = ENOMEM;
736 goto end;
737 }
738
739 ret = run_as_unlink(fullpath, uid, gid);
740 end:
741 return ret;
742 }
743
744 static
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)
747 {
748 int ret;
749 char fullpath[LTTNG_PATH_MAX];
750
751 ret = get_full_path(handle, path, fullpath, sizeof(fullpath));
752 if (ret) {
753 errno = ENOMEM;
754 goto end;
755 }
756
757 ret = run_as_mkdir_recursive(fullpath, mode, uid, gid);
758 end:
759 return ret;
760 }
761
762 static
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)
768 {
769 int ret;
770 char old_fullpath[LTTNG_PATH_MAX];
771 char new_fullpath[LTTNG_PATH_MAX];
772
773 ret = get_full_path(old_handle, old_name, old_fullpath,
774 sizeof(old_fullpath));
775 if (ret) {
776 errno = ENOMEM;
777 goto end;
778 }
779 ret = get_full_path(new_handle, new_name, new_fullpath,
780 sizeof(new_fullpath));
781 if (ret) {
782 errno = ENOMEM;
783 goto end;
784 }
785
786 ret = rename(old_fullpath, new_fullpath);
787 end:
788 return ret;
789 }
790
791 static
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)
796 {
797 int ret;
798 char old_fullpath[LTTNG_PATH_MAX];
799 char new_fullpath[LTTNG_PATH_MAX];
800
801 ret = get_full_path(old_handle, old_name, old_fullpath,
802 sizeof(old_fullpath));
803 if (ret) {
804 errno = ENOMEM;
805 goto end;
806 }
807 ret = get_full_path(new_handle, new_name, new_fullpath,
808 sizeof(new_fullpath));
809 if (ret) {
810 errno = ENOMEM;
811 goto end;
812 }
813
814 ret = run_as_rename(old_fullpath, new_fullpath, uid, gid);
815 end:
816 return ret;
817 }
818
819 static
820 DIR *lttng_directory_handle_opendir(const struct lttng_directory_handle *handle,
821 const char *path)
822 {
823 int ret;
824 DIR *dir_stream = NULL;
825 char fullpath[LTTNG_PATH_MAX];
826
827 ret = get_full_path(handle, path, fullpath, sizeof(fullpath));
828 if (ret) {
829 errno = ENOMEM;
830 goto end;
831 }
832
833 dir_stream = opendir(fullpath);
834 end:
835 return dir_stream;
836 }
837
838 static
839 int lttng_directory_handle_rmdir(
840 const struct lttng_directory_handle *handle, const char *name)
841 {
842 int ret;
843 char fullpath[LTTNG_PATH_MAX];
844
845 ret = get_full_path(handle, name, fullpath, sizeof(fullpath));
846 if (ret) {
847 errno = ENOMEM;
848 goto end;
849 }
850
851 ret = rmdir(fullpath);
852 end:
853 return ret;
854 }
855
856 static
857 int _run_as_rmdir(const struct lttng_directory_handle *handle,
858 const char *name, uid_t uid, gid_t gid)
859 {
860 int ret;
861 char fullpath[LTTNG_PATH_MAX];
862
863 ret = get_full_path(handle, name, fullpath, sizeof(fullpath));
864 if (ret) {
865 errno = ENOMEM;
866 goto end;
867 }
868
869 ret = run_as_rmdir(fullpath, uid, gid);
870 end:
871 return ret;
872 }
873
874 static
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)
878 {
879 int ret;
880 char fullpath[LTTNG_PATH_MAX];
881
882 ret = get_full_path(handle, name, fullpath, sizeof(fullpath));
883 if (ret) {
884 errno = ENOMEM;
885 goto end;
886 }
887
888 ret = run_as_rmdir_recursive(fullpath, uid, gid, flags);
889 end:
890 return ret;
891 }
892
893 #endif /* HAVE_DIRFD */
894
895 /* Common implementation. */
896
897 /*
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", ...).
903 *
904 * Checking the path for existence allows us to work around this behaviour.
905 */
906 static
907 int create_directory_check_exists(const struct lttng_directory_handle *handle,
908 const char *path, mode_t mode)
909 {
910 int ret = 0;
911 struct stat st;
912
913 ret = lttng_directory_handle_stat(handle, path, &st);
914 if (ret == 0) {
915 if (S_ISDIR(st.st_mode)) {
916 /* Directory exists, skip. */
917 goto end;
918 } else {
919 /* Exists, but is not a directory. */
920 errno = ENOTDIR;
921 ret = -1;
922 goto end;
923 }
924 } else if (errno != ENOENT) {
925 goto end;
926 }
927
928 /*
929 * Let mkdir handle other errors as the caller expects mkdir
930 * semantics.
931 */
932 ret = lttng_directory_handle_mkdir(handle, path, mode);
933 end:
934 return ret;
935 }
936
937 static
938 int create_directory_recursive(const struct lttng_directory_handle *handle,
939 const char *path, mode_t mode)
940 {
941 char *p, tmp[LTTNG_PATH_MAX];
942 size_t len;
943 int ret;
944
945 LTTNG_ASSERT(path);
946
947 ret = lttng_strncpy(tmp, path, sizeof(tmp));
948 if (ret) {
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));
951 goto error;
952 }
953
954 len = strlen(path);
955 if (tmp[len - 1] == '/') {
956 tmp[len - 1] = 0;
957 }
958
959 for (p = tmp + 1; *p; p++) {
960 if (*p == '/') {
961 *p = 0;
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)",
966 tmp);
967 ret = -1;
968 goto error;
969 }
970 ret = create_directory_check_exists(handle, tmp, mode);
971 if (ret < 0) {
972 if (errno != EACCES) {
973 PERROR("Failed to create directory \"%s\"",
974 path);
975 ret = -errno;
976 goto error;
977 }
978 }
979 *p = '/';
980 }
981 }
982
983 ret = create_directory_check_exists(handle, tmp, mode);
984 if (ret < 0) {
985 PERROR("mkdirat recursive last element");
986 ret = -errno;
987 }
988 error:
989 return ret;
990 }
991
992 LTTNG_HIDDEN
993 bool lttng_directory_handle_get(struct lttng_directory_handle *handle)
994 {
995 return urcu_ref_get_unless_zero(&handle->ref);
996 }
997
998 LTTNG_HIDDEN
999 void lttng_directory_handle_put(struct lttng_directory_handle *handle)
1000 {
1001 if (!handle) {
1002 return;
1003 }
1004 LTTNG_ASSERT(handle->ref.refcount);
1005 urcu_ref_put(&handle->ref, lttng_directory_handle_release);
1006 }
1007
1008 LTTNG_HIDDEN
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)
1013 {
1014 int ret;
1015
1016 if (!creds) {
1017 /* Run as current user. */
1018 ret = create_directory_check_exists(handle,
1019 subdirectory, mode);
1020 } else {
1021 ret = _run_as_mkdir(handle, subdirectory, mode,
1022 lttng_credentials_get_uid(creds),
1023 lttng_credentials_get_gid(creds));
1024 }
1025
1026 return ret;
1027 }
1028
1029 LTTNG_HIDDEN
1030 int lttng_directory_handle_create_subdirectory_recursive_as_user(
1031 const struct lttng_directory_handle *handle,
1032 const char *subdirectory_path,
1033 mode_t mode, const struct lttng_credentials *creds)
1034 {
1035 int ret;
1036
1037 if (!creds) {
1038 /* Run as current user. */
1039 ret = create_directory_recursive(handle,
1040 subdirectory_path, mode);
1041 } else {
1042 ret = _run_as_mkdir_recursive(handle, subdirectory_path,
1043 mode, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
1044 }
1045
1046 return ret;
1047 }
1048
1049 LTTNG_HIDDEN
1050 int lttng_directory_handle_create_subdirectory(
1051 const struct lttng_directory_handle *handle,
1052 const char *subdirectory,
1053 mode_t mode)
1054 {
1055 return lttng_directory_handle_create_subdirectory_as_user(
1056 handle, subdirectory, mode, NULL);
1057 }
1058
1059 LTTNG_HIDDEN
1060 int lttng_directory_handle_create_subdirectory_recursive(
1061 const struct lttng_directory_handle *handle,
1062 const char *subdirectory_path,
1063 mode_t mode)
1064 {
1065 return lttng_directory_handle_create_subdirectory_recursive_as_user(
1066 handle, subdirectory_path, mode, NULL);
1067 }
1068
1069 LTTNG_HIDDEN
1070 int lttng_directory_handle_open_file_as_user(
1071 const struct lttng_directory_handle *handle,
1072 const char *filename,
1073 int flags, mode_t mode,
1074 const struct lttng_credentials *creds)
1075 {
1076 int ret;
1077
1078 if (!creds) {
1079 /* Run as current user. */
1080 ret = lttng_directory_handle_open(handle, filename, flags,
1081 mode);
1082 } else {
1083 ret = _run_as_open(handle, filename, flags, mode,
1084 lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
1085 }
1086 return ret;
1087 }
1088
1089 LTTNG_HIDDEN
1090 int lttng_directory_handle_open_file(
1091 const struct lttng_directory_handle *handle,
1092 const char *filename,
1093 int flags, mode_t mode)
1094 {
1095 return lttng_directory_handle_open_file_as_user(handle, filename, flags,
1096 mode, NULL);
1097 }
1098
1099 LTTNG_HIDDEN
1100 int lttng_directory_handle_unlink_file_as_user(
1101 const struct lttng_directory_handle *handle,
1102 const char *filename,
1103 const struct lttng_credentials *creds)
1104 {
1105 int ret;
1106
1107 if (!creds) {
1108 /* Run as current user. */
1109 ret = lttng_directory_handle_unlink(handle, filename);
1110 } else {
1111 ret = _run_as_unlink(handle, filename, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
1112 }
1113 return ret;
1114 }
1115
1116 LTTNG_HIDDEN
1117 int lttng_directory_handle_unlink_file(
1118 const struct lttng_directory_handle *handle,
1119 const char *filename)
1120 {
1121 return lttng_directory_handle_unlink_file_as_user(handle,
1122 filename, NULL);
1123 }
1124
1125 LTTNG_HIDDEN
1126 int lttng_directory_handle_rename(
1127 const struct lttng_directory_handle *old_handle,
1128 const char *old_name,
1129 const struct lttng_directory_handle *new_handle,
1130 const char *new_name)
1131 {
1132 return lttng_directory_handle_rename_as_user(old_handle, old_name,
1133 new_handle, new_name, NULL);
1134 }
1135
1136 LTTNG_HIDDEN
1137 int lttng_directory_handle_rename_as_user(
1138 const struct lttng_directory_handle *old_handle,
1139 const char *old_name,
1140 const struct lttng_directory_handle *new_handle,
1141 const char *new_name,
1142 const struct lttng_credentials *creds)
1143 {
1144 int ret;
1145
1146 if (!creds) {
1147 /* Run as current user. */
1148 ret = _lttng_directory_handle_rename(old_handle,
1149 old_name, new_handle, new_name);
1150 } else {
1151 ret = _run_as_rename(old_handle, old_name, new_handle,
1152 new_name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
1153 }
1154 return ret;
1155 }
1156
1157 LTTNG_HIDDEN
1158 int lttng_directory_handle_remove_subdirectory(
1159 const struct lttng_directory_handle *handle,
1160 const char *name)
1161 {
1162 return lttng_directory_handle_remove_subdirectory_as_user(handle, name,
1163 NULL);
1164 }
1165
1166 LTTNG_HIDDEN
1167 int lttng_directory_handle_remove_subdirectory_as_user(
1168 const struct lttng_directory_handle *handle,
1169 const char *name,
1170 const struct lttng_credentials *creds)
1171 {
1172 int ret;
1173
1174 if (!creds) {
1175 /* Run as current user. */
1176 ret = lttng_directory_handle_rmdir(handle, name);
1177 } else {
1178 ret = _run_as_rmdir(handle, name, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds));
1179 }
1180 return ret;
1181 }
1182
1183 struct rmdir_frame {
1184 ssize_t parent_frame_idx;
1185 DIR *dir;
1186 bool empty;
1187 /* Size including '\0'. */
1188 size_t path_size;
1189 };
1190
1191 static
1192 void rmdir_frame_fini(void *data)
1193 {
1194 int ret;
1195 struct rmdir_frame *frame = data;
1196
1197 ret = closedir(frame->dir);
1198 if (ret == -1) {
1199 PERROR("Failed to close directory stream");
1200 }
1201 }
1202
1203 static
1204 int remove_directory_recursive(const struct lttng_directory_handle *handle,
1205 const char *path, int flags)
1206 {
1207 int ret;
1208 struct lttng_dynamic_array frames;
1209 size_t current_frame_idx = 0;
1210 struct rmdir_frame initial_frame = {
1211 .parent_frame_idx = -1,
1212 .dir = lttng_directory_handle_opendir(handle, path),
1213 .empty = true,
1214 .path_size = strlen(path) + 1,
1215 };
1216 struct lttng_dynamic_buffer current_path;
1217 const char separator = '/';
1218
1219 lttng_dynamic_buffer_init(&current_path);
1220 lttng_dynamic_array_init(&frames, sizeof(struct rmdir_frame),
1221 rmdir_frame_fini);
1222
1223 if (flags & ~(LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG |
1224 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG)) {
1225 ERR("Unknown flags %d", flags);
1226 ret = -1;
1227 goto end;
1228 }
1229
1230 if (!initial_frame.dir) {
1231 if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG &&
1232 errno == ENOENT) {
1233 DBG("Cannot rmdir \"%s\": root does not exist", path);
1234 ret = 0;
1235 goto end;
1236 } else {
1237 PERROR("Failed to rmdir \"%s\"", path);
1238 ret = -1;
1239 goto end;
1240 }
1241 }
1242
1243 ret = lttng_dynamic_array_add_element(&frames, &initial_frame);
1244 if (ret) {
1245 ERR("Failed to push context frame during recursive directory removal");
1246 rmdir_frame_fini(&initial_frame);
1247 goto end;
1248 }
1249
1250 ret = lttng_dynamic_buffer_append(
1251 &current_path, path, initial_frame.path_size);
1252 if (ret) {
1253 ERR("Failed to set initial path during recursive directory removal");
1254 ret = -1;
1255 goto end;
1256 }
1257
1258 while (lttng_dynamic_array_get_count(&frames) > 0) {
1259 struct dirent *entry;
1260 struct rmdir_frame *current_frame =
1261 lttng_dynamic_array_get_element(
1262 &frames, current_frame_idx);
1263
1264 LTTNG_ASSERT(current_frame->dir);
1265 ret = lttng_dynamic_buffer_set_size(
1266 &current_path, current_frame->path_size);
1267 LTTNG_ASSERT(!ret);
1268 current_path.data[current_path.size - 1] = '\0';
1269
1270 while ((entry = readdir(current_frame->dir))) {
1271 struct stat st;
1272
1273 if (!strcmp(entry->d_name, ".") ||
1274 !strcmp(entry->d_name, "..")) {
1275 continue;
1276 }
1277
1278 /* Set current_path to the entry's path. */
1279 ret = lttng_dynamic_buffer_set_size(
1280 &current_path, current_path.size - 1);
1281 LTTNG_ASSERT(!ret);
1282 ret = lttng_dynamic_buffer_append(&current_path,
1283 &separator, sizeof(separator));
1284 if (ret) {
1285 goto end;
1286 }
1287 ret = lttng_dynamic_buffer_append(&current_path,
1288 entry->d_name,
1289 strlen(entry->d_name) + 1);
1290 if (ret) {
1291 goto end;
1292 }
1293
1294 if (lttng_directory_handle_stat(
1295 handle, current_path.data, &st)) {
1296 if ((flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG) &&
1297 errno == ENOENT) {
1298 break;
1299 }
1300 PERROR("Failed to stat \"%s\"",
1301 current_path.data);
1302 ret = -1;
1303 goto end;
1304 }
1305
1306 if (!S_ISDIR(st.st_mode)) {
1307 if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG) {
1308 current_frame->empty = false;
1309 break;
1310 } else {
1311 /* Not empty, abort. */
1312 DBG("Directory \"%s\" is not empty; refusing to remove directory",
1313 current_path.data);
1314 ret = -1;
1315 goto end;
1316 }
1317 } else {
1318 struct rmdir_frame new_frame = {
1319 .path_size = current_path.size,
1320 .dir = lttng_directory_handle_opendir(
1321 handle,
1322 current_path.data),
1323 .empty = true,
1324 .parent_frame_idx = current_frame_idx,
1325 };
1326
1327 if (!new_frame.dir) {
1328 if (flags & LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG &&
1329 errno == ENOENT) {
1330 DBG("Non-existing directory stream during recursive directory removal");
1331 break;
1332 } else {
1333 PERROR("Failed to open directory stream during recursive directory removal");
1334 ret = -1;
1335 goto end;
1336 }
1337 }
1338 ret = lttng_dynamic_array_add_element(
1339 &frames, &new_frame);
1340 if (ret) {
1341 ERR("Failed to push context frame during recursive directory removal");
1342 rmdir_frame_fini(&new_frame);
1343 goto end;
1344 }
1345 current_frame_idx++;
1346 /* We break iteration on readdir. */
1347 break;
1348 }
1349 }
1350 if (entry) {
1351 continue;
1352 }
1353
1354 /* Pop rmdir frame. */
1355 if (current_frame->empty) {
1356 ret = lttng_directory_handle_rmdir(
1357 handle, current_path.data);
1358 if (ret) {
1359 if ((flags & LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG) ||
1360 errno != ENOENT) {
1361 PERROR("Failed to remove \"%s\" during recursive directory removal",
1362 current_path.data);
1363 goto end;
1364 }
1365 DBG("Non-existing directory stream during recursive directory removal");
1366 }
1367 } else if (current_frame->parent_frame_idx >= 0) {
1368 struct rmdir_frame *parent_frame;
1369
1370 parent_frame = lttng_dynamic_array_get_element(&frames,
1371 current_frame->parent_frame_idx);
1372 LTTNG_ASSERT(parent_frame);
1373 parent_frame->empty = false;
1374 }
1375 ret = lttng_dynamic_array_remove_element(
1376 &frames, current_frame_idx);
1377 if (ret) {
1378 ERR("Failed to pop context frame during recursive directory removal");
1379 goto end;
1380 }
1381 current_frame_idx--;
1382 }
1383 end:
1384 lttng_dynamic_array_reset(&frames);
1385 lttng_dynamic_buffer_reset(&current_path);
1386 return ret;
1387 }
1388
1389 LTTNG_HIDDEN
1390 int lttng_directory_handle_remove_subdirectory_recursive(
1391 const struct lttng_directory_handle *handle,
1392 const char *name,
1393 int flags)
1394 {
1395 return lttng_directory_handle_remove_subdirectory_recursive_as_user(
1396 handle, name, NULL, flags);
1397 }
1398
1399 LTTNG_HIDDEN
1400 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
1401 const struct lttng_directory_handle *handle,
1402 const char *name,
1403 const struct lttng_credentials *creds,
1404 int flags)
1405 {
1406 int ret;
1407
1408 if (!creds) {
1409 /* Run as current user. */
1410 ret = remove_directory_recursive(handle, name, flags);
1411 } else {
1412 ret = _run_as_rmdir_recursive(handle, name, lttng_credentials_get_uid(creds),
1413 lttng_credentials_get_gid(creds), flags);
1414 }
1415 return ret;
1416 }
This page took 0.096978 seconds and 4 git commands to generate.