forked from canonical/lxd
/
main_nsexec.go
839 lines (697 loc) · 18.1 KB
/
main_nsexec.go
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
/**
* This file is a bit funny. The goal here is to use setns() to manipulate
* files inside the container, so we don't have to reason about the paths to
* make sure they don't escape (we can simply rely on the kernel for
* correctness). Unfortunately, you can't setns() to a mount namespace with a
* multi-threaded program, which every golang binary is. However, by declaring
* our init as an initializer, we can capture process control before it is
* transferred to the golang runtime, so we can then setns() as we'd like
* before golang has a chance to set up any threads. So, we implement two new
* lxd fork* commands which are captured here, and take a file on the host fs
* and copy it into the container ns.
*
* An alternative to this would be to move this code into a separate binary,
* which of course has problems of its own when it comes to packaging (how do
* we find the binary, what do we do if someone does file push and it is
* missing, etc.). After some discussion, even though the embedded method is
* somewhat convoluted, it was preferred.
*/
package main
/*
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sched.h>
#include <linux/sched.h>
#include <linux/limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <alloca.h>
#include <libgen.h>
#include <ifaddrs.h>
#include <dirent.h>
#include <grp.h>
// This expects:
// ./lxd forkputfile /source/path <pid> /target/path
// or
// ./lxd forkgetfile /target/path <pid> /soruce/path <uid> <gid> <mode>
// i.e. 8 arguments, each which have a max length of PATH_MAX.
// Unfortunately, lseek() and fstat() both fail (EINVAL and 0 size) for
// procfs. Also, we can't mmap, because procfs doesn't support that, either.
//
#define CMDLINE_SIZE (8 * PATH_MAX)
void error(char *msg)
{
int old_errno = errno;
if (old_errno == 0) {
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "errno: 0\n");
return;
}
perror(msg);
fprintf(stderr, "errno: %d\n", old_errno);
}
int mkdir_p(const char *dir, mode_t mode)
{
const char *tmp = dir;
const char *orig = dir;
char *makeme;
do {
dir = tmp + strspn(tmp, "/");
tmp = dir + strcspn(dir, "/");
makeme = strndup(orig, dir - orig);
if (*makeme) {
if (mkdir(makeme, mode) && errno != EEXIST) {
fprintf(stderr, "failed to create directory '%s': %s\n", makeme, strerror(errno));
free(makeme);
return -1;
}
}
free(makeme);
} while(tmp != dir);
return 0;
}
int copy(int target, int source, bool append)
{
ssize_t n;
char buf[1024];
if (!append && ftruncate(target, 0) < 0) {
error("error: truncate");
return -1;
}
if (append && lseek(target, 0, SEEK_END) < 0) {
error("error: seek");
return -1;
}
while ((n = read(source, buf, 1024)) > 0) {
if (write(target, buf, n) != n) {
error("error: write");
return -1;
}
}
if (n < 0) {
error("error: read");
return -1;
}
return 0;
}
int dosetns(int pid, char *nstype) {
int mntns;
char buf[PATH_MAX];
sprintf(buf, "/proc/%d/ns/%s", pid, nstype);
mntns = open(buf, O_RDONLY);
if (mntns < 0) {
error("error: open mntns");
return -1;
}
if (setns(mntns, 0) < 0) {
error("error: setns");
close(mntns);
return -1;
}
close(mntns);
return 0;
}
void attach_userns(int pid) {
char nspath[PATH_MAX];
char userns_source[PATH_MAX];
char userns_target[PATH_MAX];
sprintf(nspath, "/proc/%d/ns/user", pid);
if (access(nspath, F_OK) == 0) {
if (readlink("/proc/self/ns/user", userns_source, 18) < 0) {
fprintf(stderr, "Failed readlink of source namespace: %s\n", strerror(errno));
_exit(1);
}
if (readlink(nspath, userns_target, PATH_MAX) < 0) {
fprintf(stderr, "Failed readlink of target namespace: %s\n", strerror(errno));
_exit(1);
}
if (strncmp(userns_source, userns_target, PATH_MAX) != 0) {
if (dosetns(pid, "user") < 0) {
fprintf(stderr, "Failed setns to container user namespace: %s\n", strerror(errno));
_exit(1);
}
if (setgroups(0, NULL) < 0) {
fprintf(stderr, "Failed setgroups to container root groups: %s\n", strerror(errno));
_exit(1);
}
if (setgid(0) < 0) {
fprintf(stderr, "Failed setgid to container root group: %s\n", strerror(errno));
_exit(1);
}
if (setuid(0) < 0) {
fprintf(stderr, "Failed setuid to container root user: %s\n", strerror(errno));
_exit(1);
}
}
}
}
int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is_put, char *type, uid_t uid, gid_t gid, mode_t mode, uid_t defaultUid, gid_t defaultGid, mode_t defaultMode, bool append) {
int host_fd = -1, container_fd = -1;
int ret = -1;
int container_open_flags;
struct stat st;
int exists = 1;
bool is_dir_manip = type != NULL && !strcmp(type, "directory");
bool is_symlink_manip = type != NULL && !strcmp(type, "symlink");
char link_target[PATH_MAX];
ssize_t link_length;
if (!is_dir_manip && !is_symlink_manip) {
host_fd = open(host, O_RDWR);
if (host_fd < 0) {
error("error: open");
return -1;
}
}
if (pid > 0) {
attach_userns(pid);
if (dosetns(pid, "mnt") < 0) {
error("error: setns");
goto close_host;
}
} else {
if (chroot(rootfs) < 0) {
error("error: chroot");
goto close_host;
}
if (chdir("/") < 0) {
error("error: chdir");
goto close_host;
}
}
if (is_put && is_dir_manip) {
if (mode == -1) {
mode = defaultMode;
}
if (uid == -1) {
uid = defaultUid;
}
if (gid == -1) {
gid = defaultGid;
}
if (mkdir(container, mode) < 0 && errno != EEXIST) {
error("error: mkdir");
return -1;
}
if (chown(container, uid, gid) < 0) {
error("error: chown");
return -1;
}
return 0;
}
if (is_put && is_symlink_manip) {
if (mode == -1) {
mode = defaultMode;
}
if (uid == -1) {
uid = defaultUid;
}
if (gid == -1) {
gid = defaultGid;
}
if (symlink(host, container) < 0 && errno != EEXIST) {
error("error: symlink");
return -1;
}
if (fchownat(0, container, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) {
error("error: chown");
return -1;
}
return 0;
}
if (fstatat(AT_FDCWD, container, &st, AT_SYMLINK_NOFOLLOW) < 0)
exists = 0;
container_open_flags = O_RDWR;
if (is_put)
container_open_flags |= O_CREAT;
if (is_put && !is_dir_manip && exists && S_ISDIR(st.st_mode)) {
error("error: Path already exists as a directory");
goto close_host;
}
if (exists && S_ISDIR(st.st_mode))
container_open_flags = O_DIRECTORY;
if (!is_put && exists && S_ISLNK(st.st_mode)) {
fprintf(stderr, "uid: %ld\n", (long)st.st_uid);
fprintf(stderr, "gid: %ld\n", (long)st.st_gid);
fprintf(stderr, "mode: %ld\n", (unsigned long)st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
fprintf(stderr, "type: symlink\n");
link_length = readlink(container, link_target, PATH_MAX);
if (link_length < 0 || link_length >= PATH_MAX) {
error("error: readlink");
goto close_host;
}
link_target[link_length] = '\0';
dprintf(host_fd, "%s\n", link_target);
goto close_container;
}
umask(0);
container_fd = open(container, container_open_flags, 0);
if (container_fd < 0) {
error("error: open");
goto close_host;
}
if (is_put) {
if (!exists) {
if (mode == -1) {
mode = defaultMode;
}
if (uid == -1) {
uid = defaultUid;
}
if (gid == -1) {
gid = defaultGid;
}
}
if (copy(container_fd, host_fd, append) < 0) {
error("error: copy");
goto close_container;
}
if (mode != -1 && fchmod(container_fd, mode) < 0) {
error("error: chmod");
goto close_container;
}
if (fchown(container_fd, uid, gid) < 0) {
error("error: chown");
goto close_container;
}
ret = 0;
} else {
if (fstat(container_fd, &st) < 0) {
error("error: stat");
goto close_container;
}
fprintf(stderr, "uid: %ld\n", (long)st.st_uid);
fprintf(stderr, "gid: %ld\n", (long)st.st_gid);
fprintf(stderr, "mode: %ld\n", (unsigned long)st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
if (S_ISDIR(st.st_mode)) {
DIR *fdir;
struct dirent *de;
fdir = fdopendir(container_fd);
if (!fdir) {
error("error: fdopendir");
goto close_container;
}
fprintf(stderr, "type: directory\n");
while((de = readdir(fdir))) {
int len, i;
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
fprintf(stderr, "entry: ");
// swap \n to \0 since we split this output by line
for (i = 0, len = strlen(de->d_name); i < len; i++) {
if (*(de->d_name + i) == '\n')
putc(0, stderr);
else
putc(*(de->d_name + i), stderr);
}
fprintf(stderr, "\n");
}
closedir(fdir);
// container_fd is dead now that we fdopendir'd it
goto close_host;
} else {
fprintf(stderr, "type: file\n");
ret = copy(host_fd, container_fd, false);
}
fprintf(stderr, "type: %s", S_ISDIR(st.st_mode) ? "directory" : "file");
}
close_container:
close(container_fd);
close_host:
close(host_fd);
return ret;
}
#define ADVANCE_ARG_REQUIRED() \
do { \
while (*cur != 0) \
cur++; \
cur++; \
if (size <= cur - buf) { \
fprintf(stderr, "not enough arguments\n"); \
_exit(1); \
} \
} while(0)
void ensure_dir(char *dest) {
struct stat sb;
if (stat(dest, &sb) == 0) {
if ((sb.st_mode & S_IFMT) == S_IFDIR)
return;
if (unlink(dest) < 0) {
fprintf(stderr, "Failed to remove old %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
if (mkdir(dest, 0755) < 0) {
fprintf(stderr, "Failed to mkdir %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
void ensure_file(char *dest) {
struct stat sb;
int fd;
if (stat(dest, &sb) == 0) {
if ((sb.st_mode & S_IFMT) != S_IFDIR)
return;
if (rmdir(dest) < 0) {
fprintf(stderr, "Failed to remove old %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
fd = creat(dest, 0755);
if (fd < 0) {
fprintf(stderr, "Failed to mkdir %s: %s\n", dest, strerror(errno));
_exit(1);
}
close(fd);
}
void create(char *src, char *dest) {
char *dirdup;
char *destdirname;
struct stat sb;
if (stat(src, &sb) < 0) {
fprintf(stderr, "source %s does not exist\n", src);
_exit(1);
}
dirdup = strdup(dest);
if (!dirdup)
_exit(1);
destdirname = dirname(dirdup);
if (mkdir_p(destdirname, 0755) < 0) {
fprintf(stderr, "failed to create path: %s\n", destdirname);
free(dirdup);
_exit(1);
}
free(dirdup);
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
ensure_dir(dest);
return;
default:
ensure_file(dest);
return;
}
}
void forkmount(char *buf, char *cur, ssize_t size) {
char *src, *dest, *opts;
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
attach_userns(pid);
if (dosetns(pid, "mnt") < 0) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
ADVANCE_ARG_REQUIRED();
src = cur;
ADVANCE_ARG_REQUIRED();
dest = cur;
create(src, dest);
if (access(src, F_OK) < 0) {
fprintf(stderr, "Mount source doesn't exist: %s\n", strerror(errno));
_exit(1);
}
if (access(dest, F_OK) < 0) {
fprintf(stderr, "Mount destination doesn't exist: %s\n", strerror(errno));
_exit(1);
}
// Here, we always move recursively, because we sometimes allow
// recursive mounts. If the mount has no kids then it doesn't matter,
// but if it does, we want to move those too.
if (mount(src, dest, "none", MS_MOVE | MS_REC, NULL) < 0) {
fprintf(stderr, "Failed mounting %s onto %s: %s\n", src, dest, strerror(errno));
_exit(1);
}
_exit(0);
}
void forkumount(char *buf, char *cur, ssize_t size) {
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
if (dosetns(pid, "mnt") < 0) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
ADVANCE_ARG_REQUIRED();
if (access(cur, F_OK) < 0) {
fprintf(stderr, "Mount path doesn't exist: %s\n", strerror(errno));
_exit(1);
}
if (umount2(cur, MNT_DETACH) < 0) {
fprintf(stderr, "Error unmounting %s: %s\n", cur, strerror(errno));
_exit(1);
}
_exit(0);
}
void forkdofile(char *buf, char *cur, bool is_put, ssize_t size) {
uid_t uid = 0;
gid_t gid = 0;
mode_t mode = 0;
uid_t defaultUid = 0;
gid_t defaultGid = 0;
mode_t defaultMode = 0;
char *command = cur, *rootfs = NULL, *source = NULL, *target = NULL, *writeMode = NULL, *type = NULL;
pid_t pid;
bool append = false;
ADVANCE_ARG_REQUIRED();
rootfs = cur;
ADVANCE_ARG_REQUIRED();
pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
source = cur;
ADVANCE_ARG_REQUIRED();
target = cur;
if (is_put) {
ADVANCE_ARG_REQUIRED();
type = cur;
ADVANCE_ARG_REQUIRED();
uid = atoi(cur);
ADVANCE_ARG_REQUIRED();
gid = atoi(cur);
ADVANCE_ARG_REQUIRED();
mode = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultUid = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultGid = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultMode = atoi(cur);
ADVANCE_ARG_REQUIRED();
if (strcmp(cur, "append") == 0) {
append = true;
}
}
_exit(manip_file_in_ns(rootfs, pid, source, target, is_put, type, uid, gid, mode, defaultUid, defaultGid, defaultMode, append));
}
void forkcheckfile(char *buf, char *cur, bool is_put, ssize_t size) {
char *command = cur, *rootfs = NULL, *path = NULL;
pid_t pid;
ADVANCE_ARG_REQUIRED();
rootfs = cur;
ADVANCE_ARG_REQUIRED();
pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
path = cur;
if (pid > 0) {
attach_userns(pid);
if (dosetns(pid, "mnt") < 0) {
error("error: setns");
_exit(1);
}
} else {
if (chroot(rootfs) < 0) {
error("error: chroot");
_exit(1);
}
if (chdir("/") < 0) {
error("error: chdir");
_exit(1);
}
}
if (access(path, F_OK) < 0) {
fprintf(stderr, "Path doesn't exist: %s\n", strerror(errno));
_exit(1);
}
_exit(0);
}
void forkremovefile(char *buf, char *cur, bool is_put, ssize_t size) {
char *command = cur, *rootfs = NULL, *path = NULL;
pid_t pid;
struct stat sb;
ADVANCE_ARG_REQUIRED();
rootfs = cur;
ADVANCE_ARG_REQUIRED();
pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
path = cur;
if (pid > 0) {
attach_userns(pid);
if (dosetns(pid, "mnt") < 0) {
error("error: setns");
_exit(1);
}
} else {
if (chroot(rootfs) < 0) {
error("error: chroot");
_exit(1);
}
if (chdir("/") < 0) {
error("error: chdir");
_exit(1);
}
}
if (stat(path, &sb) < 0) {
error("error: stat");
_exit(1);
}
if ((sb.st_mode & S_IFMT) == S_IFDIR) {
if (rmdir(path) < 0) {
fprintf(stderr, "Failed to remove %s: %s\n", path, strerror(errno));
_exit(1);
}
} else {
if (unlink(path) < 0) {
fprintf(stderr, "Failed to remove %s: %s\n", path, strerror(errno));
_exit(1);
}
}
_exit(0);
}
void forkgetnet(char *buf, char *cur, ssize_t size) {
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
if (dosetns(pid, "net") < 0) {
fprintf(stderr, "Failed setns to container network namespace: %s\n", strerror(errno));
_exit(1);
}
// The rest happens in Go
}
void forkproxy(char *buf, char *cur, ssize_t size) {
int cmdline, listen_pid, connect_pid, fdnum, forked, childPid, ret;
char fdpath[80];
char *logPath = NULL, *pidPath = NULL;
FILE *logFile = NULL, *pidFile = NULL;
// Get the arguments
ADVANCE_ARG_REQUIRED();
listen_pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
ADVANCE_ARG_REQUIRED();
connect_pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
ADVANCE_ARG_REQUIRED();
fdnum = atoi(cur);
ADVANCE_ARG_REQUIRED();
forked = atoi(cur);
ADVANCE_ARG_REQUIRED();
logPath = cur;
ADVANCE_ARG_REQUIRED();
pidPath = cur;
// Check if proxy daemon already forked
if (forked == 0) {
logFile = fopen(logPath, "w+");
if (logFile == NULL) {
_exit(1);
}
if (dup2(fileno(logFile), STDOUT_FILENO) < 0) {
fprintf(logFile, "Failed to redirect STDOUT to logfile: %s\n", strerror(errno));
_exit(1);
}
if (dup2(fileno(logFile), STDERR_FILENO) < 0) {
fprintf(logFile, "Failed to redirect STDERR to logfile: %s\n", strerror(errno));
_exit(1);
}
fclose(logFile);
pidFile = fopen(pidPath, "w+");
if (pidFile == NULL) {
fprintf(stderr, "Failed to create pid file for proxy daemon: %s\n", strerror(errno));
_exit(1);
}
childPid = fork();
if (childPid < 0) {
fprintf(stderr, "Failed to fork proxy daemon: %s\n", strerror(errno));
_exit(1);
} else if (childPid != 0) {
fprintf(pidFile, "%d", childPid);
fclose(pidFile);
fclose(stdin);
fclose(stdout);
fclose(stderr);
_exit(0);
} else {
ret = setsid();
if (ret < 0) {
fprintf(stderr, "Failed to setsid in proxy daemon: %s\n", strerror(errno));
_exit(1);
}
}
}
// Cannot pass through -1 to runCommand since it is interpreted as a flag
fdnum = fdnum == 0 ? -1 : fdnum;
ret = snprintf(fdpath, sizeof(fdpath), "/proc/self/fd/%d", fdnum);
if (ret < 0 || (size_t)ret >= sizeof(fdpath)) {
fprintf(stderr, "Failed to format file descriptor path\n");
_exit(1);
}
// Join the listener ns if not already setup
if (access(fdpath, F_OK) < 0) {
// Attach to the network namespace of the listener
if (dosetns(listen_pid, "net") < 0) {
fprintf(stderr, "Failed setns to listener network namespace: %s\n", strerror(errno));
_exit(1);
}
} else {
// Join the connector ns now
if (dosetns(connect_pid, "net") < 0) {
fprintf(stderr, "Failed setns to connector network namespace: %s\n", strerror(errno));
_exit(1);
}
}
}
__attribute__((constructor)) void init(void) {
int cmdline;
char buf[CMDLINE_SIZE];
ssize_t size;
char *cur;
cmdline = open("/proc/self/cmdline", O_RDONLY);
if (cmdline < 0) {
error("error: open");
_exit(232);
}
memset(buf, 0, sizeof(buf));
if ((size = read(cmdline, buf, sizeof(buf)-1)) < 0) {
close(cmdline);
error("error: read");
_exit(232);
}
close(cmdline);
cur = buf;
// skip argv[0]
while (*cur != 0)
cur++;
cur++;
if (size <= cur - buf)
return;
if (strcmp(cur, "forkputfile") == 0) {
forkdofile(buf, cur, true, size);
} else if (strcmp(cur, "forkgetfile") == 0) {
forkdofile(buf, cur, false, size);
} else if (strcmp(cur, "forkcheckfile") == 0) {
forkcheckfile(buf, cur, false, size);
} else if (strcmp(cur, "forkremovefile") == 0) {
forkremovefile(buf, cur, false, size);
} else if (strcmp(cur, "forkmount") == 0) {
forkmount(buf, cur, size);
} else if (strcmp(cur, "forkumount") == 0) {
forkumount(buf, cur, size);
} else if (strcmp(cur, "forkgetnet") == 0) {
forkgetnet(buf, cur, size);
} else if (strcmp(cur, "forkproxy") == 0) {
forkproxy(buf, cur, size);
}
}
*/
import "C"