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.1"
39 #define OCI_VERSION_STRING "1.0.2"
40 #define UXC_CONFDIR "/etc/uxc"
41 #define UXC_RUNDIR "/var/run/uxc"
43 struct runtime_state
{
51 struct blob_attr
*ocistate
;
67 #define OPT_ARGS "ab:fp:vV"
68 static struct option long_options
[] = {
69 {"autostart", no_argument
, 0, 'a' },
70 {"bundle", required_argument
, 0, 'b' },
71 {"force", no_argument
, 0, 'f' },
72 {"pid-file", required_argument
, 0, 'p' },
73 {"verbose", no_argument
, 0, 'v' },
74 {"version", no_argument
, 0, 'V' },
78 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
79 static struct blob_buf conf
;
80 static struct blob_buf state
;
81 static struct ubus_context
*ctx
;
83 static int usage(void) {
84 printf("syntax: uxc <command> [parameters ...]\n");
85 printf("commands:\n");
86 printf("\tlist\t\t\t\t\t\tlist all configured containers\n");
87 printf("\tcreate <conf> [--bundle <path>] [--autostart]\tcreate <conf> for OCI bundle at <path>\n");
88 printf("\tstart <conf>\t\t\t\t\tstart container <conf>\n");
89 printf("\tstate <conf>\t\t\t\t\tget state of container <conf>\n");
90 printf("\tkill <conf> [<signal>]\t\t\t\tsend signal to container <conf>\n");
91 printf("\tenable <conf>\t\t\t\t\tstart container <conf> on boot\n");
92 printf("\tdisable <conf>\t\t\t\t\tdon't start container <conf> on boot\n");
93 printf("\tdelete <conf> [--force]\t\t\t\tdelete <conf>\n");
106 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
107 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
108 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
109 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
110 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
111 [CONF_PIDFILE
] = { .name
= "pidfile", .type
= BLOBMSG_TYPE_STRING
},
114 static int conf_load(bool load_state
)
116 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
120 struct blob_buf
*target
= load_state
?&state
:&conf
;
123 if (asprintf(&globstr
, "%s/*.json", load_state
?UXC_RUNDIR
:UXC_CONFDIR
) == -1)
126 blob_buf_init(target
, 0);
127 c
= blobmsg_open_table(target
, NULL
);
129 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
134 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
135 o
= blobmsg_open_table(target
, strdup(gl
.gl_pathv
[j
]));
136 if (!blobmsg_add_json_from_file(target
, gl
.gl_pathv
[j
])) {
137 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
140 blobmsg_close_table(target
, o
);
142 blobmsg_close_table(target
, c
);
153 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
154 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
165 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
166 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
167 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
168 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
169 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
177 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
178 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
181 static struct runtime_state
*
182 runtime_alloc(const char *container_name
)
184 struct runtime_state
*s
;
186 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
187 strcpy(new_name
, container_name
);
188 s
->container_name
= new_name
;
189 s
->avl
.key
= s
->container_name
;
203 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
204 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
205 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
206 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
207 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
208 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
209 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
213 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
215 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
216 struct blob_attr
*tb
[__STATE_MAX
];
218 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
220 if (!tb
[STATE_OCIVERSION
] ||
226 *ocistate
= blob_memdup(msg
);
229 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
236 asprintf(&objname
, "container.%s", name
);
237 ret
= ubus_lookup_id(ctx
, objname
, &id
);
242 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
245 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
247 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
249 const char *container_name
, *instance_name
, *jail_name
;
252 struct runtime_state
*rs
;
254 blobmsg_for_each_attr(cur
, msg
, rem
) {
255 container_name
= blobmsg_name(cur
);
256 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
257 if (!tl
[LIST_INSTANCES
])
260 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
261 instance_name
= blobmsg_name(curi
);
262 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
264 if (!ti
[INSTANCE_JAIL
])
267 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
271 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
273 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
275 if (ti
[INSTANCE_PID
])
276 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
280 if (ti
[INSTANCE_EXITCODE
])
281 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
285 rs
= runtime_alloc(container_name
);
286 rs
->instance_name
= strdup(instance_name
);
287 rs
->jail_name
= strdup(jail_name
);
288 rs
->runtime_pid
= pid
;
289 rs
->exitcode
= exitcode
;
290 rs
->running
= running
;
291 avl_insert(&runtime
, &rs
->avl
);
298 static int runtime_load(void)
300 struct runtime_state
*item
, *tmp
;
303 avl_init(&runtime
, avl_strcmp
, false, NULL
);
304 if (ubus_lookup_id(ctx
, "container", &id
) ||
305 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
309 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
310 get_ocistate(&item
->ocistate
, item
->jail_name
);
315 static void runtime_free(void)
317 struct runtime_state
*item
, *tmp
;
319 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
320 avl_delete(&runtime
, &item
->avl
);
321 free(item
->instance_name
);
322 free(item
->jail_name
);
323 free(item
->ocistate
);
330 static int uxc_state(char *name
)
332 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
333 struct blob_attr
*ocistate
= NULL
;
334 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
337 char *jail_name
= NULL
;
338 static struct blob_buf buf
;
341 ocistate
= s
->ocistate
;
344 printf("%s\n", blobmsg_format_json_indent(ocistate
, true, 0));
348 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
349 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
350 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
353 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
355 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
359 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
367 blob_buf_init(&buf
, 0);
368 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
369 blobmsg_add_string(&buf
, "id", jail_name
);
370 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
371 blobmsg_add_string(&buf
, "bundle", bundle
);
373 printf("%s\n", blobmsg_format_json_indent(buf
.head
, true, 0));
379 static int uxc_list(void)
381 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
383 struct runtime_state
*s
= NULL
;
386 int container_pid
= -1;
389 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
390 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
391 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
394 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
397 name
= blobmsg_get_string(tb
[CONF_NAME
]);
398 s
= avl_find_element(&runtime
, name
, s
, avl
);
400 if (s
&& s
->ocistate
) {
401 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
402 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
403 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
406 printf("[%c] %s %s", autostart
?'*':' ', name
, ocistatus
?:(s
&& s
->running
)?"creating":"stopped");
408 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
409 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
411 if (s
&& s
->running
&& (s
->runtime_pid
>= 0))
412 printf(" runtime pid: %d", s
->runtime_pid
);
414 if (s
&& s
->running
&& (container_pid
>= 0))
415 printf(" container pid: %d", container_pid
);
423 static int uxc_create(char *name
, bool immediately
)
425 static struct blob_buf req
;
426 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
429 struct runtime_state
*s
= NULL
;
430 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
;
434 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
435 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
436 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
439 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
443 path
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
445 if (tb
[CONF_PIDFILE
])
446 pidfile
= strdup(blobmsg_get_string(tb
[CONF_PIDFILE
]));
453 s
= avl_find_element(&runtime
, name
, s
, avl
);
455 if (s
&& (s
->running
))
459 jailname
= strdup(blobmsg_get_string(tb
[CONF_JAIL
]));
461 blob_buf_init(&req
, 0);
462 blobmsg_add_string(&req
, "name", name
);
463 ins
= blobmsg_open_table(&req
, "instances");
464 in
= blobmsg_open_table(&req
, name
);
465 blobmsg_add_string(&req
, "bundle", path
);
466 j
= blobmsg_open_table(&req
, "jail");
467 blobmsg_add_string(&req
, "name", jailname
?:name
);
468 blobmsg_add_u8(&req
, "immediately", immediately
);
470 blobmsg_add_string(&req
, "pidfile", pidfile
);
472 blobmsg_close_table(&req
, j
);
473 blobmsg_close_table(&req
, in
);
474 blobmsg_close_table(&req
, ins
);
477 if (ubus_lookup_id(ctx
, "container", &id
) ||
478 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
489 static int uxc_start(const char *name
)
494 asprintf(&objname
, "container.%s", name
);
495 if (ubus_lookup_id(ctx
, objname
, &id
))
498 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
501 static int uxc_kill(char *name
, int signal
)
503 static struct blob_buf req
;
504 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
508 struct runtime_state
*s
= NULL
;
511 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
512 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
513 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
516 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
526 s
= avl_find_element(&runtime
, name
, s
, avl
);
528 if (!s
|| !(s
->running
))
531 blob_buf_init(&req
, 0);
532 blobmsg_add_u32(&req
, "signal", signal
);
533 blobmsg_add_string(&req
, "name", name
);
535 asprintf(&objname
, "container.%s", name
);
536 ret
= ubus_lookup_id(ctx
, objname
, &id
);
541 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
548 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
)
550 static struct blob_buf req
;
551 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
555 char *keeppath
= NULL
;
559 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
560 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
561 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
564 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
581 if (stat(path
, &sb
) == -1)
584 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
588 ret
= mkdir(UXC_CONFDIR
, 0755);
590 if (ret
&& errno
!= EEXIST
)
593 if (asprintf(&fname
, "%s/%s.json", UXC_CONFDIR
, name
) < 1)
596 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
601 keeppath
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
603 blob_buf_init(&req
, 0);
604 blobmsg_add_string(&req
, "name", name
);
605 blobmsg_add_string(&req
, "path", path
?:keeppath
);
606 blobmsg_add_u8(&req
, "autostart", autostart
);
608 blobmsg_add_string(&req
, "pidfile", pidfile
);
610 dprintf(f
, "%s\n", blobmsg_format_json_indent(req
.head
, true, 0));
620 static int uxc_boot(void)
622 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
626 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
627 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
628 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
631 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
632 ret
+= uxc_create(name
, true);
639 static int uxc_delete(char *name
, bool force
)
641 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
642 struct runtime_state
*s
= NULL
;
643 static struct blob_buf req
;
650 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
651 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
652 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
655 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
658 fname
= strdup(blobmsg_name(cur
));
669 s
= avl_find_element(&runtime
, name
, s
, avl
);
671 if (s
&& s
->running
) {
673 ret
= uxc_kill(name
, SIGKILL
);
684 ret
= ubus_lookup_id(ctx
, "container", &id
);
688 blob_buf_init(&req
, 0);
689 blobmsg_add_string(&req
, "name", s
->container_name
);
690 blobmsg_add_string(&req
, "instance", s
->instance_name
);
692 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
699 if (stat(fname
, &sb
) == -1) {
704 if (unlink(fname
) == -1)
712 static void reload_conf(void)
714 blob_buf_free(&conf
);
719 int main(int argc
, char **argv
)
721 enum uxc_cmd cmd
= CMD_UNKNOWN
;
724 char *pidfile
= NULL
;
725 bool autostart
= false;
727 bool verbose
= false;
728 int signal
= SIGTERM
;
734 ctx
= ubus_connect(NULL
);
738 ret
= conf_load(false);
742 ret
= mkdir(UXC_RUNDIR
, 0755);
743 if (ret
&& errno
!= EEXIST
)
746 ret
= conf_load(true);
750 ret
= runtime_load();
755 int option_index
= 0;
756 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
782 printf("uxc %s\n", UXC_VERSION
);
790 if (!strcmp("list", argv
[optind
]))
792 else if (!strcmp("boot", argv
[optind
]))
794 else if(!strcmp("start", argv
[optind
]))
796 else if(!strcmp("state", argv
[optind
]))
798 else if(!strcmp("kill", argv
[optind
]))
800 else if(!strcmp("enable", argv
[optind
]))
802 else if(!strcmp("disable", argv
[optind
]))
804 else if(!strcmp("delete", argv
[optind
]))
806 else if(!strcmp("create", argv
[optind
]))
819 if (optind
!= argc
- 2)
822 ret
= uxc_start(argv
[optind
+ 1]);
826 if (optind
!= argc
- 2)
829 ret
= uxc_state(argv
[optind
+ 1]);
833 if (optind
== (argc
- 3))
834 signal
= atoi(argv
[optind
+ 2]);
835 else if (optind
> argc
- 2)
838 ret
= uxc_kill(argv
[optind
+ 1], signal
);
842 if (optind
!= argc
- 2)
845 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
);
849 if (optind
!= argc
- 2)
852 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
);
856 if (optind
!= argc
- 2)
859 ret
= uxc_delete(argv
[optind
+ 1], force
);
863 if (optind
!= argc
- 2)
867 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
);
874 ret
= uxc_create(argv
[optind
+ 1], false);
888 blob_buf_free(&state
);
890 blob_buf_free(&conf
);
895 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));