Add mkdirat utils and runas wrappers
[lttng-tools.git] / src / common / utils.c
index a092d940f422979b724fa5a61a72365c54b83ad7..5be67c11f1e1dc896fa2e5d5e9cb7a6299903c73 100644 (file)
 #include <unistd.h>
 
 #include <common/common.h>
+#include <common/readwrite.h>
 #include <common/runas.h>
 #include <common/compat/getenv.h>
 #include <common/compat/string.h>
 #include <common/compat/dirent.h>
+#include <common/compat/directory-handle.h>
 #include <lttng/constant.h>
 
 #include "utils.h"
 #include "defaults.h"
 #include "time.h"
 
+#define PROC_MEMINFO_PATH               "/proc/meminfo"
+#define PROC_MEMINFO_MEMAVAILABLE_LINE  "MemAvailable:"
+#define PROC_MEMINFO_MEMTOTAL_LINE      "MemTotal:"
+
+/* The length of the longest field of `/proc/meminfo`. */
+#define PROC_MEMINFO_FIELD_MAX_NAME_LEN        20
+
+#if (PROC_MEMINFO_FIELD_MAX_NAME_LEN == 20)
+#define MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "19"
+#else
+#error MAX_NAME_LEN_SCANF_IS_A_BROKEN_API must be updated to match (PROC_MEMINFO_FIELD_MAX_NAME_LEN - 1)
+#endif
+
 /*
  * Return a partial realpath(3) of the path even if the full path does not
  * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist
@@ -653,44 +668,6 @@ error:
        return fd;
 }
 
-/*
- * On some filesystems (e.g. nfs), mkdir will validate access rights before
- * checking for the existence of the path element. This means that on a setup
- * where "/home/" is a mounted NFS share, and running as an unpriviledged user,
- * recursively creating a path of the form "/home/my_user/trace/" will fail with
- * EACCES on mkdir("/home", ...).
- *
- * Performing a stat(...) on the path to check for existence allows us to
- * work around this behaviour.
- */
-static
-int mkdir_check_exists(const char *path, mode_t mode)
-{
-       int ret = 0;
-       struct stat st;
-
-       ret = stat(path, &st);
-       if (ret == 0) {
-               if (S_ISDIR(st.st_mode)) {
-                       /* Directory exists, skip. */
-                       goto end;
-               } else {
-                       /* Exists, but is not a directory. */
-                       errno = ENOTDIR;
-                       ret = -1;
-                       goto end;
-               }
-       }
-
-       /*
-        * Let mkdir handle other errors as the caller expects mkdir
-        * semantics.
-        */
-       ret = mkdir(path, mode);
-end:
-       return ret;
-}
-
 /*
  * Create directory using the given path and mode.
  *
@@ -700,82 +677,17 @@ LTTNG_HIDDEN
 int utils_mkdir(const char *path, mode_t mode, int uid, int gid)
 {
        int ret;
-
-       if (uid < 0 || gid < 0) {
-               ret = mkdir_check_exists(path, mode);
-       } else {
-               ret = run_as_mkdir(path, mode, uid, gid);
-       }
-       if (ret < 0) {
-               if (errno != EEXIST) {
-                       PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL",
-                                       uid, gid);
-               } else {
-                       ret = 0;
-               }
-       }
-
-       return ret;
-}
-
-/*
- * Internal version of mkdir_recursive. Runs as the current user.
- * Don't call directly; use utils_mkdir_recursive().
- *
- * This function is ominously marked as "unsafe" since it should only
- * be called by a caller that has transitioned to the uid and gid under which
- * the directory creation should occur.
- */
-LTTNG_HIDDEN
-int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
-{
-       char *p, tmp[PATH_MAX];
-       size_t len;
-       int ret;
-
-       assert(path);
-
-       ret = snprintf(tmp, sizeof(tmp), "%s", path);
-       if (ret < 0) {
-               PERROR("snprintf mkdir");
-               goto error;
-       }
-
-       len = ret;
-       if (tmp[len - 1] == '/') {
-               tmp[len - 1] = 0;
-       }
-
-       for (p = tmp + 1; *p; p++) {
-               if (*p == '/') {
-                       *p = 0;
-                       if (tmp[strlen(tmp) - 1] == '.' &&
-                                       tmp[strlen(tmp) - 2] == '.' &&
-                                       tmp[strlen(tmp) - 3] == '/') {
-                               ERR("Using '/../' is not permitted in the trace path (%s)",
-                                               tmp);
-                               ret = -1;
-                               goto error;
-                       }
-                       ret = mkdir_check_exists(tmp, mode);
-                       if (ret < 0) {
-                               if (errno != EACCES) {
-                                       PERROR("mkdir recursive");
-                                       ret = -errno;
-                                       goto error;
-                               }
-                       }
-                       *p = '/';
-               }
-       }
-
-       ret = mkdir_check_exists(tmp, mode);
-       if (ret < 0) {
-               PERROR("mkdir recursive last element");
-               ret = -errno;
-       }
-
-error:
+       struct lttng_directory_handle handle;
+       struct lttng_credentials creds = {
+               .uid = (uid_t) uid,
+               .gid = (gid_t) gid,
+       };
+
+       (void) lttng_directory_handle_init(&handle, NULL);
+       ret = lttng_directory_handle_create_subdirectory_as_user(
+                       &handle, path, mode,
+                       (uid >= 0 || gid >= 0) ? &creds : NULL);
+       lttng_directory_handle_fini(&handle);
        return ret;
 }
 
@@ -789,18 +701,17 @@ LTTNG_HIDDEN
 int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid)
 {
        int ret;
-
-       if (uid < 0 || gid < 0) {
-               /* Run as current user. */
-               ret = _utils_mkdir_recursive_unsafe(path, mode);
-       } else {
-               ret = run_as_mkdir_recursive(path, mode, uid, gid);
-       }
-       if (ret < 0) {
-               PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL",
-                               uid, gid);
-       }
-
+       struct lttng_directory_handle handle;
+       struct lttng_credentials creds = {
+               .uid = (uid_t) uid,
+               .gid = (gid_t) gid,
+       };
+
+       (void) lttng_directory_handle_init(&handle, NULL);
+       ret = lttng_directory_handle_create_subdirectory_recursive_as_user(
+                       &handle, path, mode,
+                       (uid >= 0 || gid >= 0) ? &creds : NULL);
+       lttng_directory_handle_fini(&handle);
        return ret;
 }
 
@@ -1673,3 +1584,77 @@ int utils_show_help(int section, const char *page_name,
 end:
        return ret;
 }
+
+static
+int read_proc_meminfo_field(const char *field, size_t *value)
+{
+       int ret;
+       FILE *proc_meminfo;
+       char name[PROC_MEMINFO_FIELD_MAX_NAME_LEN] = {};
+
+       proc_meminfo = fopen(PROC_MEMINFO_PATH, "r");
+       if (!proc_meminfo) {
+               PERROR("Failed to fopen() " PROC_MEMINFO_PATH);
+               ret = -1;
+               goto fopen_error;
+        }
+
+       /*
+        * Read the contents of /proc/meminfo line by line to find the right
+        * field.
+        */
+       while (!feof(proc_meminfo)) {
+               unsigned long value_kb;
+
+               ret = fscanf(proc_meminfo,
+                               "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %lu kB\n",
+                               name, &value_kb);
+               if (ret == EOF) {
+                       /*
+                        * fscanf() returning EOF can indicate EOF or an error.
+                        */
+                       if (ferror(proc_meminfo)) {
+                               PERROR("Failed to parse " PROC_MEMINFO_PATH);
+                       }
+                       break;
+               }
+
+               if (ret == 2 && strcmp(name, field) == 0) {
+                       /*
+                        * This number is displayed in kilo-bytes. Return the
+                        * number of bytes.
+                        */
+                       *value = ((size_t) value_kb) * 1024;
+                       ret = 0;
+                       goto found;
+               }
+       }
+       /* Reached the end of the file without finding the right field. */
+       ret = -1;
+
+found:
+       fclose(proc_meminfo);
+fopen_error:
+       return ret;
+}
+
+/*
+ * Returns an estimate of the number of bytes of memory available based on the
+ * the information in `/proc/meminfo`. The number returned by this function is
+ * a best guess.
+ */
+LTTNG_HIDDEN
+int utils_get_memory_available(size_t *value)
+{
+       return read_proc_meminfo_field(PROC_MEMINFO_MEMAVAILABLE_LINE, value);
+}
+
+/*
+ * Returns the total size of the memory on the system in bytes based on the
+ * the information in `/proc/meminfo`.
+ */
+LTTNG_HIDDEN
+int utils_get_memory_total(size_t *value)
+{
+       return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value);
+}
This page took 0.025666 seconds and 4 git commands to generate.