Repository navigation
Expand file tree
/
Copy pathuxc.c
More file actions
7264 lines (5937 loc) · 173 KB
/
Copy pathuxc.c
File metadata and controls
7264 lines (5937 loc) · 173 KB
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 <daniel@makrotopia.org>
*
* 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 <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <getopt.h>
#include <sys/file.h>
#include <glob.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <libubus.h>
#include <libubox/avl-cmp.h>
#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubox/ustream.h>
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
#include "container.h"
#include "log.h"
#include "state-file.h"
#include "uptime.h"
#include "stdio-fds.h"
#define UXC_VERSION "0.3"
#define UXC_ETC_CONFDIR "/etc/uxc"
#define UXC_VOL_CONFDIR "/tmp/run/uvol/.meta/uxc"
#define UXC_VOL_SECRETDIR "/tmp/run/uvol/.meta/secrets"
#define UXC_RUN_DIR "/tmp/run/uxc"
#define UXC_VOL_PROVISIONDIR "/tmp/run/uvol/.meta/provision"
#define UXC_DISABLED_MARKER UXC_ETC_CONFDIR "/disabled"
#define PROVISION_STAGE_SUFFIX ".new"
#define HOSTS_FILE_DEST "/etc/hosts"
static bool verbose = false;
static bool json_output = false;
static bool quiet_output = false;
static int stdio_fds[STDIO_FDS_NUM] = { STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO };
static const 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;
uint64_t incarnation;
uint32_t stop_timeout;
char *started_by;
struct blob_attr *instance;
struct blob_attr *ocistate;
};
enum {
OPT_CONSOLE_SOCKET = 0x100,
OPT_NO_PIVOT,
OPT_NO_NEW_KEYRING,
OPT_PRESERVE_FDS,
OPT_PROCESS,
OPT_RESOURCES,
OPT_AUTOSTART,
OPT_PID_FILE,
OPT_TEMP_OVERLAY_SIZE,
OPT_WRITE_OVERLAY_PATH,
OPT_MOUNTS,
OPT_MODULE_VERSION,
OPT_DATA_VOLUME,
OPT_ENV,
OPT_PROVISION,
OPT_RESPAWN,
OPT_STOP_TIMEOUT,
OPT_STDOUT,
OPT_STDERR,
OPT_UNSET,
};
static const struct option create_opts[] = {
{"autostart", no_argument, 0, 'a' },
{"bundle", required_argument, 0, 'b' },
{"console-socket", required_argument, 0, OPT_CONSOLE_SOCKET },
{"mounts", required_argument, 0, 'm' },
{"no-new-keyring", no_argument, 0, OPT_NO_NEW_KEYRING },
{"no-pivot", no_argument, 0, OPT_NO_PIVOT },
{"pid-file", required_argument, 0, 'p' },
{"preserve-fds", required_argument, 0, OPT_PRESERVE_FDS },
{"temp-overlay-size", required_argument, 0, 't' },
{"write-overlay-path", required_argument, 0, 'w' },
{0, 0, 0, 0 }
};
static const struct option start_opts[] = {
{"console", no_argument, 0, 'c' },
{0, 0, 0, 0 }
};
static const struct option kill_opts[] = {
{"signal", required_argument, 0, 's' },
{"all", no_argument, 0, 'a' },
{0, 0, 0, 0 }
};
static const struct option delete_opts[] = {
{"force", no_argument, 0, 'f' },
{"volumes", no_argument, 0, 'V' },
{0, 0, 0, 0 }
};
static const struct option list_opts[] = {
{"format", required_argument, 0, 'f' },
{"json", no_argument, 0, 'j' },
{"quiet", no_argument, 0, 'q' },
{0, 0, 0, 0 }
};
static const struct option exec_opts[] = {
{"console-socket", required_argument, 0, OPT_CONSOLE_SOCKET },
{"detach", no_argument, 0, 'd' },
{"pid-file", required_argument, 0, 'p' },
{"preserve-fds", required_argument, 0, OPT_PRESERVE_FDS },
{"process", required_argument, 0, OPT_PROCESS },
{"tty", no_argument, 0, 't' },
{0, 0, 0, 0 }
};
static const struct option update_opts[] = {
{"resources", required_argument, 0, OPT_RESOURCES },
{0, 0, 0, 0 }
};
static const struct option set_opts[] = {
{"autostart", required_argument, 0, OPT_AUTOSTART },
{"data-volume", required_argument, 0, OPT_DATA_VOLUME },
{"env", required_argument, 0, OPT_ENV },
{"module-version", required_argument, 0, OPT_MODULE_VERSION },
{"mounts", required_argument, 0, OPT_MOUNTS },
{"pid-file", required_argument, 0, OPT_PID_FILE },
{"provision", required_argument, 0, OPT_PROVISION },
{"resources", required_argument, 0, OPT_RESOURCES },
{"respawn", required_argument, 0, OPT_RESPAWN },
{"stderr", required_argument, 0, OPT_STDERR },
{"stdout", required_argument, 0, OPT_STDOUT },
{"stop-timeout", required_argument, 0, OPT_STOP_TIMEOUT },
{"temp-overlay-size", required_argument, 0, OPT_TEMP_OVERLAY_SIZE },
{"unset", required_argument, 0, OPT_UNSET },
{"write-overlay-path", required_argument, 0, OPT_WRITE_OVERLAY_PATH },
{0, 0, 0, 0 }
};
struct signame {
int signal;
char name[7];
};
static const struct signame signames[] = {
#ifdef SIGABRT
{ .signal = SIGABRT, .name = "ABRT" },
#endif
#ifdef SIGALRM
{ .signal = SIGALRM, .name = "ALRM" },
#endif
#ifdef SIGBUS
{ .signal = SIGBUS, .name = "BUS" },
#endif
#ifdef SIGCHLD
{ .signal = SIGCHLD, .name = "CHLD" },
#endif
#ifdef SIGCLD
{ .signal = SIGCLD, .name = "CLD" },
#endif
#ifdef SIGCONT
{ .signal = SIGCONT, .name = "CONT" },
#endif
#ifdef SIGEMT
{ .signal = SIGEMT, .name = "EMT" },
#endif
#ifdef SIGFPE
{ .signal = SIGFPE, .name = "FPE" },
#endif
#ifdef SIGHUP
{ .signal = SIGHUP, .name = "HUP" },
#endif
#ifdef SIGILL
{ .signal = SIGILL, .name = "ILL" },
#endif
#ifdef SIGINFO
{ .signal = SIGINFO, .name = "INFO" },
#endif
#ifdef SIGINT
{ .signal = SIGINT, .name = "INT" },
#endif
#ifdef SIGIO
{ .signal = SIGIO, .name = "IO" },
#endif
#ifdef SIGIOT
{ .signal = SIGIOT, .name = "IOT" },
#endif
#ifdef SIGKILL
{ .signal = SIGKILL, .name = "KILL" },
#endif
#ifdef SIGLOST
{ .signal = SIGLOST, .name = "LOST" },
#endif
#ifdef SIGPIPE
{ .signal = SIGPIPE, .name = "PIPE" },
#endif
#ifdef SIGPOLL
{ .signal = SIGPOLL, .name = "POLL" },
#endif
#ifdef SIGPROF
{ .signal = SIGPROF, .name = "PROF" },
#endif
#ifdef SIGPWR
{ .signal = SIGPWR, .name = "PWR" },
#endif
#ifdef SIGQUIT
{ .signal = SIGQUIT, .name = "QUIT" },
#endif
#ifdef SIGSEGV
{ .signal = SIGSEGV, .name = "SEGV" },
#endif
#ifdef SIGSTKFLT
{ .signal = SIGSTKFLT, .name = "STKFLT" },
#endif
#ifdef SIGSTOP
{ .signal = SIGSTOP, .name = "STOP" },
#endif
#ifdef SIGSYS
{ .signal = SIGSYS, .name = "SYS" },
#endif
#ifdef SIGTERM
{ .signal = SIGTERM, .name = "TERM" },
#endif
#ifdef SIGTRAP
{ .signal = SIGTRAP, .name = "TRAP" },
#endif
#ifdef SIGTSTP
{ .signal = SIGTSTP, .name = "TSTP" },
#endif
#ifdef SIGTTIN
{ .signal = SIGTTIN, .name = "TTIN" },
#endif
#ifdef SIGTTOU
{ .signal = SIGTTOU, .name = "TTOU" },
#endif
#ifdef SIGUNUSED
{ .signal = SIGUNUSED, .name = "UNUSED" },
#endif
#ifdef SIGURG
{ .signal = SIGURG, .name = "URG" },
#endif
#ifdef SIGUSR1
{ .signal = SIGUSR1, .name = "USR1" },
#endif
#ifdef SIGUSR2
{ .signal = SIGUSR2, .name = "USR2" },
#endif
#ifdef SIGVTALRM
{ .signal = SIGVTALRM, .name = "VTALRM" },
#endif
#ifdef SIGWINCH
{ .signal = SIGWINCH, .name = "WINCH" },
#endif
#ifdef SIGXCPU
{ .signal = SIGXCPU, .name = "XCPU" },
#endif
#ifdef SIGXFSZ
{ .signal = SIGXFSZ, .name = "XFSZ" },
#endif
};
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;
#define UXC_WAIT_UNSET 0
#define UXC_WAIT_OK 1
#define UXC_WAIT_STOPPED 2
#define UXC_WAIT_FAILED 3
#define UXC_WAIT_GONE 4
struct uxc_wait_state {
const char *service;
const char *instance;
const char *success_event;
const char *fail_event;
const char *object;
char reason[96];
uint64_t since_incarnation;
char resources_refused[64];
int resources_errno;
bool escalated;
int result;
int err;
};
static struct uxc_wait_state *active_wait;
enum {
UXC_WAIT_SERVICE,
UXC_WAIT_INSTANCE,
UXC_WAIT_REASON,
UXC_WAIT_ERRNO,
UXC_WAIT_INCARNATION,
UXC_WAIT_ESCALATED,
UXC_WAIT_RESOURCES_REFUSED,
UXC_WAIT_RESOURCES_ERRNO,
__UXC_WAIT_INST_MAX,
};
static const struct blobmsg_policy uxc_wait_inst_policy[__UXC_WAIT_INST_MAX] = {
[UXC_WAIT_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
[UXC_WAIT_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
[UXC_WAIT_REASON] = { "reason", BLOBMSG_TYPE_STRING },
[UXC_WAIT_ERRNO] = { "errno", BLOBMSG_TYPE_INT32 },
[UXC_WAIT_INCARNATION] = { "incarnation", BLOBMSG_TYPE_INT64 },
[UXC_WAIT_ESCALATED] = { "escalated", BLOBMSG_TYPE_BOOL },
[UXC_WAIT_RESOURCES_REFUSED] = { "resources_refused", BLOBMSG_TYPE_STRING },
[UXC_WAIT_RESOURCES_ERRNO] = { "resources_errno", BLOBMSG_TYPE_INT32 },
};
static void uxc_wait_timeout_cb(struct uloop_timeout *t)
{
uloop_end();
}
static void uxc_wait_event_cb(struct ubus_context *uctx,
struct ubus_event_handler *ev,
const char *type, struct blob_attr *msg)
{
struct blob_attr *tb[__UXC_WAIT_INST_MAX];
struct uxc_wait_state *w = active_wait;
int result;
if (!w)
return;
blobmsg_parse(uxc_wait_inst_policy, __UXC_WAIT_INST_MAX, tb,
blobmsg_data(msg), blobmsg_data_len(msg));
if (!tb[UXC_WAIT_SERVICE] || !tb[UXC_WAIT_INSTANCE])
return;
if (w->service && strcmp(blobmsg_get_string(tb[UXC_WAIT_SERVICE]), w->service))
return;
if (w->instance && strcmp(blobmsg_get_string(tb[UXC_WAIT_INSTANCE]), w->instance))
return;
if (w->since_incarnation && tb[UXC_WAIT_INCARNATION] &&
blobmsg_get_u64(tb[UXC_WAIT_INCARNATION]) < w->since_incarnation)
return;
if (w->success_event && !strcmp(type, w->success_event))
result = UXC_WAIT_OK;
else if (w->fail_event && !strcmp(type, w->fail_event))
result = UXC_WAIT_FAILED;
else if (!strcmp(type, "instance.stopped"))
result = UXC_WAIT_STOPPED;
else
return;
if (tb[UXC_WAIT_REASON])
snprintf(w->reason, sizeof(w->reason), "%s",
blobmsg_get_string(tb[UXC_WAIT_REASON]));
if (tb[UXC_WAIT_ERRNO])
w->err = blobmsg_get_u32(tb[UXC_WAIT_ERRNO]);
w->escalated = tb[UXC_WAIT_ESCALATED] &&
blobmsg_get_bool(tb[UXC_WAIT_ESCALATED]);
if (tb[UXC_WAIT_RESOURCES_REFUSED])
snprintf(w->resources_refused, sizeof(w->resources_refused), "%s",
blobmsg_get_string(tb[UXC_WAIT_RESOURCES_REFUSED]));
if (tb[UXC_WAIT_RESOURCES_ERRNO])
w->resources_errno = blobmsg_get_u32(tb[UXC_WAIT_RESOURCES_ERRNO]);
w->result = result;
uloop_end();
}
static const char *uxc_object_remove_path(struct blob_attr *msg)
{
static const struct blobmsg_policy pol = {
.name = "path", .type = BLOBMSG_TYPE_STRING
};
struct blob_attr *tb;
if (!msg)
return NULL;
blobmsg_parse(&pol, 1, &tb, blob_data(msg), blob_len(msg));
if (!tb)
return NULL;
return blobmsg_get_string(tb);
}
static void uxc_wait_gone_cb(struct ubus_context *uctx,
struct ubus_event_handler *ev,
const char *type, struct blob_attr *msg)
{
struct uxc_wait_state *w = active_wait;
const char *path;
if (!w || !w->object)
return;
path = uxc_object_remove_path(msg);
if (!path || strcmp(path, w->object))
return;
w->result = UXC_WAIT_GONE;
uloop_end();
}
static const char *uxc_wait_why(struct uxc_wait_state *w)
{
static char buf[160];
if (!w->reason[0])
return NULL;
if (w->err)
snprintf(buf, sizeof(buf), "%s: %s", w->reason,
strerror(w->err));
else
snprintf(buf, sizeof(buf), "%s", w->reason);
return buf;
}
static struct ubus_event_handler uxc_wait_ev;
static struct ubus_event_handler uxc_wait_gone_ev;
static bool uxc_wait_armed;
static bool uxc_uloop_ready;
static void uxc_uloop_init(void)
{
if (uxc_uloop_ready)
return;
uloop_init();
ubus_add_uloop(ctx);
uxc_uloop_ready = true;
}
static int uxc_wait_arm(struct uxc_wait_state *w)
{
uxc_uloop_init();
if (!uxc_wait_armed) {
uxc_wait_ev.cb = uxc_wait_event_cb;
if (ubus_register_event_handler(ctx, &uxc_wait_ev, "instance.*"))
return -EIO;
uxc_wait_gone_ev.cb = uxc_wait_gone_cb;
if (ubus_register_event_handler(ctx, &uxc_wait_gone_ev,
"ubus.object.remove")) {
ubus_unregister_event_handler(ctx, &uxc_wait_ev);
return -EIO;
}
uxc_wait_armed = true;
}
active_wait = w;
return 0;
}
static int uxc_wait_run(struct uxc_wait_state *w, unsigned int timeout_ms)
{
struct uloop_timeout t = { .cb = uxc_wait_timeout_cb };
if (timeout_ms)
uloop_timeout_set(&t, timeout_ms);
if (w->result == UXC_WAIT_UNSET)
uloop_run();
if (timeout_ms)
uloop_timeout_cancel(&t);
if (w->result == UXC_WAIT_UNSET)
return timeout_ms ? -ETIMEDOUT : -EINTR;
return 0;
}
static void uxc_wait_disarm(void)
{
active_wait = NULL;
}
static void usage(void)
{
printf("syntax: uxc [global options] <command> [parameters ...]\n");
printf("global options:\n");
printf("\t[--debug|-v] [--log <path>] [--log-format <text|json>]\n");
printf("\t[--root <dir>] [--rootless[=auto|true|false]]\n");
printf("\t[--systemd-cgroup] [--criu <path>]\n");
printf("commands:\n");
printf("\tlist [--json|--format json] [--quiet]\t"
"list all configured containers (runc-compatible)\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[--pid-file <path>]\t\t\twrite container PID to <path>\n");
printf("\t\t[--console-socket <path>]\t\tAF_UNIX socket to receive the PTY master fd\n");
printf("\t\t[--no-pivot|--no-new-keyring|--preserve-fds <N>] "
"runc-compat, currently ignored\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("\ttrace <conf>\t\t\t\trun <conf> logging every syscall (seccomp trace)\n");
printf("\taudit <conf>\t\t\t\t"
"run <conf> enforcing its seccomp filter and logging denials\n");
printf("\tcomplain <conf>\t\t\t\trun <conf> permitting but logging seccomp denials\n");
printf("\tstate <conf>\t\t\t\tget state of container <conf>\n");
printf("\tkill [--signal <sig>] [--all] <conf> [<sig>]\t"
"signal <conf> (no signal: graceful stop); --all+KILL: whole cgroup\n");
printf("\trestart <conf>\t\t\t\tstop <conf> if running, then re-create and start it\n");
printf("\trespawn-reset <conf> [<count>]\t\tset the respawn retry count of <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("\tset <conf> [--autostart 0|1] [--pid-file <path>]\n");
printf("\t\t[--temp-overlay-size <size>] [--write-overlay-path <path>]\n");
printf("\t\t[--mounts <v1>,<v2>,...,<vN>] [--module-version <string>]\n");
printf("\t\t[--data-volume <vol>:<mnt>[:<size>]]... [--env <KEY>=<value>]...\n");
printf("\t\t[--respawn enable=0|1[,min=N,max=N,multiplier=N,"
"max-retries=N,reset-period=N]]\n");
printf("\t\t[--provision <file>:<dst>[:secret]]...\n");
printf("\t\t[--stop-timeout <seconds>] [--resources <file>]\n");
printf("\t\t[--stdout 0|1] [--stderr 0|1]\t\tlog stdout/stderr of <conf> to syslog\n");
printf("\t\t[--unset <key>]...\t\t\tedit the settings of <conf> in one rewrite\n");
printf("\t\t\t\t\t\t--unset keys: autostart pidfile temp-overlay-size\n");
printf("\t\t\t\t\t\twrite-overlay-path volumes module-version\n");
printf("\t\t\t\t\t\tdata-volumes data-volume=<vol> env env=<KEY> respawn\n");
printf("\t\t\t\t\t\tprovision provision=<dst> stop-timeout resources\n");
printf("\t\t\t\t\t\tstdout stderr\n");
printf("\tdelete <conf> [--force] [--volumes]\t"
"delete <conf>; --volumes also reaps its rw state and per-container volumes\n");
printf("\treconcile\t\t\t\t"
"reap orphaned state of containers whose package was removed (data volumes kept)\n");
printf("\tdisable-all\t\t\t\tstop every container and refuse creates until enable-all\n");
printf("\tenable-all\t\t\t\tallow creates again and start the autostart containers\n");
printf("\tpause <conf>\t\t\t\tfreeze every process in container <conf>'s cgroup\n");
printf("\tresume <conf>\t\t\t\tthaw a previously paused container <conf>\n");
printf("\texec <conf> [--process <file>] [-d] [-p <pid-file>] [-- cmd args]\n");
printf("\t\t\t\t\t\trun a command inside running container <conf>\n");
printf("\tupdate <conf> --resources <file>\t"
"apply linux.resources from <file> to running container <conf>\n");
}
static int error_report(int ret)
{
fprintf(stderr, "uxc error: %s\n", strerror(-ret));
return ret;
}
enum {
CONF_NAME,
CONF_PATH,
CONF_JAIL,
CONF_AUTOSTART,
CONF_PIDFILE,
CONF_TEMP_OVERLAY_SIZE,
CONF_WRITE_OVERLAY_PATH,
CONF_VOLUMES,
CONF_IDMAP_OFFSET,
CONF_DATA_VOLUMES,
CONF_OVERLAY_SIZE,
CONF_INITENV,
CONF_HOSTS_FILE,
CONF_PROVISION,
CONF_ORIGIN,
CONF_MODULE_VERSION,
CONF_ENV,
CONF_RESPAWN,
CONF_STOP_TIMEOUT,
CONF_RESOURCES,
CONF_IMAGE,
CONF_IMAGE_DIGEST,
CONF_PACKAGE,
CONF_PACKAGE_VERSION,
CONF_PUBLISHER,
CONF_STDOUT,
CONF_STDERR,
__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 },
[CONF_IDMAP_OFFSET] = { .name = "idmap-offset", .type = BLOBMSG_TYPE_STRING },
[CONF_DATA_VOLUMES] = { .name = "data-volumes", .type = BLOBMSG_TYPE_ARRAY },
[CONF_OVERLAY_SIZE] = { .name = "overlay-size", .type = BLOBMSG_TYPE_STRING },
[CONF_INITENV] = { .name = "initenv", .type = BLOBMSG_TYPE_TABLE },
[CONF_HOSTS_FILE] = { .name = "hosts-file", .type = BLOBMSG_TYPE_STRING },
[CONF_PROVISION] = { .name = "provision", .type = BLOBMSG_TYPE_ARRAY },
[CONF_ORIGIN] = { .name = "origin", .type = BLOBMSG_TYPE_STRING },
[CONF_MODULE_VERSION] = { .name = "module-version", .type = BLOBMSG_TYPE_STRING },
[CONF_ENV] = { .name = "env", .type = BLOBMSG_TYPE_TABLE },
[CONF_RESPAWN] = { .name = "respawn", .type = BLOBMSG_TYPE_TABLE },
[CONF_STOP_TIMEOUT] = { .name = "stop-timeout", .type = BLOBMSG_TYPE_INT32 },
[CONF_RESOURCES] = { .name = "resources", .type = BLOBMSG_TYPE_TABLE },
[CONF_IMAGE] = { .name = "image", .type = BLOBMSG_TYPE_STRING },
[CONF_IMAGE_DIGEST] = { .name = "image-digest", .type = BLOBMSG_TYPE_STRING },
[CONF_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
[CONF_PACKAGE_VERSION] = { .name = "package-version", .type = BLOBMSG_TYPE_STRING },
[CONF_PUBLISHER] = { .name = "publisher", .type = BLOBMSG_TYPE_STRING },
[CONF_STDOUT] = { .name = "stdout", .type = BLOBMSG_TYPE_BOOL },
[CONF_STDERR] = { .name = "stderr", .type = BLOBMSG_TYPE_BOOL },
};
struct settings {
struct avl_node avl;
char *container_name;
const char *fname;
char *tmprwsize;
char *writepath;
signed char autostart;
struct blob_attr *volumes;
struct blob_attr *file;
struct blob_attr *tb[__CONF_MAX];
};
enum {
PROV_SOURCE,
PROV_DESTINATION,
PROV_SECRET,
__PROV_MAX,
};
static const struct blobmsg_policy prov_policy[__PROV_MAX] = {
[PROV_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
[PROV_DESTINATION] = { .name = "destination", .type = BLOBMSG_TYPE_STRING },
[PROV_SECRET] = { .name = "secret", .type = BLOBMSG_TYPE_BOOL },
};
static int conf_load(bool load_settings)
{
int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
struct blob_buf *target;
struct stat sb;
char *globstr;
void *c, *o;
int j, res;
glob_t gl;
if (asprintf(&globstr, "%s/%s*.json", confdir, load_settings?"settings/":"") == -1)
return error_report(-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 error_report(-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);
for (j = 0; j < gl.gl_pathc; j++) {
o = blobmsg_open_table(target, 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]);
blobmsg_close_table(target, o);
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;
s->file = 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 error_report(-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);
s->file = cur;
memcpy(s->tb, tb, sizeof(s->tb));
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;
}
static int registration_dir(const char *cfname, char *out, size_t len)
{
const char *slash;
int ret;
slash = strrchr(cfname, '/');
if (!slash)
return -EINVAL;
ret = snprintf(out, len, "%.*s", (int)(slash - cfname), cfname);
if (ret < 0 || (size_t)ret >= len)
return -ENAMETOOLONG;
return 0;
}
static int settings_path_new(const char *cfname, const char *name, char *out, size_t len)
{
char conf_dir[PATH_MAX];
char dir[PATH_MAX];
int ret;
ret = registration_dir(cfname, conf_dir, sizeof(conf_dir));
if (ret)
return ret;
ret = snprintf(dir, sizeof(dir), "%s/settings", conf_dir);
if (ret < 0 || (size_t)ret >= sizeof(dir))
return -ENAMETOOLONG;
if (mkdir(dir, 0755) && errno != EEXIST)
return -errno;
ret = snprintf(out, len, "%s/%s.json", dir, name);
if (ret < 0 || (size_t)ret >= len)
return -ENAMETOOLONG;
return 0;
}
static struct blob_attr *registration_find(const char *name, struct blob_attr **tb)
{
struct blob_attr *cur;
int rem;
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])))
return cur;
}
return NULL;
}
static struct blob_attr *blob_key_find(struct blob_attr *root, const char *name)
{
struct blob_attr *cur;
int rem;
blobmsg_for_each_attr(cur, root, rem)
if (!strcmp(blobmsg_name(cur), name))
return cur;
return NULL;
}
static bool blob_has_key(struct blob_attr *root, const char *name)
{
return !!blob_key_find(root, name);
}
static void blob_table_overlay(struct blob_buf *out, struct blob_attr *base,
struct blob_attr *over)
{
struct blob_attr *cur;
void *t;
int rem;
t = blobmsg_open_table(out, NULL);
blobmsg_for_each_attr(cur, base, rem)
if (!blob_has_key(over, blobmsg_name(cur)))
blobmsg_add_blob(out, cur);
blobmsg_for_each_attr(cur, over, rem)
blobmsg_add_blob(out, cur);
blobmsg_close_table(out, t);
}
static void *blob_container_open(struct blob_buf *out, const char *name, int type)
{
if (type == BLOBMSG_TYPE_TABLE)
return blobmsg_open_table(out, name);
return blobmsg_open_array(out, name);
}
enum {
STORE_STATE,
STORE_PROVISION,
STORE_SECRETS,
__STORE_MAX,
};
static const char *const store_vol[__STORE_MAX] = {
[STORE_STATE] = UXC_VOL_CONFDIR "/state",
[STORE_PROVISION] = UXC_VOL_PROVISIONDIR,
[STORE_SECRETS] = UXC_VOL_SECRETDIR,
};
static const char *const store_sub[__STORE_MAX] = {
[STORE_STATE] = "state",
[STORE_PROVISION] = "provision",
[STORE_SECRETS] = "secrets",
};
static int store_root(const char *regdir, int store, char *out, size_t len)
{
int ret;
if (!strcmp(regdir, UXC_VOL_CONFDIR))
ret = snprintf(out, len, "%s", store_vol[store]);
else
ret = snprintf(out, len, "%s/%s", regdir, store_sub[store]);
if (ret < 0 || (size_t)ret >= len)
return -ENAMETOOLONG;
return 0;
}
static int store_root_find(const char *name, int store, char *out, size_t len)
{
struct blob_attr *tb[__CONF_MAX], *found;
char regdir[PATH_MAX];
int ret;
found = registration_find(name, tb);
if (!found)
return -ENOENT;
ret = registration_dir(blobmsg_name(found), regdir, sizeof(regdir));
if (ret)
return ret;
return store_root(regdir, store, out, len);
}
static int store_dir(const char *name, int store, char *out, size_t len)
{
char root[PATH_MAX];
int ret;
ret = store_root_find(name, store, root, sizeof(root));
if (ret)
return ret;
ret = snprintf(out, len, "%s/%s", root, name);
if (ret < 0 || (size_t)ret >= len)
return -ENAMETOOLONG;
return 0;
}
static int rundir_path(const char *name, const char *file, char *out, size_t len)
{
int ret;
if (file)
ret = snprintf(out, len, "%s/%s/%s", UXC_RUN_DIR, name, file);
else
ret = snprintf(out, len, "%s/%s", UXC_RUN_DIR, name);
if (ret < 0 || (size_t)ret >= len)
return -ENAMETOOLONG;
return 0;
}
static int rundir_prepare(const char *name, const char *file, char *out, size_t len)
{
char dir[PATH_MAX];
int ret;
ret = rundir_path(name, NULL, dir, sizeof(dir));
if (ret)
return ret;
if (mkdir(UXC_RUN_DIR, 0700) && errno != EEXIST)
return -errno;
if (mkdir(dir, 0700) && errno != EEXIST)
return -errno;
return rundir_path(name, file, out, len);
}
enum {
LIST_INSTANCES,
__LIST_MAX,