aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/lyre/generic/generic.cpp
blob: 549e9f0a60882521575b8dc24d52811fdb5ee7c0 (plain)
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
#include <bits/ensure.h>
#include <mlibc/allocator.hpp>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <asm/ioctls.h>
#include <stdlib.h>
#include <abi-bits/fcntl.h>
#include <lyre/syscall.h>
#include <frg/hash.hpp>
#include <frg/hash_map.hpp>

#define STRINGIFY_(X) #X
#define STRINGIFY(X) STRINGIFY_(X)
#define STUB_ONLY { \
	sys_libc_log("STUB_ONLY function on line " STRINGIFY(__LINE__) " was called"); \
	sys_libc_panic(); \
}

namespace {

int fcntl_helper(int fd, int request, int *result, ...) {
	va_list args;
	va_start(args, result);
	if(!mlibc::sys_fcntl) {
		return ENOSYS;
	}
	int ret = mlibc::sys_fcntl(fd, request, args, result);
	va_end(args);
	return ret;
}

}

namespace mlibc {

void sys_libc_log(const char *message) {
	__syscall(SYS_debug, message);
}

void sys_libc_panic() {
	sys_libc_log("\nMLIBC PANIC\n");
	sys_exit(1);
	__builtin_unreachable();
}

void sys_exit(int status) {
	__syscall(SYS_exit, status);
	__builtin_unreachable();
}

#ifndef MLIBC_BUILDING_RTDL

[[noreturn]] void sys_thread_exit() {
	__syscall(SYS_exit_thread);
	__builtin_unreachable();
}

extern "C" void __mlibc_thread_entry();

int sys_clone(void *tcb, pid_t *pid_out, void *stack) {
	(void)tcb;

	__syscall_ret ret = __syscall(SYS_new_thread, (uintptr_t)__mlibc_thread_entry, (uintptr_t)stack);
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}

	*pid_out = ret_value;
	return 0;
}

int sys_kill(pid_t, int) STUB_ONLY

int sys_tcgetattr(int fd, struct termios *attr) {
	int ret;
	if (int r = sys_ioctl(fd, TCGETS, attr, &ret) != 0) {
		return r;
	}
	return 0;
}

int sys_tcsetattr(int fd, int optional_action, const struct termios *attr) {
	int ret;
	switch (optional_action) {
		case TCSANOW:
			optional_action = TCSETS; break;
		case TCSADRAIN:
			optional_action = TCSETSW; break;
		case TCSAFLUSH:
			optional_action = TCSETSF; break;
		default:
			__ensure(!"Unsupported tcsetattr");
	}

	if (int r = sys_ioctl(fd, optional_action, (void *)attr, &ret) != 0) {
		return r;
	}

	return 0;
}

#endif

int sys_tcb_set(void *pointer) {
	__syscall(SYS_set_fs_base, pointer);
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL

int sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout, const sigset_t *sigmask, int *num_events) {
	__syscall_ret ret = __syscall(SYS_ppoll, fds, nfds, timeout, sigmask);
	int ret_value = (int)ret.ret;

	if (ret_value == -1)
		return ret.errno;

	*num_events = ret_value;
	return 0;
}

int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
	struct timespec ts;
	ts.tv_sec = timeout / 1000;
	ts.tv_nsec = (timeout % 1000) * 1000000;
	return sys_ppoll(fds, count, timeout < 0 ? NULL : &ts, NULL, num_events);
}

int sys_epoll_pwait(int, struct epoll_event *, int,
					int, const sigset_t *, int *) STUB_ONLY

int sys_epoll_create(int, int *) STUB_ONLY

int sys_epoll_ctl(int, int, int, struct epoll_event *) STUB_ONLY

int sys_pselect(int nfds, fd_set *read_set, fd_set *write_set,
		fd_set *except_set, const struct timespec *timeout,
		const sigset_t *sigmask, int *num_events) {
	struct pollfd *fds = (struct pollfd *)calloc(nfds, sizeof(struct pollfd));
	if (fds == NULL) {
		return ENOMEM;
	}

	for (int i = 0; i < nfds; i++) {
		struct pollfd *fd = &fds[i];

		if (read_set && FD_ISSET(i, read_set)) {
			fd->events |= POLLIN;
		}
		if (write_set && FD_ISSET(i, write_set)) {
			fd->events |= POLLOUT;
		}
		if (except_set && FD_ISSET(i, except_set)) {
			fd->events |= POLLPRI;
		}

		if (!fd->events) {
			fd->fd = -1;
			continue;
		}
		fd->fd = i;
	}

	int ret = sys_ppoll(fds, nfds, timeout, sigmask, num_events);
	if (ret != 0) {
		free(fds);
		return ret;
	}

	fd_set res_read_set, res_write_set, res_except_set;
	FD_ZERO(&res_read_set);
	FD_ZERO(&res_write_set);
	FD_ZERO(&res_except_set);

	for (int i = 0; i < nfds; i++) {
		struct pollfd *fd = &fds[i];

		if (read_set && FD_ISSET(i, read_set) && (fd->revents & (POLLIN | POLLERR | POLLHUP)) != 0) {
			FD_SET(i, &res_read_set);
		}
		if (write_set && FD_ISSET(i, write_set) && (fd->revents & (POLLOUT | POLLERR | POLLHUP)) != 0) {
			FD_SET(i, &res_write_set);
		}
		if (except_set && FD_ISSET(i, except_set) && (fd->revents & POLLPRI) != 0) {
			FD_SET(i, &res_except_set);
		}
	}

	free(fds);
	if (read_set) {
		*read_set = res_read_set;
	}
	if (write_set) {
		*write_set = res_write_set;
	}
	if (except_set) {
		*except_set = res_except_set;
	}

	return 0;
}

#endif

int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
	__syscall_ret ret = __syscall(SYS_futex_wait, pointer, expected, time);

	if ((int)ret.ret == -1)
		return ret.errno;

	return 0;
}

int sys_futex_wake(int *pointer) {
	__syscall_ret ret = __syscall(SYS_futex_wake, pointer);

	if ((int)ret.ret == -1)
		return ret.errno;

	int num_woken = ret.ret;

	__ensure(num_woken >= 0 && num_woken <= 1);
	return num_woken;
}

#ifndef MLIBC_BUILDING_RTDL

int sys_timerfd_create(int, int *) STUB_ONLY

int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
	__syscall_ret ret = __syscall(SYS_ioctl, fd, request, arg);

	if ((int)ret.ret == -1)
		return ret.errno;

	*result = (int)ret.ret;
	return 0;
}

int sys_isatty(int fd) {
	struct winsize ws;
	int ret;

	if (!sys_ioctl(fd, TIOCGWINSZ, &ws, &ret))
		return 0;

	return ENOTTY;
}

int sys_getcwd(char *buffer, size_t size) {
	__syscall_ret ret = __syscall(SYS_getcwd, buffer, size);

	if ((int)ret.ret == -1)
		return ret.errno;

	return 0;
}

#endif

int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) {
	__syscall_ret ret = __syscall(SYS_openat, dirfd, path, flags, mode);

	if ((int)ret.ret == -1)
		return ret.errno;

	*fd = (int)ret.ret;
	return 0;
}

int sys_open(const char *path, int flags, mode_t mode, int *fd) {
	return sys_openat(AT_FDCWD, path, flags, mode, fd);
}

#ifndef MLIBC_BUILDING_RTDL

int sys_open_dir(const char *path, int *handle) {
	return sys_openat(AT_FDCWD, path, O_DIRECTORY, 0, handle);
}

struct ReadDirState {
	size_t offset;
	size_t capacity;
	void *buffer;
};

static frg::hash_map<int, ReadDirState *, frg::hash<int>, MemoryAllocator> open_dirs{frg::hash<int>{}, getAllocator()};

static ReadDirState *get_dir_state(int fdnum) {
	ReadDirState *result;
	if (auto value = open_dirs.get(fdnum)) {
		result = *value;
	} else {
		result = (ReadDirState *)malloc(sizeof(ReadDirState));
		result->offset = 0;
		result->capacity = 1024;
		result->buffer = malloc(result->capacity);
		open_dirs.insert(fdnum, result);
	}
	return result;
}

int sys_read_entries(int fdnum, void *buffer, size_t max_size, size_t *bytes_read) {
	ReadDirState *state = get_dir_state(fdnum);

retry:
	__syscall_ret ret = __syscall(SYS_readdir, fdnum, state->buffer, &state->capacity);
	if ((int)ret.ret == -1) {
		if (ret.errno == ENOBUFS) {
			state->buffer = realloc(state->buffer, state->capacity);
			goto retry;
		} else {
			return ret.errno;
		}
	}

	size_t offset = 0;
	while (offset < max_size) {
		struct dirent *ent = (struct dirent *)((char *)state->buffer + state->offset);
		if (ent->d_reclen == 0) {
			break;
		}

		if (offset + ent->d_reclen >= max_size) {
			break;
		}

		memcpy((char *)buffer + offset, ent, ent->d_reclen);
		offset += ent->d_reclen;
		state->offset += ent->d_reclen;
	}

	*bytes_read = offset;
	return 0;
}

#endif

int sys_close(int fd) {
	__syscall_ret ret = __syscall(SYS_close, fd);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
#ifndef MLIBC_BUILDING_RTDL
	open_dirs.remove(fd);
#endif
	return 0;
}

int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
	__syscall_ret ret = __syscall(SYS_seek, fd, offset, whence);
	off_t ret_value = (off_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*new_offset = ret_value;
	return 0;
}

int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
	__syscall_ret ret = __syscall(SYS_read, fd, buf, count);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*bytes_read = ret_value;
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL

int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
	__syscall_ret ret = __syscall(SYS_write, fd, buf, count);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*bytes_written = ret_value;
	return 0;
}

int sys_readlink(const char *path, void *data, size_t max_size, ssize_t *length) {
	__syscall_ret ret = __syscall(SYS_readlinkat, AT_FDCWD, path, data, max_size);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*length = ret_value;
	return 0;
}

int sys_link(const char *old_path, const char *new_path) {
	return sys_linkat(AT_FDCWD, old_path, AT_FDCWD, new_path, 0);
}

int sys_linkat(int olddirfd, const char *old_path, int newdirfd, const char *new_path, int flags) {
	__syscall_ret ret = __syscall(SYS_linkat, olddirfd, old_path, newdirfd, new_path, flags);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_unlinkat(int fd, const char *path, int flags) {
	__syscall_ret ret = __syscall(SYS_unlinkat, fd, path, flags);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) {
	__syscall_ret ret = __syscall(SYS_fchmodat, fd, pathname, mode, flags);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_fchmod(int fd, mode_t mode) {
	return sys_fchmodat(fd, "", mode, AT_EMPTY_PATH);
}

int sys_chmod(const char *pathname, mode_t mode) {
	return sys_fchmodat(AT_FDCWD, pathname, mode, 0);
}

int sys_rmdir(const char *) STUB_ONLY

#endif

int sys_vm_map(void *hint, size_t size, int prot, int flags,
			   int fd, off_t offset, void **window) {
	__syscall_ret ret = __syscall(SYS_mmap, hint, size, (uint64_t)prot << 32 | flags, fd, offset);
	void *ret_value = (void *)ret.ret;
	if (ret_value == MAP_FAILED) {
		return ret.errno;
	}
	*window = ret_value;
	return 0;
}

int sys_vm_unmap(void *pointer, size_t size) {
	__syscall_ret ret = __syscall(SYS_unmmap, pointer, size);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

#ifndef MLIBC_BUILDING_RTDL

int sys_vm_protect(void *pointer, size_t size, int prot) {
	__syscall_ret ret = __syscall(SYS_mprotect, pointer, size, prot);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

#endif

int sys_anon_allocate(size_t size, void **pointer) {
	return sys_vm_map(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0, pointer);
}

int sys_anon_free(void *pointer, size_t size) {
	return sys_vm_unmap(pointer, size);
}

#ifndef MLIBC_BUILDING_RTDL

pid_t sys_getpid() {
	__syscall_ret ret = __syscall(SYS_getpid);
	return (pid_t)ret.ret;
}

pid_t sys_getppid() {
	return 0;
}

uid_t sys_getuid() {
	return 0;
}

uid_t sys_geteuid() {
	return 0;
}

gid_t sys_getgid() {
	return 0;
}

int sys_setgid(gid_t) {
	return 0;
}

int sys_getpgid(pid_t, pid_t *) {
	return 0;
}

gid_t sys_getegid() {
	return 0;
}

int sys_setpgid(pid_t, pid_t) {
	return 0;
}

int sys_ttyname(int, char *, size_t) {
	return ENOSYS;
}

int sys_clock_get(int clock, time_t *secs, long *nanos) {
	struct timespec buf;
	__syscall_ret ret = __syscall(SYS_getclock, clock, &buf);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	*secs = buf.tv_sec;
	*nanos = buf.tv_nsec;
	return 0;
}

int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat *statbuf) {
	__syscall_ret ret;
	switch (fsfdt) {
		case fsfd_target::fd:
			ret = __syscall(SYS_stat, fd, "", flags | AT_EMPTY_PATH, statbuf);
			break;
		case fsfd_target::path:
			ret = __syscall(SYS_stat, AT_FDCWD, path, flags, statbuf);
			break;
		case fsfd_target::fd_path:
			ret = __syscall(SYS_stat, fd, path, flags, statbuf);
			break;
		default:
			__ensure(!"sys_stat: Invalid fsfdt");
			__builtin_unreachable();
	}
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_faccessat(int dirfd, const char *pathname, int mode, int flags) {
	(void)flags;
	struct stat buf;
	if (int r = sys_stat(fsfd_target::fd_path, dirfd, pathname, mode & AT_SYMLINK_FOLLOW, &buf)) {
		return r;
	}
	return 0;
}

int sys_access(const char *path, int mode) {
	return sys_faccessat(AT_FDCWD, path, mode, 0);
}

int sys_pipe(int *fds, int flags) {
	__syscall_ret ret = __syscall(SYS_pipe, fds, flags);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_chdir(const char *path) {
	__syscall_ret ret = __syscall(SYS_chdir, path);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_mkdir(const char *path, mode_t mode) {
	return sys_mkdirat(AT_FDCWD, path, mode);
}

int sys_mkdirat(int dirfd, const char *path, mode_t mode) {
	__syscall_ret ret = __syscall(SYS_mkdirat, dirfd, path, mode);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_socket(int domain, int type_and_flags, int proto, int *fd) {
	__syscall_ret ret = __syscall(SYS_socket, domain, type_and_flags, proto);
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*fd = ret_value;
	return 0;
}

int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) {
	__syscall_ret ret = __syscall(SYS_socketpair, domain, type_and_flags, proto, fds);
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
	__syscall_ret ret = __syscall(SYS_bind, fd, addr_ptr, addr_length);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) {
	__syscall_ret ret = __syscall(SYS_connect, fd, addr_ptr, addr_length);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_accept(int fd, int *newfd, struct sockaddr *addr_ptr, socklen_t *addr_length, int flags) {
	__syscall_ret ret = __syscall(SYS_accept, fd, addr_ptr, addr_length);
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*newfd = ret_value;

	if(flags & SOCK_NONBLOCK) {
		int fcntl_ret = 0;
		fcntl_helper(*newfd, F_GETFL, &fcntl_ret);
		fcntl_helper(*newfd, F_SETFL, &fcntl_ret, fcntl_ret | O_NONBLOCK);
	}

	if(flags & SOCK_CLOEXEC) {
		int fcntl_ret = 0;
		fcntl_helper(*newfd, F_GETFD, &fcntl_ret);
		fcntl_helper(*newfd, F_SETFD, &fcntl_ret, fcntl_ret | FD_CLOEXEC);
	}

	return 0;
}

int sys_getsockopt(int fd, int layer, int number, void *__restrict buffer, socklen_t *__restrict size) {
	__syscall_ret ret = __syscall(SYS_getsockopt, fd, layer, number, buffer, size);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_setsockopt(int fd, int layer, int number, const void *buffer, socklen_t size) {
	__syscall_ret ret = __syscall(SYS_setsockopt, fd, layer, number, buffer, size);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_msg_recv(int sockfd, struct msghdr *hdr, int flags, ssize_t *length) {
	__syscall_ret ret = __syscall(SYS_recvmsg, sockfd, hdr, flags);
	ssize_t ret_value = (ssize_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*length = ret_value;
	return 0;
}

int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *length) {
    __syscall_ret ret = __syscall(SYS_sendmsg, sockfd, hdr, flags);
    ssize_t ret_value = (ssize_t)ret.ret;
    if (ret_value == -1) {
        return ret.errno;
    }
    *length = ret_value;
    return 0;
}

int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
	__syscall_ret ret = __syscall(SYS_getpeername, fd, addr_ptr, &max_addr_length);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	*actual_length = max_addr_length;
	return 0;
}

int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length) {
	__syscall_ret ret = __syscall(SYS_getsockname, fd, addr_ptr, &max_addr_length);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	*actual_length = max_addr_length;
	return 0;
}

int sys_listen(int fd, int backlog) {
	__syscall_ret ret = __syscall(SYS_listen, fd, backlog);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_inotify_create(int, int *) {
	mlibc::infoLogger() << "mlibc: sys_inotify_create() is unimplemented" << frg::endlog;
	return ENOSYS;
}

int sys_fork(pid_t *child) {
	__syscall_ret ret = __syscall(SYS_fork);
	pid_t ret_value = (pid_t)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*child = ret_value;
	return 0;
}

int sys_execve(const char *path, char *const argv[], char *const envp[]) {
	__syscall_ret ret = __syscall(SYS_exec, path, argv, envp);
	return ret.errno;
}

int sys_fcntl(int fd, int request, va_list args, int *result) {
	__syscall_ret ret = __syscall(SYS_fcntl, fd, request, va_arg(args, uint64_t));
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*result = ret_value;
	return 0;
}

int sys_dup(int fd, int flags, int *newfd) {
	(void)flags;
	__syscall_ret ret = __syscall(SYS_fcntl, fd, F_DUPFD, 0);
	int ret_value = (int)ret.ret;
	if (ret_value == -1) {
		return ret.errno;
	}
	*newfd = ret_value;
	return 0;
}

int sys_dup2(int fd, int flags, int newfd) {
	__syscall_ret ret = __syscall(SYS_dup3, fd, newfd, flags);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_sigprocmask(int, const sigset_t *__restrict, sigset_t *__restrict) {
	mlibc::infoLogger() << "mlibc: sys_sigprocmask() is a stub" << frg::endlog;
	return 0;
}

int sys_sigaction(int, const struct sigaction *, struct sigaction *) {
	mlibc::infoLogger() << "mlibc: sys_sigaction() is a stub" << frg::endlog;
	return 0;
}

int sys_signalfd_create(sigset_t, int, int *) STUB_ONLY

int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru, pid_t *ret_pid) {
	if (ru != NULL) {
		mlibc::infoLogger() << "mlibc: struct rusage in sys_waitpid is unsupported" << frg::endlog;
		return ENOSYS;
	}
again:
	__syscall_ret ret = __syscall(SYS_waitpid, pid, status, flags);
	pid_t ret_value = (pid_t)ret.ret;
	if (ret_value == -1) {
		if (ret.errno == EINTR) {
			goto again;
		}
		return ret.errno;
	}
	*ret_pid = ret_value;
	return 0;
}

int sys_getgroups(size_t, const gid_t *, int *) {
	mlibc::infoLogger() << "mlibc: sys_getgroups() is unimplemented" << frg::endlog;
	return ENOSYS;
}

int sys_mount(const char *, const char *, const char *, unsigned long, const void *) STUB_ONLY

int sys_umount2(const char *, int) STUB_ONLY

int sys_gethostname(char *buffer, size_t bufsize) {
	struct utsname utsname;
	if (int err = sys_uname(&utsname)) {
		return err;
	}
	if (strlen(utsname.nodename) >= bufsize) {
		return ENAMETOOLONG;
	}
	strncpy(buffer, utsname.nodename, bufsize);
	return 0;
}

int sys_sethostname(const char *, size_t) STUB_ONLY

int sys_sleep(time_t *secs, long *nanos) {
	struct timespec time = {.tv_sec = *secs, .tv_nsec = *nanos};
	struct timespec rem = {.tv_sec = 0, .tv_nsec = 0};
	__syscall_ret ret = __syscall(SYS_sleep, &time, &rem);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	*secs = rem.tv_sec;
	*nanos = rem.tv_nsec;
	return 0;
}

int sys_getitimer(int, struct itimerval *) {
	mlibc::infoLogger() << "mlibc: sys_getitimer() is unimplemented" << frg::endlog;
	return ENOSYS;
}

int sys_setitimer(int, const struct itimerval *, struct itimerval *) {
	mlibc::infoLogger() << "mlibc: sys_setitimer() is unimplemented" << frg::endlog;
	return ENOSYS;
}

int sys_umask(mode_t mode, mode_t *old) {
	__syscall_ret ret = __syscall(SYS_umask, mode);
	*old = (mode_t)ret.ret;
	return 0;
}

int sys_uname(struct utsname *buf) {
	__syscall_ret ret = __syscall(SYS_uname, buf);
	if ((int)ret.ret == -1) {
		return ret.errno;
	}
	return 0;
}

int sys_fsync(int) {
	mlibc::infoLogger() << "sys_fsync is a stub" << frg::endlog;
	return 0;
}

#endif

} // namespace mlibc