uxc: free string returned by blobmsg_format_json_indent()
[project/procd.git] / uxc.c
diff --git a/uxc.c b/uxc.c
index 210084dcd8fc1855484560f8b164cad4dedd87ac..3af4fa99bfcd87508753137aa7b23bfee71b8b42 100644 (file)
--- a/uxc.c
+++ b/uxc.c
 
 #define UXC_VERSION "0.2"
 #define OCI_VERSION_STRING "1.0.2"
-#define UXC_CONFDIR "/etc/uxc"
+#define UXC_ETC_CONFDIR "/etc/uxc"
+#define UXC_VOL_CONFDIR "/var/state/uxc"
 
 static bool verbose = false;
+static char *confdir = UXC_ETC_CONFDIR;
 
 struct runtime_state {
        struct avl_node avl;
@@ -65,11 +67,12 @@ enum uxc_cmd {
        CMD_UNKNOWN
 };
 
-#define OPT_ARGS "ab:fp:t:vVw:"
+#define OPT_ARGS "ab:fm:p:t:vVw:"
 static struct option long_options[] = {
        {"autostart",           no_argument,            0,      'a'     },
        {"bundle",              required_argument,      0,      'b'     },
        {"force",               no_argument,            0,      'f'     },
+       {"mounts",              required_argument,      0,      'm'     },
        {"pid-file",            required_argument,      0,      'p'     },
        {"temp-overlay-size",   required_argument,      0,      't'     },
        {"write-overlay-path",  required_argument,      0,      'w'     },
@@ -80,13 +83,20 @@ static struct option long_options[] = {
 
 AVL_TREE(runtime, avl_strcmp, false, NULL);
 static struct blob_buf conf;
+static struct blob_attr *blockinfo;
+static struct blob_attr *fstabinfo;
 static struct ubus_context *ctx;
 
 static int usage(void) {
        printf("syntax: uxc <command> [parameters ...]\n");
        printf("commands:\n");
        printf("\tlist\t\t\t\t\t\tlist all configured containers\n");
-       printf("\tcreate <conf> [--bundle <path>] [--autostart]\tcreate <conf> for OCI bundle at <path>\n");
+       printf("\tcreate <conf>\t\t\t\t\t(re-)create <conf>\n");
+       printf("                [--bundle <path>]\t\t\tOCI bundle at <path>\n");
+       printf("                [--autostart]\t\t\t\tstart on boot\n");
+       printf("                [--temp-overlay-size size]\t\tuse tmpfs overlay with {size}\n");
+       printf("                [--write-overlay-path path]\t\tuse overlay on {path}\n");
+       printf("                [--mounts v1,v2,...,vN]\t\trequire filesystems to be available\n");
        printf("\tstart <conf>\t\t\t\t\tstart container <conf>\n");
        printf("\tstate <conf>\t\t\t\t\tget state of container <conf>\n");
        printf("\tkill <conf> [<signal>]\t\t\t\tsend signal to container <conf>\n");
@@ -104,6 +114,7 @@ enum {
        CONF_PIDFILE,
        CONF_TEMP_OVERLAY_SIZE,
        CONF_WRITE_OVERLAY_PATH,
+       CONF_VOLUMES,
        __CONF_MAX,
 };
 
@@ -115,6 +126,7 @@ static const struct blobmsg_policy conf_policy[__CONF_MAX] = {
        [CONF_PIDFILE] = { .name = "pidfile", .type = BLOBMSG_TYPE_STRING },
        [CONF_TEMP_OVERLAY_SIZE] = { .name = "temp-overlay-size", .type = BLOBMSG_TYPE_STRING },
        [CONF_WRITE_OVERLAY_PATH] = { .name = "write-overlay-path", .type = BLOBMSG_TYPE_STRING },
+       [CONF_VOLUMES] = { .name = "volumes", .type = BLOBMSG_TYPE_ARRAY },
 };
 
 static int conf_load(void)
@@ -124,8 +136,14 @@ static int conf_load(void)
        glob_t gl;
        char *globstr;
        void *c, *o;
+       struct stat sb;
+
+       if (!stat(UXC_VOL_CONFDIR, &sb)) {
+               if (sb.st_mode & S_IFDIR)
+                       confdir = UXC_VOL_CONFDIR;
+       }
 
-       if (asprintf(&globstr, "%s/*.json", UXC_CONFDIR) == -1)
+       if (asprintf(&globstr, "%s/*.json", confdir) == -1)
                return ENOMEM;
 
        blob_buf_init(&conf, 0);
@@ -238,7 +256,9 @@ static void get_ocistate(struct blob_attr **ocistate, const char *name)
        int ret;
        *ocistate = NULL;
 
-       asprintf(&objname, "container.%s", name);
+       if (asprintf(&objname, "container.%s", name) == -1)
+               exit(ENOMEM);
+
        ret = ubus_lookup_id(ctx, objname, &id);
        free(objname);
        if (ret)
@@ -340,13 +360,20 @@ static int uxc_state(char *name)
        int rem;
        char *bundle = NULL;
        char *jail_name = NULL;
+       char *state = NULL;
+       char *tmp;
        static struct blob_buf buf;
 
        if (s)
                ocistate = s->ocistate;
 
        if (ocistate) {
-               printf("%s\n", blobmsg_format_json_indent(ocistate, true, 0));
+               state = blobmsg_format_json_indent(ocistate, true, 0);
+               if (!state)
+                       return 1;
+
+               printf("%s\n", state);
+               free(state);
                return 0;
        }
 
@@ -375,7 +402,15 @@ static int uxc_state(char *name)
        blobmsg_add_string(&buf, "status", s?"stopped":"uninitialized");
        blobmsg_add_string(&buf, "bundle", bundle);
 
-       printf("%s\n", blobmsg_format_json_indent(buf.head, true, 0));
+       tmp = blobmsg_format_json_indent(buf.head, true, 0);
+       if (!tmp) {
+               blob_buf_free(&buf);
+               return ENOMEM;
+       }
+
+       printf("%s\n", tmp);
+       free(tmp);
+
        blob_buf_free(&buf);
 
        return 0;
@@ -437,6 +472,11 @@ static int uxc_create(char *name, bool immediately)
        void *in, *ins, *j;
        bool found = false;
 
+       s = avl_find_element(&runtime, name, s, avl);
+
+       if (s && (s->running))
+               return EEXIST;
+
        blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
                blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
                if (!tb[CONF_NAME] || !tb[CONF_PATH])
@@ -446,30 +486,25 @@ static int uxc_create(char *name, bool immediately)
                        continue;
 
                found = true;
-               path = strdup(blobmsg_get_string(tb[CONF_PATH]));
-
-               if (tb[CONF_PIDFILE])
-                       pidfile = strdup(blobmsg_get_string(tb[CONF_PIDFILE]));
-
-               if (tb[CONF_TEMP_OVERLAY_SIZE])
-                       tmprwsize = strdup(blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]));
-
-               if (tb[CONF_WRITE_OVERLAY_PATH])
-                       writepath = strdup(blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]));
-
-               break;
        }
 
        if (!found)
                return ENOENT;
 
-       s = avl_find_element(&runtime, name, s, avl);
+       path = blobmsg_get_string(tb[CONF_PATH]);
+
+       if (tb[CONF_PIDFILE])
+               pidfile = blobmsg_get_string(tb[CONF_PIDFILE]);
+
+       if (tb[CONF_TEMP_OVERLAY_SIZE])
+               tmprwsize = blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]);
+
+       if (tb[CONF_WRITE_OVERLAY_PATH])
+               writepath = blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]);
 
-       if (s && (s->running))
-               return EEXIST;
 
        if (tb[CONF_JAIL])
-               jailname = strdup(blobmsg_get_string(tb[CONF_JAIL]));
+               jailname = blobmsg_get_string(tb[CONF_JAIL]);
 
        blob_buf_init(&req, 0);
        blobmsg_add_string(&req, "name", name);
@@ -494,9 +529,15 @@ static int uxc_create(char *name, bool immediately)
        blobmsg_close_table(&req, in);
        blobmsg_close_table(&req, ins);
 
-       if (verbose)
-               fprintf(stderr, "adding container to procd:\n\t%s\n",
-                       blobmsg_format_json_indent(req.head, true, 1));
+       if (verbose) {
+               char *tmp;
+               tmp = blobmsg_format_json_indent(req.head, true, 1);
+               if (!tmp)
+                       return ENOMEM;
+
+               fprintf(stderr, "adding container to procd:\n\t%s\n", tmp);
+               free(tmp);
+       }
 
        ret = 0;
        if (ubus_lookup_id(ctx, "container", &id) ||
@@ -516,7 +557,9 @@ static int uxc_start(const char *name)
        char *objname;
        unsigned int id;
 
-       asprintf(&objname, "container.%s", name);
+       if (asprintf(&objname, "container.%s", name) == -1)
+               return ENOMEM;
+
        if (ubus_lookup_id(ctx, objname, &id))
                return ENOENT;
 
@@ -557,7 +600,9 @@ static int uxc_kill(char *name, int signal)
        blobmsg_add_u32(&req, "signal", signal);
        blobmsg_add_string(&req, "name", name);
 
-       asprintf(&objname, "container.%s", name);
+       if (asprintf(&objname, "container.%s", name) == -1)
+               return ENOMEM;
+
        ret = ubus_lookup_id(ctx, objname, &id);
        free(objname);
        if (ret)
@@ -570,7 +615,7 @@ static int uxc_kill(char *name, int signal)
 }
 
 
-static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfile, char *_tmprwsize, char *_writepath)
+static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfile, char *_tmprwsize, char *_writepath, char *requiredmounts)
 {
        static struct blob_buf req;
        struct blob_attr *cur, *tb[__CONF_MAX];
@@ -580,7 +625,8 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
        char *keeppath = NULL;
        char *tmprwsize = _tmprwsize;
        char *writepath = _writepath;
-
+       char *curvol, *tmp, *mnttok;
+       void *mntarr;
        int f;
        struct stat sb;
 
@@ -613,12 +659,12 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
                        return ENOTDIR;
        }
 
-       ret = mkdir(UXC_CONFDIR, 0755);
+       ret = mkdir(confdir, 0755);
 
        if (ret && errno != EEXIST)
                return ret;
 
-       if (asprintf(&fname, "%s/%s.json", UXC_CONFDIR, name) < 1)
+       if (asprintf(&fname, "%s/%s.json", confdir, name) == -1)
                return ENOMEM;
 
        f = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
@@ -626,12 +672,12 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
                return errno;
 
        if (!add) {
-               keeppath = strdup(blobmsg_get_string(tb[CONF_PATH]));
+               keeppath = blobmsg_get_string(tb[CONF_PATH]);
                if (tb[CONF_WRITE_OVERLAY_PATH])
-                       writepath = strdup(blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]));
+                       writepath = blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]);
 
                if (tb[CONF_TEMP_OVERLAY_SIZE])
-                       tmprwsize = strdup(blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]));
+                       tmprwsize = blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]);
        }
 
        blob_buf_init(&req, 0);
@@ -647,27 +693,183 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
        if (writepath)
                blobmsg_add_string(&req, "write-overlay-path", writepath);
 
-       dprintf(f, "%s\n", blobmsg_format_json_indent(req.head, true, 0));
+       if (!add && tb[CONF_VOLUMES])
+               blobmsg_add_blob(&req, tb[CONF_VOLUMES]);
+
+       if (add && requiredmounts) {
+               mntarr = blobmsg_open_array(&req, "volumes");
+               for (mnttok = requiredmounts; ; mnttok = NULL) {
+                       curvol = strtok_r(mnttok, ",;", &tmp);
+                       if (!curvol)
+                               break;
 
-       if (!add)
-               free(keeppath);
+                       blobmsg_add_string(&req, NULL, curvol);
+               }
+               blobmsg_close_array(&req, mntarr);
+       }
+       tmp = blobmsg_format_json_indent(req.head, true, 0);
+       if (tmp) {
+               dprintf(f, "%s\n", tmp);
+               free(tmp);
+       }
 
        blob_buf_free(&req);
+       close(f);
 
        return 0;
 }
 
+enum {
+       BLOCK_INFO_DEVICE,
+       BLOCK_INFO_UUID,
+       BLOCK_INFO_TARGET,
+       BLOCK_INFO_TYPE,
+       BLOCK_INFO_MOUNT,
+       __BLOCK_INFO_MAX,
+};
+
+static const struct blobmsg_policy block_info_policy[__BLOCK_INFO_MAX] = {
+       [BLOCK_INFO_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
+       [BLOCK_INFO_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
+       [BLOCK_INFO_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
+       [BLOCK_INFO_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
+       [BLOCK_INFO_MOUNT] = { .name = "mount", .type = BLOBMSG_TYPE_STRING },
+};
+
+
+/* check if device 'devname' is mounted according to blockd */
+static int checkblock(const char *uuid)
+{
+       struct blob_attr *tb[__BLOCK_INFO_MAX];
+       struct blob_attr *cur;
+       int rem;
+
+       blobmsg_for_each_attr(cur, blockinfo, rem) {
+               blobmsg_parse(block_info_policy, __BLOCK_INFO_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
+
+               if (!tb[BLOCK_INFO_UUID] || !tb[BLOCK_INFO_MOUNT])
+                       continue;
+
+               if (!strcmp(uuid, blobmsg_get_string(tb[BLOCK_INFO_UUID])))
+                       return 0;
+       }
+
+       return 1;
+}
+
+enum {
+       UCI_FSTAB_UUID,
+       UCI_FSTAB_ANONYMOUS,
+       __UCI_FSTAB_MAX,
+};
+
+static const struct blobmsg_policy uci_fstab_policy[__UCI_FSTAB_MAX] = {
+       [UCI_FSTAB_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
+       [UCI_FSTAB_ANONYMOUS] = { .name = ".anonymous", .type = BLOBMSG_TYPE_BOOL },
+};
+
+static const char *resolveuuid(const char *volname)
+{
+       struct blob_attr *tb[__UCI_FSTAB_MAX];
+       struct blob_attr *cur;
+       const char *mntname;
+       char *tmpvolname, *replc;
+       int rem, res;
+
+       blobmsg_for_each_attr(cur, fstabinfo, rem) {
+               blobmsg_parse(uci_fstab_policy, __UCI_FSTAB_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
+
+               if (!tb[UCI_FSTAB_UUID])
+                       continue;
+
+               if (tb[UCI_FSTAB_ANONYMOUS] && blobmsg_get_bool(tb[UCI_FSTAB_ANONYMOUS]))
+                       continue;
+
+               mntname = blobmsg_name(cur);
+               if (!mntname)
+                       continue;
+
+               tmpvolname = strdup(volname);
+               while ((replc = strchr(tmpvolname, '-')))
+                       *replc = '_';
+
+               res = strcmp(tmpvolname, mntname);
+               free(tmpvolname);
+
+               if (!res)
+                       return blobmsg_get_string(tb[UCI_FSTAB_UUID]);
+       };
+
+       return volname;
+};
+
+/* check status of each required volume */
+static int checkvolumes(struct blob_attr *volumes)
+{
+       struct blob_attr *cur;
+       int rem;
+
+       blobmsg_for_each_attr(cur, volumes, rem) {
+               if (checkblock(resolveuuid(blobmsg_get_string(cur))))
+                       return 1;
+       }
+
+       return 0;
+}
+
+static void block_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       blockinfo = blob_memdup(blobmsg_data(msg));
+}
+
+static void fstab_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       fstabinfo = blob_memdup(blobmsg_data(msg));
+}
+
 static int uxc_boot(void)
 {
        struct blob_attr *cur, *tb[__CONF_MAX];
+       struct runtime_state *s;
+       static struct blob_buf req;
        int rem, ret = 0;
        char *name;
+       unsigned int id;
+
+       ret = ubus_lookup_id(ctx, "block", &id);
+       if (ret)
+               return ENOENT;
+
+       ret = ubus_invoke(ctx, id, "info", NULL, block_cb, NULL, 3000);
+       if (ret)
+               return ENXIO;
+
+       ret = ubus_lookup_id(ctx, "uci", &id);
+       if (ret)
+               return ENOENT;
+
+       blob_buf_init(&req, 0);
+       blobmsg_add_string(&req, "config", "fstab");
+       blobmsg_add_string(&req, "type", "mount");
+
+       ret = ubus_invoke(ctx, id, "get", req.head, fstab_cb, NULL, 3000);
+       if (ret)
+               return ENXIO;
 
        blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
                blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
                if (!tb[CONF_NAME] || !tb[CONF_PATH] || !tb[CONF_AUTOSTART] || !blobmsg_get_bool(tb[CONF_AUTOSTART]))
                        continue;
 
+               s = avl_find_element(&runtime, blobmsg_get_string(tb[CONF_NAME]), s, avl);
+               if (s)
+                       continue;
+
+               /* make sure all volumes are ready before starting */
+               if (tb[CONF_VOLUMES])
+                       if (checkvolumes(tb[CONF_VOLUMES]))
+                               continue;
+
                name = strdup(blobmsg_get_string(tb[CONF_NAME]));
                ret += uxc_create(name, true);
                free(name);
@@ -763,6 +965,7 @@ int main(int argc, char **argv)
        char *pidfile = NULL;
        char *tmprwsize = NULL;
        char *writepath = NULL;
+       char *requiredmounts = NULL;
        bool autostart = false;
        bool force = false;
        int signal = SIGTERM;
@@ -821,6 +1024,10 @@ int main(int argc, char **argv)
                        case 'w':
                                writepath = optarg;
                                break;
+
+                       case 'm':
+                               requiredmounts = optarg;
+                               break;
                }
        }
 
@@ -882,14 +1089,14 @@ int main(int argc, char **argv)
                        if (optind != argc - 2)
                                goto usage_out;
 
-                       ret = uxc_set(argv[optind + 1], NULL, true, false, NULL, NULL, NULL);
+                       ret = uxc_set(argv[optind + 1], NULL, true, false, NULL, NULL, NULL, NULL);
                        break;
 
                case CMD_DISABLE:
                        if (optind != argc - 2)
                                goto usage_out;
 
-                       ret = uxc_set(argv[optind + 1], NULL, false, false, NULL, NULL, NULL);
+                       ret = uxc_set(argv[optind + 1], NULL, false, false, NULL, NULL, NULL, NULL);
                        break;
 
                case CMD_DELETE:
@@ -904,7 +1111,7 @@ int main(int argc, char **argv)
                                goto usage_out;
 
                        if (bundle) {
-                               ret = uxc_set(argv[optind + 1], bundle, autostart, true, pidfile, tmprwsize, writepath);
+                               ret = uxc_set(argv[optind + 1], bundle, autostart, true, pidfile, tmprwsize, writepath, requiredmounts);
                                if (ret)
                                        goto runtime_out;