]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tools: move lxc-ls to API symbols only
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 12 Jan 2018 14:47:32 +0000 (15:47 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 6 Feb 2018 20:10:48 +0000 (21:10 +0100)
Closes #2073.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/tools/lxc_ls.c
src/lxc/tools/tool_utils.c
src/lxc/tools/tool_utils.h

index 7908f20cacf9e7b13d0e02e9fab97a677aaa286c..cb057774b51e47cc5982c650d057cdb77f67b7a5 100644 (file)
@@ -276,20 +276,20 @@ lxc_console_SOURCES = tools/lxc_console.c tools/arguments.c tools/tool_utils.c
 lxc_destroy_SOURCES = tools/lxc_destroy.c tools/arguments.c tools/tool_utils.c
 lxc_device_SOURCES = tools/lxc_device.c tools/arguments.c tools/tool_utils.c
 lxc_execute_SOURCES = tools/lxc_execute.c tools/arguments.c tools/tool_utils.c
-lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c
-lxc_info_SOURCES = tools/lxc_info.c tools/arguments.c
-lxc_monitor_SOURCES = tools/lxc_monitor.c tools/arguments.c
-lxc_ls_SOURCES = tools/lxc_ls.c tools/arguments.c
-lxc_copy_SOURCES = tools/lxc_copy.c tools/arguments.c
-lxc_start_SOURCES = tools/lxc_start.c tools/arguments.c
-lxc_stop_SOURCES = tools/lxc_stop.c tools/arguments.c
-lxc_top_SOURCES = tools/lxc_top.c tools/arguments.c
-lxc_unfreeze_SOURCES = tools/lxc_unfreeze.c tools/arguments.c
-lxc_unshare_SOURCES = tools/lxc_unshare.c tools/arguments.c
-lxc_wait_SOURCES = tools/lxc_wait.c tools/arguments.c
-lxc_create_SOURCES = tools/lxc_create.c tools/arguments.c
-lxc_snapshot_SOURCES = tools/lxc_snapshot.c tools/arguments.c
-lxc_checkpoint_SOURCES = tools/lxc_checkpoint.c tools/arguments.c
+lxc_freeze_SOURCES = tools/lxc_freeze.c tools/arguments.c tools/tool_utils.c
+lxc_info_SOURCES = tools/lxc_info.c tools/arguments.c tools/tool_utils.c
+lxc_monitor_SOURCES = tools/lxc_monitor.c tools/arguments.c tools/tool_utils.c
+lxc_ls_SOURCES = tools/lxc_ls.c tools/arguments.c tools/tool_utils.c
+lxc_copy_SOURCES = tools/lxc_copy.c tools/arguments.c tools/tool_utils.c
+lxc_start_SOURCES = tools/lxc_start.c tools/arguments.c tools/tool_utils.c
+lxc_stop_SOURCES = tools/lxc_stop.c tools/arguments.c tools/tool_utils.c
+lxc_top_SOURCES = tools/lxc_top.c tools/arguments.c tools/tool_utils.c
+lxc_unfreeze_SOURCES = tools/lxc_unfreeze.c tools/arguments.c tools/tool_utils.c
+lxc_unshare_SOURCES = tools/lxc_unshare.c tools/arguments.c tools/tool_utils.c
+lxc_wait_SOURCES = tools/lxc_wait.c tools/arguments.c tools/tool_utils.c
+lxc_create_SOURCES = tools/lxc_create.c tools/arguments.c tools/tool_utils.c
+lxc_snapshot_SOURCES = tools/lxc_snapshot.c tools/arguments.c tools/tool_utils.c
+lxc_checkpoint_SOURCES = tools/lxc_checkpoint.c tools/arguments.c tools/tool_utils.c
 
 # Binaries shipping with liblxc
 init_lxc_SOURCES = cmd/lxc_init.c
index 6f9719a868d84a3c93f3cb2a787cfd2f7771e33c..1cec7d5d97b298e0c91d7c43d601d6c6d184d89b 100644 (file)
@@ -16,9 +16,9 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "config.h"
-
+#define _GNU_SOURCE
 #include <getopt.h>
+#include <limits.h>
 #include <regex.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <lxc/lxccontainer.h>
 
 #include "arguments.h"
-#include "conf.h"
-#include "confile.h"
-#include "log.h"
-#include "lxc.h"
-#include "utils.h"
+#include "tool_utils.h"
 
 /* Per default we only allow five levels of recursion to protect the stack at
  * least a little bit. */
@@ -231,7 +227,6 @@ int main(int argc, char *argv[])
 
        if (lxc_log_init(&log))
                exit(EXIT_FAILURE);
-       lxc_log_options_no_override();
 
        /* REMOVE IN LXC 3.0 */
        setenv("LXC_UPDATE_CONFIG_FORMAT", "1", 0);
@@ -1072,7 +1067,7 @@ static int ls_remove_lock(const char *path, const char *name,
        if (check < 0 || (size_t)check >= *len_lockpath)
                goto out;
 
-       lxc_rmdir_onedev(*lockpath, NULL);
+       (void)rm_r(*lockpath);
        ret = 0;
 
 out:
index 462a07a2232e221e3bf935bbb204458ee851bc7a..71b499659098a908cdc3922bcaf996db05b79718 100644 (file)
@@ -20,6 +20,7 @@
 #define _GNU_SOURCE
 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
 #include <ctype.h>
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
@@ -850,3 +851,283 @@ int lxc_read_from_file(const char *filename, void* buf, size_t count)
        errno = saved_errno;
        return ret;
 }
+
+char **lxc_string_split_and_trim(const char *string, char _sep)
+{
+       char *token, *str, *saveptr = NULL;
+       char sep[2] = { _sep, '\0' };
+       char **result = NULL;
+       size_t result_capacity = 0;
+       size_t result_count = 0;
+       int r, saved_errno;
+       size_t i = 0;
+
+       if (!string)
+               return calloc(1, sizeof(char *));
+
+       str = alloca(strlen(string)+1);
+       strcpy(str, string);
+       for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
+               while (token[0] == ' ' || token[0] == '\t')
+                       token++;
+               i = strlen(token);
+               while (i > 0 && (token[i - 1] == ' ' || token[i - 1] == '\t')) {
+                       token[i - 1] = '\0';
+                       i--;
+               }
+               r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
+               if (r < 0)
+                       goto error_out;
+               result[result_count] = strdup(token);
+               if (!result[result_count])
+                       goto error_out;
+               result_count++;
+       }
+
+       /* if we allocated too much, reduce it */
+       return realloc(result, (result_count + 1) * sizeof(char *));
+error_out:
+       saved_errno = errno;
+       lxc_free_array((void **)result, free);
+       errno = saved_errno;
+       return NULL;
+}
+
+char *lxc_append_paths(const char *first, const char *second)
+{
+       int ret;
+       size_t len;
+       char *result = NULL;
+       const char *pattern = "%s%s";
+
+       len = strlen(first) + strlen(second) + 1;
+       if (second[0] != '/') {
+               len += 1;
+               pattern = "%s/%s";
+       }
+
+       result = calloc(1, len);
+       if (!result)
+               return NULL;
+
+       ret = snprintf(result, len, pattern, first, second);
+       if (ret < 0 || (size_t)ret >= len) {
+               free(result);
+               return NULL;
+       }
+
+       return result;
+}
+
+bool dir_exists(const char *path)
+{
+       struct stat sb;
+       int ret;
+
+       ret = stat(path, &sb);
+       if (ret < 0)
+               /* Could be something other than eexist, just say "no". */
+               return false;
+       return S_ISDIR(sb.st_mode);
+}
+
+char *lxc_string_replace(const char *needle, const char *replacement,
+                        const char *haystack)
+{
+       ssize_t len = -1, saved_len = -1;
+       char *result = NULL;
+       size_t replacement_len = strlen(replacement);
+       size_t needle_len = strlen(needle);
+
+       /* should be executed exactly twice */
+       while (len == -1 || result == NULL) {
+               char *p;
+               char *last_p;
+               ssize_t part_len;
+
+               if (len != -1) {
+                       result = calloc(1, len + 1);
+                       if (!result)
+                               return NULL;
+                       saved_len = len;
+               }
+
+               len = 0;
+
+               for (last_p = (char *)haystack, p = strstr(last_p, needle); p; last_p = p, p = strstr(last_p, needle)) {
+                       part_len = (ssize_t)(p - last_p);
+                       if (result && part_len > 0)
+                               memcpy(&result[len], last_p, part_len);
+                       len += part_len;
+                       if (result && replacement_len > 0)
+                               memcpy(&result[len], replacement, replacement_len);
+                       len += replacement_len;
+                       p += needle_len;
+               }
+               part_len = strlen(last_p);
+               if (result && part_len > 0)
+                       memcpy(&result[len], last_p, part_len);
+               len += part_len;
+       }
+
+       /* make sure we did the same thing twice,
+        * once for calculating length, the other
+        * time for copying data */
+       if (saved_len != len) {
+               free(result);
+               return NULL;
+       }
+       /* make sure we didn't overwrite any buffer,
+        * due to calloc the string should be 0-terminated */
+       if (result[len] != '\0') {
+               free(result);
+               return NULL;
+       }
+
+       return result;
+}
+
+ssize_t lxc_write_nointr(int fd, const void* buf, size_t count)
+{
+       ssize_t ret;
+again:
+       ret = write(fd, buf, count);
+       if (ret < 0 && errno == EINTR)
+               goto again;
+       return ret;
+}
+
+char *get_rundir()
+{
+       char *rundir;
+       const char *homedir;
+
+       if (geteuid() == 0) {
+               rundir = strdup(RUNTIME_PATH);
+               return rundir;
+       }
+
+       rundir = getenv("XDG_RUNTIME_DIR");
+       if (rundir) {
+               rundir = strdup(rundir);
+               return rundir;
+       }
+
+       homedir = getenv("HOME");
+       if (!homedir)
+               return NULL;
+
+       rundir = malloc(sizeof(char) * (17 + strlen(homedir)));
+       sprintf(rundir, "%s/.cache/lxc/run/", homedir);
+
+       return rundir;
+}
+
+char *must_copy_string(const char *entry)
+{
+       char *ret;
+
+       if (!entry)
+               return NULL;
+       do {
+               ret = strdup(entry);
+       } while (!ret);
+
+       return ret;
+}
+
+
+void *must_realloc(void *orig, size_t sz)
+{
+       void *ret;
+
+       do {
+               ret = realloc(orig, sz);
+       } while (!ret);
+
+       return ret;
+}
+
+char *must_make_path(const char *first, ...)
+{
+       va_list args;
+       char *cur, *dest;
+       size_t full_len = strlen(first);
+
+       dest = must_copy_string(first);
+
+       va_start(args, first);
+       while ((cur = va_arg(args, char *)) != NULL) {
+               full_len += strlen(cur);
+               if (cur[0] != '/')
+                       full_len++;
+               dest = must_realloc(dest, full_len + 1);
+               if (cur[0] != '/')
+                       strcat(dest, "/");
+               strcat(dest, cur);
+       }
+       va_end(args);
+
+       return dest;
+}
+
+int rm_r(char *dirname)
+{
+       int ret;
+       struct dirent *direntp;
+       DIR *dir;
+       int r = 0;
+
+       dir = opendir(dirname);
+       if (!dir)
+               return -1;
+
+       while ((direntp = readdir(dir))) {
+               char *pathname;
+               struct stat mystat;
+
+               if (!direntp)
+                       break;
+
+               if (!strcmp(direntp->d_name, ".") ||
+                   !strcmp(direntp->d_name, ".."))
+                       continue;
+
+               pathname = must_make_path(dirname, direntp->d_name, NULL);
+
+               ret = lstat(pathname, &mystat);
+               if (ret < 0) {
+                       r = -1;
+                       goto next;
+               }
+
+               if (!S_ISDIR(mystat.st_mode))
+                       goto next;
+
+               ret = rm_r(pathname);
+               if (ret < 0)
+                       r = -1;
+       next:
+               free(pathname);
+       }
+
+       ret = rmdir(dirname);
+       if (ret < 0)
+               r = -1;
+
+       ret = closedir(dir);
+       if (ret < 0)
+               r = -1;
+
+       return r;
+}
+
+ssize_t lxc_read_nointr(int fd, void* buf, size_t count)
+{
+       ssize_t ret;
+again:
+       ret = read(fd, buf, count);
+       if (ret < 0 && errno == EINTR)
+               goto again;
+       return ret;
+}
index 2816376801f82fa5b5cece2ffbbd1dc0a11fcf39..278402ab3a42d011efef8bb262b76ffae178d44f 100644 (file)
@@ -139,9 +139,18 @@ extern char **lxc_normalize_path(const char *path);
 extern char *lxc_string_join(const char *sep, const char **parts,
                             bool use_as_prefix);
 extern char **lxc_string_split_quoted(char *string);
+extern char **lxc_string_split_and_trim(const char *string, char _sep);
+extern char *lxc_append_paths(const char *first, const char *second);
+extern char *lxc_string_replace(const char *needle, const char *replacement,
+                               const char *haystack);
+extern char *must_copy_string(const char *entry);
+extern void *must_realloc(void *orig, size_t sz);
+extern char *must_make_path(const char *first, ...);
 
 extern int mkdir_p(const char *dir, mode_t mode);
+extern int rm_r(char *dirname);
 extern bool file_exists(const char *f);
+extern bool dir_exists(const char *path);
 extern int is_dir(const char *path);
 extern int lxc_read_from_file(const char *filename, void* buf, size_t count);
 
@@ -162,4 +171,9 @@ struct new_config_item {
 };
 extern struct new_config_item *parse_line(char *buffer);
 
+extern ssize_t lxc_read_nointr(int fd, void* buf, size_t count);
+extern ssize_t lxc_write_nointr(int fd, const void* buf, size_t count);
+
+extern char *get_rundir();
+
 #endif /* __LXC_UTILS_H */