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_ETC_CONFDIR "/etc/uxc"
41 #define UXC_VOL_CONFDIR "/var/run/uvol/.meta/uxc"
43 static bool verbose
= false;
44 static bool json_output
= false;
45 static char *confdir
= UXC_ETC_CONFDIR
;
47 struct runtime_state
{
55 struct blob_attr
*ocistate
;
71 #define OPT_ARGS "ab:fjm:p:t:vVw:"
72 static struct option long_options
[] = {
73 {"autostart", no_argument
, 0, 'a' },
74 {"bundle", required_argument
, 0, 'b' },
75 {"force", no_argument
, 0, 'f' },
76 {"json", no_argument
, 0, 'j' },
77 {"mounts", required_argument
, 0, 'm' },
78 {"pid-file", required_argument
, 0, 'p' },
79 {"temp-overlay-size", required_argument
, 0, 't' },
80 {"write-overlay-path", required_argument
, 0, 'w' },
81 {"verbose", no_argument
, 0, 'v' },
82 {"version", no_argument
, 0, 'V' },
86 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
87 static struct blob_buf conf
;
88 static struct blob_attr
*blockinfo
;
89 static struct blob_attr
*fstabinfo
;
90 static struct ubus_context
*ctx
;
92 static int usage(void) {
93 printf("syntax: uxc <command> [parameters ...]\n");
94 printf("commands:\n");
95 printf("\tlist [--json]\t\t\t\tlist all configured containers\n");
96 printf("\tcreate <conf>\t\t\t\t\t(re-)create <conf>\n");
97 printf(" [--bundle <path>]\t\t\tOCI bundle at <path>\n");
98 printf(" [--autostart]\t\t\t\tstart on boot\n");
99 printf(" [--temp-overlay-size size]\t\tuse tmpfs overlay with {size}\n");
100 printf(" [--write-overlay-path path]\t\tuse overlay on {path}\n");
101 printf(" [--mounts v1,v2,...,vN]\t\trequire filesystems to be available\n");
102 printf("\tstart <conf>\t\t\t\t\tstart container <conf>\n");
103 printf("\tstate <conf>\t\t\t\t\tget state of container <conf>\n");
104 printf("\tkill <conf> [<signal>]\t\t\t\tsend signal to container <conf>\n");
105 printf("\tenable <conf>\t\t\t\t\tstart container <conf> on boot\n");
106 printf("\tdisable <conf>\t\t\t\t\tdon't start container <conf> on boot\n");
107 printf("\tdelete <conf> [--force]\t\t\t\tdelete <conf>\n");
117 CONF_TEMP_OVERLAY_SIZE
,
118 CONF_WRITE_OVERLAY_PATH
,
123 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
124 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
125 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
126 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
127 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
128 [CONF_PIDFILE
] = { .name
= "pidfile", .type
= BLOBMSG_TYPE_STRING
},
129 [CONF_TEMP_OVERLAY_SIZE
] = { .name
= "temp-overlay-size", .type
= BLOBMSG_TYPE_STRING
},
130 [CONF_WRITE_OVERLAY_PATH
] = { .name
= "write-overlay-path", .type
= BLOBMSG_TYPE_STRING
},
131 [CONF_VOLUMES
] = { .name
= "volumes", .type
= BLOBMSG_TYPE_ARRAY
},
134 static int conf_load(void)
136 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
144 if (asprintf(&globstr
, "%s/*.json", UXC_ETC_CONFDIR
) == -1)
147 if (glob(globstr
, gl_flags
, NULL
, &gl
) == 0)
148 gl_flags
|= GLOB_APPEND
;
152 if (!stat(UXC_VOL_CONFDIR
, &sb
)) {
153 if (sb
.st_mode
& S_IFDIR
) {
154 if (asprintf(&globstr
, "%s/*.json", UXC_VOL_CONFDIR
) == -1)
157 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
162 blob_buf_init(&conf
, 0);
163 c
= blobmsg_open_table(&conf
, NULL
);
168 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
169 o
= blobmsg_open_table(&conf
, strdup(gl
.gl_pathv
[j
]));
170 if (!blobmsg_add_json_from_file(&conf
, gl
.gl_pathv
[j
])) {
171 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
174 blobmsg_close_table(&conf
, o
);
176 blobmsg_close_table(&conf
, c
);
187 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
188 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
199 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
200 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
201 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
202 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
203 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
211 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
212 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
215 static struct runtime_state
*
216 runtime_alloc(const char *container_name
)
218 struct runtime_state
*s
;
220 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
221 strcpy(new_name
, container_name
);
222 s
->container_name
= new_name
;
223 s
->avl
.key
= s
->container_name
;
237 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
238 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
239 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
240 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
241 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
242 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
243 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
247 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
249 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
250 struct blob_attr
*tb
[__STATE_MAX
];
252 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
254 if (!tb
[STATE_OCIVERSION
] ||
260 *ocistate
= blob_memdup(msg
);
263 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
270 if (asprintf(&objname
, "container.%s", name
) == -1)
273 ret
= ubus_lookup_id(ctx
, objname
, &id
);
278 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
281 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
283 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
285 const char *container_name
, *instance_name
, *jail_name
;
288 struct runtime_state
*rs
;
290 blobmsg_for_each_attr(cur
, msg
, rem
) {
291 container_name
= blobmsg_name(cur
);
292 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
293 if (!tl
[LIST_INSTANCES
])
296 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
297 instance_name
= blobmsg_name(curi
);
298 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
300 if (!ti
[INSTANCE_JAIL
])
303 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
307 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
309 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
311 if (ti
[INSTANCE_PID
])
312 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
316 if (ti
[INSTANCE_EXITCODE
])
317 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
321 rs
= runtime_alloc(container_name
);
322 rs
->instance_name
= strdup(instance_name
);
323 rs
->jail_name
= strdup(jail_name
);
324 rs
->runtime_pid
= pid
;
325 rs
->exitcode
= exitcode
;
326 rs
->running
= running
;
327 avl_insert(&runtime
, &rs
->avl
);
334 static int runtime_load(void)
336 struct runtime_state
*item
, *tmp
;
339 avl_init(&runtime
, avl_strcmp
, false, NULL
);
340 if (ubus_lookup_id(ctx
, "container", &id
) ||
341 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
345 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
346 get_ocistate(&item
->ocistate
, item
->jail_name
);
351 static void runtime_free(void)
353 struct runtime_state
*item
, *tmp
;
355 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
356 avl_delete(&runtime
, &item
->avl
);
357 free(item
->instance_name
);
358 free(item
->jail_name
);
359 free(item
->ocistate
);
366 static int uxc_state(char *name
)
368 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
369 struct blob_attr
*ocistate
= NULL
;
370 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
373 char *jail_name
= NULL
;
376 static struct blob_buf buf
;
379 ocistate
= s
->ocistate
;
382 state
= blobmsg_format_json_indent(ocistate
, true, 0);
386 printf("%s\n", state
);
391 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
392 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
393 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
396 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
398 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
402 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
410 blob_buf_init(&buf
, 0);
411 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
412 blobmsg_add_string(&buf
, "id", jail_name
);
413 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
414 blobmsg_add_string(&buf
, "bundle", bundle
);
416 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
430 static int uxc_list(void)
432 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
434 struct runtime_state
*s
= NULL
;
435 char *name
, *ocistatus
, *status
, *tmp
;
436 int container_pid
= -1;
438 static struct blob_buf buf
;
442 blob_buf_init(&buf
, 0);
443 arr
= blobmsg_open_array(&buf
, "");
446 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
447 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
448 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
451 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
454 name
= blobmsg_get_string(tb
[CONF_NAME
]);
455 s
= avl_find_element(&runtime
, name
, s
, avl
);
457 if (s
&& s
->ocistate
) {
458 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
459 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
460 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
463 status
= ocistatus
?:(s
&& s
->running
)?"creating":"stopped";
466 obj
= blobmsg_open_table(&buf
, "");
467 blobmsg_add_string(&buf
, "name", name
);
468 blobmsg_add_string(&buf
, "status", status
);
469 blobmsg_add_u8(&buf
, "autostart", autostart
);
471 printf("[%c] %s %s", autostart
?'*':' ', name
, status
);
474 if (s
&& !s
->running
&& (s
->exitcode
>= 0)) {
476 blobmsg_add_u32(&buf
, "exitcode", s
->exitcode
);
478 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
481 if (s
&& s
->running
&& (s
->runtime_pid
>= 0)) {
483 blobmsg_add_u32(&buf
, "runtime_pid", s
->runtime_pid
);
485 printf(" runtime pid: %d", s
->runtime_pid
);
488 if (s
&& s
->running
&& (container_pid
>= 0)) {
490 blobmsg_add_u32(&buf
, "container_pid", container_pid
);
492 printf(" container pid: %d", container_pid
);
498 blobmsg_close_table(&buf
, obj
);
502 blobmsg_close_array(&buf
, arr
);
503 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
516 static int uxc_create(char *name
, bool immediately
)
518 static struct blob_buf req
;
519 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
522 struct runtime_state
*s
= NULL
;
523 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
528 s
= avl_find_element(&runtime
, name
, s
, avl
);
530 if (s
&& (s
->running
))
533 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
534 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
535 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
538 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
548 path
= blobmsg_get_string(tb
[CONF_PATH
]);
550 if (tb
[CONF_PIDFILE
])
551 pidfile
= blobmsg_get_string(tb
[CONF_PIDFILE
]);
553 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
554 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
556 if (tb
[CONF_WRITE_OVERLAY_PATH
])
557 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
560 jailname
= blobmsg_get_string(tb
[CONF_JAIL
]);
562 blob_buf_init(&req
, 0);
563 blobmsg_add_string(&req
, "name", name
);
564 ins
= blobmsg_open_table(&req
, "instances");
565 in
= blobmsg_open_table(&req
, name
);
566 blobmsg_add_string(&req
, "bundle", path
);
567 j
= blobmsg_open_table(&req
, "jail");
568 blobmsg_add_string(&req
, "name", jailname
?:name
);
569 blobmsg_add_u8(&req
, "immediately", immediately
);
572 blobmsg_add_string(&req
, "pidfile", pidfile
);
574 blobmsg_close_table(&req
, j
);
577 blobmsg_add_string(&req
, "overlaydir", writepath
);
580 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
582 blobmsg_close_table(&req
, in
);
583 blobmsg_close_table(&req
, ins
);
587 tmp
= blobmsg_format_json_indent(req
.head
, true, 1);
591 fprintf(stderr
, "adding container to procd:\n\t%s\n", tmp
);
596 if (ubus_lookup_id(ctx
, "container", &id
) ||
597 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
605 static int uxc_start(const char *name
)
610 if (asprintf(&objname
, "container.%s", name
) == -1)
613 if (ubus_lookup_id(ctx
, objname
, &id
))
616 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
619 static int uxc_kill(char *name
, int signal
)
621 static struct blob_buf req
;
622 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
626 struct runtime_state
*s
= NULL
;
629 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
630 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
631 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
634 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
644 s
= avl_find_element(&runtime
, name
, s
, avl
);
646 if (!s
|| !(s
->running
))
649 blob_buf_init(&req
, 0);
650 blobmsg_add_u32(&req
, "signal", signal
);
651 blobmsg_add_string(&req
, "name", name
);
653 if (asprintf(&objname
, "container.%s", name
) == -1)
656 ret
= ubus_lookup_id(ctx
, objname
, &id
);
661 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
668 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
670 static struct blob_buf req
;
671 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
673 const char *cfname
= NULL
;
675 char *keeppath
= NULL
;
676 char *tmprwsize
= _tmprwsize
;
677 char *writepath
= _writepath
;
678 char *curvol
, *tmp
, *mnttok
;
683 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
684 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
685 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
688 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
691 cfname
= blobmsg_name(cur
);
705 if (stat(path
, &sb
) == -1)
708 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
713 ret
= mkdir(confdir
, 0755);
715 if (ret
&& errno
!= EEXIST
)
718 if (asprintf(&fname
, "%s/%s.json", confdir
, name
) == -1)
721 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
725 f
= open(cfname
, O_WRONLY
| O_TRUNC
, 0644);
731 keeppath
= blobmsg_get_string(tb
[CONF_PATH
]);
732 if (tb
[CONF_WRITE_OVERLAY_PATH
])
733 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
735 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
736 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
739 blob_buf_init(&req
, 0);
740 blobmsg_add_string(&req
, "name", name
);
741 blobmsg_add_string(&req
, "path", path
?:keeppath
);
742 blobmsg_add_u8(&req
, "autostart", autostart
);
744 blobmsg_add_string(&req
, "pidfile", pidfile
);
747 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
750 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
752 if (!add
&& tb
[CONF_VOLUMES
])
753 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
755 if (add
&& requiredmounts
) {
756 mntarr
= blobmsg_open_array(&req
, "volumes");
757 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
758 curvol
= strtok_r(mnttok
, ",;", &tmp
);
762 blobmsg_add_string(&req
, NULL
, curvol
);
764 blobmsg_close_array(&req
, mntarr
);
766 tmp
= blobmsg_format_json_indent(req
.head
, true, 0);
768 dprintf(f
, "%s\n", tmp
);
787 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
788 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
789 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
790 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
791 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
792 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
796 /* check if device 'devname' is mounted according to blockd */
797 static int checkblock(const char *uuid
)
799 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
800 struct blob_attr
*cur
;
803 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
804 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
806 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
809 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
822 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
823 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
824 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
827 static const char *resolveuuid(const char *volname
)
829 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
830 struct blob_attr
*cur
;
832 char *tmpvolname
, *replc
;
835 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
836 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
838 if (!tb
[UCI_FSTAB_UUID
])
841 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
844 mntname
= blobmsg_name(cur
);
848 tmpvolname
= strdup(volname
);
849 while ((replc
= strchr(tmpvolname
, '-')))
852 res
= strcmp(tmpvolname
, mntname
);
856 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
862 /* check status of each required volume */
863 static int checkvolumes(struct blob_attr
*volumes
)
865 struct blob_attr
*cur
;
868 blobmsg_for_each_attr(cur
, volumes
, rem
) {
869 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
876 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
878 blockinfo
= blob_memdup(blobmsg_data(msg
));
881 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
883 fstabinfo
= blob_memdup(blobmsg_data(msg
));
886 static int uxc_boot(void)
888 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
889 struct runtime_state
*s
;
890 static struct blob_buf req
;
895 ret
= ubus_lookup_id(ctx
, "block", &id
);
899 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
903 ret
= ubus_lookup_id(ctx
, "uci", &id
);
907 blob_buf_init(&req
, 0);
908 blobmsg_add_string(&req
, "config", "fstab");
909 blobmsg_add_string(&req
, "type", "mount");
911 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
915 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
916 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
917 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
920 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
924 /* make sure all volumes are ready before starting */
925 if (tb
[CONF_VOLUMES
])
926 if (checkvolumes(tb
[CONF_VOLUMES
]))
929 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
930 ret
+= uxc_create(name
, true);
937 static int uxc_delete(char *name
, bool force
)
939 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
940 struct runtime_state
*s
= NULL
;
941 static struct blob_buf req
;
944 const char *fname
= NULL
;
947 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
948 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
949 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
952 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
955 fname
= blobmsg_name(cur
);
962 s
= avl_find_element(&runtime
, name
, s
, avl
);
964 if (s
&& s
->running
) {
966 ret
= uxc_kill(name
, SIGKILL
);
977 ret
= ubus_lookup_id(ctx
, "container", &id
);
981 blob_buf_init(&req
, 0);
982 blobmsg_add_string(&req
, "name", s
->container_name
);
983 blobmsg_add_string(&req
, "instance", s
->instance_name
);
985 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
992 if (stat(fname
, &sb
) == -1) {
997 if (unlink(fname
) == -1)
1004 static void reload_conf(void)
1006 blob_buf_free(&conf
);
1010 int main(int argc
, char **argv
)
1012 enum uxc_cmd cmd
= CMD_UNKNOWN
;
1014 char *bundle
= NULL
;
1015 char *pidfile
= NULL
;
1016 char *tmprwsize
= NULL
;
1017 char *writepath
= NULL
;
1018 char *requiredmounts
= NULL
;
1019 bool autostart
= false;
1021 int signal
= SIGTERM
;
1027 ctx
= ubus_connect(NULL
);
1035 ret
= runtime_load();
1040 int option_index
= 0;
1041 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
1075 printf("uxc %s\n", UXC_VERSION
);
1083 requiredmounts
= optarg
;
1091 if (!strcmp("list", argv
[optind
]))
1093 else if (!strcmp("boot", argv
[optind
]))
1095 else if(!strcmp("start", argv
[optind
]))
1097 else if(!strcmp("state", argv
[optind
]))
1099 else if(!strcmp("kill", argv
[optind
]))
1101 else if(!strcmp("enable", argv
[optind
]))
1103 else if(!strcmp("disable", argv
[optind
]))
1105 else if(!strcmp("delete", argv
[optind
]))
1107 else if(!strcmp("create", argv
[optind
]))
1120 if (optind
!= argc
- 2)
1123 ret
= uxc_start(argv
[optind
+ 1]);
1127 if (optind
!= argc
- 2)
1130 ret
= uxc_state(argv
[optind
+ 1]);
1134 if (optind
== (argc
- 3))
1135 signal
= atoi(argv
[optind
+ 2]);
1136 else if (optind
> argc
- 2)
1139 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1143 if (optind
!= argc
- 2)
1146 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1150 if (optind
!= argc
- 2)
1153 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1157 if (optind
!= argc
- 2)
1160 ret
= uxc_delete(argv
[optind
+ 1], force
);
1164 if (optind
!= argc
- 2)
1168 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1175 ret
= uxc_create(argv
[optind
+ 1], false);
1189 blob_buf_free(&conf
);
1194 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));