-
Notifications
You must be signed in to change notification settings - Fork 17
/
uxc.c
1594 lines (1276 loc) · 35.5 KB
/
uxc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020 Daniel Golle <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <glob.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libubus.h>
#include <libubox/avl-cmp.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/ustream.h>
#include "log.h"
#define UXC_VERSION "0.3"
#define OCI_VERSION_STRING "1.0.2"
#define UXC_ETC_CONFDIR "/etc/uxc"
#define UXC_VOL_CONFDIR "/tmp/run/uvol/.meta/uxc"
static bool verbose = false;
static bool json_output = false;
static char *confdir = UXC_ETC_CONFDIR;
static struct ustream_fd cufd;
static struct ustream_fd lufd;
struct runtime_state {
struct avl_node avl;
char *container_name;
char *instance_name;
char *jail_name;
bool running;
int runtime_pid;
int exitcode;
struct blob_attr *ocistate;
};
struct settings {
struct avl_node avl;
char *container_name;
const char *fname;
char *tmprwsize;
char *writepath;
signed char autostart;
struct blob_attr *volumes;
};
enum uxc_cmd {
CMD_ATTACH,
CMD_LIST,
CMD_BOOT,
CMD_START,
CMD_STATE,
CMD_KILL,
CMD_ENABLE,
CMD_DISABLE,
CMD_DELETE,
CMD_CREATE,
CMD_UNKNOWN
};
#define OPT_ARGS "ab:fjm:p:t:vVw:"
static struct option long_options[] = {
{"autostart", no_argument, 0, 'a' },
{"console", no_argument, 0, 'c' },
{"bundle", required_argument, 0, 'b' },
{"force", no_argument, 0, 'f' },
{"json", no_argument, 0, 'j' },
{"mounts", required_argument, 0, 'm' },
{"pid-file", required_argument, 0, 'p' },
{"temp-overlay-size", required_argument, 0, 't' },
{"write-overlay-path", required_argument, 0, 'w' },
{"verbose", no_argument, 0, 'v' },
{"version", no_argument, 0, 'V' },
{0, 0, 0, 0 }
};
AVL_TREE(runtime, avl_strcmp, false, NULL);
AVL_TREE(settings, avl_strcmp, false, NULL);
static struct blob_buf conf;
static struct blob_buf settingsbuf;
static struct blob_attr *blockinfo;
static struct blob_attr *fstabinfo;
static struct ubus_context *ctx;
static int usage(void) {
printf("syntax: uxc <command> [parameters ...]\n");
printf("commands:\n");
printf("\tlist [--json]\t\t\t\tlist all configured containers\n");
printf("\tattach <conf>\t\t\t\tattach to container console\n");
printf("\tcreate <conf>\t\t\t\t(re-)create <conf>\n");
printf("\t\t[--bundle <path>]\t\t\tOCI bundle at <path>\n");
printf("\t\t[--autostart]\t\t\t\tstart on boot\n");
printf("\t\t[--temp-overlay-size <size>]\t\tuse tmpfs overlay with {size}\n");
printf("\t\t[--write-overlay-path <path>]\t\tuse overlay on {path}\n");
printf("\t\t[--mounts <v1>,<v2>,...,<vN>]\t\trequire filesystems to be available\n");
printf("\tstart [--console] <conf>\t\tstart container <conf>\n");
printf("\tstate <conf>\t\t\t\tget state of container <conf>\n");
printf("\tkill <conf> [<signal>]\t\t\tsend signal to container <conf>\n");
printf("\tenable <conf>\t\t\t\tstart container <conf> on boot\n");
printf("\tdisable <conf>\t\t\t\tdon't start container <conf> on boot\n");
printf("\tdelete <conf> [--force]\t\t\tdelete <conf>\n");
return -EINVAL;
}
enum {
CONF_NAME,
CONF_PATH,
CONF_JAIL,
CONF_AUTOSTART,
CONF_PIDFILE,
CONF_TEMP_OVERLAY_SIZE,
CONF_WRITE_OVERLAY_PATH,
CONF_VOLUMES,
__CONF_MAX,
};
static const struct blobmsg_policy conf_policy[__CONF_MAX] = {
[CONF_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
[CONF_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
[CONF_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
[CONF_AUTOSTART] = { .name = "autostart", .type = BLOBMSG_TYPE_BOOL },
[CONF_PIDFILE] = { .name = "pidfile", .type = BLOBMSG_TYPE_STRING },
[CONF_TEMP_OVERLAY_SIZE] = { .name = "temp-overlay-size", .type = BLOBMSG_TYPE_STRING },
[CONF_WRITE_OVERLAY_PATH] = { .name = "write-overlay-path", .type = BLOBMSG_TYPE_STRING },
[CONF_VOLUMES] = { .name = "volumes", .type = BLOBMSG_TYPE_ARRAY },
};
static int conf_load(bool load_settings)
{
int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
int j, res;
glob_t gl;
char *globstr;
void *c, *o;
struct stat sb;
struct blob_buf *target;
if (asprintf(&globstr, "%s/%s*.json", UXC_ETC_CONFDIR, load_settings?"settings/":"") == -1)
return -ENOMEM;
res = glob(globstr, gl_flags, NULL, &gl);
if (res == 0)
gl_flags |= GLOB_APPEND;
free(globstr);
if (!stat(UXC_VOL_CONFDIR, &sb)) {
if (sb.st_mode & S_IFDIR) {
if (asprintf(&globstr, "%s/%s*.json", UXC_VOL_CONFDIR, load_settings?"settings/":"") == -1)
return -ENOMEM;
res = glob(globstr, gl_flags, NULL, &gl);
free(globstr);
}
}
target = load_settings ? &settingsbuf : &conf;
blob_buf_init(target, 0);
c = blobmsg_open_table(target, NULL);
if (res < 0)
return 0;
for (j = 0; j < gl.gl_pathc; j++) {
o = blobmsg_open_table(target, strdup(gl.gl_pathv[j]));
if (!blobmsg_add_json_from_file(target, gl.gl_pathv[j])) {
ERROR("uxc: failed to load %s\n", gl.gl_pathv[j]);
continue;
}
blobmsg_close_table(target, o);
}
blobmsg_close_table(target, c);
globfree(&gl);
return 0;
}
static struct settings *
settings_alloc(const char *container_name)
{
struct settings *s;
char *new_name;
s = calloc_a(sizeof(*s), &new_name, strlen(container_name) + 1);
strcpy(new_name, container_name);
s->container_name = new_name;
s->avl.key = s->container_name;
s->autostart = -1;
s->tmprwsize = NULL;
s->writepath = NULL;
s->volumes = NULL;
return s;
}
static int settings_add(void)
{
struct blob_attr *cur, *tb[__CONF_MAX];
struct settings *s;
int rem, err;
avl_init(&settings, avl_strcmp, false, NULL);
blobmsg_for_each_attr(cur, blob_data(settingsbuf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME])
continue;
if (tb[CONF_TEMP_OVERLAY_SIZE] && tb[CONF_WRITE_OVERLAY_PATH])
return -EINVAL;
s = settings_alloc(blobmsg_get_string(tb[CONF_NAME]));
if (tb[CONF_AUTOSTART])
s->autostart = blobmsg_get_bool(tb[CONF_AUTOSTART]);
if (tb[CONF_TEMP_OVERLAY_SIZE])
s->tmprwsize = blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]);
if (tb[CONF_WRITE_OVERLAY_PATH])
s->writepath = blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]);
s->volumes = tb[CONF_VOLUMES];
s->fname = blobmsg_name(cur);
err = avl_insert(&settings, &s->avl);
if (err) {
fprintf(stderr, "error adding settings for %s\n", blobmsg_get_string(tb[CONF_NAME]));
free(s);
}
}
return 0;
}
static void settings_free(void)
{
struct settings *item, *tmp;
avl_for_each_element_safe(&settings, item, avl, tmp) {
avl_delete(&settings, &item->avl);
free(item);
}
return;
}
enum {
LIST_INSTANCES,
__LIST_MAX,
};
static const struct blobmsg_policy list_policy[__LIST_MAX] = {
[LIST_INSTANCES] = { .name = "instances", .type = BLOBMSG_TYPE_TABLE },
};
enum {
INSTANCE_RUNNING,
INSTANCE_PID,
INSTANCE_EXITCODE,
INSTANCE_JAIL,
__INSTANCE_MAX,
};
static const struct blobmsg_policy instance_policy[__INSTANCE_MAX] = {
[INSTANCE_RUNNING] = { .name = "running", .type = BLOBMSG_TYPE_BOOL },
[INSTANCE_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
[INSTANCE_EXITCODE] = { .name = "exit_code", .type = BLOBMSG_TYPE_INT32 },
[INSTANCE_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_TABLE },
};
enum {
JAIL_NAME,
__JAIL_MAX,
};
static const struct blobmsg_policy jail_policy[__JAIL_MAX] = {
[JAIL_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
};
static struct runtime_state *
runtime_alloc(const char *container_name)
{
struct runtime_state *s;
char *new_name;
s = calloc_a(sizeof(*s), &new_name, strlen(container_name) + 1);
strcpy(new_name, container_name);
s->container_name = new_name;
s->avl.key = s->container_name;
return s;
}
enum {
STATE_OCIVERSION,
STATE_ID,
STATE_STATUS,
STATE_PID,
STATE_BUNDLE,
STATE_ANNOTATIONS,
__STATE_MAX,
};
static const struct blobmsg_policy state_policy[__STATE_MAX] = {
[STATE_OCIVERSION] = { .name = "ociVersion", .type = BLOBMSG_TYPE_STRING },
[STATE_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
[STATE_STATUS] = { .name = "status", .type = BLOBMSG_TYPE_STRING },
[STATE_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
[STATE_BUNDLE] = { .name = "bundle", .type = BLOBMSG_TYPE_STRING },
[STATE_ANNOTATIONS] = { .name = "annotations", .type = BLOBMSG_TYPE_TABLE },
};
static void ocistate_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
struct blob_attr **ocistate = (struct blob_attr **)req->priv;
struct blob_attr *tb[__STATE_MAX];
blobmsg_parse(state_policy, __STATE_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
if (!tb[STATE_OCIVERSION] ||
!tb[STATE_ID] ||
!tb[STATE_STATUS] ||
!tb[STATE_BUNDLE])
return;
*ocistate = blob_memdup(msg);
}
static void get_ocistate(struct blob_attr **ocistate, const char *name)
{
char *objname;
unsigned int id;
int ret;
*ocistate = NULL;
if (asprintf(&objname, "container.%s", name) == -1)
exit(ENOMEM);
ret = ubus_lookup_id(ctx, objname, &id);
free(objname);
if (ret)
return;
ubus_invoke(ctx, id, "state", NULL, ocistate_cb, ocistate, 3000);
}
static void list_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
struct blob_attr *cur, *curi, *tl[__LIST_MAX], *ti[__INSTANCE_MAX], *tj[__JAIL_MAX];
int rem, remi;
const char *container_name, *instance_name, *jail_name;
bool running;
int pid, exitcode;
struct runtime_state *rs;
blobmsg_for_each_attr(cur, msg, rem) {
container_name = blobmsg_name(cur);
blobmsg_parse(list_policy, __LIST_MAX, tl, blobmsg_data(cur), blobmsg_len(cur));
if (!tl[LIST_INSTANCES])
continue;
blobmsg_for_each_attr(curi, tl[LIST_INSTANCES], remi) {
instance_name = blobmsg_name(curi);
blobmsg_parse(instance_policy, __INSTANCE_MAX, ti, blobmsg_data(curi), blobmsg_len(curi));
if (!ti[INSTANCE_JAIL])
continue;
blobmsg_parse(jail_policy, __JAIL_MAX, tj, blobmsg_data(ti[INSTANCE_JAIL]), blobmsg_len(ti[INSTANCE_JAIL]));
if (!tj[JAIL_NAME])
continue;
jail_name = blobmsg_get_string(tj[JAIL_NAME]);
running = ti[INSTANCE_RUNNING] && blobmsg_get_bool(ti[INSTANCE_RUNNING]);
if (ti[INSTANCE_PID])
pid = blobmsg_get_u32(ti[INSTANCE_PID]);
else
pid = -1;
if (ti[INSTANCE_EXITCODE])
exitcode = blobmsg_get_u32(ti[INSTANCE_EXITCODE]);
else
exitcode = -1;
rs = runtime_alloc(container_name);
rs->instance_name = strdup(instance_name);
rs->jail_name = strdup(jail_name);
rs->runtime_pid = pid;
rs->exitcode = exitcode;
rs->running = running;
avl_insert(&runtime, &rs->avl);
}
}
return;
}
static int runtime_load(void)
{
struct runtime_state *item, *tmp;
uint32_t id;
avl_init(&runtime, avl_strcmp, false, NULL);
if (ubus_lookup_id(ctx, "container", &id) ||
ubus_invoke(ctx, id, "list", NULL, list_cb, &runtime, 3000))
return -EIO;
avl_for_each_element_safe(&runtime, item, avl, tmp)
get_ocistate(&item->ocistate, item->jail_name);
return 0;
}
static void runtime_free(void)
{
struct runtime_state *item, *tmp;
avl_for_each_element_safe(&runtime, item, avl, tmp) {
avl_delete(&runtime, &item->avl);
free(item->instance_name);
free(item->jail_name);
free(item->ocistate);
free(item);
}
return;
}
static inline int setup_tios(int fd, struct termios *oldtios)
{
struct termios newtios;
if (!isatty(fd)) {
return -EIO;
}
/* Get current termios */
if (tcgetattr(fd, oldtios) < 0)
return -errno;
newtios = *oldtios;
/* We use the same settings that ssh does. */
newtios.c_iflag |= IGNPAR;
newtios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
newtios.c_lflag &= ~(TOSTOP | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
newtios.c_oflag &= ~ONLCR;
newtios.c_oflag |= OPOST;
newtios.c_cc[VMIN] = 1;
newtios.c_cc[VTIME] = 0;
/* Set new attributes */
if (tcsetattr(fd, TCSAFLUSH, &newtios) < 0)
return -errno;
return 0;
}
static void client_cb(struct ustream *s, int bytes)
{
char *buf;
int len, rv;
do {
buf = ustream_get_read_buf(s, &len);
if (!buf)
break;
rv = ustream_write(&lufd.stream, buf, len, false);
if (rv > 0)
ustream_consume(s, rv);
if (rv <= len)
break;
} while(1);
}
static void local_cb(struct ustream *s, int bytes)
{
char *buf;
int len, rv;
do {
buf = ustream_get_read_buf(s, &len);
if (!buf)
break;
if ((len > 0) && (buf[0] == 2))
uloop_end();
rv = ustream_write(&cufd.stream, buf, len, false);
if (rv > 0)
ustream_consume(s, rv);
if (rv <= len)
break;
} while(1);
}
static int uxc_attach(const char *container_name)
{
struct ubus_context *ctx;
uint32_t id;
static struct blob_buf req;
int client_fd, server_fd, tty_fd;
struct termios oldtermios;
ctx = ubus_connect(NULL);
if (!ctx) {
fprintf(stderr, "can't connect to ubus!\n");
return -ECONNREFUSED;
}
/* open pseudo-terminal pair */
client_fd = posix_openpt(O_RDWR | O_NOCTTY);
if (client_fd < 0) {
fprintf(stderr, "can't create virtual console!\n");
ubus_free(ctx);
return -EIO;
}
setup_tios(client_fd, &oldtermios);
grantpt(client_fd);
unlockpt(client_fd);
server_fd = open(ptsname(client_fd), O_RDWR | O_NOCTTY);
if (server_fd < 0) {
fprintf(stderr, "can't open virtual console!\n");
close(client_fd);
ubus_free(ctx);
return -EIO;
}
setup_tios(server_fd, &oldtermios);
tty_fd = open("/dev/tty", O_RDWR);
if (tty_fd < 0) {
fprintf(stderr, "can't open local console!\n");
close(server_fd);
close(client_fd);
ubus_free(ctx);
return -EIO;
}
setup_tios(tty_fd, &oldtermios);
/* register server-side with procd */
blob_buf_init(&req, 0);
blobmsg_add_string(&req, "name", container_name);
blobmsg_add_string(&req, "instance", container_name);
if (ubus_lookup_id(ctx, "container", &id) ||
ubus_invoke_fd(ctx, id, "console_attach", req.head, NULL, NULL, 3000, server_fd)) {
fprintf(stderr, "ubus request failed\n");
close(tty_fd);
close(server_fd);
close(client_fd);
blob_buf_free(&req);
ubus_free(ctx);
return -ENXIO;
}
close(server_fd);
blob_buf_free(&req);
ubus_free(ctx);
uloop_init();
/* forward between stdio and client_fd until detach is requested */
lufd.stream.notify_read = local_cb;
ustream_fd_init(&lufd, tty_fd);
cufd.stream.notify_read = client_cb;
/* ToDo: handle remote close and other events */
// cufd.stream.notify_state = client_state_cb;
ustream_fd_init(&cufd, client_fd);
fprintf(stderr, "attaching to jail console. press [CTRL]+[B] to exit.\n");
close(0);
close(1);
close(2);
uloop_run();
tcsetattr(tty_fd, TCSAFLUSH, &oldtermios);
ustream_free(&lufd.stream);
ustream_free(&cufd.stream);
close(client_fd);
return 0;
}
static int uxc_state(char *name)
{
struct runtime_state *rsstate = avl_find_element(&runtime, name, rsstate, avl);
struct blob_attr *ocistate = NULL;
struct blob_attr *cur, *tb[__CONF_MAX];
int rem;
char *bundle = NULL;
char *jail_name = NULL;
char *state = NULL;
char *tmp;
static struct blob_buf buf;
if (rsstate)
ocistate = rsstate->ocistate;
if (ocistate) {
state = blobmsg_format_json_indent(ocistate, true, 0);
if (!state)
return -ENOMEM;
printf("%s\n", state);
free(state);
return 0;
}
blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME] || !tb[CONF_PATH])
continue;
if (!strcmp(name, blobmsg_get_string(tb[CONF_NAME]))) {
if (tb[CONF_JAIL])
jail_name = blobmsg_get_string(tb[CONF_JAIL]);
else
jail_name = name;
bundle = blobmsg_get_string(tb[CONF_PATH]);
break;
}
}
if (!bundle)
return -ENOENT;
blob_buf_init(&buf, 0);
blobmsg_add_string(&buf, "ociVersion", OCI_VERSION_STRING);
blobmsg_add_string(&buf, "id", jail_name);
blobmsg_add_string(&buf, "status", rsstate?"stopped":"uninitialized");
blobmsg_add_string(&buf, "bundle", bundle);
tmp = blobmsg_format_json_indent(buf.head, true, 0);
if (!tmp) {
blob_buf_free(&buf);
return -ENOMEM;
}
printf("%s\n", tmp);
free(tmp);
blob_buf_free(&buf);
return 0;
}
static int uxc_list(void)
{
struct blob_attr *cur, *tb[__CONF_MAX], *ts[__STATE_MAX];
int rem;
struct runtime_state *rsstate = NULL;
struct settings *usettings = NULL;
char *name, *ocistatus, *status, *tmp;
int container_pid = -1;
bool autostart;
static struct blob_buf buf;
void *arr, *obj;
if (json_output) {
blob_buf_init(&buf, 0);
arr = blobmsg_open_array(&buf, "");
}
blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME] || !tb[CONF_PATH])
continue;
autostart = tb[CONF_AUTOSTART] && blobmsg_get_bool(tb[CONF_AUTOSTART]);
ocistatus = NULL;
container_pid = 0;
name = blobmsg_get_string(tb[CONF_NAME]);
rsstate = avl_find_element(&runtime, name, rsstate, avl);
if (rsstate && rsstate->ocistate) {
blobmsg_parse(state_policy, __STATE_MAX, ts, blobmsg_data(rsstate->ocistate), blobmsg_len(rsstate->ocistate));
ocistatus = blobmsg_get_string(ts[STATE_STATUS]);
container_pid = blobmsg_get_u32(ts[STATE_PID]);
}
status = ocistatus?:(rsstate && rsstate->running)?"creating":"stopped";
usettings = avl_find_element(&settings, name, usettings, avl);
if (usettings && (usettings->autostart >= 0))
autostart = !!(usettings->autostart);
if (json_output) {
obj = blobmsg_open_table(&buf, "");
blobmsg_add_string(&buf, "name", name);
blobmsg_add_string(&buf, "status", status);
blobmsg_add_u8(&buf, "autostart", autostart);
} else {
printf("[%c] %s %s", autostart?'*':' ', name, status);
}
if (rsstate && !rsstate->running && (rsstate->exitcode >= 0)) {
if (json_output)
blobmsg_add_u32(&buf, "exitcode", rsstate->exitcode);
else
printf(" exitcode: %d (%s)", rsstate->exitcode, strerror(rsstate->exitcode));
}
if (rsstate && rsstate->running && (rsstate->runtime_pid >= 0)) {
if (json_output)
blobmsg_add_u32(&buf, "runtime_pid", rsstate->runtime_pid);
else
printf(" runtime pid: %d", rsstate->runtime_pid);
}
if (rsstate && rsstate->running && (container_pid >= 0)) {
if (json_output)
blobmsg_add_u32(&buf, "container_pid", container_pid);
else
printf(" container pid: %d", container_pid);
}
if (!json_output)
printf("\n");
else
blobmsg_close_table(&buf, obj);
}
if (json_output) {
blobmsg_close_array(&buf, arr);
tmp = blobmsg_format_json_indent(buf.head, true, 0);
if (!tmp) {
blob_buf_free(&buf);
return -ENOMEM;
}
printf("%s\n", tmp);
free(tmp);
blob_buf_free(&buf);
};
return 0;
}
static int uxc_exists(char *name)
{
struct runtime_state *rsstate = NULL;
rsstate = avl_find_element(&runtime, name, rsstate, avl);
if (rsstate && (rsstate->running))
return -EEXIST;
return 0;
}
static int uxc_create(char *name, bool immediately)
{
static struct blob_buf req;
struct blob_attr *cur, *tb[__CONF_MAX];
int rem, ret = 0;
uint32_t id;
struct settings *usettings = NULL;
char *path = NULL, *jailname = NULL, *pidfile = NULL, *tmprwsize = NULL, *writepath = NULL;
void *in, *ins, *j;
bool found = false;
blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME] || !tb[CONF_PATH])
continue;
if (strcmp(name, blobmsg_get_string(tb[CONF_NAME])))
continue;
found = true;
break;
}
if (!found)
return -ENOENT;
path = blobmsg_get_string(tb[CONF_PATH]);
if (tb[CONF_PIDFILE])
pidfile = blobmsg_get_string(tb[CONF_PIDFILE]);
if (tb[CONF_TEMP_OVERLAY_SIZE])
tmprwsize = blobmsg_get_string(tb[CONF_TEMP_OVERLAY_SIZE]);
if (tb[CONF_WRITE_OVERLAY_PATH])
writepath = blobmsg_get_string(tb[CONF_WRITE_OVERLAY_PATH]);
if (tb[CONF_JAIL])
jailname = blobmsg_get_string(tb[CONF_JAIL]);
usettings = avl_find_element(&settings, blobmsg_get_string(tb[CONF_NAME]), usettings, avl);
if (usettings) {
if (usettings->writepath) {
writepath = usettings->writepath;
tmprwsize = NULL;
}
if (usettings->tmprwsize) {
tmprwsize = usettings->tmprwsize;
writepath = NULL;
}
}
blob_buf_init(&req, 0);
blobmsg_add_string(&req, "name", name);
ins = blobmsg_open_table(&req, "instances");
in = blobmsg_open_table(&req, name);
blobmsg_add_string(&req, "bundle", path);
j = blobmsg_open_table(&req, "jail");
blobmsg_add_string(&req, "name", jailname?:name);
blobmsg_add_u8(&req, "immediately", immediately);
if (pidfile)
blobmsg_add_string(&req, "pidfile", pidfile);
blobmsg_close_table(&req, j);
if (writepath)
blobmsg_add_string(&req, "overlaydir", writepath);
if (tmprwsize)
blobmsg_add_string(&req, "tmpoverlaysize", tmprwsize);
blobmsg_close_table(&req, in);
blobmsg_close_table(&req, ins);
if (verbose) {
char *tmp;
tmp = blobmsg_format_json_indent(req.head, true, 1);
if (!tmp)
return -ENOMEM;
fprintf(stderr, "adding container to procd:\n\t%s\n", tmp);
free(tmp);
}
if (ubus_lookup_id(ctx, "container", &id) ||
ubus_invoke(ctx, id, "add", req.head, NULL, NULL, 3000)) {
blob_buf_free(&req);
ret = -EIO;
}
return ret;
}
static int uxc_start(const char *name, bool console)
{
char *objname;
unsigned int id;
pid_t pid;
if (console) {
pid = fork();
if (pid > 0)
exit(uxc_attach(name));
}
if (asprintf(&objname, "container.%s", name) == -1)
return -ENOMEM;
if (ubus_lookup_id(ctx, objname, &id))
return -ENOENT;
free(objname);
return ubus_invoke(ctx, id, "start", NULL, NULL, NULL, 3000);
}
static int uxc_kill(char *name, int signal)
{
static struct blob_buf req;
struct blob_attr *cur, *tb[__CONF_MAX];
int rem, ret;
char *objname;
unsigned int id;
struct runtime_state *rsstate = NULL;
bool found = false;
blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME] || !tb[CONF_PATH])
continue;
if (strcmp(name, blobmsg_get_string(tb[CONF_NAME])))
continue;
found = true;
break;
}
if (!found)
return -ENOENT;
rsstate = avl_find_element(&runtime, name, rsstate, avl);
if (!rsstate || !(rsstate->running))
return -ENOENT;
blob_buf_init(&req, 0);
blobmsg_add_u32(&req, "signal", signal);
blobmsg_add_string(&req, "name", name);
if (asprintf(&objname, "container.%s", name) == -1)
return -ENOMEM;
ret = ubus_lookup_id(ctx, objname, &id);
free(objname);
if (ret)
return -ENOENT;
if (ubus_invoke(ctx, id, "kill", req.head, NULL, NULL, 3000))
return -EIO;
return 0;
}
static int uxc_set(char *name, char *path, signed char autostart, char *pidfile, char *tmprwsize, char *writepath, char *requiredmounts)
{
static struct blob_buf req;
struct settings *usettings = NULL;
struct blob_attr *cur, *tb[__CONF_MAX];
int rem, ret;
const char *cfname = NULL;
const char *sfname = NULL;
char *fname = NULL;
char *curvol, *tmp, *mnttok;
void *mntarr;
int f;
struct stat sb;
/* nothing to do */
if (!path && (autostart<0) && !pidfile && !tmprwsize && !writepath && !requiredmounts)
return 0;
blobmsg_for_each_attr(cur, blob_data(conf.head), rem) {
blobmsg_parse(conf_policy, __CONF_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
if (!tb[CONF_NAME] || !tb[CONF_PATH])
continue;
if (strcmp(name, blobmsg_get_string(tb[CONF_NAME])))
continue;
cfname = blobmsg_name(cur);
break;
}
if (cfname && path)
return -EEXIST;
if (!cfname && !path)
return -ENOENT;
if (path) {
if (stat(path, &sb) == -1)
return -ENOENT;
if ((sb.st_mode & S_IFMT) != S_IFDIR)
return -ENOTDIR;
}
usettings = avl_find_element(&settings, blobmsg_get_string(tb[CONF_NAME]), usettings, avl);
if (path && usettings)
return -EIO;
if (usettings) {
sfname = usettings->fname;
if (!tmprwsize && !writepath) {