-
Notifications
You must be signed in to change notification settings - Fork 9
/
lxroot.cpp
1669 lines (1138 loc) · 58.5 KB
/
lxroot.cpp
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
// lxroot.cpp - Create and use chroot-style virtual software environments.
// Copyright (c) 2022 Parke Bostrom, parke.nexus at gmail.com
// Distributed under GPLv3 (see end of file) WITHOUT ANY WARRANTY.
#define LXROOT_VERSION "0.22.1" // version 20220822
// Welcome to the source code for Lxroot.
//
// Lxroot's command line interface is documented in help.cpp.
//
// The classes and structs in Lxroot can be divided into three categories:
//
// low level data storage classes
// convenience wrappers around system APIs
// high level classes
//
// -- String management classes
//
// Several types come in mutable vs. immutable pairs. For example:
//
typedef const char * mstr; // a mutable pointer (to const char)
typedef const mstr str; // an immutable pointer (to const char)
//
// In general, if a type name starts with 'm', then it may be the
// mutable half of a mutable vs. immutable type pair.
//
// struct mStr a mutable string (pointer and length, both mutable).
// typedef Str an immutable string (pointer and length).
// struct CatN represents the concatenation of up to 4 strings.
// struct oStr an dynamicly appendable string that owns its memory.
// struct Argv a convenience and saftey wrapper around mstr[].
//
// -- Low level data storage classes
//
// enum opt an enumeration that represents various options.
// struct Option represents a parsed command line option.
// struct Bind represents (and specifies) a potential bind mount.
// struct Env a list that specifies the new environment.
// struct State contains shared, mutable, global variables.
//
// -- Convenience wrappers
//
// class Dirent represents a directory entry (from readdir()).
// struct Fgetpwent parses /etc/passwd.
// struct Lib contains generally useful functions.
// struct Syscall provides error handling and tracing of syscalls.
//
// -- High level classes
//
// struct Option_Reader parses Argv to generate Options and/or Binds.
// struct Logic analyzes data and performs loops over data.
// struct Init_Tool records certain Options in State.
// struct Env_Tool configures the new envirnoment variables.
// class Lxroot one class to rule them all.
//
// If you wish to read Lxroot's source code, I recommend starting at
// the bottom of lxroot.cpp with class Lxroot (the highest level
// class), and then reading deeper into the other (lower level)
// classes as needed.
#include <dirent.h> // man 3 opendir
#include <errno.h> // man 3 errno
#include <fcntl.h> // man 2 open
#include <limits.h> // what uses limits.h ?
#include <pwd.h> // man 3 fgetpwent
#include <sched.h> // man 2 unshare
#include <signal.h> // raise(SIGINT), used with gdb
#include <stdio.h> // man 3 fprintf
#include <stdlib.h> // man 3 malloc
#include <string.h> // man 3 strlen
#include <unistd.h> // man 2 getuid stat
#include <sys/mount.h> // man 2 mount
#include <sys/stat.h> // man 2 stat
#include <sys/statfs.h> // man 2 statfs
#include <sys/statvfs.h> // man 2 statfs ST_RELATIME
#include <sys/syscall.h> // man 8 pivot_root
#include <sys/types.h> // man 3 opendir
#include <sys/wait.h> // man 2 wait
#include <vector> // class Env .vec
#include <functional> // std :: function
#include "help.cpp" // Lxroot's help strings
#include "str.cpp" // Parke's string library
template < class C > // --------------------------- template using sink
using sink = std :: function < void ( const C & ) >;
template < class C > // -------------------------- template using msink
using msink = std :: function < void ( C & ) >;
// macro printe -------------------------------------------- macro printe
#define printe( fmt, ... ) fprintf ( stderr, fmt "\n", ##__VA_ARGS__ );
#define writee( fmt, ... ) fprintf ( stderr, fmt , ##__VA_ARGS__ );
// macro die_pe -------------------------------------------- macro die_pe
// see https://stackoverflow.com/q/5588855 regarding ##__VA_ARGS__
// see https://stackoverflow.com/a/11172679 regarding ##__VA_ARGS__
#define die_pe( format, ... ) { \
fprintf ( stderr, "lxroot error " format " ", ##__VA_ARGS__ ); \
perror ( NULL ); \
exit ( 1 ); }
// macro die1 ------------------------------------------------ macro die1
#define die1( format, ... ) { \
fprintf ( stderr, "lxroot error " format "\n", ##__VA_ARGS__ ); \
exit ( 1 ); } // or call abort() instead of exit?
// macro warn ------------------------------------------------ macro warn
#define warn( format, ... ) { \
fprintf ( stderr, "lxroot warn " format "\n", ##__VA_ARGS__ ); }
template < typename T > // ---------------------------- template assert2
T assert2 ( T v, str file, const int line ) {
// Discussion of assert2 being a function vs being a macro:
// A function ensures that v is evaluated only once.
// My hope is that assert2 as a template (rather than a macro) will
// result in more readable compile time errors.
// However, a template might result in unnecessary copying of a T.
// Does assert2 need to be a function so that it can return a value?
// Or could a macro "return" a value via the comma operator? Not sure.
if ( (bool) v == false ) {
printe ( "lxroot assert2() failed %s %d", file, line ); abort(); }
return v; }
// macro assert -------------------------------------------- macro assert
#define assert( v ) assert2 ( v, __FILE__, __LINE__ )
mstr bash_command[] = { // ------------------------------ bash_command
"/bin/bash", "--norc", nullptr };
typedef const int cint; // xxci ------------------ typedefg cint
typedef unsigned char Opt_t; // xxop ------------------ typedef Opt_t
// 20220822 mopt is deprecated, and will eventually be replaced by Opt_t.
enum mopt { // xxop --------------------------------------- enum mopt
o_none,
/* literal arg types */
o_bind, o_dashdash, o_cd, o_ra, o_ro, o_rw, o_src, o_wd,
/* non-literal option types */
o_full, o_newroot, o_partial, o_setenv, o_shortopt,
/* literal long options */
o_env, o_help, o_help_more, o_network, o_pulse,
o_root, o_trace, o_uid, o_version, o_write, o_x11,
};
str opt_name[] = { // --------------------------------------- opt_name
"0",
/* literal arg types */
"bind", "--", "cd", "ra", "ro", "rw", "src", "wd",
/* non-literal option types */
"full", "newroot", "partial", "setenv", "shortopt",
/* literal long options */
"--env", "--help", "--help-more", "--network", "--pulseaudio",
"--root", "--trace", "--uid", "--version", "--write", "--x11",
nullptr };
// 20220822 opt_t is deprecated, and will eventually be replaced by Opt_t.
typedef const mopt opt_t;
mopt operator || ( opt_t a, opt_t b ) { // -------- global opt op ||
return a ? a : b ; }
mopt global_opt_trace = o_none; // -------------- :: global_opt_trace
template < class... Args > // -------------------------- template trace
void trace ( str format, Args... args ) {
if ( global_opt_trace == o_trace ) {
fprintf ( stderr, format, args... );
fputc ( '\n', stderr ); } }
void trace ( str s ) { trace ( "%s", s ); } // ------------- trace
mstr o2s ( const opt_t n ) { // ------------------------------ :: o2s
if ( 0 <= n && n < ( sizeof opt_name / sizeof(char*) ) ) {
return opt_name[n]; }
return "INVALID_OPTION"; }
mopt s2o ( str s ) { // xxs2 -------------------------------- :: s2o
if ( s ) {
for ( int n = o_none; opt_name[n]; n++ ) {
if ( strcmp ( s, opt_name[n] ) == 0 ) { return (mopt) n; } } }
return o_none; }
mopt s2o ( Str s ) { // xxs2 -------------------------------- :: s2o
return s2o ( s.s ); }
// end enum opt ------------------------------------------ end enum opt
typedef unsigned long flags_t; // xxfl -------------- typedef flags_t
struct Option { // xxop ------------------------------- struct Option
Argv a; // points to the first Str of the current option
Argv p; // a traveller that advances to the next option
mopt type = o_none; // the type of this option
mopt mode = o_none; // the read-write mode of this option
mStr arg0, arg1; // optional args
mStr newroot; // the newroot
mStr overlay; // the current overlay
Option ( Argv p ) : p(p) {} // ----------------------- Option ctor
operator bool () const { return type; } // --- Option cast bool
void print ( Str m ) const { // --------------------- Option print
printe ( "%-8s %-7s %s", m.s, o2s(type), arg0.s ); }
protected:
Option () {}
}; // end struct Option -------------------------- end struct Option
struct Bind { // xxbi ----------------------------------- struct Bind
mopt type = o_none;
mopt mode = o_none; // the specified mode ro, rw, ra, none
mopt actual = o_none; // the actual mode ro, rw
mStr full; // the path of the full overlay
mStr dst; // 20210619 at present, dst never begins with '/'
oStr src;
oStr newroot_dst; // newroot + dst
const Option * option = nullptr; // the source option for this bind
void clear () { // ------------------------------------- Bind clear
* this = Bind(); }
void print ( Str s ) const { // ----------------------- Bind print
printe ( "%s bind %-7s %-2s %-2s '%s' '%s' '%s'",
s.s, o2s(type), o2s(mode), o2s(actual), dst.s, src.s,
newroot_dst.s ); }
Bind & set // ---------------------------------------------- Bind set
( const Option & o, Str childname = 0 ) {
// 20210620 note: a Bind may outlive the Option o.
Str ov = o.overlay;
Str a0 = o.arg0;
Str a1 = o.arg1;
Str cn = childname;
type = o.type;
mode = o.mode;
full = nullptr;
option = & o;
switch ( o.type ) {
case o_newroot: dst=""; src=a0; mode=mode||o_ra; break;
case o_bind: dst=a0; src=a1; break;
// 20210620 todo consider setting dst and src to ""
// 20210620 nope, it appears setting dst and src is required
// 20210620 I wonder why?
case o_full: dst=cn; src=a0+"/"+cn; full=a0; break;
//se o_full: dst=""; src=""; full=a0; break;
// 20210620 todo set mode (and write unit tests)
case o_partial: dst=a0; src=ov+"/"+a0; break;
default: dst="error"; src="error"; break; }
if ( dst ) { newroot_dst = o.newroot + "/" + dst; }
else { newroot_dst = o.newroot; }
// 20210619
// if ( dst[0] == '/' ) { // 20210619
// die1 ( "bind set bad dst %s", dst.s ); }
return * this; }
const Bind & trace ( Str s ) const { // --------------- Bind trace
if ( global_opt_trace ) { print ( s ); }
return * this; }
}; // end struct Bind ------------------------------ end struct Bind
class Env { // xxen --------------------------------------- class Env
std :: vector < mstr > vec;
public:
str * data () const { return vec .data(); } // ------- Env data
bool not_set ( Str name ) const { return get(name) == nullptr; }
Str get ( Str name ) const { // -------------------------- Env get
for ( auto & o : vec ) {
if ( Str(o) .env_name() == name ) {
return Str(o) .tail ( "=" ); } }
return nullptr; }
void set ( Str pair ) { // -------------------------------- Env set
Str name = pair .env_name();
if ( name.n == 0 ) { return; }
for ( auto & o : vec ) {
if ( Str(o) .env_name() == name ) { o = pair.s; return; } }
if ( vec .size() == 0 ) { vec .push_back ( nullptr ); }
vec .back() = pair.s;
vec .push_back ( nullptr ); }
void set ( Str name, Str value ) { // --------------------- Env set
set ( oStr ( name + "=" + value ) .leak() ); }
void soft ( Str pair ) { // ------------------------------ Env soft
if ( not_set ( pair .env_name() ) ) { set ( pair ); } }
void soft ( Str name, Str value ) { // ------------------- Env soft
if ( not_set ( name ) ) {
set ( oStr ( name + "=" + value ) .leak() ); } }
void soft_copy ( Str name ) { // -------------------- Env soft_copy
Str pair = Argv(environ) .env_get ( name );
if ( pair ) { soft ( pair ); } }
}; // end class Env ---------------------------------- end class Env
struct Fgetpwent { // xxfg ------------------------- struct Fgetpwent
oStr dir, name, shell; // see man 3 fgetpwent
void fgetpwent ( Str path, uid_t uid ) { // -------------- fgetpwent
FILE * f = fopen ( path.s, "r" );
if ( f == nullptr ) {
warn ( "fopen failed %s", path.s ); return; }
struct passwd * pwent = nullptr;
while (( pwent = :: fgetpwent ( f ) )) { // parse next entry
if ( pwent -> pw_uid == uid ) {
dir = pwent -> pw_dir;
name = pwent -> pw_name;
shell = pwent -> pw_shell; break; } }
endpwent();
fclose ( f ); }
}; // end struct Fgetpwent -------------------- end struct Fgetpwent
struct State { // xxst --------------------------------- struct State
Argv argv;
Env env; // specifies the new environment
const uid_t uid = getuid(); // uid prior to unshare()
const gid_t gid = getgid(); // gid prior to unshare()
Fgetpwent outside ; // from /etc/passwd outside the lxroot
Fgetpwent inside ; // from /etc/passwd inside the lxroot
mopt newroot_mode = o_none;
Opt_t opts[128] = { 0 }; // command line options
bool before_pivot = true;
mStr newroot;
mStr guestname;
mStr chdir; // from the first and only cd option
mStr workdir; // from the last wd option
mStr xauthority; // contents of $HOME/.Xauthority
Argv dash; // points to '--' in argv ("dash dash")
Argv command; // points to the command to exec()
Opt_t opt_get ( Opt_t k ) const { return opts[k]; } // ---- opt
Opt_t & opt ( Opt_t k ) { return opts[k]; } // --
}; // end struct State ---------------------------- end struct State
State mut; // ----------------------------- mutable global State mut
const State & st = mut; // ----------------- const global State & st
Opt_t opt ( Opt_t k ) { return st .opt_get (k); } // -- global opt
class Dirent { // xxdi --------------------------------- class Dirent
struct dirent * p = nullptr;
public:
Dirent & operator = ( dirent * pp ) { p = pp; return * this; }
bool operator == ( Str s ) const { return name() == s; }
operator bool () const { return p; }
ino_t inode () const { return p -> d_ino; }
Str name () const { return p -> d_name; }
bool is_dir () const { // -------------------------- Dirent is_dir
if ( p -> d_type == DT_UNKNOWN ) { die1("dirent type is DT_UNKNOWN"); }
return p -> d_type == DT_DIR; }
}; // end class Dirent ---------------------------- end class Dirent
struct Lib { // xxli ------------------------------------- struct Lib
// 20210530 apparent redundancy: assert_is_dir vs directory_require
static void assert_is_dir ( str path, str m ) { // --- assert_is_dir
if ( is_dir ( path ) ) { return; }
printe ( "lxroot %s directory not found '%s'", m, path );
exit ( 1 ); }
static void directory_require // ------------- Lib directory_require
( Str path, Str m ) {
if ( Lib :: is_dir ( path ) ) { return; }
die1 ( "%s directory not found\n '%s'", m.s, path.s ); }
static bool eq ( str a, str b ) { // ----------------------- Lib eq
if ( a == NULL || b == NULL ) return false;
return strcmp ( a, b ) == 0; }
static oStr getcwd () { // ----------------------------- Lib getcwd
return oStr :: claim ( get_current_dir_name() ); }
static Str getenv ( Str name ) { // -------------------- Lib getenv
return :: getenv ( name.s ); }
static void help_print ( int n = 0 ) { // -------- Lib :: help_print
writee ( "%s%s", help, help2 ); exit ( n ); }
static void help_more_print () { // --------- Lib :: help_more_print
writee ( "%s%s", help, help_more ); exit ( 0 ); }
static Str home () { // ---------------------------------- Lib home
return getenv ( "HOME" ); }
static bool is_dir ( Str path ) { // ------------------- Lib is_dir
struct stat st;
if ( path.n &&
stat ( path.s, & st ) == 0 && st .st_mode & S_IFDIR ) {
return 1; }
errno = ENOENT;
return false; }
static bool is_empty_dir ( str path ) { // ------------ is_empty_dir
if ( not is_dir ( path ) ) { return false; }
DIR * dirp = assert ( opendir ( path ) );
for ( struct dirent * p; ( p = readdir ( dirp ) ); ) {
str s = p -> d_name;
if ( eq(s,".") || eq(s,"..") ) { continue; }
closedir ( dirp ); return false; }
closedir ( dirp ); return true; }
static bool is_exists ( Str path ) { // -------------- Lib is_exists
struct stat st;
return path.n && :: lstat ( path.s, & st ) == 0 ; }
static bool is_file ( Str path ) { // ----------------- Lib is_file
struct stat st;
if ( path.n && :: stat ( path.s, & st ) == 0
&& st .st_mode & S_IFREG ) {
return true; }
errno = ENOENT; // 20210521 so perror() is useful?
return false; }
static bool is_link ( Str path ) { // ----------------- Lib is_link
struct stat st;
if ( path.s
&& lstat ( path.s, & st ) == 0
&& S_ISLNK ( st .st_mode ) ) { return true; }
errno = ENOENT;
return false; }
enum prompt_rv { prompt_notty=1, prompt_timeout=2 }; // -----------
static int prompt ( Str message, int timeout ) { // ---- Lib prompt
// prompt() returns: prompt_notty or prompt_timeout
// or -1 on invalid input or the character c on input of "c\n"
if ( not isatty(0) || not isatty(1) ) { return prompt_notty; }
const int stdout = 1;
const ssize_t expect = strlen ( message.s );
const ssize_t actual = write ( stdout, message.s, expect );
if ( actual not_eq expect ) { die1 ( "prompt failed" ); }
fd_set rfds; FD_ZERO(&rfds); FD_SET(0,&rfds);
struct timeval tv = { timeout, 0 };
int rv = select ( 1, & rfds, 0, 0, & tv );
if ( rv == 0 ) { return prompt_timeout; }
if ( rv != 1 ) { return -1; } // select() error?
unsigned char buf[3]; rv = read ( 0, buf, 3 );
return ( rv == 2 && buf[1] == '\n' ) ? buf[0] : -1 ; }
static Str readlink ( Str path ) { // --------------- Lib readlink
struct stat st;
if ( lstat ( path.s, & st ) == 0 && st .st_mode & S_IFLNK ) {
ssize_t lim = st .st_size + 2;
char * buf = (char*) malloc ( lim );
if ( buf ) {
memset ( buf, '\0', lim );
ssize_t len = :: readlink ( path.s, buf, lim );
if ( len == lim - 2 ) { return buf; } }
printe ( "lxroot readlink failed %s", path.s );
exit ( 1 ); }
return nullptr; }
}; // end struct Lib -------------------------------- end struct Lib
// try ---------------------------------------------------------------- try
struct Try2_Format_String { void(*fp)(); str format; }; // -- struct
Try2_Format_String try2_lookup_table[] = { // ------ try2_lookup_table
#define m(fn) ( (void(*)()) fn )
{ m(chdir), " chdir %s" },
{ m(chroot), " chroot %s" },
{ m(close), " close %d" },
{ m(umount2), " umount2 %s 0x%x" },
{ m(unshare), " unshare 0x%08x" },
#undef m
{ nullptr, nullptr } };
mstr try2_format ( void(*fp)() ) { // --------------------- try2_format
for ( const auto & en : try2_lookup_table ) {
if ( en.fp == fp ) { return en.format; } }
return "try2_format unexpected function"; }
template < typename F > // ----------------------- template try2_format
mstr try2_format ( F fp ) { return try2_format ( (void(*)()) fp ); }
template < typename F, class... Args > // --------------- template try1
int try1 ( str s, F fn, Args... args ) {
int rv = fn ( args... );
if ( rv == -1 ) { die1 ( "try1 %s", s ); }
return rv; }
template < typename F, class... Args > // --------------- template try2
int try2 ( str s, F fn, Args... args ) {
trace ( try2_format(fn), args... ); return try1 ( s, fn, args... ); }
struct Syscall { // xxsy ----------------------------- struct Syscall
// note all Syscall methods call exit(1) on error.
pid_t fork_pid = -2;
pid_t wstatus = 0;
static void chdir (Str path ){ try2( "chdir", ::chdir, path.s );}
static void chroot (Str path ){ try2( "chroot", ::chroot, path.s );}
static void close (int fd ){ try1( "close", ::close, fd );}
static void unshare (int flags){ try2( "unshare", ::unshare, flags );}
static void umount2 ( Str target, int flags ) {
try2 ( "umount2", :: umount2, target.s, flags ); }
void bind ( Str target, Str source ) { // ------------ Syscall bind
//
// from /usr/include/linux/mount.h
// MS_REC 0x 4000
// MS_BIND 0x 1000
// MS_REMOUNT 0x 0020
// MS_RDONLY 0x 0001
//
// 20210520 I seem to remember that if MS_BIND is set, then MS_RDONLY
// has no effect. Therefore, to make a bind mount
// readonly, first do the bind mount, and then call
// mount() a second time with MS_REMOUNT | MS_RDONLY.
//
// 20210520 I also seem to remember that, when mount is called by a
// non-root user, MS_BIND (often? always?) requires MS_REC.
//
// 20210520 It appears that non-root users can only set MS_RDONLY.
// Non-root users are not allowed to clear MS_RDONLY.
//
Lib :: directory_require ( source, "source" );
Lib :: directory_require ( target, "target" );
trace ( " bind '%s' '%s'", target.s, source.s );
const flags_t flags = MS_BIND | MS_REC;
const int rv = :: mount ( source.s, target.s, 0, flags, 0 );
if ( rv == 0 ) { return; }
die_pe ( "bind %s %s\n", source.s, target.s ); }
void execve ( const Str pathname, // -------------- Syscall execve
const Argv argv,
const Argv envp ) {
trace ( " execve %s %s", pathname.s, argv .concat().s );
if ( pathname .chr ( '/' ) ) { // path is specified, so use execve()
:: execve ( pathname.s, (char**) argv.p, (char**) envp.p ); }
else { // only filename is specified, so use execvpe()
char ** old = environ;
environ = (char**) envp.p;
:: execvpe ( pathname.s, (char**) argv.p, (char**) envp.p );
environ = old; }
// execve only returns on failure, so ...
die_pe ( "execve %s", pathname.s ); }
static void exit ( int status ) { // ----------------- Syscall exit
trace ( " exit %d", status ); :: exit ( status ); }
static void file_copy ( Str src, Str dst, const mode_t mode ) { // --
int src_fd = open ( src, O_RDONLY );
int dst_fd = open ( dst, O_WRONLY | O_CREAT, mode );
char buf[1024];
while ( true ) {
int a = try1 ( "read", :: read, src_fd, buf, sizeof(buf) );
if ( a == 0 ) { break; }
int b = try1 ( "write", :: write, dst_fd, buf, a );
assert ( a == b ); }
try1 ( "close", :: close, src_fd );
try1 ( "close", :: close, dst_fd ); }
static Str file_read ( Str path ) { // ---------- Syscall file_read
const auto st = Syscall :: stat ( path );
const int fd = Syscall :: open ( path, O_RDONLY );
Str rv = Syscall :: read ( fd, nullptr, st.st_size );
; Syscall :: close ( fd );
return rv; }
static void file_write ( Str path, Str s, const mode_t mode ) { // ---
const int fd = Syscall :: open ( path, O_WRONLY | O_CREAT, mode );
const int actual = :: write ( fd, s.s, s.n );
if ( actual not_eq s.n ) { die1 ( "file_write %s", path.s ); }
; Syscall :: close(fd); }
void fork () { // ------------------------------------ Syscall fork
if ( fork_pid != -2 ) { die_pe ( "extra fork?" ); }
if ( ( fork_pid = :: fork() ) >= 0 ) {
trace ( " fork (fork returned %d)", fork_pid );
return; }
die_pe ( "fork" ); }
void mount ( Str source, Str target, Str filesystemtype ) { // mount
Lib :: directory_require ( target, "target" );
trace ( " mount %s %s %s", source.s, target.s, filesystemtype.s );
if ( :: mount ( source.s, target.s, filesystemtype.s, 0, 0 ) == 0 ) {
return; }
die_pe ( "mount %s %s %s\n", source.s, target.s, filesystemtype.s ); }
static int open ( Str path, const int flags, const mode_t mode ) { // --
const int fd = :: open ( path.s, flags, mode );
if ( fd == -1 ) { die1 ( "open %s %s", path.s, strerror(errno) ); }
return fd; }
static int open ( Str pathname, const int flags ) { // -------- open
return open ( pathname, flags, 0 ); }
static void open ( int * fd, Str pathname, const int flags ) { // ---
* fd = open ( pathname, flags ); }
void pivot ( Str new_root, Str put_old ) { // -------- Syscall pivot
trace ( " pivot '%s' '%s'", new_root.s, put_old.s );
if ( syscall ( SYS_pivot_root, new_root.s, put_old.s ) == 0 ) {
mut .before_pivot = false; return; }
die_pe ( "pivot %s %s", new_root.s, put_old.s ); }
void rdonly ( Str target ) { // -------------------- Syscall rdonly
struct statfs st; // we will store the current statfs() flags in st.
if ( statfs ( target.s, & st ) not_eq 0 ) {
die_pe ( "rdonly statfs err '%s'\n", target.s ); }
const flags_t flags = ( st_to_ms ( st .f_flags )
| MS_BIND | MS_REMOUNT | MS_RDONLY );
if ( :: mount ( NULL, target.s, NULL, flags, NULL ) == 0 ) {
trace ( " rdonly %lx '%s'", flags, target.s ); return; }
die_pe ( "rdonly %lx '%s'\n", flags, target.s ); }
static Str read ( cint fd, void * buf, cint count ) { // ------ read
if ( buf == nullptr ) { buf = malloc(count); }
if ( buf == nullptr ) { die1 ( "read failed buf == nullptr" ); }
const int actual = :: read ( fd, buf, count );
if ( actual not_eq count ) {
die1 ( "read failed actual not_eq count" ); }
return Str ( (char*) buf, actual ); }
static flags_t st_to_ms ( flags_t n ) { // ------- Syscall st_to_ms
// convert a statfs() flag to a mount() flag. (ST_ to MS_ conversion.)
// see / usr / include / x86_64-linux-gnu / bits / statvfs.h
// see / usr / include / linux / mount.h
// flags from man 2 statfs flags from man 2 mount
// ST_RDONLY 1 MS_RDONLY 1
// ST_NOSUID 2 MS_NOSUID 2
// ST_NODEV 4 MS_NODEV 4
// ST_NOEXEC 8 MS_NOEXEC 8
// ST_SYNCHRONOUS 16 MS_SYNCHRONOUS 16
// ST_MANDLOCK 64 MS_MANDLOCK 64
// ST_NOATIME 1024 MS_NOATIME 1024
// ST_NODIRATIME 2048 MS_NODIRATIME 2048
// ST_RELATIME 4096 MS_RELATIME 1<<21
// note on x86_64 ST_RELATIME != MS_RELATIME
// note on x86_64 ST_RELATIME == MS_BIND == 4096
// the below verbose yet simple implementation should optimize well.
#define c(a) ( (flags_t) (a) )
#define if_equal( a, b ) ( c(a) == c(b) ? b : 0 )
#define if_not_equal( a, b ) ( ( c(a) != c(b) ) && ( n & a ) ? b : 0 )
constexpr flags_t copy_these_bits =
if_equal ( ST_RDONLY, MS_RDONLY )
| if_equal ( ST_NOSUID, MS_NOSUID )
| if_equal ( ST_NODEV, MS_NODEV )
| if_equal ( ST_NOEXEC, MS_NOEXEC )
| if_equal ( ST_SYNCHRONOUS, MS_SYNCHRONOUS )
| if_equal ( ST_MANDLOCK, MS_MANDLOCK )
| if_equal ( ST_NOATIME, MS_NOATIME )
| if_equal ( ST_NODIRATIME, MS_NODIRATIME )
| if_equal ( ST_RELATIME, MS_RELATIME );
const flags_t shifted_bits =
if_not_equal ( ST_RDONLY, MS_RDONLY )
| if_not_equal ( ST_NOSUID, MS_NOSUID )
| if_not_equal ( ST_NODEV, MS_NODEV )
| if_not_equal ( ST_NOEXEC, MS_NOEXEC )
| if_not_equal ( ST_SYNCHRONOUS, MS_SYNCHRONOUS )
| if_not_equal ( ST_MANDLOCK, MS_MANDLOCK )
| if_not_equal ( ST_NOATIME, MS_NOATIME )
| if_not_equal ( ST_NODIRATIME, MS_NODIRATIME )
| if_not_equal ( ST_RELATIME, MS_RELATIME );
#undef c
#undef if_equal
#undef if_not_equal
return ( n & copy_these_bits ) | shifted_bits; }
static struct stat stat ( Str path ) { // ------------ Syscall stat
struct stat st;
if ( :: stat ( path.s, & st ) == -1 ) { die1 ( "stat %s", path.s ); }
return st; }
pid_t wait () { // ----------------------------------- Syscall wait
trace ( " wait (parent calls wait)" );
pid_t pid = :: wait ( & wstatus );
if ( pid > 0 ) {
trace ( " wait wait returned pid %d status 0x%x",
pid, wstatus );
return pid; }
die_pe ( "wait" ); return -1; }
void write ( int fd, const void * buf, ssize_t count ) { // -- write
assert ( count >= 0 );
if ( :: write ( fd, buf, count ) == count ) { return; }
die_pe ( "write %d %ld", fd, (long int) count ); }
}; // end struct Syscall ------------------------ end struct Syscall
Syscall sys; // xxsy -------------------------------------- global sys
struct Option_Reader // xxop -------------------- struct Option_Reader
: private Option {
const Option & ref; // const access to the Option base class
Option_Reader ( Argv p ) : Option(p), ref(*this) {} // ------ ctor
Option_Reader ( const Option * other ) : ref(*this) { // ----- ctor
* (Option*) this = * other; }
const Option & next () { next_impl(); return ref; } // ---- next
private:
void next_impl () { // -------------------- Option_Reader next_impl
// input type mode arg0 arg1
//
// --<longopt> o_<longopt> - - -
// -short o_shortopt - - -
// name=value o_setenv - - -
// [mode] path o_newroot mode path -
// [mode] path o_full mode path -
// [mode] path o_partial mode path -
// src [mode] path o_src mode path -
// bind [mode] dst src o_bind mode dst src
// cd path o_cd - path -
// wd path o_wd - path -
// -- cmd [arg ...] o_dashdash - - -
a = p; // advance
type = s2o ( a[0] );
mode = o_none;
if ( p.p == nullptr || p[0] == nullptr ) { return; }
switch ( type ) {
case o_ra: // fallthrough to o_rw
case o_ro: // fallthrough to o_rw
case o_rw: mode_path(); return;
case o_src: p++; opt_mode(); overlay=*p++; return;
case o_bind: p++; opt_mode(); arg0=*p++; arg1=*p++; return;
case o_cd: // fallthrough to o_wd
case o_wd: p++; arg0=*p++; return;
case o_dashdash: p=(char**)nullptr; return;
default: break; }
if ( a[0].starts_with("--") ) {
if ( type ) { p++; return; }
else { die1 ( "bad option %s", a[0].s ); } }
if ( a[0].starts_with("-") ) { type=o_shortopt; p++; return; }
if ( is_setenv() ) { type=o_setenv; p++; return; }
path(); }
bool is_setenv () { // -------------------- Option_Reader is_setenv
str var_name_allowed =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
cint n = a[0].spn ( var_name_allowed );
return n > 0 && n < a[0].n && a[0].s[n] == '='; }
void mode_path () { // -------------------- Option_Reader mode_path
// mode path
mode=type; p++; path(); }
void opt_mode () { // ---------------------- Option_Reader opt_mode
// [mode]
opt_t n = s2o(p[0]); switch ( n ) {
case o_ra: case o_ro: case o_rw: mode=n; p++; break;
default: break; } }
void path () { // ------------------------------ Option_Reader path
// path
arg0 = * p ++ ;
if ( newroot ) { type = overlay.n ? o_partial : o_full ; }
else { type=o_newroot; newroot=arg0; } }
}; // end class Option_Reader -------------- end class Option_Reader
struct Logic : Lib { // xxlo ------------------------- struct Logic
static mopt actual ( Str path, opt_t mode ) { // ----- Logic actual
return ( mode == o_rw
|| is_inside_workdir ( path )
|| ( mode == o_ra
&& ( opt('r')
|| opt('w')
|| is_inside_readauto ( path ) ) )
? o_rw : o_ro ); }
static void binds ( sink<Bind> fn ) { // -------------- Logic binds
// Transform each command line Option into zero or more Binds.
// Pass each Bind to fn.
Bind b;
auto single = [&] () {
if ( is_overbound ( b ) ) { return; }