]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tools: lxc-device: share internal API symbols
author2xsec <dh48.jeong@samsung.com>
Fri, 29 Jun 2018 06:23:43 +0000 (15:23 +0900)
committer2xsec <dh48.jeong@samsung.com>
Sat, 30 Jun 2018 09:45:21 +0000 (18:45 +0900)
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
src/lxc/Makefile.am
src/lxc/tools/lxc_device.c
src/lxc/utils.c

index 71ffb15db14a9a8f6b2aee6f52715cfc1958cbcd..6b8ce2cba7ccdb6c15b64210ce31bb1ba92d6552 100644 (file)
@@ -271,7 +271,7 @@ lxc_cgroup_SOURCES = tools/lxc_cgroup.c tools/arguments.c
 lxc_config_SOURCES = tools/lxc_config.c tools/arguments.c
 lxc_console_SOURCES = tools/lxc_console.c tools/arguments.c
 lxc_destroy_SOURCES = tools/lxc_destroy.c tools/arguments.c
-lxc_device_SOURCES = tools/lxc_device.c tools/arguments.c tools/tool_utils.c
+lxc_device_SOURCES = tools/lxc_device.c tools/arguments.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
index 6295b89d7c1f170f0d75a0c10dcfde56c69106c2..3c85eb9937d5de4a269d0f6d93b7173ad5d249a2 100644 (file)
 #include <lxc/lxccontainer.h>
 
 #include "arguments.h"
-#include "tool_utils.h"
+#include "log.h"
+#include "utils.h"
+
+lxc_log_define(lxc_device, lxc);
 
 #if HAVE_IFADDRS_H
 #include <ifaddrs.h>
@@ -57,12 +60,11 @@ Options :\n\
        .checker  = NULL,
 };
 
-static bool is_interface(const chardev_name, pid_t pid)
+static bool is_interface(const char *dev_name, pid_t pid)
 {
        pid_t p = fork();
-
        if (p < 0) {
-               fprintf(stderr, "failed to fork task.\n");
+               ERROR("Failed to fork task.");
                exit(EXIT_FAILURE);
        }
 
@@ -70,28 +72,28 @@ static bool is_interface(const char* dev_name, pid_t pid)
                struct ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
 
                if (!switch_to_ns(pid, "net")) {
-                       fprintf(stderr, "failed to enter netns of container.\n");
-                       exit(-1);
+                       ERROR("Failed to enter netns of container.");
+                       _exit(-1);
                }
 
                /* Grab the list of interfaces */
                if (getifaddrs(&interfaceArray)) {
-                       fprintf(stderr, "failed to get interfaces list\n");
-                       exit(-1);
+                       ERROR("Failed to get interfaces list");
+                       _exit(-1);
                }
 
                /* Iterate through the interfaces */
                for (tempIfAddr = interfaceArray; tempIfAddr != NULL; tempIfAddr = tempIfAddr->ifa_next) {
-                       if (strcmp(tempIfAddr->ifa_name, dev_name) == 0) {
-                               exit(EXIT_SUCCESS);
-                       }
+                       if (!strncmp(tempIfAddr->ifa_name, dev_name, strlen(tempIfAddr->ifa_name)))
+                               _exit(EXIT_SUCCESS);
                }
-               exit(EXIT_FAILURE);
+
+               _exit(EXIT_FAILURE);
        }
 
-       if (wait_for_pid(p) == 0) {
+       if (wait_for_pid(p) == 0)
                return true;
-       }
+
        return false;
 }
 
@@ -103,7 +105,7 @@ int main(int argc, char *argv[])
        bool ret = false;
 
        if (geteuid() != 0) {
-               fprintf(stderr, "%s must be run as root\n", argv[0]);
+               ERROR("%s must be run as root", argv[0]);
                exit(EXIT_FAILURE);
        }
 
@@ -125,30 +127,32 @@ int main(int argc, char *argv[])
 
        c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
        if (!c) {
-               fprintf(stderr, "%s doesn't exist\n", my_args.name);
+               ERROR("%s doesn't exist", my_args.name);
                goto err;
        }
 
        if (my_args.rcfile) {
                c->clear_config(c);
+
                if (!c->load_config(c, my_args.rcfile)) {
-                       fprintf(stderr, "Failed to load rcfile\n");
+                       ERROR("Failed to load rcfile");
                        goto err1;
                }
+
                c->configfile = strdup(my_args.rcfile);
                if (!c->configfile) {
-                       fprintf(stderr, "Out of memory setting new config filename\n");
+                       ERROR("Out of memory setting new config filename");
                        goto err1;
                }
        }
 
        if (!c->is_running(c)) {
-               fprintf(stderr, "Container %s is not running.\n", c->name);
+               ERROR("Container %s is not running.", c->name);
                goto err1;
        }
 
        if (my_args.argc < 2) {
-               fprintf(stderr, "Error: no command given (Please see --help output)\n");
+               ERROR("Error: no command given (Please see --help output)");
                goto err1;
        }
 
@@ -159,33 +163,38 @@ int main(int argc, char *argv[])
        else
                dst_name = my_args.argv[2];
 
-       if (strcmp(cmd, "add") == 0) {
+       if (!strncmp(cmd, "add", strlen(cmd))) {
                if (is_interface(dev_name, 1)) {
                        ret = c->attach_interface(c, dev_name, dst_name);
                } else {
                        ret = c->add_device_node(c, dev_name, dst_name);
                }
+
                if (ret != true) {
-                       fprintf(stderr, "Failed to add %s to %s.\n", dev_name, c->name);
+                       ERROR("Failed to add %s to %s.", dev_name, c->name);
                        goto err1;
                }
-       } else if (strcmp(cmd, "del") == 0) {
+       } else if (!strncmp(cmd, "del", strlen(cmd))) {
                if (is_interface(dev_name, c->init_pid(c))) {
                        ret = c->detach_interface(c, dev_name, dst_name);
                } else {
                        ret = c->remove_device_node(c, dev_name, dst_name);
                }
+
                if (ret != true) {
-                       fprintf(stderr, "Failed to del %s from %s.\n", dev_name, c->name);
+                       ERROR("Failed to del %s from %s.", dev_name, c->name);
                        goto err1;
                }
        } else {
-               fprintf(stderr, "Error: Please use add or del (Please see --help output)\n");
+               ERROR("Error: Please use add or del (Please see --help output)");
                goto err1;
        }
+
        exit(EXIT_SUCCESS);
+
 err1:
        lxc_container_put(c);
+
 err:
        exit(EXIT_FAILURE);
 }
index ea84c428ebd90d9bdf4bd16d5d37f080589384ac..2b00f0d42e70c2213cd229fc6572c21edc21f51d 100644 (file)
@@ -1273,13 +1273,13 @@ bool switch_to_ns(pid_t pid, const char *ns) {
 
        fd = open(nspath, O_RDONLY);
        if (fd < 0) {
-               SYSERROR("failed to open %s", nspath);
+               SYSERROR("Failed to open %s", nspath);
                return false;
        }
 
        ret = setns(fd, 0);
        if (ret) {
-               SYSERROR("failed to set process %d to %s of %d.", pid, ns, fd);
+               SYSERROR("Failed to set process %d to %s of %d.", pid, ns, fd);
                close(fd);
                return false;
        }