| 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 | |
| 24 | /* |
| 25 | * Some platforms, such as Solaris 10, do not support directory file descriptors |
| 26 | * and their associated functions (*at(...)), which are defined in POSIX.2008. |
| 27 | * |
| 28 | * This wrapper provides a handle that is either a copy of a directory's path |
| 29 | * or a directory file descriptors, depending on the platform's capabilities. |
| 30 | */ |
| 31 | #ifdef COMPAT_DIRFD |
| 32 | struct lttng_directory_handle { |
| 33 | int dirfd; |
| 34 | }; |
| 35 | #else |
| 36 | struct lttng_directory_handle { |
| 37 | char *base_path; |
| 38 | }; |
| 39 | #endif |
| 40 | |
| 41 | /* |
| 42 | * Initialize a directory handle to the provided path. Passing a NULL path |
| 43 | * returns a handle to the current working directory. |
| 44 | * |
| 45 | * An initialized directory handle must be finalized using |
| 46 | * lttng_directory_handle_fini(). |
| 47 | */ |
| 48 | LTTNG_HIDDEN |
| 49 | int lttng_directory_handle_init(struct lttng_directory_handle *handle, |
| 50 | const char *path); |
| 51 | |
| 52 | /* |
| 53 | * Initialize a new directory handle to a path relative to an existing handle. |
| 54 | * |
| 55 | * The provided path must already exist. Note that the creation of a |
| 56 | * subdirectory and the creation of a handle are kept as separate operations |
| 57 | * to highlight the fact that there is an inherent race between the creation of |
| 58 | * a directory and the creation of a handle to it. |
| 59 | * |
| 60 | * Passing a NULL path effectively copies the original handle. |
| 61 | * |
| 62 | * An initialized directory handle must be finalized using |
| 63 | * lttng_directory_handle_fini(). |
| 64 | */ |
| 65 | LTTNG_HIDDEN |
| 66 | int lttng_directory_handle_init_from_handle( |
| 67 | struct lttng_directory_handle *new_handle, |
| 68 | const char *path, |
| 69 | const struct lttng_directory_handle *handle); |
| 70 | |
| 71 | /* |
| 72 | * Initialize a new directory handle from an existing directory fd. |
| 73 | * |
| 74 | * The new directory handle assumes the ownership of the directory fd. |
| 75 | * Note that this method should only be used in very specific cases, such as |
| 76 | * re-creating a directory handle from a dirfd passed over a unix socket. |
| 77 | * |
| 78 | * An initialized directory handle must be finalized using |
| 79 | * lttng_directory_handle_fini(). |
| 80 | */ |
| 81 | LTTNG_HIDDEN |
| 82 | int lttng_directory_handle_init_from_dirfd( |
| 83 | struct lttng_directory_handle *handle, int dirfd); |
| 84 | |
| 85 | /* |
| 86 | * Copy a directory handle. |
| 87 | */ |
| 88 | LTTNG_HIDDEN |
| 89 | int lttng_directory_handle_copy(const struct lttng_directory_handle *handle, |
| 90 | struct lttng_directory_handle *new_copy); |
| 91 | |
| 92 | /* |
| 93 | * Move a directory handle. The original directory handle may no longer be |
| 94 | * used after this call. This call cannot fail; directly assign the |
| 95 | * return value to the new directory handle. |
| 96 | * |
| 97 | * It is safe (but unnecessary) to call lttng_directory_handle_fini on the |
| 98 | * original handle. |
| 99 | */ |
| 100 | LTTNG_HIDDEN |
| 101 | struct lttng_directory_handle |
| 102 | lttng_directory_handle_move(struct lttng_directory_handle *original); |
| 103 | |
| 104 | /* |
| 105 | * Release the resources of a directory handle. |
| 106 | */ |
| 107 | LTTNG_HIDDEN |
| 108 | void lttng_directory_handle_fini(struct lttng_directory_handle *handle); |
| 109 | |
| 110 | /* |
| 111 | * Create a subdirectory relative to a directory handle. |
| 112 | */ |
| 113 | LTTNG_HIDDEN |
| 114 | int lttng_directory_handle_create_subdirectory( |
| 115 | const struct lttng_directory_handle *handle, |
| 116 | const char *subdirectory, |
| 117 | mode_t mode); |
| 118 | |
| 119 | /* |
| 120 | * Create a subdirectory relative to a directory handle |
| 121 | * as a given user. |
| 122 | */ |
| 123 | LTTNG_HIDDEN |
| 124 | int lttng_directory_handle_create_subdirectory_as_user( |
| 125 | const struct lttng_directory_handle *handle, |
| 126 | const char *subdirectory, |
| 127 | mode_t mode, const struct lttng_credentials *creds); |
| 128 | |
| 129 | /* |
| 130 | * Recursively create a directory relative to a directory handle. |
| 131 | */ |
| 132 | LTTNG_HIDDEN |
| 133 | int lttng_directory_handle_create_subdirectory_recursive( |
| 134 | const struct lttng_directory_handle *handle, |
| 135 | const char *subdirectory_path, |
| 136 | mode_t mode); |
| 137 | |
| 138 | /* |
| 139 | * Recursively create a directory relative to a directory handle |
| 140 | * as a given user. |
| 141 | */ |
| 142 | LTTNG_HIDDEN |
| 143 | int lttng_directory_handle_create_subdirectory_recursive_as_user( |
| 144 | const struct lttng_directory_handle *handle, |
| 145 | const char *subdirectory_path, |
| 146 | mode_t mode, const struct lttng_credentials *creds); |
| 147 | |
| 148 | #endif /* _COMPAT_PATH_HANDLE_H */ |