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.
18 #include <libubox/avl-cmp.h>
19 #include <libubox/blobmsg.h>
20 #include <libubox/blobmsg_json.h>
27 #include <sys/types.h>
32 #define UXC_CONFDIR "/etc/uxc"
33 #define UXC_RUNDIR "/var/run/uxc"
35 struct runtime_state
{
37 const char *container_name
;
38 const char *instance_name
;
39 const char *jail_name
;
45 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
46 static struct blob_buf conf
;
47 static struct blob_buf state
;
49 static struct ubus_context
*ctx
;
51 static int usage(void) {
52 printf("syntax: uxc {command} [parameters ...]\n");
53 printf("commands:\n");
54 printf("\tlist\t\t\t\tlist all configured containers\n");
55 printf("\tcreate {conf} {path} [enabled]\tcreate {conf} for OCI bundle at {path}\n");
56 printf("\tstart {conf}\t\t\tstart container {conf}\n");
57 printf("\tstop {conf}\t\t\tstop container {conf}\n");
58 printf("\tenable {conf}\t\t\tstart container {conf} on boot\n");
59 printf("\tdisable {conf}\t\t\tdon't start container {conf} on boot\n");
60 printf("\tdelete {conf}\t\t\tdelete {conf}\n");
72 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
73 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
74 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
75 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
76 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
79 static int conf_load(bool load_state
)
81 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
85 struct blob_buf
*target
= load_state
?&state
:&conf
;
88 if (asprintf(&globstr
, "%s/*.json", load_state
?UXC_RUNDIR
:UXC_CONFDIR
) == -1)
91 blob_buf_init(target
, 0);
92 c
= blobmsg_open_table(target
, NULL
);
94 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
99 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
100 o
= blobmsg_open_table(target
, strdup(gl
.gl_pathv
[j
]));
101 if (!blobmsg_add_json_from_file(target
, gl
.gl_pathv
[j
])) {
102 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
105 blobmsg_close_table(target
, o
);
107 blobmsg_close_table(target
, c
);
118 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
119 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
130 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
131 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
132 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
133 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
134 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
142 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
143 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
146 static struct runtime_state
*
147 runtime_alloc(const char *container_name
)
149 struct runtime_state
*s
;
151 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
152 strcpy(new_name
, container_name
);
153 s
->container_name
= new_name
;
154 s
->avl
.key
= s
->container_name
;
158 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
160 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
162 const char *container_name
, *instance_name
, *jail_name
;
165 struct runtime_state
*rs
;
167 blobmsg_for_each_attr(cur
, msg
, rem
) {
168 container_name
= blobmsg_name(cur
);
169 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
170 if (!tl
[LIST_INSTANCES
])
173 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
174 instance_name
= blobmsg_name(curi
);
175 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
177 if (!ti
[INSTANCE_JAIL
])
180 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
184 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
186 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
188 if (ti
[INSTANCE_PID
])
189 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
193 if (ti
[INSTANCE_EXITCODE
])
194 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
198 rs
= runtime_alloc(container_name
);
199 rs
->instance_name
= strdup(instance_name
);
200 rs
->jail_name
= strdup(jail_name
);
202 rs
->exitcode
= exitcode
;
203 rs
->running
= running
;
204 avl_insert(&runtime
, &rs
->avl
);
211 static int runtime_load(void)
215 avl_init(&runtime
, avl_strcmp
, false, NULL
);
216 if (ubus_lookup_id(ctx
, "container", &id
) ||
217 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
223 static void runtime_free(void)
225 struct runtime_state
*item
, *tmp
;
227 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
228 avl_delete(&runtime
, &item
->avl
);
235 static int uxc_list(void)
237 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
239 struct runtime_state
*s
= NULL
;
243 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
244 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
245 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
248 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
250 name
= blobmsg_get_string(tb
[CONF_NAME
]);
251 s
= avl_find_element(&runtime
, name
, s
, avl
);
253 printf("[%c] %s %s", autostart
?'*':' ', name
, (s
&& s
->running
)?"RUNNING":"STOPPED");
255 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
256 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
258 if (s
&& s
->running
&& (s
->pid
>= 0))
259 printf(" pid: %d", s
->pid
);
267 static int uxc_start(char *name
)
269 static struct blob_buf req
;
270 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
273 struct runtime_state
*s
= NULL
;
274 char *path
= NULL
, *jailname
= NULL
;
278 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
279 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
280 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
283 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
287 path
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
296 s
= avl_find_element(&runtime
, name
, s
, avl
);
298 if (s
&& (s
->running
))
302 jailname
= strdup(blobmsg_get_string(tb
[CONF_JAIL
]));
304 blob_buf_init(&req
, 0);
305 blobmsg_add_string(&req
, "name", name
);
306 ins
= blobmsg_open_table(&req
, "instances");
307 in
= blobmsg_open_table(&req
, name
);
308 blobmsg_add_string(&req
, "bundle", path
);
309 j
= blobmsg_open_table(&req
, "jail");
310 blobmsg_add_string(&req
, "name", jailname
?:name
);
311 blobmsg_close_table(&req
, j
);
312 blobmsg_close_table(&req
, in
);
313 blobmsg_close_table(&req
, ins
);
316 if (ubus_lookup_id(ctx
, "container", &id
) ||
317 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
328 static int uxc_stop(char *name
)
330 static struct blob_buf req
;
331 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
334 struct runtime_state
*s
= NULL
;
337 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
338 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
339 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
342 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
352 s
= avl_find_element(&runtime
, name
, s
, avl
);
354 if (!s
|| !(s
->running
))
357 blob_buf_init(&req
, 0);
358 blobmsg_add_string(&req
, "name", name
);
359 blobmsg_add_string(&req
, "instance", s
->instance_name
);
362 if (ubus_lookup_id(ctx
, "container", &id
) ||
363 ubus_invoke(ctx
, id
, "del", req
.head
, NULL
, NULL
, 3000)) {
372 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
)
374 static struct blob_buf req
;
375 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
379 char *keeppath
= NULL
;
383 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
384 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
385 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
388 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
405 if (stat(path
, &sb
) == -1)
408 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
412 ret
= mkdir(UXC_CONFDIR
, 0755);
414 if (ret
&& errno
!= EEXIST
)
417 if (asprintf(&fname
, "%s/%s.json", UXC_CONFDIR
, name
) < 1)
420 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
425 keeppath
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
427 blob_buf_init(&req
, 0);
428 blobmsg_add_string(&req
, "name", name
);
429 blobmsg_add_string(&req
, "path", path
?:keeppath
);
430 blobmsg_add_u8(&req
, "autostart", autostart
);
432 dprintf(f
, "%s\n", blobmsg_format_json_indent(req
.head
, true, 0));
439 /* ToDo: tell ujail to run createRuntime and createContainer hooks */
443 static int uxc_boot(void)
445 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
449 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
450 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
451 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
454 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
455 ret
+= uxc_start(name
);
462 static int uxc_delete(char *name
)
464 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
470 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
471 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
472 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
475 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
478 fname
= strdup(blobmsg_name(cur
));
489 if (stat(fname
, &sb
) == -1) {
494 if (unlink(fname
) == -1)
502 int main(int argc
, char **argv
)
509 ctx
= ubus_connect(NULL
);
513 ret
= conf_load(false);
517 ret
= mkdir(UXC_RUNDIR
, 0755);
518 if (ret
&& errno
!= EEXIST
)
521 ret
= conf_load(true);
525 ret
= runtime_load();
529 if (!strcmp("list", argv
[1]))
531 else if (!strcmp("boot", argv
[1]))
533 else if(!strcmp("start", argv
[1])) {
537 ret
= uxc_start(argv
[2]);
538 } else if(!strcmp("stop", argv
[1])) {
542 ret
= uxc_stop(argv
[2]);
543 } else if(!strcmp("enable", argv
[1])) {
547 ret
= uxc_set(argv
[2], NULL
, true, false);
548 } else if(!strcmp("disable", argv
[1])) {
552 ret
= uxc_set(argv
[2], NULL
, false, false);
553 } else if(!strcmp("delete", argv
[1])) {
557 ret
= uxc_delete(argv
[2]);
558 } else if(!strcmp("create", argv
[1])) {
559 bool autostart
= false;
564 if (!strncmp("true", argv
[4], 5))
567 autostart
= atoi(argv
[4]);
569 ret
= uxc_set(argv
[2], argv
[3], autostart
, true);
580 blob_buf_free(&state
);
582 blob_buf_free(&conf
);
587 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));