818493ac1351abbd20165cc4666edeaab46377fe
[lunaix-os.git] / lunaix-os / kernel / process / process.c
1 #include <klibc/string.h>
2 #include <lunaix/clock.h>
3 #include <lunaix/mm/mmap.h>
4 #include <lunaix/mm/vmm.h>
5 #include <lunaix/mm/region.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/process.h>
8 #include <lunaix/spike.h>
9 #include <lunaix/status.h>
10 #include <lunaix/syscall.h>
11 #include <lunaix/syslog.h>
12 #include <lunaix/exec.h>
13 #include <lunaix/fs.h>
14
15 #include <sys/abi.h>
16 #include <sys/mm/mm_defs.h>
17
18 LOG_MODULE("PROC")
19
20 __DEFINE_LXSYSCALL(pid_t, getpid)
21 {
22     return __current->pid;
23 }
24
25 __DEFINE_LXSYSCALL(pid_t, getppid)
26 {
27     return __current->parent->pid;
28 }
29
30 __DEFINE_LXSYSCALL(pid_t, getpgid)
31 {
32     return __current->pgid;
33 }
34
35 __DEFINE_LXSYSCALL2(int, setpgid, pid_t, pid, pid_t, pgid)
36 {
37     struct proc_info* proc = pid ? get_process(pid) : __current;
38
39     if (!proc) {
40         syscall_result(EINVAL);
41         return -1;
42     }
43
44     pgid = pgid ? pgid : proc->pid;
45
46     struct proc_info* gruppenfuhrer = get_process(pgid);
47
48     if (!gruppenfuhrer || proc->pgid == gruppenfuhrer->pid) {
49         syscall_result(EINVAL);
50         return -1;
51     }
52
53     llist_delete(&proc->grp_member);
54     llist_append(&gruppenfuhrer->grp_member, &proc->grp_member);
55
56     proc->pgid = pgid;
57     return 0;
58 }
59
60 int
61 spawn_process(struct thread** created, ptr_t entry, bool with_ustack) 
62 {
63     struct proc_info* kproc = alloc_process();
64     struct proc_mm* mm = vmspace(kproc);
65
66     procvm_initvms_mount(mm);
67
68     struct thread* kthread = create_thread(kproc, with_ustack);
69
70     if (!kthread) {
71         procvm_unmount(mm);
72         delete_process(kproc);
73         return -1;
74     }
75
76     commit_process(kproc);
77     start_thread(kthread, entry);
78
79     procvm_unmount(mm);
80
81     if (created) {
82         *created = kthread;
83     }
84
85     return 0;
86 }
87
88 int
89 spawn_process_usr(struct thread** created, char* path, 
90                     const char** argv, const char** envp)
91 {
92     // FIXME remote injection of user stack not yet implemented
93
94     struct proc_info* proc   = alloc_process();
95     struct proc_mm*   mm     = vmspace(proc);
96     
97     assert(!kernel_process(proc));
98
99     procvm_initvms_mount(mm);
100
101     int errno = 0;
102     struct thread* main_thread;
103     if (!(main_thread = create_thread(proc, true))) {
104         errno = ENOMEM;
105         goto fail;
106     }
107
108     struct exec_container container;
109     exec_init_container(&container, main_thread, VMS_MOUNT_1, argv, envp);
110     if ((errno = exec_load_byname(&container, path))) {
111         goto fail;
112     }
113
114     commit_process(proc);
115     start_thread(main_thread, container.exe.entry);
116
117     if (created) {
118         *created = main_thread;
119     }
120
121     procvm_unmount(mm);
122     return 0;
123
124 fail:
125     procvm_unmount(mm);
126     delete_process(proc);
127     return errno;
128 }
129
130
131 ptr_t proc_vmroot() {
132     return __current->mm->vmroot;
133 }