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/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
;
143 if (!stat(UXC_VOL_CONFDIR
, &sb
)) {
144 if (sb
.st_mode
& S_IFDIR
)
145 confdir
= UXC_VOL_CONFDIR
;
148 if (asprintf(&globstr
, "%s/*.json", confdir
) == -1)
151 blob_buf_init(&conf
, 0);
152 c
= blobmsg_open_table(&conf
, NULL
);
154 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
159 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
160 o
= blobmsg_open_table(&conf
, strdup(gl
.gl_pathv
[j
]));
161 if (!blobmsg_add_json_from_file(&conf
, gl
.gl_pathv
[j
])) {
162 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
165 blobmsg_close_table(&conf
, o
);
167 blobmsg_close_table(&conf
, c
);
178 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
179 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
190 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
191 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
192 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
193 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
194 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
202 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
203 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
206 static struct runtime_state
*
207 runtime_alloc(const char *container_name
)
209 struct runtime_state
*s
;
211 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
212 strcpy(new_name
, container_name
);
213 s
->container_name
= new_name
;
214 s
->avl
.key
= s
->container_name
;
228 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
229 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
230 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
231 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
232 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
233 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
234 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
238 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
240 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
241 struct blob_attr
*tb
[__STATE_MAX
];
243 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
245 if (!tb
[STATE_OCIVERSION
] ||
251 *ocistate
= blob_memdup(msg
);
254 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
261 if (asprintf(&objname
, "container.%s", name
) == -1)
264 ret
= ubus_lookup_id(ctx
, objname
, &id
);
269 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
272 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
274 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
276 const char *container_name
, *instance_name
, *jail_name
;
279 struct runtime_state
*rs
;
281 blobmsg_for_each_attr(cur
, msg
, rem
) {
282 container_name
= blobmsg_name(cur
);
283 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
284 if (!tl
[LIST_INSTANCES
])
287 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
288 instance_name
= blobmsg_name(curi
);
289 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
291 if (!ti
[INSTANCE_JAIL
])
294 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
298 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
300 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
302 if (ti
[INSTANCE_PID
])
303 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
307 if (ti
[INSTANCE_EXITCODE
])
308 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
312 rs
= runtime_alloc(container_name
);
313 rs
->instance_name
= strdup(instance_name
);
314 rs
->jail_name
= strdup(jail_name
);
315 rs
->runtime_pid
= pid
;
316 rs
->exitcode
= exitcode
;
317 rs
->running
= running
;
318 avl_insert(&runtime
, &rs
->avl
);
325 static int runtime_load(void)
327 struct runtime_state
*item
, *tmp
;
330 avl_init(&runtime
, avl_strcmp
, false, NULL
);
331 if (ubus_lookup_id(ctx
, "container", &id
) ||
332 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
336 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
337 get_ocistate(&item
->ocistate
, item
->jail_name
);
342 static void runtime_free(void)
344 struct runtime_state
*item
, *tmp
;
346 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
347 avl_delete(&runtime
, &item
->avl
);
348 free(item
->instance_name
);
349 free(item
->jail_name
);
350 free(item
->ocistate
);
357 static int uxc_state(char *name
)
359 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
360 struct blob_attr
*ocistate
= NULL
;
361 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
364 char *jail_name
= NULL
;
367 static struct blob_buf buf
;
370 ocistate
= s
->ocistate
;
373 state
= blobmsg_format_json_indent(ocistate
, true, 0);
377 printf("%s\n", state
);
382 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
383 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
384 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
387 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
389 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
393 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
401 blob_buf_init(&buf
, 0);
402 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
403 blobmsg_add_string(&buf
, "id", jail_name
);
404 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
405 blobmsg_add_string(&buf
, "bundle", bundle
);
407 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
421 static int uxc_list(void)
423 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
425 struct runtime_state
*s
= NULL
;
426 char *name
, *ocistatus
, *status
, *tmp
;
427 int container_pid
= -1;
429 static struct blob_buf buf
;
433 blob_buf_init(&buf
, 0);
434 arr
= blobmsg_open_array(&buf
, "");
437 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
438 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
439 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
442 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
445 name
= blobmsg_get_string(tb
[CONF_NAME
]);
446 s
= avl_find_element(&runtime
, name
, s
, avl
);
448 if (s
&& s
->ocistate
) {
449 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
450 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
451 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
454 status
= ocistatus
?:(s
&& s
->running
)?"creating":"stopped";
457 obj
= blobmsg_open_table(&buf
, "");
458 blobmsg_add_string(&buf
, "name", name
);
459 blobmsg_add_string(&buf
, "status", status
);
460 blobmsg_add_u8(&buf
, "autostart", autostart
);
462 printf("[%c] %s %s", autostart
?'*':' ', name
, status
);
465 if (s
&& !s
->running
&& (s
->exitcode
>= 0)) {
467 blobmsg_add_u32(&buf
, "exitcode", s
->exitcode
);
469 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
472 if (s
&& s
->running
&& (s
->runtime_pid
>= 0)) {
474 blobmsg_add_u32(&buf
, "runtime_pid", s
->runtime_pid
);
476 printf(" runtime pid: %d", s
->runtime_pid
);
479 if (s
&& s
->running
&& (container_pid
>= 0)) {
481 blobmsg_add_u32(&buf
, "container_pid", container_pid
);
483 printf(" container pid: %d", container_pid
);
489 blobmsg_close_table(&buf
, obj
);
493 blobmsg_close_array(&buf
, arr
);
494 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
507 static int uxc_create(char *name
, bool immediately
)
509 static struct blob_buf req
;
510 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
513 struct runtime_state
*s
= NULL
;
514 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
519 s
= avl_find_element(&runtime
, name
, s
, avl
);
521 if (s
&& (s
->running
))
524 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
525 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
526 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
529 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
539 path
= blobmsg_get_string(tb
[CONF_PATH
]);
541 if (tb
[CONF_PIDFILE
])
542 pidfile
= blobmsg_get_string(tb
[CONF_PIDFILE
]);
544 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
545 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
547 if (tb
[CONF_WRITE_OVERLAY_PATH
])
548 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
551 jailname
= blobmsg_get_string(tb
[CONF_JAIL
]);
553 blob_buf_init(&req
, 0);
554 blobmsg_add_string(&req
, "name", name
);
555 ins
= blobmsg_open_table(&req
, "instances");
556 in
= blobmsg_open_table(&req
, name
);
557 blobmsg_add_string(&req
, "bundle", path
);
558 j
= blobmsg_open_table(&req
, "jail");
559 blobmsg_add_string(&req
, "name", jailname
?:name
);
560 blobmsg_add_u8(&req
, "immediately", immediately
);
563 blobmsg_add_string(&req
, "pidfile", pidfile
);
565 blobmsg_close_table(&req
, j
);
568 blobmsg_add_string(&req
, "overlaydir", writepath
);
571 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
573 blobmsg_close_table(&req
, in
);
574 blobmsg_close_table(&req
, ins
);
578 tmp
= blobmsg_format_json_indent(req
.head
, true, 1);
582 fprintf(stderr
, "adding container to procd:\n\t%s\n", tmp
);
587 if (ubus_lookup_id(ctx
, "container", &id
) ||
588 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
596 static int uxc_start(const char *name
)
601 if (asprintf(&objname
, "container.%s", name
) == -1)
604 if (ubus_lookup_id(ctx
, objname
, &id
))
607 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
610 static int uxc_kill(char *name
, int signal
)
612 static struct blob_buf req
;
613 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
617 struct runtime_state
*s
= NULL
;
620 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
621 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
622 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
625 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
635 s
= avl_find_element(&runtime
, name
, s
, avl
);
637 if (!s
|| !(s
->running
))
640 blob_buf_init(&req
, 0);
641 blobmsg_add_u32(&req
, "signal", signal
);
642 blobmsg_add_string(&req
, "name", name
);
644 if (asprintf(&objname
, "container.%s", name
) == -1)
647 ret
= ubus_lookup_id(ctx
, objname
, &id
);
652 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
659 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
661 static struct blob_buf req
;
662 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
666 char *keeppath
= NULL
;
667 char *tmprwsize
= _tmprwsize
;
668 char *writepath
= _writepath
;
669 char *curvol
, *tmp
, *mnttok
;
674 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
675 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
676 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
679 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
696 if (stat(path
, &sb
) == -1)
699 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
703 ret
= mkdir(confdir
, 0755);
705 if (ret
&& errno
!= EEXIST
)
708 if (asprintf(&fname
, "%s/%s.json", confdir
, name
) == -1)
711 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
716 keeppath
= blobmsg_get_string(tb
[CONF_PATH
]);
717 if (tb
[CONF_WRITE_OVERLAY_PATH
])
718 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
720 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
721 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
724 blob_buf_init(&req
, 0);
725 blobmsg_add_string(&req
, "name", name
);
726 blobmsg_add_string(&req
, "path", path
?:keeppath
);
727 blobmsg_add_u8(&req
, "autostart", autostart
);
729 blobmsg_add_string(&req
, "pidfile", pidfile
);
732 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
735 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
737 if (!add
&& tb
[CONF_VOLUMES
])
738 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
740 if (add
&& requiredmounts
) {
741 mntarr
= blobmsg_open_array(&req
, "volumes");
742 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
743 curvol
= strtok_r(mnttok
, ",;", &tmp
);
747 blobmsg_add_string(&req
, NULL
, curvol
);
749 blobmsg_close_array(&req
, mntarr
);
751 tmp
= blobmsg_format_json_indent(req
.head
, true, 0);
753 dprintf(f
, "%s\n", tmp
);
772 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
773 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
774 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
775 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
776 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
777 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
781 /* check if device 'devname' is mounted according to blockd */
782 static int checkblock(const char *uuid
)
784 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
785 struct blob_attr
*cur
;
788 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
789 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
791 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
794 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
807 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
808 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
809 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
812 static const char *resolveuuid(const char *volname
)
814 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
815 struct blob_attr
*cur
;
817 char *tmpvolname
, *replc
;
820 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
821 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
823 if (!tb
[UCI_FSTAB_UUID
])
826 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
829 mntname
= blobmsg_name(cur
);
833 tmpvolname
= strdup(volname
);
834 while ((replc
= strchr(tmpvolname
, '-')))
837 res
= strcmp(tmpvolname
, mntname
);
841 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
847 /* check status of each required volume */
848 static int checkvolumes(struct blob_attr
*volumes
)
850 struct blob_attr
*cur
;
853 blobmsg_for_each_attr(cur
, volumes
, rem
) {
854 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
861 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
863 blockinfo
= blob_memdup(blobmsg_data(msg
));
866 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
868 fstabinfo
= blob_memdup(blobmsg_data(msg
));
871 static int uxc_boot(void)
873 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
874 struct runtime_state
*s
;
875 static struct blob_buf req
;
880 ret
= ubus_lookup_id(ctx
, "block", &id
);
884 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
888 ret
= ubus_lookup_id(ctx
, "uci", &id
);
892 blob_buf_init(&req
, 0);
893 blobmsg_add_string(&req
, "config", "fstab");
894 blobmsg_add_string(&req
, "type", "mount");
896 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
900 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
901 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
902 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
905 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
909 /* make sure all volumes are ready before starting */
910 if (tb
[CONF_VOLUMES
])
911 if (checkvolumes(tb
[CONF_VOLUMES
]))
914 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
915 ret
+= uxc_create(name
, true);
922 static int uxc_delete(char *name
, bool force
)
924 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
925 struct runtime_state
*s
= NULL
;
926 static struct blob_buf req
;
933 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
934 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
935 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
938 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
941 fname
= strdup(blobmsg_name(cur
));
952 s
= avl_find_element(&runtime
, name
, s
, avl
);
954 if (s
&& s
->running
) {
956 ret
= uxc_kill(name
, SIGKILL
);
967 ret
= ubus_lookup_id(ctx
, "container", &id
);
971 blob_buf_init(&req
, 0);
972 blobmsg_add_string(&req
, "name", s
->container_name
);
973 blobmsg_add_string(&req
, "instance", s
->instance_name
);
975 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
982 if (stat(fname
, &sb
) == -1) {
987 if (unlink(fname
) == -1)
995 static void reload_conf(void)
997 blob_buf_free(&conf
);
1001 int main(int argc
, char **argv
)
1003 enum uxc_cmd cmd
= CMD_UNKNOWN
;
1005 char *bundle
= NULL
;
1006 char *pidfile
= NULL
;
1007 char *tmprwsize
= NULL
;
1008 char *writepath
= NULL
;
1009 char *requiredmounts
= NULL
;
1010 bool autostart
= false;
1012 int signal
= SIGTERM
;
1018 ctx
= ubus_connect(NULL
);
1026 ret
= runtime_load();
1031 int option_index
= 0;
1032 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
1066 printf("uxc %s\n", UXC_VERSION
);
1074 requiredmounts
= optarg
;
1082 if (!strcmp("list", argv
[optind
]))
1084 else if (!strcmp("boot", argv
[optind
]))
1086 else if(!strcmp("start", argv
[optind
]))
1088 else if(!strcmp("state", argv
[optind
]))
1090 else if(!strcmp("kill", argv
[optind
]))
1092 else if(!strcmp("enable", argv
[optind
]))
1094 else if(!strcmp("disable", argv
[optind
]))
1096 else if(!strcmp("delete", argv
[optind
]))
1098 else if(!strcmp("create", argv
[optind
]))
1111 if (optind
!= argc
- 2)
1114 ret
= uxc_start(argv
[optind
+ 1]);
1118 if (optind
!= argc
- 2)
1121 ret
= uxc_state(argv
[optind
+ 1]);
1125 if (optind
== (argc
- 3))
1126 signal
= atoi(argv
[optind
+ 2]);
1127 else if (optind
> argc
- 2)
1130 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1134 if (optind
!= argc
- 2)
1137 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1141 if (optind
!= argc
- 2)
1144 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1148 if (optind
!= argc
- 2)
1151 ret
= uxc_delete(argv
[optind
+ 1], force
);
1155 if (optind
!= argc
- 2)
1159 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1166 ret
= uxc_create(argv
[optind
+ 1], false);
1180 blob_buf_free(&conf
);
1185 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));