initd: fix off-by-one error in mkdev.c
[project/procd.git] / uxc.c
diff --git a/uxc.c b/uxc.c
index 026fc7e645c123e874e3e814e3fb346c126fc6f6..50262a6db2ffb67789771ac88c2d737149836183 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 char *confdir = UXC_ETC_CONFDIR;
 
 struct runtime_state {
        struct avl_node avl;
@@ -94,7 +96,7 @@ static int usage(void) {
        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 +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);
@@ -353,6 +361,7 @@ static int uxc_state(char *name)
        char *bundle = NULL;
        char *jail_name = NULL;
        char *state = NULL;
+       char *tmp;
        static struct blob_buf buf;
 
        if (s)
@@ -393,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;
@@ -642,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);
@@ -697,6 +714,7 @@ static int uxc_set(char *name, char *path, bool autostart, bool add, char *pidfi
        }
 
        blob_buf_free(&req);
+       close(f);
 
        return 0;
 }