-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsyscall.c
1209 lines (1051 loc) · 31.1 KB
/
syscall.c
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
/**
* @file kernel/sys/syscall.c
* @brief System call handlers.
*
* @copyright
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2011-2021 K. Lange
*/
#include <stdint.h>
#include <errno.h>
#include <sys/sysfunc.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/ptrace.h>
#include <syscall_nums.h>
#include <kernel/printf.h>
#include <kernel/process.h>
#include <kernel/string.h>
#include <kernel/version.h>
#include <kernel/pipe.h>
#include <kernel/shm.h>
#include <kernel/mmu.h>
#include <kernel/pty.h>
#include <kernel/spinlock.h>
#include <kernel/signal.h>
#include <kernel/time.h>
#include <kernel/syscall.h>
#include <kernel/misc.h>
#include <kernel/ptrace.h>
static char hostname[256];
static size_t hostname_len = 0;
int ptr_validate(void * ptr, const char * syscall) {
if (ptr) {
if (!PTR_INRANGE(ptr)) {
send_signal(this_core->current_process->id, SIGSEGV, 1);
return 1;
}
if (!mmu_validate_user_pointer(ptr,1,0)) return 1;
}
return 0;
}
#define PTRCHECK(addr,size,flags) do { if (!mmu_validate_user_pointer(addr,size,flags)) return -EFAULT; } while (0)
long sys_sbrk(ssize_t size) {
if (size & 0xFFF) return -EINVAL;
volatile process_t * volatile proc = this_core->current_process;
if (proc->group != 0) {
proc = process_from_pid(proc->group);
}
spin_lock(proc->image.lock);
uintptr_t out = proc->image.heap;
for (uintptr_t i = out; i < out + size; i += 0x1000) {
union PML * page = mmu_get_page(i, MMU_GET_MAKE);
if (page->bits.page != 0) {
printf("odd, %#zx is already allocated?\n", i);
}
mmu_frame_allocate(page, MMU_FLAG_WRITABLE);
}
proc->image.heap += size;
spin_unlock(proc->image.lock);
return (long)out;
}
extern int elf_module(char ** args);
long sys_sysfunc(long fn, char ** args) {
/* FIXME: Most of these should be top-level, many are hacks/broken in Misaka */
switch (fn) {
case TOARU_SYS_FUNC_SYNC:
/* FIXME: There is no sync ability in the VFS at the moment.
* XXX: Should this just be an ioctl on individual devices?
* Or possibly even an ioctl we can send to arbitrary files? */
printf("sync: not implemented\n");
return -EINVAL;
case TOARU_SYS_FUNC_LOGHERE:
/* FIXME: The entire kernel logging system needs to be revamped as
* Misaka switched everything to raw printfs, and then also
* removed most of them for cleanliness... first task would
* be to reintroduce kernel fprintf() to printf to fs_nodes. */
//if (this_core->current_process->user != 0) return -EACCES;
printf("\033[32m%s\033[0m", (char*)args);
return -EINVAL;
case TOARU_SYS_FUNC_KDEBUG:
/* FIXME: The kernel debugger is completely deprecated and fully removed
* in Misaka, and I'm not sure I want to add it back... */
if (this_core->current_process->user != 0) return -EACCES;
printf("kdebug: not implemented\n");
return -EINVAL;
case 42:
#ifdef __aarch64__
PTR_VALIDATE(&args[0]);
PTR_VALIDATE(&args[1]);
extern void arch_clear_icache(uintptr_t,uintptr_t);
arch_clear_icache((uintptr_t)args[0], (uintptr_t)args[1]);
#endif
return 0;
case 43: {
extern void mmu_unmap_user(uintptr_t addr, size_t size);
PTR_VALIDATE(&args[0]);
PTR_VALIDATE(&args[1]);
mmu_unmap_user((uintptr_t)args[0], (size_t)args[1]);
return 0;
}
case TOARU_SYS_FUNC_INSMOD:
/* Linux has init_module as a system call? */
if (this_core->current_process->user != 0) return -EACCES;
#if defined(__aarch64__)
/* TODO: Most modules are not right for this and we are missing relocations */
return -EINVAL;
#endif
PTR_VALIDATE(args);
if (!args) return -EFAULT;
PTR_VALIDATE(args[0]);
if (!args[0]) return -EFAULT;
for (char ** aa = args; *aa; ++aa) { PTR_VALIDATE(*aa); }
return elf_module(args);
case TOARU_SYS_FUNC_SETHEAP: {
/* I'm not really sure how this should be done...
* traditional brk() would be expected to map everything in-between,
* but we use this to move the heap in ld.so, and we don't want
* the stuff in the middle to be mapped necessarily... */
PTR_VALIDATE(args);
if (!args) return -EFAULT;
if (!PTR_INRANGE(args[0])) return -EFAULT;
if (!args[0]) return -EFAULT;
volatile process_t * volatile proc = this_core->current_process;
if (proc->group != 0) proc = process_from_pid(proc->group);
spin_lock(proc->image.lock);
proc->image.heap = (uintptr_t)args[0];
spin_unlock(proc->image.lock);
return 0;
}
case TOARU_SYS_FUNC_MMAP: {
/* FIXME: This whole thing should be removed; we need a proper mmap interface,
* preferrably with all of the file mapping options, too. And it should
* probably also interact with the SHM subsystem... */
PTR_VALIDATE(args);
if (!args) return -EFAULT;
volatile process_t * volatile proc = this_core->current_process;
if (proc->group != 0) proc = process_from_pid(proc->group);
spin_lock(proc->image.lock);
/* Align inputs */
uintptr_t start = ((uintptr_t)args[0]) & 0xFFFFffffFFFFf000UL;
uintptr_t end = ((uintptr_t)args[0] + (size_t)args[1] + 0xFFF) & 0xFFFFffffFFFFf000UL;
if (!PTR_INRANGE(start)) return -EFAULT;
if (!PTR_INRANGE(end)) return -EFAULT;
for (uintptr_t i = start; i < end; i += 0x1000) {
union PML * page = mmu_get_page(i, MMU_GET_MAKE);
mmu_frame_allocate(page, MMU_FLAG_WRITABLE);
}
spin_unlock(proc->image.lock);
return 0;
}
case TOARU_SYS_FUNC_THREADNAME: {
/* This should probably be moved to a new system call. */
int count = 0;
char **arg = args;
PTR_VALIDATE(args);
if (!args) return -EFAULT;
while (*arg) {
PTR_VALIDATE(*arg);
count++;
arg++;
}
this_core->current_process->cmdline = malloc(sizeof(char*)*(count+1));
int i = 0;
while (i < count) {
this_core->current_process->cmdline[i] = strdup(args[i]);
i++;
}
this_core->current_process->cmdline[i] = NULL;
return 0;
}
case TOARU_SYS_FUNC_SETGSBASE:
/* This should be a new system call; see what Linux, et al., call it. */
PTR_VALIDATE(args);
if (!args) return -EFAULT;
PTR_VALIDATE(args[0]);
this_core->current_process->thread.context.tls_base = (uintptr_t)args[0];
arch_set_tls_base(this_core->current_process->thread.context.tls_base);
return 0;
case TOARU_SYS_FUNC_NPROC:
return processor_count;
default:
printf("Bad system function: %ld\n", fn);
return -EINVAL;
}
return -EINVAL;
}
__attribute__((noreturn))
long sys_exit(long exitcode) {
task_exit((exitcode & 0xFF) << 8);
__builtin_unreachable();
}
long sys_write(int fd, char * ptr, unsigned long len) {
#if 0
/* Enable this to force stderr output to always be printed by the kernel. */
if (fd == 2) {
printf_output(len,ptr);
}
#endif
if (FD_CHECK(fd)) {
PTRCHECK(ptr,len,MMU_PTR_NULL);
fs_node_t * node = FD_ENTRY(fd);
if (!(FD_MODE(fd) & 2)) return -EACCES;
if (len && !ptr) {
return -EFAULT;
}
int64_t out = write_fs(node, FD_OFFSET(fd), len, (uint8_t*)ptr);
if (out > 0) {
FD_OFFSET(fd) += out;
}
return out;
}
return -EBADF;
}
static long stat_node(fs_node_t * fn, uintptr_t st) {
struct stat * f = (struct stat *)st;
if (!fn) {
/* XXX: Does this need to zero the stat struct when returning -ENOENT? */
memset(f, 0x00, sizeof(struct stat));
return -ENOENT;
}
f->st_dev = (uint16_t)(((uint64_t)fn->device & 0xFFFF0) >> 8);
f->st_ino = fn->inode;
uint32_t flags = 0;
if (fn->flags & FS_FILE) { flags |= _IFREG; }
if (fn->flags & FS_DIRECTORY) { flags |= _IFDIR; }
if (fn->flags & FS_CHARDEVICE) { flags |= _IFCHR; }
if (fn->flags & FS_BLOCKDEVICE) { flags |= _IFBLK; }
if (fn->flags & FS_PIPE) { flags |= _IFIFO; }
if (fn->flags & FS_SYMLINK) { flags |= _IFLNK; }
f->st_mode = fn->mask | flags;
f->st_nlink = fn->nlink;
f->st_uid = fn->uid;
f->st_gid = fn->gid;
f->st_rdev = 0;
f->st_size = fn->length;
f->st_atime = fn->atime;
f->st_mtime = fn->mtime;
f->st_ctime = fn->ctime;
f->st_blksize = 512; /* whatever */
if (fn->get_size) {
f->st_size = fn->get_size(fn);
}
return 0;
}
long sys_stat(int fd, uintptr_t st) {
PTR_VALIDATE(st);
if (!st) return -EFAULT;
if (FD_CHECK(fd)) {
return stat_node(FD_ENTRY(fd), st);
}
return -EBADF;
}
long sys_statf(char * file, uintptr_t st) {
int result;
PTR_VALIDATE(file);
PTR_VALIDATE(st);
if (!file || !st) return -EFAULT;
fs_node_t * fn = kopen(file, 0);
result = stat_node(fn, st);
if (fn) {
close_fs(fn);
}
return result;
}
long sys_symlink(char * target, char * name) {
PTR_VALIDATE(target);
PTR_VALIDATE(name);
if (!target || !name) return -EFAULT;
return symlink_fs(target, name);
}
long sys_readlink(const char * file, char * ptr, long len) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
fs_node_t * node = kopen((char *) file, O_PATH | O_NOFOLLOW);
if (!node) {
return -ENOENT;
}
long rv = readlink_fs(node, ptr, len);
close_fs(node);
return rv;
}
long sys_lstat(char * file, uintptr_t st) {
PTR_VALIDATE(file);
PTR_VALIDATE(st);
if (!file || !st) return -EFAULT;
fs_node_t * fn = kopen(file, O_PATH | O_NOFOLLOW);
long result = stat_node(fn, st);
if (fn) {
close_fs(fn);
}
return result;
}
long sys_open(const char * file, long flags, long mode) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
fs_node_t * node = kopen((char *)file, flags);
int access_bits = 0;
if (node && (flags & O_CREAT) && (flags & O_EXCL)) {
close_fs(node);
return -EEXIST;
}
if (!(flags & O_WRONLY) || (flags & O_RDWR)) {
if (node && !has_permission(node, 04)) {
close_fs(node);
return -EACCES;
} else {
access_bits |= 01;
}
}
if ((flags & O_RDWR) || (flags & O_WRONLY)) {
if (node && !has_permission(node, 02)) {
close_fs(node);
return -EACCES;
}
if (node && (node->flags & FS_DIRECTORY)) {
return -EISDIR;
}
if ((flags & O_RDWR) || (flags & O_WRONLY)) {
/* truncate doesn't grant write permissions */
access_bits |= 02;
}
}
if (!node && (flags & O_CREAT)) {
/* TODO check directory permissions */
int result = create_file_fs((char *)file, mode);
if (!result) {
node = kopen((char *)file, flags);
} else {
return result;
}
}
if (node && (flags & O_DIRECTORY)) {
if (!(node->flags & FS_DIRECTORY)) {
return -ENOTDIR;
}
}
if (node && (flags & O_TRUNC)) {
if (!(access_bits & 02)) {
close_fs(node);
return -EINVAL;
}
truncate_fs(node);
}
if (!node) {
return -ENOENT;
}
if (node && (flags & O_CREAT) && (node->flags & FS_DIRECTORY)) {
close_fs(node);
return -EISDIR;
}
int fd = process_append_fd((process_t *)this_core->current_process, node);
FD_MODE(fd) = access_bits;
if (flags & O_APPEND) {
FD_OFFSET(fd) = node->length;
} else {
FD_OFFSET(fd) = 0;
}
return fd;
}
long sys_close(int fd) {
if (FD_CHECK(fd)) {
close_fs(FD_ENTRY(fd));
FD_ENTRY(fd) = NULL;
return 0;
}
return -EBADF;
}
long sys_seek(int fd, long offset, long whence) {
if (FD_CHECK(fd)) {
if ((FD_ENTRY(fd)->flags & FS_PIPE) || (FD_ENTRY(fd)->flags & FS_CHARDEVICE)) return -ESPIPE;
switch (whence) {
case 0:
FD_OFFSET(fd) = offset;
break;
case 1:
FD_OFFSET(fd) += offset;
break;
case 2:
FD_OFFSET(fd) = FD_ENTRY(fd)->length + offset;
break;
default:
return -EINVAL;
}
return FD_OFFSET(fd);
}
return -EBADF;
}
long sys_read(int fd, char * ptr, unsigned long len) {
if (FD_CHECK(fd)) {
PTRCHECK(ptr,len,MMU_PTR_NULL|MMU_PTR_WRITE);
if (len && !ptr) {
return -EFAULT;
}
fs_node_t * node = FD_ENTRY(fd);
if (!(FD_MODE(fd) & 01)) {
return -EACCES;
}
uint64_t out = read_fs(node, FD_OFFSET(fd), len, (uint8_t *)ptr);
FD_OFFSET(fd) += out;
return out;
}
return -EBADF;
}
long sys_ioctl(int fd, unsigned long request, void * argp) {
if (FD_CHECK(fd)) {
PTR_VALIDATE(argp);
return ioctl_fs(FD_ENTRY(fd), request, argp);
}
return -EBADF;
}
long sys_readdir(int fd, long index, struct dirent * entry) {
if (FD_CHECK(fd)) {
PTR_VALIDATE(entry);
if (!entry) return -EFAULT;
struct dirent * kentry = readdir_fs(FD_ENTRY(fd), (uint64_t)index);
if (kentry) {
memcpy(entry, kentry, sizeof *entry);
free(kentry);
return 1;
} else {
return 0;
}
}
return -EBADF;
}
long sys_mkdir(char * path, uint64_t mode) {
PTR_VALIDATE(path);
if (!path) return -EFAULT;
return mkdir_fs(path, mode);
}
long sys_access(const char * file, long flags) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
fs_node_t * node = kopen((char *)file, 0);
if (!node) return -ENOENT;
close_fs(node);
return 0;
}
long sys_chmod(char * file, long mode) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
fs_node_t * fn = kopen(file, 0);
if (fn) {
/* Can group members change bits? I think it's only owners. */
if (this_core->current_process->user != 0 && this_core->current_process->user != fn->uid) {
close_fs(fn);
return -EACCES;
}
long result = chmod_fs(fn, mode);
close_fs(fn);
return result;
} else {
return -ENOENT;
}
}
static int current_group_matches(gid_t gid) {
if (gid == this_core->current_process->user_group) return 1;
for (int i = 0; i < this_core->current_process->supplementary_group_count; ++i) {
if (gid == this_core->current_process->supplementary_group_list[i]) return 1;
}
return 0;
}
long sys_chown(char * file, uid_t uid, uid_t gid) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
fs_node_t * fn = kopen(file, 0);
if (fn) {
/* Only a privileged user can change the owner of a file. */
if (this_core->current_process->user != USER_ROOT_UID && uid != -1) {
goto _access;
}
if (this_core->current_process->user != USER_ROOT_UID && gid != -1) {
/* The owner of a file... */
if (this_core->current_process->user != fn->uid) {
goto _access;
}
/* May change the group of the file to one that the owner is a member of... */
if (!current_group_matches(gid)) {
goto _access;
}
}
if ((uid != -1 || gid != -1) && (fn->mask & 0x800)) {
/* Whenever the owner or group of a setuid executable is changed, it
* loses the setuid bit. */
chmod_fs(fn, fn->mask & (~0x800));
}
long result = chown_fs(fn, uid, gid);
close_fs(fn);
return result;
} else {
return -ENOENT;
}
_access:
close_fs(fn);
return -EACCES;
}
long sys_gettimeofday(struct timeval * tv, void * tz) {
PTR_VALIDATE(tv);
PTR_VALIDATE(tz);
if (!tv) return -EFAULT;
return gettimeofday(tv, tz);
}
long sys_settimeofday(struct timeval * tv, void * tz) {
extern int settimeofday(struct timeval * t, void *z);
if (this_core->current_process->user != USER_ROOT_UID) return -EPERM;
PTR_VALIDATE(tv);
PTR_VALIDATE(tz);
return settimeofday(tv,tz);
}
long sys_getuid(void) {
return (long)this_core->current_process->real_user;
}
long sys_geteuid(void) {
return (long)this_core->current_process->user;
}
long sys_setuid(uid_t new_uid) {
if (this_core->current_process->user == USER_ROOT_UID) {
this_core->current_process->user = new_uid;
this_core->current_process->real_user = new_uid;
return 0;
}
return -EPERM;
}
long sys_getgid(void) {
return (long)this_core->current_process->real_user_group;
}
long sys_getegid(void) {
return (long)this_core->current_process->user_group;
}
long sys_setgid(gid_t new_gid) {
if (this_core->current_process->user == USER_ROOT_UID) {
this_core->current_process->user_group = new_gid;
this_core->current_process->real_user_group = new_gid;
return 0;
}
return -EPERM;
}
long sys_getgroups(int size, gid_t list[]) {
if (size == 0) {
return this_core->current_process->supplementary_group_count;
} else if (size < this_core->current_process->supplementary_group_count) {
return -EINVAL;
} else {
PTR_VALIDATE(list);
if (!list) return -EFAULT;
for (int i = 0; i < this_core->current_process->supplementary_group_count; ++i) {
PTR_VALIDATE(list + i);
list[i] = this_core->current_process->supplementary_group_list[i];
}
return this_core->current_process->supplementary_group_count;
}
}
long sys_setgroups(int size, const gid_t list[]) {
if (this_core->current_process->user != USER_ROOT_UID) return -EPERM;
if (size < 0) return -EINVAL;
if (size > 32) return -EINVAL; /* Arbitrary decision */
/* Free the current set. */
if (this_core->current_process->supplementary_group_count) {
free(this_core->current_process->supplementary_group_list);
this_core->current_process->supplementary_group_list = NULL;
}
this_core->current_process->supplementary_group_count = size;
if (size == 0) return 0;
this_core->current_process->supplementary_group_list = malloc(sizeof(gid_t) * size);
PTR_VALIDATE(list);
if (!list) return -EFAULT;
for (int i = 0; i < size; ++i) {
PTR_VALIDATE(list + i);
this_core->current_process->supplementary_group_list[i] = list[i];
}
return 0;
}
long sys_getpid(void) {
/* The user actually wants the pid of the originating thread (which can be us). */
return this_core->current_process->group ? (long)this_core->current_process->group : (long)this_core->current_process->id;
}
long sys_gettid(void) {
return (long)this_core->current_process->id;
}
long sys_setsid(void) {
if (this_core->current_process->job == this_core->current_process->group) {
return -EPERM;
}
this_core->current_process->session = this_core->current_process->group;
this_core->current_process->job = this_core->current_process->group;
return this_core->current_process->session;
}
long sys_setpgid(pid_t pid, pid_t pgid) {
if (pgid < 0) {
return -EINVAL;
}
process_t * proc = NULL;
if (pid == 0) {
proc = (process_t*)this_core->current_process;
} else {
proc = process_from_pid(pid);
}
if (!proc) {
return -ESRCH;
}
if (proc->session != this_core->current_process->session || proc->session == proc->group) {
return -EPERM;
}
if (pgid == 0) {
proc->job = proc->group;
} else {
process_t * pgroup = process_from_pid(pgid);
if (!pgroup || pgroup->session != proc->session) {
return -EPERM;
}
proc->job = pgid;
}
return 0;
}
long sys_getpgid(pid_t pid) {
process_t * proc;
if (pid == 0) {
proc = (process_t*)this_core->current_process;
} else {
proc = process_from_pid(pid);
}
if (!proc) {
return -ESRCH;
}
return proc->job;
}
long sys_uname(struct utsname * name) {
PTR_VALIDATE(name);
if (!name) return -EFAULT;
char version_number[256];
snprintf(version_number, 255, __kernel_version_format,
__kernel_version_major,
__kernel_version_minor,
__kernel_version_lower,
__kernel_version_suffix);
char version_string[256];
snprintf(version_string, 255, "%s %s %s",
__kernel_version_codename,
__kernel_build_date,
__kernel_build_time);
strcpy(name->sysname, __kernel_name);
strcpy(name->nodename, hostname);
strcpy(name->release, version_number);
strcpy(name->version, version_string);
strcpy(name->machine, __kernel_arch);
strcpy(name->domainname, ""); /* TODO */
return 0;
}
long sys_chdir(char * newdir) {
PTR_VALIDATE(newdir);
if (!newdir) return -EFAULT;
char * path = canonicalize_path(this_core->current_process->wd_name, newdir);
fs_node_t * chd = kopen(path, 0);
if (chd) {
if ((chd->flags & FS_DIRECTORY) == 0) {
close_fs(chd);
return -ENOTDIR;
}
if (!has_permission(chd, 01)) {
close_fs(chd);
return -EACCES;
}
close_fs(chd);
free(this_core->current_process->wd_name);
this_core->current_process->wd_name = malloc(strlen(path) + 1);
memcpy(this_core->current_process->wd_name, path, strlen(path) + 1);
return 0;
} else {
return -ENOENT;
}
}
long sys_getcwd(char * buf, size_t size) {
if (buf) {
PTR_VALIDATE(buf);
size_t len = strlen(this_core->current_process->wd_name) + 1;
return (long)memcpy(buf, this_core->current_process->wd_name, size < len ? size : len);
}
return 0;
}
long sys_dup2(int old, int new) {
return process_move_fd((process_t *)this_core->current_process, old, new);
}
long sys_sethostname(char * new_hostname) {
if (this_core->current_process->user == USER_ROOT_UID) {
PTR_VALIDATE(new_hostname);
if (!new_hostname) return -EFAULT;
size_t len = strlen(new_hostname) + 1;
if (len > 256) {
return -ENAMETOOLONG;
}
hostname_len = len;
memcpy(hostname, new_hostname, hostname_len);
return 0;
} else {
return -EPERM;
}
}
long sys_gethostname(char * buffer) {
PTR_VALIDATE(buffer);
if (!buffer) return -EFAULT;
memcpy(buffer, hostname, hostname_len);
return hostname_len;
}
long sys_mount(char * arg, char * mountpoint, char * type, unsigned long flags, void * data) {
/* TODO: Make use of flags and data from mount command. */
(void)flags;
(void)data;
if (this_core->current_process->user != USER_ROOT_UID) {
return -EPERM;
}
if (PTR_INRANGE(arg) && PTR_INRANGE(mountpoint) && PTR_INRANGE(type)) {
return vfs_mount_type(type, arg, mountpoint);
}
return -EFAULT;
}
long sys_umask(long mode) {
this_core->current_process->mask = mode & 0777;
return 0;
}
long sys_unlink(char * file) {
PTR_VALIDATE(file);
if (!file) return -EFAULT;
return unlink_fs(file);
}
long sys_execve(const char * filename, char *const argv[], char *const envp[]) {
PTR_VALIDATE(filename);
PTR_VALIDATE(argv);
PTR_VALIDATE(envp);
if (!filename || !argv) return -EFAULT;
int argc = 0;
int envc = 0;
while (argv[argc]) {
PTR_VALIDATE(argv[argc]);
++argc;
}
if (envp) {
while (envp[envc]) {
PTR_VALIDATE(envp[envc]);
++envc;
}
}
char **argv_ = malloc(sizeof(char*) * (argc + 1));
for (int j = 0; j < argc; ++j) {
argv_[j] = malloc(strlen(argv[j]) + 1);
memcpy(argv_[j], argv[j], strlen(argv[j]) + 1);
}
argv_[argc] = 0;
char ** envp_;
if (envp && envc) {
envp_ = malloc(sizeof(char*) * (envc + 1));
for (int j = 0; j < envc; ++j) {
envp_[j] = malloc(strlen(envp[j]) + 1);
memcpy(envp_[j], envp[j], strlen(envp[j]) + 1);
}
envp_[envc] = 0;
} else {
envp_ = malloc(sizeof(char*));
envp_[0] = NULL;
}
/**
* FIXME: For legacy reasons, we're just going to close everything >2 for now,
* but we should really implement proper CLOEXEC semantics...
*/
for (unsigned int i = 3; i < this_core->current_process->fds->length; ++i) {
if (this_core->current_process->fds->entries[i]) {
close_fs(this_core->current_process->fds->entries[i]);
this_core->current_process->fds->entries[i] = NULL;
}
}
shm_release_all((process_t *)this_core->current_process);
this_core->current_process->cmdline = argv_;
return exec(filename, argc, argv_, envp_, 0);
}
long sys_fork(void) {
return fork();
}
long sys_clone(uintptr_t new_stack, uintptr_t thread_func, uintptr_t arg) {
if (!new_stack || !PTR_INRANGE(new_stack)) return -EINVAL;
if (!thread_func || !PTR_INRANGE(thread_func)) return -EINVAL;
return (int)clone(new_stack, thread_func, arg);
}
long sys_waitpid(int pid, int * status, int options) {
if (status && !PTR_INRANGE(status)) return -EINVAL;
return waitpid(pid, status, options);
}
long sys_yield(void) {
switch_task(1);
return 1;
}
long sys_sleepabs(unsigned long seconds, unsigned long subseconds) {
/* Mark us as asleep until <some time period> */
sleep_until((process_t *)this_core->current_process, seconds, subseconds);
/* Switch without adding us to the queue */
//printf("process %p (pid=%d) entering sleep until %ld.%06ld\n", current_process, current_process->id, seconds, subseconds);
switch_task(0);
unsigned long timer_ticks = 0, timer_subticks = 0;
relative_time(0,0,&timer_ticks,&timer_subticks);
//printf("process %p (pid=%d) resumed from sleep at %ld.%06ld\n", current_process, current_process->id, timer_ticks, timer_subticks);
if (seconds > timer_ticks || (seconds == timer_ticks && subseconds >= timer_subticks)) {
return 1;
} else {
return 0;
}
}
long sys_sleep(unsigned long seconds, unsigned long subseconds) {
unsigned long s, ss;
relative_time(seconds, subseconds * 10000, &s, &ss);
return sys_sleepabs(s, ss);
}
long sys_pipe(int pipes[2]) {
if (!pipes || !PTR_INRANGE(pipes)) {
return -EFAULT;
}
fs_node_t * outpipes[2];
make_unix_pipe(outpipes);
open_fs(outpipes[0], 0);
open_fs(outpipes[1], 0);
pipes[0] = process_append_fd((process_t *)this_core->current_process, outpipes[0]);
pipes[1] = process_append_fd((process_t *)this_core->current_process, outpipes[1]);
FD_MODE(pipes[0]) = 03;
FD_MODE(pipes[1]) = 03;
return 0;
}
long sys_signal(long signum, uintptr_t handler) {
if (signum > NUMSIGNALS) {
return -EINVAL;
}
uintptr_t old = this_core->current_process->signals[signum];
this_core->current_process->signals[signum] = handler;
return old;
}
long sys_fswait(int c, int fds[]) {
PTR_VALIDATE(fds);
if (!fds) return -EFAULT;
for (int i = 0; i < c; ++i) {
if (!FD_CHECK(fds[i])) return -EBADF;
}
fs_node_t ** nodes = malloc(sizeof(fs_node_t *)*(c+1));
for (int i = 0; i < c; ++i) {
nodes[i] = FD_ENTRY(fds[i]);
}
nodes[c] = NULL;
int result = process_wait_nodes((process_t *)this_core->current_process, nodes, -1);
free(nodes);
return result;
}
long sys_fswait_timeout(int c, int fds[], int timeout) {
PTR_VALIDATE(fds);
if (!fds) return -EFAULT;
for (int i = 0; i < c; ++i) {
if (!FD_CHECK(fds[i])) return -EBADF;
}
fs_node_t ** nodes = malloc(sizeof(fs_node_t *)*(c+1));
for (int i = 0; i < c; ++i) {
nodes[i] = FD_ENTRY(fds[i]);
}
nodes[c] = NULL;
int result = process_wait_nodes((process_t *)this_core->current_process, nodes, timeout);
free(nodes);
return result;
}
long sys_fswait_multi(int c, int fds[], int timeout, int out[]) {
PTR_VALIDATE(fds);
PTR_VALIDATE(out);
if (!fds || !out) return -EFAULT;
int has_match = -1;
for (int i = 0; i < c; ++i) {
if (!FD_CHECK(fds[i])) {
return -EBADF;