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 asprintf(&objname
, "container.%s", name
);
252 ret
= ubus_lookup_id(ctx
, objname
, &id
);
257 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
260 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
262 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
264 const char *container_name
, *instance_name
, *jail_name
;
267 struct runtime_state
*rs
;
269 blobmsg_for_each_attr(cur
, msg
, rem
) {
270 container_name
= blobmsg_name(cur
);
271 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
272 if (!tl
[LIST_INSTANCES
])
275 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
276 instance_name
= blobmsg_name(curi
);
277 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
279 if (!ti
[INSTANCE_JAIL
])
282 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
286 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
288 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
290 if (ti
[INSTANCE_PID
])
291 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
295 if (ti
[INSTANCE_EXITCODE
])
296 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
300 rs
= runtime_alloc(container_name
);
301 rs
->instance_name
= strdup(instance_name
);
302 rs
->jail_name
= strdup(jail_name
);
303 rs
->runtime_pid
= pid
;
304 rs
->exitcode
= exitcode
;
305 rs
->running
= running
;
306 avl_insert(&runtime
, &rs
->avl
);
313 static int runtime_load(void)
315 struct runtime_state
*item
, *tmp
;
318 avl_init(&runtime
, avl_strcmp
, false, NULL
);
319 if (ubus_lookup_id(ctx
, "container", &id
) ||
320 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
324 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
325 get_ocistate(&item
->ocistate
, item
->jail_name
);
330 static void runtime_free(void)
332 struct runtime_state
*item
, *tmp
;
334 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
335 avl_delete(&runtime
, &item
->avl
);
336 free(item
->instance_name
);
337 free(item
->jail_name
);
338 free(item
->ocistate
);
345 static int uxc_state(char *name
)
347 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
348 struct blob_attr
*ocistate
= NULL
;
349 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
352 char *jail_name
= NULL
;
353 static struct blob_buf buf
;
356 ocistate
= s
->ocistate
;
359 printf("%s\n", blobmsg_format_json_indent(ocistate
, true, 0));
363 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
364 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
365 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
368 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
370 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
374 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
382 blob_buf_init(&buf
, 0);
383 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
384 blobmsg_add_string(&buf
, "id", jail_name
);
385 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
386 blobmsg_add_string(&buf
, "bundle", bundle
);
388 printf("%s\n", blobmsg_format_json_indent(buf
.head
, true, 0));
394 static int uxc_list(void)
396 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
398 struct runtime_state
*s
= NULL
;
401 int container_pid
= -1;
404 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
405 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
406 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
409 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
412 name
= blobmsg_get_string(tb
[CONF_NAME
]);
413 s
= avl_find_element(&runtime
, name
, s
, avl
);
415 if (s
&& s
->ocistate
) {
416 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
417 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
418 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
421 printf("[%c] %s %s", autostart
?'*':' ', name
, ocistatus
?:(s
&& s
->running
)?"creating":"stopped");
423 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
424 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
426 if (s
&& s
->running
&& (s
->runtime_pid
>= 0))
427 printf(" runtime pid: %d", s
->runtime_pid
);
429 if (s
&& s
->running
&& (container_pid
>= 0))
430 printf(" container pid: %d", container_pid
);
438 static int uxc_create(char *name
, bool immediately
)
440 static struct blob_buf req
;
441 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
444 struct runtime_state
*s
= NULL
;
445 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
450 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
451 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
452 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
455 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
459 path
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
461 if (tb
[CONF_PIDFILE
])
462 pidfile
= strdup(blobmsg_get_string(tb
[CONF_PIDFILE
]));
464 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
465 tmprwsize
= strdup(blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]));
467 if (tb
[CONF_WRITE_OVERLAY_PATH
])
468 writepath
= strdup(blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]));
476 s
= avl_find_element(&runtime
, name
, s
, avl
);
478 if (s
&& (s
->running
))
482 jailname
= strdup(blobmsg_get_string(tb
[CONF_JAIL
]));
484 blob_buf_init(&req
, 0);
485 blobmsg_add_string(&req
, "name", name
);
486 ins
= blobmsg_open_table(&req
, "instances");
487 in
= blobmsg_open_table(&req
, name
);
488 blobmsg_add_string(&req
, "bundle", path
);
489 j
= blobmsg_open_table(&req
, "jail");
490 blobmsg_add_string(&req
, "name", jailname
?:name
);
491 blobmsg_add_u8(&req
, "immediately", immediately
);
494 blobmsg_add_string(&req
, "pidfile", pidfile
);
496 blobmsg_close_table(&req
, j
);
499 blobmsg_add_string(&req
, "overlaydir", writepath
);
502 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
504 blobmsg_close_table(&req
, in
);
505 blobmsg_close_table(&req
, ins
);
508 fprintf(stderr
, "adding container to procd:\n\t%s\n",
509 blobmsg_format_json_indent(req
.head
, true, 1));
512 if (ubus_lookup_id(ctx
, "container", &id
) ||
513 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
524 static int uxc_start(const char *name
)
529 asprintf(&objname
, "container.%s", name
);
530 if (ubus_lookup_id(ctx
, objname
, &id
))
533 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
536 static int uxc_kill(char *name
, int signal
)
538 static struct blob_buf req
;
539 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
543 struct runtime_state
*s
= NULL
;
546 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
547 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
548 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
551 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
561 s
= avl_find_element(&runtime
, name
, s
, avl
);
563 if (!s
|| !(s
->running
))
566 blob_buf_init(&req
, 0);
567 blobmsg_add_u32(&req
, "signal", signal
);
568 blobmsg_add_string(&req
, "name", name
);
570 asprintf(&objname
, "container.%s", name
);
571 ret
= ubus_lookup_id(ctx
, objname
, &id
);
576 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
583 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
585 static struct blob_buf req
;
586 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
590 char *keeppath
= NULL
;
591 char *tmprwsize
= _tmprwsize
;
592 char *writepath
= _writepath
;
593 char *curvol
, *tmp
, *mnttok
;
598 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
599 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
600 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
603 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
620 if (stat(path
, &sb
) == -1)
623 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
627 ret
= mkdir(UXC_CONFDIR
, 0755);
629 if (ret
&& errno
!= EEXIST
)
632 if (asprintf(&fname
, "%s/%s.json", UXC_CONFDIR
, name
) < 1)
635 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
640 keeppath
= strdup(blobmsg_get_string(tb
[CONF_PATH
]));
641 if (tb
[CONF_WRITE_OVERLAY_PATH
])
642 writepath
= strdup(blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]));
644 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
645 tmprwsize
= strdup(blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]));
648 blob_buf_init(&req
, 0);
649 blobmsg_add_string(&req
, "name", name
);
650 blobmsg_add_string(&req
, "path", path
?:keeppath
);
651 blobmsg_add_u8(&req
, "autostart", autostart
);
653 blobmsg_add_string(&req
, "pidfile", pidfile
);
656 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
659 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
661 if (!add
&& tb
[CONF_VOLUMES
])
662 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
664 if (add
&& requiredmounts
) {
665 mntarr
= blobmsg_open_array(&req
, "volumes");
666 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
667 curvol
= strtok_r(mnttok
, ",;", &tmp
);
671 blobmsg_add_string(&req
, NULL
, curvol
);
673 blobmsg_close_array(&req
, mntarr
);
676 dprintf(f
, "%s\n", blobmsg_format_json_indent(req
.head
, true, 0));
695 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
696 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
697 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
698 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
699 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
700 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
704 /* check if device 'devname' is mounted according to blockd */
705 static int checkblock(const char *uuid
)
707 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
708 struct blob_attr
*cur
;
711 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
712 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
714 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
717 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
730 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
731 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
732 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
735 static const char *resolveuuid(const char *volname
)
737 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
738 struct blob_attr
*cur
;
740 char *tmpvolname
, *replc
;
743 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
744 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
746 if (!tb
[UCI_FSTAB_UUID
])
749 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
752 mntname
= blobmsg_name(cur
);
756 tmpvolname
= strdup(volname
);
757 while ((replc
= strchr(tmpvolname
, '-')))
760 res
= strcmp(tmpvolname
, mntname
);
764 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
770 /* check status of each required volume */
771 static int checkvolumes(struct blob_attr
*volumes
)
773 struct blob_attr
*cur
;
776 blobmsg_for_each_attr(cur
, volumes
, rem
) {
777 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
784 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
786 blockinfo
= blob_memdup(blobmsg_data(msg
));
789 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
791 fstabinfo
= blob_memdup(blobmsg_data(msg
));
794 static int uxc_boot(void)
796 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
797 struct runtime_state
*s
;
798 static struct blob_buf req
;
803 ret
= ubus_lookup_id(ctx
, "block", &id
);
807 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
811 ret
= ubus_lookup_id(ctx
, "uci", &id
);
815 blob_buf_init(&req
, 0);
816 blobmsg_add_string(&req
, "config", "fstab");
817 blobmsg_add_string(&req
, "type", "mount");
819 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
823 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
824 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
825 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
828 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
832 /* make sure all volumes are ready before starting */
833 if (tb
[CONF_VOLUMES
])
834 if (checkvolumes(tb
[CONF_VOLUMES
]))
837 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
838 ret
+= uxc_create(name
, true);
845 static int uxc_delete(char *name
, bool force
)
847 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
848 struct runtime_state
*s
= NULL
;
849 static struct blob_buf req
;
856 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
857 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
858 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
861 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
864 fname
= strdup(blobmsg_name(cur
));
875 s
= avl_find_element(&runtime
, name
, s
, avl
);
877 if (s
&& s
->running
) {
879 ret
= uxc_kill(name
, SIGKILL
);
890 ret
= ubus_lookup_id(ctx
, "container", &id
);
894 blob_buf_init(&req
, 0);
895 blobmsg_add_string(&req
, "name", s
->container_name
);
896 blobmsg_add_string(&req
, "instance", s
->instance_name
);
898 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
905 if (stat(fname
, &sb
) == -1) {
910 if (unlink(fname
) == -1)
918 static void reload_conf(void)
920 blob_buf_free(&conf
);
924 int main(int argc
, char **argv
)
926 enum uxc_cmd cmd
= CMD_UNKNOWN
;
929 char *pidfile
= NULL
;
930 char *tmprwsize
= NULL
;
931 char *writepath
= NULL
;
932 char *requiredmounts
= NULL
;
933 bool autostart
= false;
935 int signal
= SIGTERM
;
941 ctx
= ubus_connect(NULL
);
949 ret
= runtime_load();
954 int option_index
= 0;
955 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
985 printf("uxc %s\n", UXC_VERSION
);
993 requiredmounts
= optarg
;
1001 if (!strcmp("list", argv
[optind
]))
1003 else if (!strcmp("boot", argv
[optind
]))
1005 else if(!strcmp("start", argv
[optind
]))
1007 else if(!strcmp("state", argv
[optind
]))
1009 else if(!strcmp("kill", argv
[optind
]))
1011 else if(!strcmp("enable", argv
[optind
]))
1013 else if(!strcmp("disable", argv
[optind
]))
1015 else if(!strcmp("delete", argv
[optind
]))
1017 else if(!strcmp("create", argv
[optind
]))
1030 if (optind
!= argc
- 2)
1033 ret
= uxc_start(argv
[optind
+ 1]);
1037 if (optind
!= argc
- 2)
1040 ret
= uxc_state(argv
[optind
+ 1]);
1044 if (optind
== (argc
- 3))
1045 signal
= atoi(argv
[optind
+ 2]);
1046 else if (optind
> argc
- 2)
1049 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1053 if (optind
!= argc
- 2)
1056 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1060 if (optind
!= argc
- 2)
1063 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1067 if (optind
!= argc
- 2)
1070 ret
= uxc_delete(argv
[optind
+ 1], force
);
1074 if (optind
!= argc
- 2)
1078 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1085 ret
= uxc_create(argv
[optind
+ 1], false);
1099 blob_buf_free(&conf
);
1104 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));