2 * Copyright (C) 2020 Daniel Golle <daniel@makrotopia.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
22 #include <libubox/avl-cmp.h>
23 #include <libubox/blobmsg.h>
24 #include <libubox/blobmsg_json.h>
32 #include <sys/types.h>
38 #define UXC_VERSION "0.2"
39 #define OCI_VERSION_STRING "1.0.2"
40 #define UXC_CONFDIR "/etc/uxc"
42 static bool verbose
= false;
44 struct runtime_state
{
52 struct blob_attr
*ocistate
;
68 #define OPT_ARGS "ab:fm:p:t:vVw:"
69 static struct option long_options
[] = {
70 {"autostart", no_argument
, 0, 'a' },
71 {"bundle", required_argument
, 0, 'b' },
72 {"force", no_argument
, 0, 'f' },
73 {"mounts", required_argument
, 0, 'm' },
74 {"pid-file", required_argument
, 0, 'p' },
75 {"temp-overlay-size", required_argument
, 0, 't' },
76 {"write-overlay-path", required_argument
, 0, 'w' },
77 {"verbose", no_argument
, 0, 'v' },
78 {"version", no_argument
, 0, 'V' },
82 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
83 static struct blob_buf conf
;
84 static struct blob_attr
*blockinfo
;
85 static struct blob_attr
*fstabinfo
;
86 static struct ubus_context
*ctx
;
88 static int usage(void) {
89 printf("syntax: uxc <command> [parameters ...]\n");
90 printf("commands:\n");
91 printf("\tlist\t\t\t\t\t\tlist all configured containers\n");
92 printf("\tcreate <conf>\t\t\t\t\t(re-)create <conf>\n");
93 printf(" [--bundle <path>]\t\t\tOCI bundle at <path>\n");
94 printf(" [--autostart]\t\t\t\tstart on boot\n");
95 printf(" [--temp-overlay-size size]\t\tuse tmpfs overlay with {size}\n");
96 printf(" [--write-overlay-path path]\t\tuse overlay on {path}\n");
97 printf(" [--volumes v1,v2,...,vN]\t\trequire volumes to be available\n");
98 printf("\tstart <conf>\t\t\t\t\tstart container <conf>\n");
99 printf("\tstate <conf>\t\t\t\t\tget state of container <conf>\n");
100 printf("\tkill <conf> [<signal>]\t\t\t\tsend signal to container <conf>\n");
101 printf("\tenable <conf>\t\t\t\t\tstart container <conf> on boot\n");
102 printf("\tdisable <conf>\t\t\t\t\tdon't start container <conf> on boot\n");
103 printf("\tdelete <conf> [--force]\t\t\t\tdelete <conf>\n");
113 CONF_TEMP_OVERLAY_SIZE
,
114 CONF_WRITE_OVERLAY_PATH
,
119 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
120 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
121 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
122 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
123 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
124 [CONF_PIDFILE
] = { .name
= "pidfile", .type
= BLOBMSG_TYPE_STRING
},
125 [CONF_TEMP_OVERLAY_SIZE
] = { .name
= "temp-overlay-size", .type
= BLOBMSG_TYPE_STRING
},
126 [CONF_WRITE_OVERLAY_PATH
] = { .name
= "write-overlay-path", .type
= BLOBMSG_TYPE_STRING
},
127 [CONF_VOLUMES
] = { .name
= "volumes", .type
= BLOBMSG_TYPE_ARRAY
},
130 static int conf_load(void)
132 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
138 if (asprintf(&globstr
, "%s/*.json", UXC_CONFDIR
) == -1)
141 blob_buf_init(&conf
, 0);
142 c
= blobmsg_open_table(&conf
, NULL
);
144 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
149 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
150 o
= blobmsg_open_table(&conf
, strdup(gl
.gl_pathv
[j
]));
151 if (!blobmsg_add_json_from_file(&conf
, gl
.gl_pathv
[j
])) {
152 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
155 blobmsg_close_table(&conf
, o
);
157 blobmsg_close_table(&conf
, c
);
168 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
169 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
180 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
181 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
182 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
183 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
184 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
192 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
193 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
196 static struct runtime_state
*
197 runtime_alloc(const char *container_name
)
199 struct runtime_state
*s
;
201 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
202 strcpy(new_name
, container_name
);
203 s
->container_name
= new_name
;
204 s
->avl
.key
= s
->container_name
;
218 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
219 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
220 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
221 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
222 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
223 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
224 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
228 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
230 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
231 struct blob_attr
*tb
[__STATE_MAX
];
233 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
235 if (!tb
[STATE_OCIVERSION
] ||
241 *ocistate
= blob_memdup(msg
);
244 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
251 if (asprintf(&objname
, "container.%s", name
) == -1)
254 ret
= ubus_lookup_id(ctx
, objname
, &id
);
259 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
262 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
264 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
266 const char *container_name
, *instance_name
, *jail_name
;
269 struct runtime_state
*rs
;
271 blobmsg_for_each_attr(cur
, msg
, rem
) {
272 container_name
= blobmsg_name(cur
);
273 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
274 if (!tl
[LIST_INSTANCES
])
277 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
278 instance_name
= blobmsg_name(curi
);
279 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
281 if (!ti
[INSTANCE_JAIL
])
284 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
288 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
290 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
292 if (ti
[INSTANCE_PID
])
293 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
297 if (ti
[INSTANCE_EXITCODE
])
298 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
302 rs
= runtime_alloc(container_name
);
303 rs
->instance_name
= strdup(instance_name
);
304 rs
->jail_name
= strdup(jail_name
);
305 rs
->runtime_pid
= pid
;
306 rs
->exitcode
= exitcode
;
307 rs
->running
= running
;
308 avl_insert(&runtime
, &rs
->avl
);
315 static int runtime_load(void)
317 struct runtime_state
*item
, *tmp
;
320 avl_init(&runtime
, avl_strcmp
, false, NULL
);
321 if (ubus_lookup_id(ctx
, "container", &id
) ||
322 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
326 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
327 get_ocistate(&item
->ocistate
, item
->jail_name
);
332 static void runtime_free(void)
334 struct runtime_state
*item
, *tmp
;
336 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
337 avl_delete(&runtime
, &item
->avl
);
338 free(item
->instance_name
);
339 free(item
->jail_name
);
340 free(item
->ocistate
);
347 static int uxc_state(char *name
)
349 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
350 struct blob_attr
*ocistate
= NULL
;
351 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
354 char *jail_name
= NULL
;
355 static struct blob_buf buf
;
358 ocistate
= s
->ocistate
;
361 printf("%s\n", blobmsg_format_json_indent(ocistate
, true, 0));
365 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
366 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
367 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
370 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
372 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
376 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
384 blob_buf_init(&buf
, 0);
385 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
386 blobmsg_add_string(&buf
, "id", jail_name
);
387 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
388 blobmsg_add_string(&buf
, "bundle", bundle
);
390 printf("%s\n", blobmsg_format_json_indent(buf
.head
, true, 0));
396 static int uxc_list(void)
398 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
400 struct runtime_state
*s
= NULL
;
403 int container_pid
= -1;
406 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
407 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
408 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
411 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
414 name
= blobmsg_get_string(tb
[CONF_NAME
]);
415 s
= avl_find_element(&runtime
, name
, s
, avl
);
417 if (s
&& s
->ocistate
) {
418 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
419 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
420 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
423 printf("[%c] %s %s", autostart
?'*':' ', name
, ocistatus
?:(s
&& s
->running
)?"creating":"stopped");
425 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
426 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
428 if (s
&& s
->running
&& (s
->runtime_pid
>= 0))
429 printf(" runtime pid: %d", s
->runtime_pid
);
431 if (s
&& s
->running
&& (container_pid
>= 0))
432 printf(" container pid: %d", container_pid
);
440 static int uxc_create(char *name
, bool immediately
)
442 static struct blob_buf req
;
443 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
446 struct runtime_state
*s
= NULL
;
447 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
452 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
453 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
454 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
457 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
461 path
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
463 if (tb
[CONF_PIDFILE
])
464 pidfile
= strdup(blobmsg_get_string(tb
[CONF_PIDFILE
]));
466 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
467 tmprwsize
= strdup(blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]));
469 if (tb
[CONF_WRITE_OVERLAY_PATH
])
470 writepath
= strdup(blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]));
478 s
= avl_find_element(&runtime
, name
, s
, avl
);
480 if (s
&& (s
->running
))
484 jailname
= strdup(blobmsg_get_string(tb
[CONF_JAIL
]));
486 blob_buf_init(&req
, 0);
487 blobmsg_add_string(&req
, "name", name
);
488 ins
= blobmsg_open_table(&req
, "instances");
489 in
= blobmsg_open_table(&req
, name
);
490 blobmsg_add_string(&req
, "bundle", path
);
491 j
= blobmsg_open_table(&req
, "jail");
492 blobmsg_add_string(&req
, "name", jailname
?:name
);
493 blobmsg_add_u8(&req
, "immediately", immediately
);
496 blobmsg_add_string(&req
, "pidfile", pidfile
);
498 blobmsg_close_table(&req
, j
);
501 blobmsg_add_string(&req
, "overlaydir", writepath
);
504 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
506 blobmsg_close_table(&req
, in
);
507 blobmsg_close_table(&req
, ins
);
510 fprintf(stderr
, "adding container to procd:\n\t%s\n",
511 blobmsg_format_json_indent(req
.head
, true, 1));
514 if (ubus_lookup_id(ctx
, "container", &id
) ||
515 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
526 static int uxc_start(const char *name
)
531 if (asprintf(&objname
, "container.%s", name
) == -1)
534 if (ubus_lookup_id(ctx
, objname
, &id
))
537 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
540 static int uxc_kill(char *name
, int signal
)
542 static struct blob_buf req
;
543 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
547 struct runtime_state
*s
= NULL
;
550 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
551 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
552 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
555 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
565 s
= avl_find_element(&runtime
, name
, s
, avl
);
567 if (!s
|| !(s
->running
))
570 blob_buf_init(&req
, 0);
571 blobmsg_add_u32(&req
, "signal", signal
);
572 blobmsg_add_string(&req
, "name", name
);
574 if (asprintf(&objname
, "container.%s", name
) == -1)
577 ret
= ubus_lookup_id(ctx
, objname
, &id
);
582 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
589 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
591 static struct blob_buf req
;
592 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
596 char *keeppath
= NULL
;
597 char *tmprwsize
= _tmprwsize
;
598 char *writepath
= _writepath
;
599 char *curvol
, *tmp
, *mnttok
;
604 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
605 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
606 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
609 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
626 if (stat(path
, &sb
) == -1)
629 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
633 ret
= mkdir(UXC_CONFDIR
, 0755);
635 if (ret
&& errno
!= EEXIST
)
638 if (asprintf(&fname
, "%s/%s.json", UXC_CONFDIR
, name
) == -1)
641 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
646 keeppath
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
647 if (tb
[CONF_WRITE_OVERLAY_PATH
])
648 writepath
= strdup(blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]));
650 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
651 tmprwsize
= strdup(blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]));
654 blob_buf_init(&req
, 0);
655 blobmsg_add_string(&req
, "name", name
);
656 blobmsg_add_string(&req
, "path", path
?:keeppath
);
657 blobmsg_add_u8(&req
, "autostart", autostart
);
659 blobmsg_add_string(&req
, "pidfile", pidfile
);
662 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
665 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
667 if (!add
&& tb
[CONF_VOLUMES
])
668 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
670 if (add
&& requiredmounts
) {
671 mntarr
= blobmsg_open_array(&req
, "volumes");
672 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
673 curvol
= strtok_r(mnttok
, ",;", &tmp
);
677 blobmsg_add_string(&req
, NULL
, curvol
);
679 blobmsg_close_array(&req
, mntarr
);
682 dprintf(f
, "%s\n", blobmsg_format_json_indent(req
.head
, true, 0));
701 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
702 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
703 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
704 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
705 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
706 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
710 /* check if device 'devname' is mounted according to blockd */
711 static int checkblock(const char *uuid
)
713 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
714 struct blob_attr
*cur
;
717 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
718 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
720 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
723 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
736 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
737 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
738 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
741 static const char *resolveuuid(const char *volname
)
743 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
744 struct blob_attr
*cur
;
746 char *tmpvolname
, *replc
;
749 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
750 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
752 if (!tb
[UCI_FSTAB_UUID
])
755 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
758 mntname
= blobmsg_name(cur
);
762 tmpvolname
= strdup(volname
);
763 while ((replc
= strchr(tmpvolname
, '-')))
766 res
= strcmp(tmpvolname
, mntname
);
770 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
776 /* check status of each required volume */
777 static int checkvolumes(struct blob_attr
*volumes
)
779 struct blob_attr
*cur
;
782 blobmsg_for_each_attr(cur
, volumes
, rem
) {
783 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
790 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
792 blockinfo
= blob_memdup(blobmsg_data(msg
));
795 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
797 fstabinfo
= blob_memdup(blobmsg_data(msg
));
800 static int uxc_boot(void)
802 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
803 struct runtime_state
*s
;
804 static struct blob_buf req
;
809 ret
= ubus_lookup_id(ctx
, "block", &id
);
813 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
817 ret
= ubus_lookup_id(ctx
, "uci", &id
);
821 blob_buf_init(&req
, 0);
822 blobmsg_add_string(&req
, "config", "fstab");
823 blobmsg_add_string(&req
, "type", "mount");
825 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
829 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
830 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
831 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
834 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
838 /* make sure all volumes are ready before starting */
839 if (tb
[CONF_VOLUMES
])
840 if (checkvolumes(tb
[CONF_VOLUMES
]))
843 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
844 ret
+= uxc_create(name
, true);
851 static int uxc_delete(char *name
, bool force
)
853 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
854 struct runtime_state
*s
= NULL
;
855 static struct blob_buf req
;
862 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
863 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
864 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
867 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
870 fname
= strdup(blobmsg_name(cur
));
881 s
= avl_find_element(&runtime
, name
, s
, avl
);
883 if (s
&& s
->running
) {
885 ret
= uxc_kill(name
, SIGKILL
);
896 ret
= ubus_lookup_id(ctx
, "container", &id
);
900 blob_buf_init(&req
, 0);
901 blobmsg_add_string(&req
, "name", s
->container_name
);
902 blobmsg_add_string(&req
, "instance", s
->instance_name
);
904 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
911 if (stat(fname
, &sb
) == -1) {
916 if (unlink(fname
) == -1)
924 static void reload_conf(void)
926 blob_buf_free(&conf
);
930 int main(int argc
, char **argv
)
932 enum uxc_cmd cmd
= CMD_UNKNOWN
;
935 char *pidfile
= NULL
;
936 char *tmprwsize
= NULL
;
937 char *writepath
= NULL
;
938 char *requiredmounts
= NULL
;
939 bool autostart
= false;
941 int signal
= SIGTERM
;
947 ctx
= ubus_connect(NULL
);
955 ret
= runtime_load();
960 int option_index
= 0;
961 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
991 printf("uxc %s\n", UXC_VERSION
);
999 requiredmounts
= optarg
;
1007 if (!strcmp("list", argv
[optind
]))
1009 else if (!strcmp("boot", argv
[optind
]))
1011 else if(!strcmp("start", argv
[optind
]))
1013 else if(!strcmp("state", argv
[optind
]))
1015 else if(!strcmp("kill", argv
[optind
]))
1017 else if(!strcmp("enable", argv
[optind
]))
1019 else if(!strcmp("disable", argv
[optind
]))
1021 else if(!strcmp("delete", argv
[optind
]))
1023 else if(!strcmp("create", argv
[optind
]))
1036 if (optind
!= argc
- 2)
1039 ret
= uxc_start(argv
[optind
+ 1]);
1043 if (optind
!= argc
- 2)
1046 ret
= uxc_state(argv
[optind
+ 1]);
1050 if (optind
== (argc
- 3))
1051 signal
= atoi(argv
[optind
+ 2]);
1052 else if (optind
> argc
- 2)
1055 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1059 if (optind
!= argc
- 2)
1062 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1066 if (optind
!= argc
- 2)
1069 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1073 if (optind
!= argc
- 2)
1076 ret
= uxc_delete(argv
[optind
+ 1], force
);
1080 if (optind
!= argc
- 2)
1084 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1091 ret
= uxc_create(argv
[optind
+ 1], false);
1105 blob_buf_free(&conf
);
1110 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));