jail: elf: Remove MIPS 64 warning
[project/procd.git] / uxc.c
diff --git a/uxc.c b/uxc.c
index a1d7954dadf60a1dd8000a41f6c270cd3a6c432a..d8db49d104a4be2c7ee804711c7e99bbaffdf6ea 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/run/uxc"
 
 static bool verbose = false;
+static bool json_output = false;
+static char *confdir = UXC_ETC_CONFDIR;
 
 struct runtime_state {
        struct avl_node avl;
@@ -65,11 +68,12 @@ enum uxc_cmd {
        CMD_UNKNOWN
 };
 
-#define OPT_ARGS "ab:fm:p:t:vVw:"
+#define OPT_ARGS "ab:fjm:p:t:vVw:"
 static struct option long_options[] = {
        {"autostart",           no_argument,            0,      'a'     },
        {"bundle",              required_argument,      0,      'b'     },
        {"force",               no_argument,            0,      'f'     },
+       {"json",                no_argument,            0,      'j'     },
        {"mounts",              required_argument,      0,      'm'     },
        {"pid-file",            required_argument,      0,      'p'     },
        {"temp-overlay-size",   required_argument,      0,      't'     },
@@ -88,13 +92,13 @@ 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("\tlist [--json]\t\t\t\tlist all configured containers\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("                [--volumes v1,v2,...,vN]\t\trequire volumes to be available\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");
@@ -134,8 +138,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);
@@ -352,13 +362,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;
        }
 
@@ -387,7 +404,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;
@@ -398,10 +423,16 @@ static int uxc_list(void)
        struct blob_attr *cur, *tb[__CONF_MAX], *ts[__STATE_MAX];
        int rem;
        struct runtime_state *s = NULL;
-       char *name;
-       char *ocistatus;
+       char *name, *ocistatus, *status, *tmp;
        int container_pid = -1;
        bool autostart;
+       static struct blob_buf buf;
+       void *arr, *obj;
+
+       if (json_output) {
+               blob_buf_init(&buf, 0);
+               arr = blobmsg_open_array(&buf, "");
+       }
 
        blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
                blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
@@ -420,20 +451,56 @@ static int uxc_list(void)
                        container_pid = blobmsg_get_u32(ts[STATE_PID]);
                }
 
-               printf("[%c] %s %s", autostart?'*':' ', name, ocistatus?:(s && s->running)?"creating":"stopped");
+               status = ocistatus?:(s && s->running)?"creating":"stopped";
 
-               if (s && !s->running && (s->exitcode >= 0))
-                       printf(" exitcode: %d (%s)", s->exitcode, strerror(s->exitcode));
+               if (json_output) {
+                       obj = blobmsg_open_table(&buf, "");
+                       blobmsg_add_string(&buf, "name", name);
+                       blobmsg_add_string(&buf, "status", status);
+                       blobmsg_add_u8(&buf, "autostart", autostart);
+               } else {
+                       printf("[%c] %s %s", autostart?'*':' ', name, status);
+               }
+
+               if (s && !s->running && (s->exitcode >= 0)) {
+                       if (json_output)
+                               blobmsg_add_u32(&buf, "exitcode", s->exitcode);
+                       else
+                               printf(" exitcode: %d (%s)", s->exitcode, strerror(s->exitcode));
+               }
 
-               if (s && s->running && (s->runtime_pid >= 0))
-                       printf(" runtime pid: %d", s->runtime_pid);
+               if (s && s->running && (s->runtime_pid >= 0)) {
+                       if (json_output)
+                               blobmsg_add_u32(&buf, "runtime_pid", s->runtime_pid);
+                       else
+                               printf(" runtime pid: %d", s->runtime_pid);
+               }
 
-               if (s && s->running && (container_pid >= 0))
-                       printf(" container pid: %d", container_pid);
+               if (s && s->running && (container_pid >= 0)) {
+                       if (json_output)
+                               blobmsg_add_u32(&buf, "container_pid", container_pid);
+                       else
+                               printf(" container pid: %d", container_pid);
+               }
 
-               printf("\n");
+               if (!json_output)
+                       printf("\n");
+               else
+                       blobmsg_close_table(&buf, obj);
        }
 
+       if (json_output) {
+               blobmsg_close_array(&buf, arr);
+               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;
 }
 
@@ -449,6 +516,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])
@@ -458,30 +530,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 (s && (s->running))
-               return EEXIST;
+       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 (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);
@@ -506,20 +573,23 @@ 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) ||
                ubus_invoke(ctx, id, "add", req.head, NULL, NULL, 3000)) {
+               blob_buf_free(&req);
                ret = EIO;
        }
 
-       free(jailname);
-       free(path);
-       blob_buf_free(&req);
-
        return ret;
 }
 
@@ -630,12 +700,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);
@@ -643,12 +713,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);
@@ -678,13 +748,14 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
                }
                blobmsg_close_array(&req, mntarr);
        }
-
-       dprintf(f, "%s\n", blobmsg_format_json_indent(req.head, true, 0));
-
-       if (!add)
-               free(keeppath);
+       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;
 }
@@ -975,6 +1046,10 @@ int main(int argc, char **argv)
                                force = true;
                                break;
 
+                       case 'j':
+                               json_output = true;
+                               break;
+
                        case 'p':
                                pidfile = optarg;
                                break;