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 char *confdir
= UXC_ETC_CONFDIR
;
46 struct runtime_state
{
54 struct blob_attr
*ocistate
;
70 #define OPT_ARGS "ab:fm:p:t:vVw:"
71 static struct option long_options
[] = {
72 {"autostart", no_argument
, 0, 'a' },
73 {"bundle", required_argument
, 0, 'b' },
74 {"force", no_argument
, 0, 'f' },
75 {"mounts", required_argument
, 0, 'm' },
76 {"pid-file", required_argument
, 0, 'p' },
77 {"temp-overlay-size", required_argument
, 0, 't' },
78 {"write-overlay-path", required_argument
, 0, 'w' },
79 {"verbose", no_argument
, 0, 'v' },
80 {"version", no_argument
, 0, 'V' },
84 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
85 static struct blob_buf conf
;
86 static struct blob_attr
*blockinfo
;
87 static struct blob_attr
*fstabinfo
;
88 static struct ubus_context
*ctx
;
90 static int usage(void) {
91 printf("syntax: uxc <command> [parameters ...]\n");
92 printf("commands:\n");
93 printf("\tlist\t\t\t\t\t\tlist all configured containers\n");
94 printf("\tcreate <conf>\t\t\t\t\t(re-)create <conf>\n");
95 printf(" [--bundle <path>]\t\t\tOCI bundle at <path>\n");
96 printf(" [--autostart]\t\t\t\tstart on boot\n");
97 printf(" [--temp-overlay-size size]\t\tuse tmpfs overlay with {size}\n");
98 printf(" [--write-overlay-path path]\t\tuse overlay on {path}\n");
99 printf(" [--mounts v1,v2,...,vN]\t\trequire filesystems to be available\n");
100 printf("\tstart <conf>\t\t\t\t\tstart container <conf>\n");
101 printf("\tstate <conf>\t\t\t\t\tget state of container <conf>\n");
102 printf("\tkill <conf> [<signal>]\t\t\t\tsend signal to container <conf>\n");
103 printf("\tenable <conf>\t\t\t\t\tstart container <conf> on boot\n");
104 printf("\tdisable <conf>\t\t\t\t\tdon't start container <conf> on boot\n");
105 printf("\tdelete <conf> [--force]\t\t\t\tdelete <conf>\n");
115 CONF_TEMP_OVERLAY_SIZE
,
116 CONF_WRITE_OVERLAY_PATH
,
121 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
122 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
123 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
124 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
125 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
126 [CONF_PIDFILE
] = { .name
= "pidfile", .type
= BLOBMSG_TYPE_STRING
},
127 [CONF_TEMP_OVERLAY_SIZE
] = { .name
= "temp-overlay-size", .type
= BLOBMSG_TYPE_STRING
},
128 [CONF_WRITE_OVERLAY_PATH
] = { .name
= "write-overlay-path", .type
= BLOBMSG_TYPE_STRING
},
129 [CONF_VOLUMES
] = { .name
= "volumes", .type
= BLOBMSG_TYPE_ARRAY
},
132 static int conf_load(void)
134 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
141 if (!stat(UXC_VOL_CONFDIR
, &sb
)) {
142 if (sb
.st_mode
& S_IFDIR
)
143 confdir
= UXC_VOL_CONFDIR
;
146 if (asprintf(&globstr
, "%s/*.json", confdir
) == -1)
149 blob_buf_init(&conf
, 0);
150 c
= blobmsg_open_table(&conf
, NULL
);
152 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
157 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
158 o
= blobmsg_open_table(&conf
, strdup(gl
.gl_pathv
[j
]));
159 if (!blobmsg_add_json_from_file(&conf
, gl
.gl_pathv
[j
])) {
160 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
163 blobmsg_close_table(&conf
, o
);
165 blobmsg_close_table(&conf
, c
);
176 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
177 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
188 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
189 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
190 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
191 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
192 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
200 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
201 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
204 static struct runtime_state
*
205 runtime_alloc(const char *container_name
)
207 struct runtime_state
*s
;
209 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
210 strcpy(new_name
, container_name
);
211 s
->container_name
= new_name
;
212 s
->avl
.key
= s
->container_name
;
226 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
227 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
228 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
229 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
230 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
231 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
232 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
236 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
238 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
239 struct blob_attr
*tb
[__STATE_MAX
];
241 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
243 if (!tb
[STATE_OCIVERSION
] ||
249 *ocistate
= blob_memdup(msg
);
252 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
259 if (asprintf(&objname
, "container.%s", name
) == -1)
262 ret
= ubus_lookup_id(ctx
, objname
, &id
);
267 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
270 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
272 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
274 const char *container_name
, *instance_name
, *jail_name
;
277 struct runtime_state
*rs
;
279 blobmsg_for_each_attr(cur
, msg
, rem
) {
280 container_name
= blobmsg_name(cur
);
281 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
282 if (!tl
[LIST_INSTANCES
])
285 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
286 instance_name
= blobmsg_name(curi
);
287 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
289 if (!ti
[INSTANCE_JAIL
])
292 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
296 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
298 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
300 if (ti
[INSTANCE_PID
])
301 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
305 if (ti
[INSTANCE_EXITCODE
])
306 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
310 rs
= runtime_alloc(container_name
);
311 rs
->instance_name
= strdup(instance_name
);
312 rs
->jail_name
= strdup(jail_name
);
313 rs
->runtime_pid
= pid
;
314 rs
->exitcode
= exitcode
;
315 rs
->running
= running
;
316 avl_insert(&runtime
, &rs
->avl
);
323 static int runtime_load(void)
325 struct runtime_state
*item
, *tmp
;
328 avl_init(&runtime
, avl_strcmp
, false, NULL
);
329 if (ubus_lookup_id(ctx
, "container", &id
) ||
330 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
334 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
335 get_ocistate(&item
->ocistate
, item
->jail_name
);
340 static void runtime_free(void)
342 struct runtime_state
*item
, *tmp
;
344 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
345 avl_delete(&runtime
, &item
->avl
);
346 free(item
->instance_name
);
347 free(item
->jail_name
);
348 free(item
->ocistate
);
355 static int uxc_state(char *name
)
357 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
358 struct blob_attr
*ocistate
= NULL
;
359 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
362 char *jail_name
= NULL
;
365 static struct blob_buf buf
;
368 ocistate
= s
->ocistate
;
371 state
= blobmsg_format_json_indent(ocistate
, true, 0);
375 printf("%s\n", state
);
380 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
381 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
382 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
385 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
387 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
391 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
399 blob_buf_init(&buf
, 0);
400 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
401 blobmsg_add_string(&buf
, "id", jail_name
);
402 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
403 blobmsg_add_string(&buf
, "bundle", bundle
);
405 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
419 static int uxc_list(void)
421 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
423 struct runtime_state
*s
= NULL
;
426 int container_pid
= -1;
429 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
430 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
431 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
434 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
437 name
= blobmsg_get_string(tb
[CONF_NAME
]);
438 s
= avl_find_element(&runtime
, name
, s
, avl
);
440 if (s
&& s
->ocistate
) {
441 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
442 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
443 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
446 printf("[%c] %s %s", autostart
?'*':' ', name
, ocistatus
?:(s
&& s
->running
)?"creating":"stopped");
448 if (s
&& !s
->running
&& (s
->exitcode
>= 0))
449 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
451 if (s
&& s
->running
&& (s
->runtime_pid
>= 0))
452 printf(" runtime pid: %d", s
->runtime_pid
);
454 if (s
&& s
->running
&& (container_pid
>= 0))
455 printf(" container pid: %d", container_pid
);
463 static int uxc_create(char *name
, bool immediately
)
465 static struct blob_buf req
;
466 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
469 struct runtime_state
*s
= NULL
;
470 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
475 s
= avl_find_element(&runtime
, name
, s
, avl
);
477 if (s
&& (s
->running
))
480 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
481 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
482 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
485 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
494 path
= blobmsg_get_string(tb
[CONF_PATH
]);
496 if (tb
[CONF_PIDFILE
])
497 pidfile
= blobmsg_get_string(tb
[CONF_PIDFILE
]);
499 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
500 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
502 if (tb
[CONF_WRITE_OVERLAY_PATH
])
503 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
507 jailname
= blobmsg_get_string(tb
[CONF_JAIL
]);
509 blob_buf_init(&req
, 0);
510 blobmsg_add_string(&req
, "name", name
);
511 ins
= blobmsg_open_table(&req
, "instances");
512 in
= blobmsg_open_table(&req
, name
);
513 blobmsg_add_string(&req
, "bundle", path
);
514 j
= blobmsg_open_table(&req
, "jail");
515 blobmsg_add_string(&req
, "name", jailname
?:name
);
516 blobmsg_add_u8(&req
, "immediately", immediately
);
519 blobmsg_add_string(&req
, "pidfile", pidfile
);
521 blobmsg_close_table(&req
, j
);
524 blobmsg_add_string(&req
, "overlaydir", writepath
);
527 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
529 blobmsg_close_table(&req
, in
);
530 blobmsg_close_table(&req
, ins
);
534 tmp
= blobmsg_format_json_indent(req
.head
, true, 1);
538 fprintf(stderr
, "adding container to procd:\n\t%s\n", tmp
);
543 if (ubus_lookup_id(ctx
, "container", &id
) ||
544 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
555 static int uxc_start(const char *name
)
560 if (asprintf(&objname
, "container.%s", name
) == -1)
563 if (ubus_lookup_id(ctx
, objname
, &id
))
566 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
569 static int uxc_kill(char *name
, int signal
)
571 static struct blob_buf req
;
572 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
576 struct runtime_state
*s
= NULL
;
579 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
580 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
581 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
584 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
594 s
= avl_find_element(&runtime
, name
, s
, avl
);
596 if (!s
|| !(s
->running
))
599 blob_buf_init(&req
, 0);
600 blobmsg_add_u32(&req
, "signal", signal
);
601 blobmsg_add_string(&req
, "name", name
);
603 if (asprintf(&objname
, "container.%s", name
) == -1)
606 ret
= ubus_lookup_id(ctx
, objname
, &id
);
611 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
618 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
620 static struct blob_buf req
;
621 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
625 char *keeppath
= NULL
;
626 char *tmprwsize
= _tmprwsize
;
627 char *writepath
= _writepath
;
628 char *curvol
, *tmp
, *mnttok
;
633 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
634 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
635 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
638 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
655 if (stat(path
, &sb
) == -1)
658 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
662 ret
= mkdir(confdir
, 0755);
664 if (ret
&& errno
!= EEXIST
)
667 if (asprintf(&fname
, "%s/%s.json", confdir
, name
) == -1)
670 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
675 keeppath
= blobmsg_get_string(tb
[CONF_PATH
]);
676 if (tb
[CONF_WRITE_OVERLAY_PATH
])
677 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
679 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
680 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
683 blob_buf_init(&req
, 0);
684 blobmsg_add_string(&req
, "name", name
);
685 blobmsg_add_string(&req
, "path", path
?:keeppath
);
686 blobmsg_add_u8(&req
, "autostart", autostart
);
688 blobmsg_add_string(&req
, "pidfile", pidfile
);
691 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
694 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
696 if (!add
&& tb
[CONF_VOLUMES
])
697 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
699 if (add
&& requiredmounts
) {
700 mntarr
= blobmsg_open_array(&req
, "volumes");
701 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
702 curvol
= strtok_r(mnttok
, ",;", &tmp
);
706 blobmsg_add_string(&req
, NULL
, curvol
);
708 blobmsg_close_array(&req
, mntarr
);
710 tmp
= blobmsg_format_json_indent(req
.head
, true, 0);
712 dprintf(f
, "%s\n", tmp
);
731 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
732 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
733 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
734 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
735 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
736 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
740 /* check if device 'devname' is mounted according to blockd */
741 static int checkblock(const char *uuid
)
743 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
744 struct blob_attr
*cur
;
747 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
748 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
750 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
753 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
766 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
767 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
768 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
771 static const char *resolveuuid(const char *volname
)
773 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
774 struct blob_attr
*cur
;
776 char *tmpvolname
, *replc
;
779 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
780 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
782 if (!tb
[UCI_FSTAB_UUID
])
785 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
788 mntname
= blobmsg_name(cur
);
792 tmpvolname
= strdup(volname
);
793 while ((replc
= strchr(tmpvolname
, '-')))
796 res
= strcmp(tmpvolname
, mntname
);
800 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
806 /* check status of each required volume */
807 static int checkvolumes(struct blob_attr
*volumes
)
809 struct blob_attr
*cur
;
812 blobmsg_for_each_attr(cur
, volumes
, rem
) {
813 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
820 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
822 blockinfo
= blob_memdup(blobmsg_data(msg
));
825 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
827 fstabinfo
= blob_memdup(blobmsg_data(msg
));
830 static int uxc_boot(void)
832 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
833 struct runtime_state
*s
;
834 static struct blob_buf req
;
839 ret
= ubus_lookup_id(ctx
, "block", &id
);
843 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
847 ret
= ubus_lookup_id(ctx
, "uci", &id
);
851 blob_buf_init(&req
, 0);
852 blobmsg_add_string(&req
, "config", "fstab");
853 blobmsg_add_string(&req
, "type", "mount");
855 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
859 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
860 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
861 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
864 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
868 /* make sure all volumes are ready before starting */
869 if (tb
[CONF_VOLUMES
])
870 if (checkvolumes(tb
[CONF_VOLUMES
]))
873 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
874 ret
+= uxc_create(name
, true);
881 static int uxc_delete(char *name
, bool force
)
883 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
884 struct runtime_state
*s
= NULL
;
885 static struct blob_buf req
;
892 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
893 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
894 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
897 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
900 fname
= strdup(blobmsg_name(cur
));
911 s
= avl_find_element(&runtime
, name
, s
, avl
);
913 if (s
&& s
->running
) {
915 ret
= uxc_kill(name
, SIGKILL
);
926 ret
= ubus_lookup_id(ctx
, "container", &id
);
930 blob_buf_init(&req
, 0);
931 blobmsg_add_string(&req
, "name", s
->container_name
);
932 blobmsg_add_string(&req
, "instance", s
->instance_name
);
934 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
941 if (stat(fname
, &sb
) == -1) {
946 if (unlink(fname
) == -1)
954 static void reload_conf(void)
956 blob_buf_free(&conf
);
960 int main(int argc
, char **argv
)
962 enum uxc_cmd cmd
= CMD_UNKNOWN
;
965 char *pidfile
= NULL
;
966 char *tmprwsize
= NULL
;
967 char *writepath
= NULL
;
968 char *requiredmounts
= NULL
;
969 bool autostart
= false;
971 int signal
= SIGTERM
;
977 ctx
= ubus_connect(NULL
);
985 ret
= runtime_load();
990 int option_index
= 0;
991 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
1021 printf("uxc %s\n", UXC_VERSION
);
1029 requiredmounts
= optarg
;
1037 if (!strcmp("list", argv
[optind
]))
1039 else if (!strcmp("boot", argv
[optind
]))
1041 else if(!strcmp("start", argv
[optind
]))
1043 else if(!strcmp("state", argv
[optind
]))
1045 else if(!strcmp("kill", argv
[optind
]))
1047 else if(!strcmp("enable", argv
[optind
]))
1049 else if(!strcmp("disable", argv
[optind
]))
1051 else if(!strcmp("delete", argv
[optind
]))
1053 else if(!strcmp("create", argv
[optind
]))
1066 if (optind
!= argc
- 2)
1069 ret
= uxc_start(argv
[optind
+ 1]);
1073 if (optind
!= argc
- 2)
1076 ret
= uxc_state(argv
[optind
+ 1]);
1080 if (optind
== (argc
- 3))
1081 signal
= atoi(argv
[optind
+ 2]);
1082 else if (optind
> argc
- 2)
1085 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1089 if (optind
!= argc
- 2)
1092 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1096 if (optind
!= argc
- 2)
1099 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1103 if (optind
!= argc
- 2)
1106 ret
= uxc_delete(argv
[optind
+ 1], force
);
1110 if (optind
!= argc
- 2)
1114 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1121 ret
= uxc_create(argv
[optind
+ 1], false);
1135 blob_buf_free(&conf
);
1140 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));