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.
29 #include <sys/types.h>
32 #include <libubox/avl-cmp.h>
33 #include <libubox/blobmsg.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/ustream.h>
39 #define UXC_VERSION "0.2"
40 #define OCI_VERSION_STRING "1.0.2"
41 #define UXC_ETC_CONFDIR "/etc/uxc"
42 #define UXC_VOL_CONFDIR "/var/run/uvol/.meta/uxc"
44 static bool verbose
= false;
45 static bool json_output
= false;
46 static char *confdir
= UXC_ETC_CONFDIR
;
47 static struct ustream_fd cufd
;
48 static struct ustream_fd lufd
;
51 struct runtime_state
{
59 struct blob_attr
*ocistate
;
76 #define OPT_ARGS "ab:fjm:p:t:vVw:"
77 static struct option long_options
[] = {
78 {"autostart", no_argument
, 0, 'a' },
79 {"console", no_argument
, 0, 'c' },
80 {"bundle", required_argument
, 0, 'b' },
81 {"force", no_argument
, 0, 'f' },
82 {"json", no_argument
, 0, 'j' },
83 {"mounts", required_argument
, 0, 'm' },
84 {"pid-file", required_argument
, 0, 'p' },
85 {"temp-overlay-size", required_argument
, 0, 't' },
86 {"write-overlay-path", required_argument
, 0, 'w' },
87 {"verbose", no_argument
, 0, 'v' },
88 {"version", no_argument
, 0, 'V' },
92 AVL_TREE(runtime
, avl_strcmp
, false, NULL
);
93 static struct blob_buf conf
;
94 static struct blob_attr
*blockinfo
;
95 static struct blob_attr
*fstabinfo
;
96 static struct ubus_context
*ctx
;
98 static int usage(void) {
99 printf("syntax: uxc <command> [parameters ...]\n");
100 printf("commands:\n");
101 printf("\tlist [--json]\t\t\t\tlist all configured containers\n");
102 printf("\tattach <conf>\t\t\t\tattach to container console\n");
103 printf("\tcreate <conf>\t\t\t\t(re-)create <conf>\n");
104 printf("\t\t[--bundle <path>]\t\t\tOCI bundle at <path>\n");
105 printf("\t\t[--autostart]\t\t\t\tstart on boot\n");
106 printf("\t\t[--temp-overlay-size <size>]\t\tuse tmpfs overlay with {size}\n");
107 printf("\t\t[--write-overlay-path <path>]\t\tuse overlay on {path}\n");
108 printf("\t\t[--mounts <v1>,<v2>,...,<vN>]\t\trequire filesystems to be available\n");
109 printf("\tstart [--console] <conf>\t\tstart container <conf>\n");
110 printf("\tstate <conf>\t\t\t\tget state of container <conf>\n");
111 printf("\tkill <conf> [<signal>]\t\t\tsend signal to container <conf>\n");
112 printf("\tenable <conf>\t\t\t\tstart container <conf> on boot\n");
113 printf("\tdisable <conf>\t\t\t\tdon't start container <conf> on boot\n");
114 printf("\tdelete <conf> [--force]\t\t\tdelete <conf>\n");
124 CONF_TEMP_OVERLAY_SIZE
,
125 CONF_WRITE_OVERLAY_PATH
,
130 static const struct blobmsg_policy conf_policy
[__CONF_MAX
] = {
131 [CONF_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
132 [CONF_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
133 [CONF_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
134 [CONF_AUTOSTART
] = { .name
= "autostart", .type
= BLOBMSG_TYPE_BOOL
},
135 [CONF_PIDFILE
] = { .name
= "pidfile", .type
= BLOBMSG_TYPE_STRING
},
136 [CONF_TEMP_OVERLAY_SIZE
] = { .name
= "temp-overlay-size", .type
= BLOBMSG_TYPE_STRING
},
137 [CONF_WRITE_OVERLAY_PATH
] = { .name
= "write-overlay-path", .type
= BLOBMSG_TYPE_STRING
},
138 [CONF_VOLUMES
] = { .name
= "volumes", .type
= BLOBMSG_TYPE_ARRAY
},
141 static int conf_load(void)
143 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
151 if (asprintf(&globstr
, "%s/*.json", UXC_ETC_CONFDIR
) == -1)
154 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
156 gl_flags
|= GLOB_APPEND
;
160 if (!stat(UXC_VOL_CONFDIR
, &sb
)) {
161 if (sb
.st_mode
& S_IFDIR
) {
162 if (asprintf(&globstr
, "%s/*.json", UXC_VOL_CONFDIR
) == -1)
165 res
= glob(globstr
, gl_flags
, NULL
, &gl
);
170 blob_buf_init(&conf
, 0);
171 c
= blobmsg_open_table(&conf
, NULL
);
176 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
177 o
= blobmsg_open_table(&conf
, strdup(gl
.gl_pathv
[j
]));
178 if (!blobmsg_add_json_from_file(&conf
, gl
.gl_pathv
[j
])) {
179 ERROR("uxc: failed to load %s\n", gl
.gl_pathv
[j
]);
182 blobmsg_close_table(&conf
, o
);
184 blobmsg_close_table(&conf
, c
);
195 static const struct blobmsg_policy list_policy
[__LIST_MAX
] = {
196 [LIST_INSTANCES
] = { .name
= "instances", .type
= BLOBMSG_TYPE_TABLE
},
207 static const struct blobmsg_policy instance_policy
[__INSTANCE_MAX
] = {
208 [INSTANCE_RUNNING
] = { .name
= "running", .type
= BLOBMSG_TYPE_BOOL
},
209 [INSTANCE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
210 [INSTANCE_EXITCODE
] = { .name
= "exit_code", .type
= BLOBMSG_TYPE_INT32
},
211 [INSTANCE_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_TABLE
},
219 static const struct blobmsg_policy jail_policy
[__JAIL_MAX
] = {
220 [JAIL_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
223 static struct runtime_state
*
224 runtime_alloc(const char *container_name
)
226 struct runtime_state
*s
;
228 s
= calloc_a(sizeof(*s
), &new_name
, strlen(container_name
) + 1);
229 strcpy(new_name
, container_name
);
230 s
->container_name
= new_name
;
231 s
->avl
.key
= s
->container_name
;
245 static const struct blobmsg_policy state_policy
[__STATE_MAX
] = {
246 [STATE_OCIVERSION
] = { .name
= "ociVersion", .type
= BLOBMSG_TYPE_STRING
},
247 [STATE_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
248 [STATE_STATUS
] = { .name
= "status", .type
= BLOBMSG_TYPE_STRING
},
249 [STATE_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
250 [STATE_BUNDLE
] = { .name
= "bundle", .type
= BLOBMSG_TYPE_STRING
},
251 [STATE_ANNOTATIONS
] = { .name
= "annotations", .type
= BLOBMSG_TYPE_TABLE
},
255 static void ocistate_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
257 struct blob_attr
**ocistate
= (struct blob_attr
**)req
->priv
;
258 struct blob_attr
*tb
[__STATE_MAX
];
260 blobmsg_parse(state_policy
, __STATE_MAX
, tb
, blobmsg_data(msg
), blobmsg_len(msg
));
262 if (!tb
[STATE_OCIVERSION
] ||
268 *ocistate
= blob_memdup(msg
);
271 static void get_ocistate(struct blob_attr
**ocistate
, const char *name
)
278 if (asprintf(&objname
, "container.%s", name
) == -1)
281 ret
= ubus_lookup_id(ctx
, objname
, &id
);
286 ubus_invoke(ctx
, id
, "state", NULL
, ocistate_cb
, ocistate
, 3000);
289 static void list_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
291 struct blob_attr
*cur
, *curi
, *tl
[__LIST_MAX
], *ti
[__INSTANCE_MAX
], *tj
[__JAIL_MAX
];
293 const char *container_name
, *instance_name
, *jail_name
;
296 struct runtime_state
*rs
;
298 blobmsg_for_each_attr(cur
, msg
, rem
) {
299 container_name
= blobmsg_name(cur
);
300 blobmsg_parse(list_policy
, __LIST_MAX
, tl
, blobmsg_data(cur
), blobmsg_len(cur
));
301 if (!tl
[LIST_INSTANCES
])
304 blobmsg_for_each_attr(curi
, tl
[LIST_INSTANCES
], remi
) {
305 instance_name
= blobmsg_name(curi
);
306 blobmsg_parse(instance_policy
, __INSTANCE_MAX
, ti
, blobmsg_data(curi
), blobmsg_len(curi
));
308 if (!ti
[INSTANCE_JAIL
])
311 blobmsg_parse(jail_policy
, __JAIL_MAX
, tj
, blobmsg_data(ti
[INSTANCE_JAIL
]), blobmsg_len(ti
[INSTANCE_JAIL
]));
315 jail_name
= blobmsg_get_string(tj
[JAIL_NAME
]);
317 running
= ti
[INSTANCE_RUNNING
] && blobmsg_get_bool(ti
[INSTANCE_RUNNING
]);
319 if (ti
[INSTANCE_PID
])
320 pid
= blobmsg_get_u32(ti
[INSTANCE_PID
]);
324 if (ti
[INSTANCE_EXITCODE
])
325 exitcode
= blobmsg_get_u32(ti
[INSTANCE_EXITCODE
]);
329 rs
= runtime_alloc(container_name
);
330 rs
->instance_name
= strdup(instance_name
);
331 rs
->jail_name
= strdup(jail_name
);
332 rs
->runtime_pid
= pid
;
333 rs
->exitcode
= exitcode
;
334 rs
->running
= running
;
335 avl_insert(&runtime
, &rs
->avl
);
342 static int runtime_load(void)
344 struct runtime_state
*item
, *tmp
;
347 avl_init(&runtime
, avl_strcmp
, false, NULL
);
348 if (ubus_lookup_id(ctx
, "container", &id
) ||
349 ubus_invoke(ctx
, id
, "list", NULL
, list_cb
, &runtime
, 3000))
353 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
)
354 get_ocistate(&item
->ocistate
, item
->jail_name
);
359 static void runtime_free(void)
361 struct runtime_state
*item
, *tmp
;
363 avl_for_each_element_safe(&runtime
, item
, avl
, tmp
) {
364 avl_delete(&runtime
, &item
->avl
);
365 free(item
->instance_name
);
366 free(item
->jail_name
);
367 free(item
->ocistate
);
374 static inline int setup_tios(int fd
, struct termios
*oldtios
)
376 struct termios newtios
;
382 /* Get current termios */
383 if (tcgetattr(fd
, oldtios
))
388 /* We use the same settings that ssh does. */
389 newtios
.c_iflag
|= IGNPAR
;
390 newtios
.c_iflag
&= ~(ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
| IXANY
| IXOFF
);
391 newtios
.c_lflag
&= ~(TOSTOP
| ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHONL
);
392 newtios
.c_oflag
&= ~ONLCR
;
393 newtios
.c_oflag
|= OPOST
;
394 newtios
.c_cc
[VMIN
] = 1;
395 newtios
.c_cc
[VTIME
] = 0;
397 /* Set new attributes */
398 if (tcsetattr(fd
, TCSAFLUSH
, &newtios
))
405 static void client_cb(struct ustream
*s
, int bytes
)
411 buf
= ustream_get_read_buf(s
, &len
);
415 rv
= ustream_write(&lufd
.stream
, buf
, len
, false);
418 ustream_consume(s
, rv
);
425 static void local_cb(struct ustream
*s
, int bytes
)
431 buf
= ustream_get_read_buf(s
, &len
);
435 if ((len
> 0) && (buf
[0] == 2))
438 rv
= ustream_write(&cufd
.stream
, buf
, len
, false);
441 ustream_consume(s
, rv
);
448 static int uxc_attach(const char *container_name
)
450 struct ubus_context
*ctx
;
452 static struct blob_buf req
;
453 int client_fd
, server_fd
, tty_fd
;
454 struct termios oldtermios
;
456 ctx
= ubus_connect(NULL
);
458 fprintf(stderr
, "can't connect to ubus!\n");
462 /* open pseudo-terminal pair */
463 client_fd
= posix_openpt(O_RDWR
| O_NOCTTY
);
465 fprintf(stderr
, "can't create virtual console!\n");
469 setup_tios(client_fd
, &oldtermios
);
472 server_fd
= open(ptsname(client_fd
), O_RDWR
| O_NOCTTY
);
474 fprintf(stderr
, "can't open virtual console!\n");
479 setup_tios(server_fd
, &oldtermios
);
481 tty_fd
= open("/dev/tty", O_RDWR
);
483 fprintf(stderr
, "can't open local console!\n");
489 setup_tios(tty_fd
, &oldtermios
);
491 /* register server-side with procd */
492 blob_buf_init(&req
, 0);
493 blobmsg_add_string(&req
, "name", container_name
);
494 blobmsg_add_string(&req
, "instance", container_name
);
496 if (ubus_lookup_id(ctx
, "container", &id
) ||
497 ubus_invoke_fd(ctx
, id
, "console_attach", req
.head
, NULL
, NULL
, 3000, server_fd
)) {
498 fprintf(stderr
, "ubus request failed\n");
513 /* forward between stdio and client_fd until detach is requested */
514 lufd
.stream
.notify_read
= local_cb
;
515 ustream_fd_init(&lufd
, tty_fd
);
517 cufd
.stream
.notify_read
= client_cb
;
518 /* ToDo: handle remote close and other events */
519 // cufd.stream.notify_state = client_state_cb;
520 ustream_fd_init(&cufd
, client_fd
);
522 fprintf(stderr
, "attaching to jail console. press [CTRL]+[B] to exit.\n");
528 tcsetattr(tty_fd
, TCSAFLUSH
, &oldtermios
);
529 ustream_free(&lufd
.stream
);
530 ustream_free(&cufd
.stream
);
536 static int uxc_state(char *name
)
538 struct runtime_state
*s
= avl_find_element(&runtime
, name
, s
, avl
);
539 struct blob_attr
*ocistate
= NULL
;
540 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
543 char *jail_name
= NULL
;
546 static struct blob_buf buf
;
549 ocistate
= s
->ocistate
;
552 state
= blobmsg_format_json_indent(ocistate
, true, 0);
556 printf("%s\n", state
);
561 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
562 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
563 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
566 if (!strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
]))) {
568 jail_name
= blobmsg_get_string(tb
[CONF_JAIL
]);
572 bundle
= blobmsg_get_string(tb
[CONF_PATH
]);
580 blob_buf_init(&buf
, 0);
581 blobmsg_add_string(&buf
, "ociVersion", OCI_VERSION_STRING
);
582 blobmsg_add_string(&buf
, "id", jail_name
);
583 blobmsg_add_string(&buf
, "status", s
?"stopped":"uninitialized");
584 blobmsg_add_string(&buf
, "bundle", bundle
);
586 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
600 static int uxc_list(void)
602 struct blob_attr
*cur
, *tb
[__CONF_MAX
], *ts
[__STATE_MAX
];
604 struct runtime_state
*s
= NULL
;
605 char *name
, *ocistatus
, *status
, *tmp
;
606 int container_pid
= -1;
608 static struct blob_buf buf
;
612 blob_buf_init(&buf
, 0);
613 arr
= blobmsg_open_array(&buf
, "");
616 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
617 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
618 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
621 autostart
= tb
[CONF_AUTOSTART
] && blobmsg_get_bool(tb
[CONF_AUTOSTART
]);
624 name
= blobmsg_get_string(tb
[CONF_NAME
]);
625 s
= avl_find_element(&runtime
, name
, s
, avl
);
627 if (s
&& s
->ocistate
) {
628 blobmsg_parse(state_policy
, __STATE_MAX
, ts
, blobmsg_data(s
->ocistate
), blobmsg_len(s
->ocistate
));
629 ocistatus
= blobmsg_get_string(ts
[STATE_STATUS
]);
630 container_pid
= blobmsg_get_u32(ts
[STATE_PID
]);
633 status
= ocistatus
?:(s
&& s
->running
)?"creating":"stopped";
636 obj
= blobmsg_open_table(&buf
, "");
637 blobmsg_add_string(&buf
, "name", name
);
638 blobmsg_add_string(&buf
, "status", status
);
639 blobmsg_add_u8(&buf
, "autostart", autostart
);
641 printf("[%c] %s %s", autostart
?'*':' ', name
, status
);
644 if (s
&& !s
->running
&& (s
->exitcode
>= 0)) {
646 blobmsg_add_u32(&buf
, "exitcode", s
->exitcode
);
648 printf(" exitcode: %d (%s)", s
->exitcode
, strerror(s
->exitcode
));
651 if (s
&& s
->running
&& (s
->runtime_pid
>= 0)) {
653 blobmsg_add_u32(&buf
, "runtime_pid", s
->runtime_pid
);
655 printf(" runtime pid: %d", s
->runtime_pid
);
658 if (s
&& s
->running
&& (container_pid
>= 0)) {
660 blobmsg_add_u32(&buf
, "container_pid", container_pid
);
662 printf(" container pid: %d", container_pid
);
668 blobmsg_close_table(&buf
, obj
);
672 blobmsg_close_array(&buf
, arr
);
673 tmp
= blobmsg_format_json_indent(buf
.head
, true, 0);
686 static int uxc_create(char *name
, bool immediately
)
688 static struct blob_buf req
;
689 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
692 struct runtime_state
*s
= NULL
;
693 char *path
= NULL
, *jailname
= NULL
, *pidfile
= NULL
, *tmprwsize
= NULL
, *writepath
= NULL
;
698 s
= avl_find_element(&runtime
, name
, s
, avl
);
700 if (s
&& (s
->running
))
703 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
704 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
705 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
708 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
718 path
= blobmsg_get_string(tb
[CONF_PATH
]);
720 if (tb
[CONF_PIDFILE
])
721 pidfile
= blobmsg_get_string(tb
[CONF_PIDFILE
]);
723 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
724 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
726 if (tb
[CONF_WRITE_OVERLAY_PATH
])
727 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
730 jailname
= blobmsg_get_string(tb
[CONF_JAIL
]);
732 blob_buf_init(&req
, 0);
733 blobmsg_add_string(&req
, "name", name
);
734 ins
= blobmsg_open_table(&req
, "instances");
735 in
= blobmsg_open_table(&req
, name
);
736 blobmsg_add_string(&req
, "bundle", path
);
737 j
= blobmsg_open_table(&req
, "jail");
738 blobmsg_add_string(&req
, "name", jailname
?:name
);
739 blobmsg_add_u8(&req
, "immediately", immediately
);
742 blobmsg_add_string(&req
, "pidfile", pidfile
);
744 blobmsg_close_table(&req
, j
);
747 blobmsg_add_string(&req
, "overlaydir", writepath
);
750 blobmsg_add_string(&req
, "tmpoverlaysize", tmprwsize
);
752 blobmsg_close_table(&req
, in
);
753 blobmsg_close_table(&req
, ins
);
757 tmp
= blobmsg_format_json_indent(req
.head
, true, 1);
761 fprintf(stderr
, "adding container to procd:\n\t%s\n", tmp
);
766 if (ubus_lookup_id(ctx
, "container", &id
) ||
767 ubus_invoke(ctx
, id
, "add", req
.head
, NULL
, NULL
, 3000)) {
775 static int uxc_start(const char *name
, bool console
)
784 exit(uxc_attach(name
));
787 if (asprintf(&objname
, "container.%s", name
) == -1)
790 if (ubus_lookup_id(ctx
, objname
, &id
))
793 return ubus_invoke(ctx
, id
, "start", NULL
, NULL
, NULL
, 3000);
796 static int uxc_kill(char *name
, int signal
)
798 static struct blob_buf req
;
799 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
803 struct runtime_state
*s
= NULL
;
806 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
807 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
808 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
811 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
821 s
= avl_find_element(&runtime
, name
, s
, avl
);
823 if (!s
|| !(s
->running
))
826 blob_buf_init(&req
, 0);
827 blobmsg_add_u32(&req
, "signal", signal
);
828 blobmsg_add_string(&req
, "name", name
);
830 if (asprintf(&objname
, "container.%s", name
) == -1)
833 ret
= ubus_lookup_id(ctx
, objname
, &id
);
838 if (ubus_invoke(ctx
, id
, "kill", req
.head
, NULL
, NULL
, 3000))
845 static int uxc_set(char *name
, char *path
, bool autostart
, bool add
, char *pidfile
, char *_tmprwsize
, char *_writepath
, char *requiredmounts
)
847 static struct blob_buf req
;
848 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
850 const char *cfname
= NULL
;
852 char *keeppath
= NULL
;
853 char *tmprwsize
= _tmprwsize
;
854 char *writepath
= _writepath
;
855 char *curvol
, *tmp
, *mnttok
;
860 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
861 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
862 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
865 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
868 cfname
= blobmsg_name(cur
);
882 if (stat(path
, &sb
) == -1)
885 if ((sb
.st_mode
& S_IFMT
) != S_IFDIR
)
890 ret
= mkdir(confdir
, 0755);
892 if (ret
&& errno
!= EEXIST
)
895 if (asprintf(&fname
, "%s/%s.json", confdir
, name
) == -1)
898 f
= open(fname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
902 f
= open(cfname
, O_WRONLY
| O_TRUNC
, 0644);
908 keeppath
= blobmsg_get_string(tb
[CONF_PATH
]);
909 if (tb
[CONF_WRITE_OVERLAY_PATH
])
910 writepath
= blobmsg_get_string(tb
[CONF_WRITE_OVERLAY_PATH
]);
912 if (tb
[CONF_TEMP_OVERLAY_SIZE
])
913 tmprwsize
= blobmsg_get_string(tb
[CONF_TEMP_OVERLAY_SIZE
]);
916 blob_buf_init(&req
, 0);
917 blobmsg_add_string(&req
, "name", name
);
918 blobmsg_add_string(&req
, "path", path
?:keeppath
);
919 blobmsg_add_u8(&req
, "autostart", autostart
);
921 blobmsg_add_string(&req
, "pidfile", pidfile
);
924 blobmsg_add_string(&req
, "temp-overlay-size", tmprwsize
);
927 blobmsg_add_string(&req
, "write-overlay-path", writepath
);
929 if (!add
&& tb
[CONF_VOLUMES
])
930 blobmsg_add_blob(&req
, tb
[CONF_VOLUMES
]);
932 if (add
&& requiredmounts
) {
933 mntarr
= blobmsg_open_array(&req
, "volumes");
934 for (mnttok
= requiredmounts
; ; mnttok
= NULL
) {
935 curvol
= strtok_r(mnttok
, ",;", &tmp
);
939 blobmsg_add_string(&req
, NULL
, curvol
);
941 blobmsg_close_array(&req
, mntarr
);
943 tmp
= blobmsg_format_json_indent(req
.head
, true, 0);
945 dprintf(f
, "%s\n", tmp
);
964 static const struct blobmsg_policy block_info_policy
[__BLOCK_INFO_MAX
] = {
965 [BLOCK_INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
966 [BLOCK_INFO_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
967 [BLOCK_INFO_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
968 [BLOCK_INFO_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
969 [BLOCK_INFO_MOUNT
] = { .name
= "mount", .type
= BLOBMSG_TYPE_STRING
},
973 /* check if device 'devname' is mounted according to blockd */
974 static int checkblock(const char *uuid
)
976 struct blob_attr
*tb
[__BLOCK_INFO_MAX
];
977 struct blob_attr
*cur
;
980 blobmsg_for_each_attr(cur
, blockinfo
, rem
) {
981 blobmsg_parse(block_info_policy
, __BLOCK_INFO_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
983 if (!tb
[BLOCK_INFO_UUID
] || !tb
[BLOCK_INFO_MOUNT
])
986 if (!strcmp(uuid
, blobmsg_get_string(tb
[BLOCK_INFO_UUID
])))
999 static const struct blobmsg_policy uci_fstab_policy
[__UCI_FSTAB_MAX
] = {
1000 [UCI_FSTAB_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
1001 [UCI_FSTAB_ANONYMOUS
] = { .name
= ".anonymous", .type
= BLOBMSG_TYPE_BOOL
},
1004 static const char *resolveuuid(const char *volname
)
1006 struct blob_attr
*tb
[__UCI_FSTAB_MAX
];
1007 struct blob_attr
*cur
;
1008 const char *mntname
;
1009 char *tmpvolname
, *replc
;
1012 blobmsg_for_each_attr(cur
, fstabinfo
, rem
) {
1013 blobmsg_parse(uci_fstab_policy
, __UCI_FSTAB_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1015 if (!tb
[UCI_FSTAB_UUID
])
1018 if (tb
[UCI_FSTAB_ANONYMOUS
] && blobmsg_get_bool(tb
[UCI_FSTAB_ANONYMOUS
]))
1021 mntname
= blobmsg_name(cur
);
1025 tmpvolname
= strdup(volname
);
1026 while ((replc
= strchr(tmpvolname
, '-')))
1029 res
= strcmp(tmpvolname
, mntname
);
1033 return blobmsg_get_string(tb
[UCI_FSTAB_UUID
]);
1039 /* check status of each required volume */
1040 static int checkvolumes(struct blob_attr
*volumes
)
1042 struct blob_attr
*cur
;
1045 blobmsg_for_each_attr(cur
, volumes
, rem
) {
1046 if (checkblock(resolveuuid(blobmsg_get_string(cur
))))
1053 static void block_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
1055 blockinfo
= blob_memdup(blobmsg_data(msg
));
1058 static void fstab_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
1060 fstabinfo
= blob_memdup(blobmsg_data(msg
));
1063 static int uxc_boot(void)
1065 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
1066 struct runtime_state
*s
;
1067 static struct blob_buf req
;
1072 ret
= ubus_lookup_id(ctx
, "block", &id
);
1076 ret
= ubus_invoke(ctx
, id
, "info", NULL
, block_cb
, NULL
, 3000);
1080 ret
= ubus_lookup_id(ctx
, "uci", &id
);
1084 blob_buf_init(&req
, 0);
1085 blobmsg_add_string(&req
, "config", "fstab");
1086 blobmsg_add_string(&req
, "type", "mount");
1088 ret
= ubus_invoke(ctx
, id
, "get", req
.head
, fstab_cb
, NULL
, 3000);
1092 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
1093 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1094 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
] || !tb
[CONF_AUTOSTART
] || !blobmsg_get_bool(tb
[CONF_AUTOSTART
]))
1097 s
= avl_find_element(&runtime
, blobmsg_get_string(tb
[CONF_NAME
]), s
, avl
);
1101 /* make sure all volumes are ready before starting */
1102 if (tb
[CONF_VOLUMES
])
1103 if (checkvolumes(tb
[CONF_VOLUMES
]))
1106 name
= strdup(blobmsg_get_string(tb
[CONF_NAME
]));
1107 ret
+= uxc_create(name
, true);
1114 static int uxc_delete(char *name
, bool force
)
1116 struct blob_attr
*cur
, *tb
[__CONF_MAX
];
1117 struct runtime_state
*s
= NULL
;
1118 static struct blob_buf req
;
1121 const char *fname
= NULL
;
1124 blobmsg_for_each_attr(cur
, blob_data(conf
.head
), rem
) {
1125 blobmsg_parse(conf_policy
, __CONF_MAX
, tb
, blobmsg_data(cur
), blobmsg_len(cur
));
1126 if (!tb
[CONF_NAME
] || !tb
[CONF_PATH
])
1129 if (strcmp(name
, blobmsg_get_string(tb
[CONF_NAME
])))
1132 fname
= blobmsg_name(cur
);
1139 s
= avl_find_element(&runtime
, name
, s
, avl
);
1141 if (s
&& s
->running
) {
1143 ret
= uxc_kill(name
, SIGKILL
);
1154 ret
= ubus_lookup_id(ctx
, "container", &id
);
1158 blob_buf_init(&req
, 0);
1159 blobmsg_add_string(&req
, "name", s
->container_name
);
1160 blobmsg_add_string(&req
, "instance", s
->instance_name
);
1162 if (ubus_invoke(ctx
, id
, "delete", req
.head
, NULL
, NULL
, 3000)) {
1163 blob_buf_free(&req
);
1169 if (stat(fname
, &sb
) == -1) {
1174 if (unlink(fname
) == -1)
1181 static void reload_conf(void)
1183 blob_buf_free(&conf
);
1187 int main(int argc
, char **argv
)
1189 enum uxc_cmd cmd
= CMD_UNKNOWN
;
1191 char *bundle
= NULL
;
1192 char *pidfile
= NULL
;
1193 char *tmprwsize
= NULL
;
1194 char *writepath
= NULL
;
1195 char *requiredmounts
= NULL
;
1196 bool autostart
= false;
1198 bool console
= false;
1199 int signal
= SIGTERM
;
1205 ctx
= ubus_connect(NULL
);
1213 ret
= runtime_load();
1218 int option_index
= 0;
1219 c
= getopt_long(argc
, argv
, OPT_ARGS
, long_options
, &option_index
);
1257 printf("uxc %s\n", UXC_VERSION
);
1265 requiredmounts
= optarg
;
1273 if (!strcmp("list", argv
[optind
]))
1275 else if (!strcmp("attach", argv
[optind
]))
1277 else if (!strcmp("boot", argv
[optind
]))
1279 else if(!strcmp("start", argv
[optind
]))
1281 else if(!strcmp("state", argv
[optind
]))
1283 else if(!strcmp("kill", argv
[optind
]))
1285 else if(!strcmp("enable", argv
[optind
]))
1287 else if(!strcmp("disable", argv
[optind
]))
1289 else if(!strcmp("delete", argv
[optind
]))
1291 else if(!strcmp("create", argv
[optind
]))
1296 if (optind
!= argc
- 2)
1299 ret
= uxc_attach(argv
[optind
+ 1]);
1311 if (optind
!= argc
- 2)
1314 ret
= uxc_start(argv
[optind
+ 1], console
);
1318 if (optind
!= argc
- 2)
1321 ret
= uxc_state(argv
[optind
+ 1]);
1325 if (optind
== (argc
- 3))
1326 signal
= atoi(argv
[optind
+ 2]);
1327 else if (optind
> argc
- 2)
1330 ret
= uxc_kill(argv
[optind
+ 1], signal
);
1334 if (optind
!= argc
- 2)
1337 ret
= uxc_set(argv
[optind
+ 1], NULL
, true, false, NULL
, NULL
, NULL
, NULL
);
1341 if (optind
!= argc
- 2)
1344 ret
= uxc_set(argv
[optind
+ 1], NULL
, false, false, NULL
, NULL
, NULL
, NULL
);
1348 if (optind
!= argc
- 2)
1351 ret
= uxc_delete(argv
[optind
+ 1], force
);
1355 if (optind
!= argc
- 2)
1359 ret
= uxc_set(argv
[optind
+ 1], bundle
, autostart
, true, pidfile
, tmprwsize
, writepath
, requiredmounts
);
1366 ret
= uxc_create(argv
[optind
+ 1], false);
1380 blob_buf_free(&conf
);
1385 fprintf(stderr
, "uxc error: %s\n", strerror(ret
));