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
;
356 static struct blob_buf buf
;
359 ocistate
= s
->ocistate
;
362 state
= blobmsg_format_json_indent(ocistate
, true, 0);
366 printf("%s\n", state
);
371 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
372 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
373 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
376 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
378 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
382 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
390 blob_buf_init(&buf
, 0);
391 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
392 blobmsg_add_string(&buf
, "id", jail_name
);
393 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
394 blobmsg_add_string(&buf
, "bundle", bundle
);
396 printf("%s\n", blobmsg_format_json_indent(buf
.head
, true, 0));
402 static int uxc_list(void)
404 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
406 struct runtime_state
*s
= NULL
;
409 int container_pid
= -1;
412 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
413 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
414 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
417 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
420 name
= blobmsg_get_string(tb
[CONF_NAME
]);
421 s
= avl_find_element(&runtime
, name
, s
, avl
);
423 if (s
&& s
->ocistate
) {
424 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
425 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
426 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
429 printf("[%c] %s %s", autostart
?'*':' ', name
, ocistatus
?:(s
&& s
->running
)?"creating":"stopped");
431 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
432 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
434 if (s
&& s
->running
&& (s
->runtime_pid
>= 0))
435 printf(" runtime pid: %d", s
->runtime_pid
);
437 if (s
&& s
->running
&& (container_pid
>= 0))
438 printf(" container pid: %d", container_pid
);
446 static int uxc_create(char *name
, bool immediately
)
448 static struct blob_buf req
;
449 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
452 struct runtime_state
*s
= NULL
;
453 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
458 s
= avl_find_element(&runtime
, name
, s
, avl
);
460 if (s
&& (s
->running
))
463 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
464 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
465 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
468 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
477 path
= blobmsg_get_string(tb
[CONF_PATH
]);
479 if (tb
[CONF_PIDFILE
])
480 pidfile
= blobmsg_get_string(tb
[CONF_PIDFILE
]);
482 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
483 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
485 if (tb
[CONF_WRITE_OVERLAY_PATH
])
486 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
490 jailname
= blobmsg_get_string(tb
[CONF_JAIL
]);
492 blob_buf_init(&req
, 0);
493 blobmsg_add_string(&req
, "name", name
);
494 ins
= blobmsg_open_table(&req
, "instances");
495 in
= blobmsg_open_table(&req
, name
);
496 blobmsg_add_string(&req
, "bundle", path
);
497 j
= blobmsg_open_table(&req
, "jail");
498 blobmsg_add_string(&req
, "name", jailname
?:name
);
499 blobmsg_add_u8(&req
, "immediately", immediately
);
502 blobmsg_add_string(&req
, "pidfile", pidfile
);
504 blobmsg_close_table(&req
, j
);
507 blobmsg_add_string(&req
, "overlaydir", writepath
);
510 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
512 blobmsg_close_table(&req
, in
);
513 blobmsg_close_table(&req
, ins
);
517 tmp
= blobmsg_format_json_indent(req
.head
, true, 1);
521 fprintf(stderr
, "adding container to procd:\n\t%s\n", tmp
);
526 if (ubus_lookup_id(ctx
, "container", &id
) ||
527 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
538 static int uxc_start(const char *name
)
543 if (asprintf(&objname
, "container.%s", name
) == -1)
546 if (ubus_lookup_id(ctx
, objname
, &id
))
549 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
552 static int uxc_kill(char *name
, int signal
)
554 static struct blob_buf req
;
555 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
559 struct runtime_state
*s
= NULL
;
562 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
563 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
564 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
567 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
577 s
= avl_find_element(&runtime
, name
, s
, avl
);
579 if (!s
|| !(s
->running
))
582 blob_buf_init(&req
, 0);
583 blobmsg_add_u32(&req
, "signal", signal
);
584 blobmsg_add_string(&req
, "name", name
);
586 if (asprintf(&objname
, "container.%s", name
) == -1)
589 ret
= ubus_lookup_id(ctx
, objname
, &id
);
594 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
601 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
603 static struct blob_buf req
;
604 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
608 char *keeppath
= NULL
;
609 char *tmprwsize
= _tmprwsize
;
610 char *writepath
= _writepath
;
611 char *curvol
, *tmp
, *mnttok
;
616 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
617 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
618 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
621 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
638 if (stat(path
, &sb
) == -1)
641 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
645 ret
= mkdir(UXC_CONFDIR
, 0755);
647 if (ret
&& errno
!= EEXIST
)
650 if (asprintf(&fname
, "%s/%s.json", UXC_CONFDIR
, name
) == -1)
653 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
658 keeppath
= blobmsg_get_string(tb
[CONF_PATH
]);
659 if (tb
[CONF_WRITE_OVERLAY_PATH
])
660 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
662 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
663 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
666 blob_buf_init(&req
, 0);
667 blobmsg_add_string(&req
, "name", name
);
668 blobmsg_add_string(&req
, "path", path
?:keeppath
);
669 blobmsg_add_u8(&req
, "autostart", autostart
);
671 blobmsg_add_string(&req
, "pidfile", pidfile
);
674 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
677 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
679 if (!add
&& tb
[CONF_VOLUMES
])
680 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
682 if (add
&& requiredmounts
) {
683 mntarr
= blobmsg_open_array(&req
, "volumes");
684 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
685 curvol
= strtok_r(mnttok
, ",;", &tmp
);
689 blobmsg_add_string(&req
, NULL
, curvol
);
691 blobmsg_close_array(&req
, mntarr
);
693 tmp
= blobmsg_format_json_indent(req
.head
, true, 0);
695 dprintf(f
, "%s\n", tmp
);
713 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
714 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
715 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
716 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
717 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
718 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
722 /* check if device 'devname' is mounted according to blockd */
723 static int checkblock(const char *uuid
)
725 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
726 struct blob_attr
*cur
;
729 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
730 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
732 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
735 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
748 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
749 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
750 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
753 static const char *resolveuuid(const char *volname
)
755 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
756 struct blob_attr
*cur
;
758 char *tmpvolname
, *replc
;
761 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
762 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
764 if (!tb
[UCI_FSTAB_UUID
])
767 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
770 mntname
= blobmsg_name(cur
);
774 tmpvolname
= strdup(volname
);
775 while ((replc
= strchr(tmpvolname
, '-')))
778 res
= strcmp(tmpvolname
, mntname
);
782 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
788 /* check status of each required volume */
789 static int checkvolumes(struct blob_attr
*volumes
)
791 struct blob_attr
*cur
;
794 blobmsg_for_each_attr(cur
, volumes
, rem
) {
795 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
802 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
804 blockinfo
= blob_memdup(blobmsg_data(msg
));
807 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
809 fstabinfo
= blob_memdup(blobmsg_data(msg
));
812 static int uxc_boot(void)
814 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
815 struct runtime_state
*s
;
816 static struct blob_buf req
;
821 ret
= ubus_lookup_id(ctx
, "block", &id
);
825 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
829 ret
= ubus_lookup_id(ctx
, "uci", &id
);
833 blob_buf_init(&req
, 0);
834 blobmsg_add_string(&req
, "config", "fstab");
835 blobmsg_add_string(&req
, "type", "mount");
837 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
841 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
842 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
843 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
846 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
850 /* make sure all volumes are ready before starting */
851 if (tb
[CONF_VOLUMES
])
852 if (checkvolumes(tb
[CONF_VOLUMES
]))
855 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
856 ret
+= uxc_create(name
, true);
863 static int uxc_delete(char *name
, bool force
)
865 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
866 struct runtime_state
*s
= NULL
;
867 static struct blob_buf req
;
874 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
875 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
876 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
879 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
882 fname
= strdup(blobmsg_name(cur
));
893 s
= avl_find_element(&runtime
, name
, s
, avl
);
895 if (s
&& s
->running
) {
897 ret
= uxc_kill(name
, SIGKILL
);
908 ret
= ubus_lookup_id(ctx
, "container", &id
);
912 blob_buf_init(&req
, 0);
913 blobmsg_add_string(&req
, "name", s
->container_name
);
914 blobmsg_add_string(&req
, "instance", s
->instance_name
);
916 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
923 if (stat(fname
, &sb
) == -1) {
928 if (unlink(fname
) == -1)
936 static void reload_conf(void)
938 blob_buf_free(&conf
);
942 int main(int argc
, char **argv
)
944 enum uxc_cmd cmd
= CMD_UNKNOWN
;
947 char *pidfile
= NULL
;
948 char *tmprwsize
= NULL
;
949 char *writepath
= NULL
;
950 char *requiredmounts
= NULL
;
951 bool autostart
= false;
953 int signal
= SIGTERM
;
959 ctx
= ubus_connect(NULL
);
967 ret
= runtime_load();
972 int option_index
= 0;
973 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
1003 printf("uxc %s\n", UXC_VERSION
);
1011 requiredmounts
= optarg
;
1019 if (!strcmp("list", argv
[optind
]))
1021 else if (!strcmp("boot", argv
[optind
]))
1023 else if(!strcmp("start", argv
[optind
]))
1025 else if(!strcmp("state", argv
[optind
]))
1027 else if(!strcmp("kill", argv
[optind
]))
1029 else if(!strcmp("enable", argv
[optind
]))
1031 else if(!strcmp("disable", argv
[optind
]))
1033 else if(!strcmp("delete", argv
[optind
]))
1035 else if(!strcmp("create", argv
[optind
]))
1048 if (optind
!= argc
- 2)
1051 ret
= uxc_start(argv
[optind
+ 1]);
1055 if (optind
!= argc
- 2)
1058 ret
= uxc_state(argv
[optind
+ 1]);
1062 if (optind
== (argc
- 3))
1063 signal
= atoi(argv
[optind
+ 2]);
1064 else if (optind
> argc
- 2)
1067 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1071 if (optind
!= argc
- 2)
1074 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1078 if (optind
!= argc
- 2)
1081 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1085 if (optind
!= argc
- 2)
1088 ret
= uxc_delete(argv
[optind
+ 1], force
);
1092 if (optind
!= argc
- 2)
1096 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1103 ret
= uxc_create(argv
[optind
+ 1], false);
1117 blob_buf_free(&conf
);
1122 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));