Fixed defect for insecure-regisitries setting 03/168203/1
authornshero.lee <nshero.lee@samsung.com>
Thu, 25 Jan 2018 03:57:00 +0000 (12:57 +0900)
committernshero.lee <nshero.lee@samsung.com>
Thu, 25 Jan 2018 03:57:00 +0000 (12:57 +0900)
The string of insecure-registries has to be handle with double pointer.
Due to wrong handlin of duble pointer, the parsing is not be able to do properly.
As well as '/etc/docker' folder is not present in rootfs.
When the folder is not present, setup-adaptor will creat the folder and write daemon.json
with insecure-registries.

Change-Id: Id54edad3698476b4f8bf82dd09eab309c5a5aa02
Signed-off-by: nshero.lee <nshero.lee@samsung.com>
packaging/setup-adaptor.spec
src/input_file.c
src/sa_types.h

index 3b758f069c3757bca098e5ede3e6c609d74965f4..bb6d805ba48546fbfefffcb1e8d9c3f33b596d99 100644 (file)
@@ -1,6 +1,6 @@
 Name:       setup-adaptor
 Summary:    Provide network and system data setting daemon
-Version:    0.4.0
+Version:    0.4.1
 Release:    0
 Group:      System & System Tools
 ExclusiveArch: armv7l
index 582ee2cfe7b168469c901f3f3b2b8004097ff3bc..40040243076a7637b292ec38f1114230ab773e2a 100644 (file)
@@ -20,6 +20,8 @@
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/inotify.h>
+#include <sys/stat.h>
+
 #include <fcntl.h>
 #include <json.h>
 #include <errno.h>
@@ -33,6 +35,7 @@
 
 #define ENVIRONMENT_PROXY_FILE "/etc/dockzen/environment"
 #define CONFIG_DOCKERD_DAEMON_FILE "/etc/docker/daemon.json"
+#define CONFIG_DOCKERD_FOLDER "/etc/docker"
 
 #define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed"
 #define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed"
@@ -485,25 +488,31 @@ static int __parse_system_insecure_registries(json_object * inputObj, sa_dockerd
                return -1;
        }
 
-       opt->count = json_object_array_length(inputObj);;
+       opt->count = json_object_array_length(inputObj);
        if (opt->count > 0) {
-               for (ulIndex = 0; ulIndex < opt->count; ulIndex++) {
-                       registryObj = json_object_array_get_idx(inputObj, ulIndex);
-                       if (registryObj != NULL) {
-                               value = json_object_get_string(registryObj);
-                               _D("value[%d]: %s\n", ulIndex, value);
-                               if (value != NULL) {
-                                       opt->insecureRegistries[ulIndex] = (char *) malloc(sizeof(char) * 255 + 1);
-                                       if (opt->insecureRegistries[ulIndex] != NULL) {
-                                               memcpy(opt->insecureRegistries[ulIndex], value, strlen(value)+1);
-                                               _D("insecure [%d]: %s", ulIndex, opt->insecureRegistries[ulIndex]);
-                                       } else {
-                                               ret = -1;
-                                               _E("malloc failed for insecureRegistires");
-                                               break;
+               opt->insecureRegistries = (char **) malloc(sizeof(char *) * opt->count);
+               if (opt->insecureRegistries != NULL) {
+                       for (ulIndex = 0; ulIndex < opt->count; ulIndex++) {
+                               registryObj = json_object_array_get_idx(inputObj, ulIndex);
+                               if (registryObj != NULL) {
+                                       value = json_object_get_string(registryObj);
+                                       _D("value[%d]: %s\n", ulIndex, value);
+                                       if (value != NULL) {
+                                               opt->insecureRegistries[ulIndex] = (char *) malloc(strlen(value) + 1);
+                                               if (opt->insecureRegistries[ulIndex] != NULL) {
+                                                       memcpy(opt->insecureRegistries[ulIndex], value, strlen(value) + 1);
+                                                       _D("insecure [%d]: %s", ulIndex, opt->insecureRegistries[ulIndex]);
+                                               } else {
+                                                       ret = -1;
+                                                       _E("malloc failed for insecureRegistries second");
+                                                       break;
+                                               }
                                        }
                                }
                        }
+               } else {
+                       ret = -1;
+                       _E("malloc failed for inscureRegistries first");
                }
        }
 
@@ -542,8 +551,10 @@ static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig
                                setupConfig->systemData->dockerdOpt = (sa_dockerd_opt_s *) malloc(sizeof(sa_dockerd_opt_s));
                                if (setupConfig->systemData->dockerdOpt != NULL)
                                        ret = __parse_system_insecure_registries(arrayObj, setupConfig->systemData->dockerdOpt);
-                               else
+                               else {
                                        _E("malloc failed setupConfig->systemData->dockerdOpt");
+                                       ret = -1;
+                               }
                        } else {
                                _E("The type of insecure registries is wrong");
                                ret = -1;
@@ -726,6 +737,10 @@ static void release_system_resource(sa_system_s * system)
                                if (system->dockerdOpt->insecureRegistries[i] != NULL)
                                        free(system->dockerdOpt->insecureRegistries[i]);
                        }
+
+                       if (system->dockerdOpt->insecureRegistries != NULL)
+                               free(system->dockerdOpt->insecureRegistries);
+
                        free(system->dockerdOpt);
                }
 
@@ -916,6 +931,7 @@ sa_error_e sa_inputfile_set_dockerd_opt(sa_dockerd_opt_s * opt)
        int len = 0;
        int buf_len = 0;
        sa_error_e ret = SA_ERROR_NONE;
+       struct stat sb = {0,};
        char *buf = NULL;
 
 
@@ -927,14 +943,20 @@ sa_error_e sa_inputfile_set_dockerd_opt(sa_dockerd_opt_s * opt)
        // make string to write in daemon.json
        buf_len = __create_dockerd_opt(opt, &buf);
        if (buf_len > 0) {
+               // check folder
+               if (stat(CONFIG_DOCKERD_FOLDER, &sb) == -1) {
+                       _D("/etc/docker Folder Create");
+                       mkdir(CONFIG_DOCKERD_FOLDER, 0644);
+               }
+
                //write data into file
-               fd = open(CONFIG_DOCKERD_DAEMON_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+               fd = creat(CONFIG_DOCKERD_DAEMON_FILE, 0644);
                if (fd > 0) {
                        write(fd, buf, strlen(buf));
                        close(fd);
                } else {
                        ret = SA_ERROR_NOT_AVAILABLE;
-                       _E("Cannot open file");
+                       _E("Cannot create file");
                }
 
                if (buf != NULL)
index 595077b25192e5b19f90051d88ce9f0b825404c9..187eeaae928a337bdafd2b722000f6d03e34d3c0 100644 (file)
@@ -192,7 +192,7 @@ typedef struct {
  */
 typedef struct {
        int count;
-       char *insecureRegistries[];
+       char **insecureRegistries;
 } sa_dockerd_opt_s;