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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
|
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>
#include <limits.h>
#include <termios.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <bits/ensure.h>
#include <mlibc/allocator.hpp>
#include <mlibc/arch-defs.hpp>
#include <mlibc/debug.hpp>
#include <mlibc/posix-sysdeps.hpp>
#include <mlibc/bsd-sysdeps.hpp>
#include <mlibc/thread.hpp>
namespace {
constexpr bool logExecvpeTries = false;
}
unsigned int alarm(unsigned int seconds) {
struct itimerval it = {}, old = {};
it.it_value.tv_sec = seconds;
setitimer(ITIMER_REAL, &it, &old);
return old.it_value.tv_sec + !! old.it_value.tv_usec;
}
int chdir(const char *path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_chdir, -1);
if(int e = mlibc::sys_chdir(path); e) {
errno = e;
return -1;
}
return 0;
}
int fchdir(int fd) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fchdir, -1);
if(int e = mlibc::sys_fchdir(fd); e) {
errno = e;
return -1;
}
return 0;
}
int chown(const char *path, uid_t uid, gid_t gid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fchownat, -1);
if(int e = mlibc::sys_fchownat(AT_FDCWD, path, uid, gid, 0); e) {
errno = e;
return -1;
}
return 0;
}
ssize_t confstr(int name, char *buf, size_t len) {
const char *str = "";
if (name == _CS_PATH) {
str = "/bin:/usr/bin";
} else if(name == _CS_GNU_LIBPTHREAD_VERSION) {
// We are not glibc, so we can return 0 here.
return 0;
} else if(name == _CS_GNU_LIBC_VERSION) {
// We are not glibc, so we can return 0 here.
return 0;
} else {
mlibc::infoLogger() << "\e[31mmlibc: confstr() request " << name << " is unimplemented\e[39m"
<< frg::endlog;
__ensure(!"Not implemented");
}
return snprintf(buf, len, "%s", str) + 1;
}
void _exit(int status) {
mlibc::sys_exit(status);
}
int execl(const char *path, const char *arg0, ...) {
// TODO: It's a stupid idea to limit the number of arguments here.
char *argv[16];
argv[0] = const_cast<char *>(arg0);
va_list args;
int n = 1;
va_start(args, arg0);
while(true) {
__ensure(n < 15);
auto argn = va_arg(args, const char *);
argv[n++] = const_cast<char *>(argn);
if(!argn)
break;
}
va_end(args);
argv[n] = nullptr;
return execve(path, argv, environ);
}
// This function is taken from musl.
int execle(const char *path, const char *arg0, ...) {
int argc;
va_list ap;
va_start(ap, arg0);
for(argc = 1; va_arg(ap, const char *); argc++);
va_end(ap);
int i;
char *argv[argc + 1];
char **envp;
va_start(ap, arg0);
argv[0] = (char *)argv;
for(i = 1; i <= argc; i++)
argv[i] = va_arg(ap, char *);
envp = va_arg(ap, char **);
va_end(ap);
return execve(path, argv, envp);
}
// This function is taken from musl
int execlp(const char *file, const char *argv0, ...) {
int argc;
va_list ap;
va_start(ap, argv0);
for(argc = 1; va_arg(ap, const char *); argc++);
va_end(ap);
{
int i;
char *argv[argc + 1];
va_start(ap, argv0);
argv[0] = (char *)argv0;
for(i = 1; i < argc; i++)
argv[i] = va_arg(ap, char *);
argv[i] = NULL;
va_end(ap);
return execvp(file, argv);
}
}
int execv(const char *path, char *const argv[]) {
return execve(path, argv, environ);
}
int execvp(const char *file, char *const argv[]) {
return execvpe(file, argv, environ);
}
int execvpe(const char *file, char *const argv[], char *const envp[]) {
char *null_list[] = {
nullptr
};
if(!argv)
argv = null_list;
if(!envp)
envp = null_list;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_execve, -1);
if(strchr(file, '/')) {
int e = mlibc::sys_execve(file, argv, envp);
__ensure(e && "sys_execve() is supposed to never return with success");
errno = e;
return -1;
}
frg::string_view dirs;
if(const char *pv = getenv("PATH"); pv) {
dirs = pv;
}else{
dirs = "/bin:/usr/bin";
}
size_t p = 0;
int res = ENOENT;
while(p < dirs.size()) {
size_t s; // Offset of next colon or end of string.
if(size_t cs = dirs.find_first(':', p); cs != size_t(-1)) {
s = cs;
}else{
s = dirs.size();
}
frg::string<MemoryAllocator> path{getAllocator()};
path += dirs.sub_string(p, s - p);
path += "/";
path += file;
if(logExecvpeTries)
mlibc::infoLogger() << "mlibc: execvpe() tries '" << path.data() << "'" << frg::endlog;
int e = mlibc::sys_execve(path.data(), argv, envp);
__ensure(e && "sys_execve() is supposed to never return with success");
switch(e) {
case ENOENT:
case ENOTDIR:
break;
case EACCES:
res = EACCES;
break;
default:
errno = e;
return -1;
}
p = s + 1;
}
errno = res;
return -1;
}
int faccessat(int dirfd, const char *pathname, int mode, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_faccessat, -1);
if(int e = mlibc::sys_faccessat(dirfd, pathname, mode, flags); e) {
errno = e;
return -1;
}
return 0;
}
int fchown(int fd, uid_t uid, gid_t gid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fchownat, -1);
if(int e = mlibc::sys_fchownat(fd, "", uid, gid, AT_EMPTY_PATH); e) {
errno = e;
return -1;
}
return 0;
}
int fchownat(int fd, const char *path, uid_t uid, gid_t gid, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fchownat, -1);
if(int e = mlibc::sys_fchownat(fd, path, uid, gid, flags); e) {
errno = e;
return -1;
}
return 0;
}
int fdatasync(int fd) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fdatasync, -1);
if(int e = mlibc::sys_fdatasync(fd); e) {
errno = e;
return -1;
}
return 0;
}
int fexecve(int, char *const [], char *const []) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
long fpathconf(int, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int fsync(int fd) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fsync, -1);
if(auto e = mlibc::sys_fsync(fd); e) {
errno = e;
return -1;
}
return 0;
}
int ftruncate(int fd, off_t size) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_ftruncate, -1);
if(int e = mlibc::sys_ftruncate(fd, size); e) {
errno = e;
return -1;
}
return 0;
}
char *getcwd(char *buffer, size_t size) {
if (buffer) {
if (size == 0) {
errno = EINVAL;
return NULL;
}
} else if (!buffer) {
if (size == 0)
size = PATH_MAX;
buffer = (char *)malloc(size);
}
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getcwd, nullptr);
if(int e = mlibc::sys_getcwd(buffer, size); e) {
errno = e;
return NULL;
}
return buffer;
}
int getgroups(int size, gid_t list[]) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getgroups, -1);
int ret;
if(int e = mlibc::sys_getgroups(size, list, &ret); e) {
errno = e;
return -1;
}
return ret;
}
long gethostid(void) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int gethostname(char *buffer, size_t bufsize) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_gethostname, -1);
if(auto e = mlibc::sys_gethostname(buffer, bufsize); e) {
errno = e;
return -1;
}
return 0;
}
int sethostname(const char *buffer, size_t bufsize) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_sethostname, -1);
if(auto e = mlibc::sys_sethostname(buffer, bufsize); e) {
errno = e;
return -1;
}
return 0;
}
// Code taken from musl
char *getlogin(void) {
return getenv("LOGNAME");
}
int getlogin_r(char *, size_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
// optarg and optind are provided to us by the GLIBC part of the mlibc.
static char *scan = NULL; /* Private scan pointer. */
int getopt(int argc, char *const argv[], const char *optstring) {
char c;
char *place;
optarg = NULL;
if (!scan || *scan == '\0') {
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
return EOF;
if (argv[optind][1] == '-' && argv[optind][2] == '\0') {
optind++;
return EOF;
}
scan = argv[optind]+1;
optind++;
}
c = *scan++;
place = strchr(optstring, c);
if (!place || c == ':') {
fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
return '?';
}
place++;
if (*place == ':') {
if (*scan != '\0') {
optarg = scan;
scan = NULL;
} else if( optind < argc ) {
optarg = argv[optind];
optind++;
} else {
fprintf(stderr, "%s: option requires argument -%c\n", argv[0], c);
return ':';
}
}
return c;
}
pid_t getpgid(pid_t pid) {
pid_t pgid;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getpgid, -1);
if(int e = mlibc::sys_getpgid(pid, &pgid); e) {
errno = e;
return -1;
}
return pgid;
}
pid_t getpgrp(void) {
return getpgid(0);
}
pid_t getsid(pid_t pid) {
if(!mlibc::sys_getsid) {
MLIBC_MISSING_SYSDEP();
return -1;
}
pid_t sid;
if(int e = mlibc::sys_getsid(pid, &sid); e) {
errno = e;
return -1;
}
return sid;
}
int lchown(const char *path, uid_t uid, gid_t gid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fchownat, -1);
if(int e = mlibc::sys_fchownat(AT_FDCWD, path, uid, gid, AT_SYMLINK_NOFOLLOW); e) {
errno = e;
return -1;
}
return 0;
}
int link(const char *old_path, const char *new_path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_link, -1);
if(int e = mlibc::sys_link(old_path, new_path); e) {
errno = e;
return -1;
}
return 0;
}
int linkat(int olddirfd, const char *old_path, int newdirfd, const char *new_path, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_linkat, -1);
if(int e = mlibc::sys_linkat(olddirfd, old_path, newdirfd, new_path, flags); e) {
errno = e;
return -1;
}
return 0;
}
// Code take from musl
int lockf(int fd, int op, off_t size) {
struct flock l = {
.l_type = F_WRLCK,
.l_whence = SEEK_CUR,
.l_start = 0,
.l_len = size,
.l_pid = 0,
};
switch(op) {
case F_TEST:
l.l_type = F_RDLCK;
if(fcntl(fd, F_GETLK, &l) < 0)
return -1;
if(l.l_type == F_UNLCK || l.l_pid == getpid())
return 0;
errno = EACCES;
return -1;
case F_ULOCK:
l.l_type = F_UNLCK;
[[fallthrough]];
case F_TLOCK:
return fcntl(fd, F_SETLK, &l);
case F_LOCK:
return fcntl(fd, F_SETLKW, &l);
}
errno = EINVAL;
return -1;
}
int nice(int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
long pathconf(const char *, int name) {
switch (name) {
case _PC_NAME_MAX:
return NAME_MAX;
default:
mlibc::infoLogger() << "missing pathconf() entry " << name << frg::endlog;
errno = EINVAL;
return -1;
}
}
int pause(void) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pause, -1);
if(int e = mlibc::sys_pause(); e) {
errno = e;
return -1;
}
__ensure(!"There is no successful completion return value for pause");
__builtin_unreachable();
}
int pipe(int *fds) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pipe, -1);
if(int e = mlibc::sys_pipe(fds, 0); e) {
errno = e;
return -1;
}
return 0;
}
int pipe2(int *fds, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pipe, -1);
if(int e = mlibc::sys_pipe(fds, flags); e) {
errno = e;
return -1;
}
return 0;
}
ssize_t pread(int fd, void *buf, size_t n, off_t off) {
ssize_t num_read;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pread, -1);
if(int e = mlibc::sys_pread(fd, buf, n, off, &num_read); e) {
errno = e;
return -1;
}
return num_read;
}
ssize_t pwrite(int fd, const void *buf, size_t n, off_t off) {
ssize_t num_written;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pwrite, -1);
if(int e = mlibc::sys_pwrite(fd, buf, n, off, &num_written); e) {
errno = e;
return -1;
}
return num_written;
}
ssize_t readlink(const char *__restrict path, char *__restrict buffer, size_t max_size) {
ssize_t length;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_readlink, -1);
if(int e = mlibc::sys_readlink(path, buffer, max_size, &length); e) {
errno = e;
return -1;
}
return length;
}
ssize_t readlinkat(int, const char *__restrict, char *__restrict, size_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int rmdir(const char *path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_rmdir, -1);
if(int e = mlibc::sys_rmdir(path); e) {
errno = e;
return -1;
}
return 0;
}
int setegid(gid_t egid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setegid, 0);
if(int e = mlibc::sys_setegid(egid); e) {
errno = e;
return -1;
}
return 0;
}
int seteuid(uid_t euid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_seteuid, 0);
if(int e = mlibc::sys_seteuid(euid); e) {
errno = e;
return -1;
}
return 0;
}
int setgid(gid_t gid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setgid, 0);
if(int e = mlibc::sys_setgid(gid); e) {
errno = e;
return -1;
}
return 0;
}
int setpgid(pid_t pid, pid_t pgid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setpgid, -1);
if(int e = mlibc::sys_setpgid(pid, pgid); e) {
errno = e;
return -1;
}
return 0;
}
pid_t setpgrp(void) {
return setpgid(0, 0);
}
int setregid(gid_t rgid, gid_t egid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setregid, -1);
if(int e = mlibc::sys_setregid(rgid, egid); e) {
errno = e;
return -1;
}
return 0;
}
int setreuid(uid_t ruid, uid_t euid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setreuid, -1);
if(int e = mlibc::sys_setreuid(ruid, euid); e) {
errno = e;
return -1;
}
return 0;
}
pid_t setsid(void) {
if(!mlibc::sys_setsid) {
MLIBC_MISSING_SYSDEP();
return -1;
}
pid_t sid;
if(int e = mlibc::sys_setsid(&sid); e) {
errno = e;
return -1;
}
return sid;
}
int setuid(uid_t uid) {
if(!mlibc::sys_setuid) {
MLIBC_MISSING_SYSDEP();
mlibc::infoLogger() << "mlibc: missing sysdep sys_setuid(). Returning 0" << frg::endlog;
return 0;
}
if(int e = mlibc::sys_setuid(uid); e) {
errno = e;
return -1;
}
return 0;
}
void swab(const void *__restrict, void *__restrict, ssize_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int symlink(const char *target_path, const char *link_path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_symlink, -1);
if(int e = mlibc::sys_symlink(target_path, link_path); e) {
errno = e;
return -1;
}
return 0;
}
int symlinkat(const char *target_path, int dirfd, const char *link_path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_symlinkat, -1);
if(int e = mlibc::sys_symlinkat(target_path, dirfd, link_path); e) {
errno = e;
return -1;
}
return 0;
}
void sync(void) {
if(!mlibc::sys_sync) {
MLIBC_MISSING_SYSDEP();
} else {
mlibc::sys_sync();
}
}
long sysconf(int number) {
if(mlibc::sys_sysconf) {
long ret = 0;
int e = mlibc::sys_sysconf(number, &ret);
if(e && e != EINVAL) {
errno = e;
return -1;
}
if(e != EINVAL) {
return ret;
}
}
/* default return values, if not overriden by sysdep */
switch(number) {
case _SC_ARG_MAX:
// On linux, it is defined to 2097152 in most cases, so define it to be 2097152
return 2097152;
case _SC_PAGE_SIZE:
return mlibc::page_size;
case _SC_OPEN_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_OPEN_MAX) returns fallback value 256\e[39m" << frg::endlog;
return 256;
case _SC_PHYS_PAGES:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_PHYS_PAGES) returns fallback value 1024\e[39m" << frg::endlog;
return 1024;
case _SC_NPROCESSORS_ONLN:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_NPROCESSORS_ONLN) returns fallback value 1\e[39m" << frg::endlog;
return 1;
case _SC_GETPW_R_SIZE_MAX:
return NSS_BUFLEN_PASSWD;
case _SC_GETGR_R_SIZE_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_GETGR_R_SIZE_MAX) returns fallback value 1024\e[39m" << frg::endlog;
return 1024;
case _SC_CHILD_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_CHILD_MAX) returns fallback value 25\e[39m" << frg::endlog;
// On linux, it is defined to 25 in most cases, so define it to be 25
return 25;
case _SC_JOB_CONTROL:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_JOB_CONTROL) returns fallback value 1\e[39m" << frg::endlog;
// If 1, job control is supported
return 1;
case _SC_CLK_TCK:
// TODO: This should be obsolete?
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_CLK_TCK) is obsolete and returns arbitrary value 1000000\e[39m" << frg::endlog;
return 1000000;
case _SC_NGROUPS_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_NGROUPS_MAX) returns fallback value 65536\e[39m" << frg::endlog;
// On linux, it is defined to 65536 in most cases, so define it to be 65536
return 65536;
case _SC_RE_DUP_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_RE_DUP_MAX) returns fallback value RE_DUP_MAX\e[39m" << frg::endlog;
return RE_DUP_MAX;
case _SC_LINE_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_LINE_MAX) returns fallback value 2048\e[39m" << frg::endlog;
// Linux defines it as 2048.
return 2048;
case _SC_XOPEN_CRYPT:
#if __MLIBC_CRYPT_OPTION
return _XOPEN_CRYPT;
#else
return -1;
#endif /* __MLIBC_CRYPT_OPTION */
case _SC_NPROCESSORS_CONF:
// TODO: actually return a proper value for _SC_NPROCESSORS_CONF
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_NPROCESSORS_CONF) unconditionally returns fallback value 1\e[39m" << frg::endlog;
return 1;
case _SC_HOST_NAME_MAX:
mlibc::infoLogger() << "\e[31mmlibc: sysconf(_SC_HOST_NAME_MAX) unconditionally returns fallback value 256\e[39m" << frg::endlog;
return 256;
default:
mlibc::infoLogger() << "\e[31mmlibc: sysconf() call is not implemented, number: " << number << "\e[39m" << frg::endlog;
errno = EINVAL;
return -1;
}
}
pid_t tcgetpgrp(int fd) {
int pgrp;
if(ioctl(fd, TIOCGPGRP, &pgrp) < 0) {
return -1;
}
return pgrp;
}
int tcsetpgrp(int fd, pid_t pgrp) {
return ioctl(fd, TIOCSPGRP, &pgrp);
}
int truncate(const char *, off_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
char *ttyname(int fd) {
const size_t size = 128;
static thread_local char buf[size];
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_ttyname, nullptr);
if(int e = mlibc::sys_ttyname(fd, buf, size); e) {
errno = e;
return nullptr;
}
return buf;
}
int ttyname_r(int fd, char *buf, size_t size) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_ttyname, -1);
if(int e = mlibc::sys_ttyname(fd, buf, size); e) {
return e;
}
return 0;
}
int unlink(const char *path) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_unlinkat, -1);
if(int e = mlibc::sys_unlinkat(AT_FDCWD, path, 0); e) {
errno = e;
return -1;
}
return 0;
}
int unlinkat(int fd, const char *path, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_unlinkat, -1);
if(int e = mlibc::sys_unlinkat(fd, path, flags); e) {
errno = e;
return -1;
}
return 0;
}
int getpagesize() {
return mlibc::page_size;
}
// Code taken from musl
// GLIBC extension for stdin/stdout
char *getpass(const char *prompt) {
int fdin, fdout;
struct termios s, t;
ssize_t l;
static char password[128];
if((fdin = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) {
fdin = STDIN_FILENO;
fdout = STDOUT_FILENO;
} else {
fdout = fdin;
}
tcgetattr(fdin, &t);
s = t;
t.c_lflag &= ~(ECHO | ISIG);
t.c_lflag |= ICANON;
t.c_iflag &= ~(INLCR | IGNCR);
t.c_iflag |= ICRNL;
tcsetattr(fdin, TCSAFLUSH, &t);
tcdrain(fdin);
dprintf(fdout, "%s", prompt);
l = read(fdin, password, sizeof password);
if(l >= 0) {
if((l > 0 && password[l - 1] == '\n') || l == sizeof password)
l--;
password[l] = 0;
}
tcsetattr(fdin, TCSAFLUSH, &s);
dprintf(fdout, "\n");
if(fdin != STDIN_FILENO) {
close(fdin);
}
return l < 0 ? 0 : password;
}
char *get_current_dir_name(void) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
// This is a Linux extension
pid_t gettid(void) {
if(!mlibc::sys_gettid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_gettid()");
}
return mlibc::sys_gettid();
}
int getentropy(void *buffer, size_t length) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getentropy, -1);
if(length > 256) {
errno = EIO;
return -1;
}
if(int e = mlibc::sys_getentropy(buffer, length); e) {
errno = e;
return -1;
}
return 0;
}
ssize_t write(int fd, const void *buf, size_t count) {
ssize_t bytes_written;
if(int e = mlibc::sys_write(fd, buf, count, &bytes_written); e) {
errno = e;
return (ssize_t)-1;
}
return bytes_written;
}
ssize_t read(int fd, void *buf, size_t count) {
ssize_t bytes_read;
if(int e = mlibc::sys_read(fd, buf, count, &bytes_read); e) {
errno = e;
return (ssize_t)-1;
}
return bytes_read;
}
off_t lseek(int fd, off_t offset, int whence) {
off_t new_offset;
if(int e = mlibc::sys_seek(fd, offset, whence, &new_offset); e) {
errno = e;
return (off_t)-1;
}
return new_offset;
}
off64_t lseek64(int fd, off64_t offset, int whence) {
off64_t new_offset;
if(int e = mlibc::sys_seek(fd, offset, whence, &new_offset); e) {
errno = e;
return (off64_t)-1;
}
return new_offset;
}
int close(int fd) {
return mlibc::sys_close(fd);
}
unsigned int sleep(unsigned int secs) {
time_t seconds = secs;
long nanos = 0;
if(!mlibc::sys_sleep) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_sleep()");
}
mlibc::sys_sleep(&seconds, &nanos);
return seconds;
}
// In contrast to sleep() this functions returns 0/-1 on success/failure.
int usleep(useconds_t usecs) {
time_t seconds = 0;
long nanos = usecs * 1000;
if(!mlibc::sys_sleep) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_sleep()");
}
return mlibc::sys_sleep(&seconds, &nanos);
}
int dup(int fd) {
int newfd;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_dup, -1);
if(int e = mlibc::sys_dup(fd, 0, &newfd); e) {
errno = e;
return -1;
}
return newfd;
}
int dup2(int fd, int newfd) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_dup2, -1);
if(int e = mlibc::sys_dup2(fd, 0, newfd); e) {
errno = e;
return -1;
}
return newfd;
}
pid_t fork(void) {
auto self = mlibc::get_current_tcb();
pid_t child;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fork, -1);
auto hand = self->atforkEnd;
while (hand) {
if (hand->prepare)
hand->prepare();
hand = hand->prev;
}
if(int e = mlibc::sys_fork(&child); e) {
errno = e;
return -1;
}
hand = self->atforkBegin;
while (hand) {
if (!child) {
if (hand->child)
hand->child();
} else {
if (hand->parent)
hand->parent();
}
hand = hand->next;
}
return child;
}
pid_t vfork(void) {
pid_t child;
/*
* Fork handlers established using pthread_atfork(3) are not
* called when a multithreaded program employing the NPTL
* threading library calls vfork(). Fork handlers are called
* in this case in a program using the LinuxThreads threading
* library. (See pthreads(7) for a description of Linux
* threading libraries.)
* - vfork(2), release 5.13 of the Linux man-pages project
*
* as a result, we call sys_fork instead of running atforks
*/
/* deferring to fork as implementing vfork correctly requires assembly
* to handle not mucking up the stack
*/
if(!mlibc::sys_fork) {
MLIBC_MISSING_SYSDEP();
errno = ENOSYS;
return -1;
}
if(int e = mlibc::sys_fork(&child); e) {
errno = e;
return -1;
}
return child;
}
int execve(const char *path, char *const argv[], char *const envp[]) {
char *null_list[] = {
nullptr
};
if(!argv)
argv = null_list;
if(!envp)
envp = null_list;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_execve, -1);
int e = mlibc::sys_execve(path, argv, envp);
__ensure(e && "sys_execve() is expected to fail if it returns");
errno = e;
return -1;
}
gid_t getgid(void) {
if(!mlibc::sys_getgid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_getgid()");
}
return mlibc::sys_getgid();
}
gid_t getegid(void) {
if(!mlibc::sys_getegid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_getegid()");
}
return mlibc::sys_getegid();
}
uid_t getuid(void) {
if(!mlibc::sys_getuid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_getuid()");
}
return mlibc::sys_getuid();
}
uid_t geteuid(void) {
if(!mlibc::sys_geteuid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_geteuid()");
}
return mlibc::sys_geteuid();
}
pid_t getpid(void) {
if(!mlibc::sys_getpid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_getpid()");
}
return mlibc::sys_getpid();
}
pid_t getppid(void) {
if(!mlibc::sys_getppid) {
MLIBC_MISSING_SYSDEP();
__ensure(!"Cannot continue without sys_getppid()");
}
return mlibc::sys_getppid();
}
int access(const char *path, int mode) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_access, -1);
if(int e = mlibc::sys_access(path, mode); e) {
errno = e;
return -1;
}
return 0;
}
char *getusershell(void) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
void setusershell(void) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
void endusershell(void) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int isatty(int fd) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_isatty, 0);
if(int e = mlibc::sys_isatty(fd); e) {
errno = e;
return 0;
}
return 1;
}
int chroot(const char *ptr) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_chroot, -1);
if(int e = mlibc::sys_chroot(ptr); e) {
errno = e;
return -1;
}
return 0;
}
int daemon(int, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
char *ctermid(char *s) {
return s ? strcpy(s, "/dev/tty") : const_cast<char *>("/dev/tty");
}
int setresuid(uid_t ruid, uid_t euid, uid_t suid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setresuid, -1);
if(int e = mlibc::sys_setresuid(ruid, euid, suid); e) {
errno = e;
return -1;
}
return 0;
}
int setresgid(gid_t rgid, gid_t egid, gid_t sgid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setresgid, -1);
if(int e = mlibc::sys_setresgid(rgid, egid, sgid); e) {
errno = e;
return -1;
}
return 0;
}
int getdomainname(char *, size_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int setdomainname(const char *, size_t) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getresuid, -1);
if(int e = mlibc::sys_getresuid(ruid, euid, suid); e) {
errno = e;
return -1;
}
return 0;
}
int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getresgid, -1);
if(int e = mlibc::sys_getresgid(rgid, egid, sgid); e) {
errno = e;
return -1;
}
return 0;
}
#if __MLIBC_CRYPT_OPTION
void encrypt(char[64], int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
#endif
#if __MLIBC_BSD_OPTION
void *sbrk(intptr_t increment) {
if(increment) {
errno = ENOMEM;
return (void *)-1;
}
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_brk, (void *)-1);
void *out;
if(int e = mlibc::sys_brk(&out); e) {
errno = e;
return (void *)-1;
}
return out;
}
#endif
|