headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / unistd / process.c
blob5714c6ec3b19edd9bacd0e42a0c380d084ad6c62
1 /*
2 * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <syscalls.h>
8 #include <syscall_process_info.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
14 #include <errno_private.h>
17 #define RETURN_AND_SET_ERRNO(err) \
18 if (err < 0) { \
19 __set_errno(err); \
20 return -1; \
21 } \
22 return err;
25 extern thread_id __main_thread_id;
28 pid_t
29 getpgrp(void)
31 return getpgid(__main_thread_id);
35 pid_t
36 getpid(void)
38 // this one returns the ID of the main thread
39 return __main_thread_id;
43 pid_t
44 getppid(void)
46 return _kern_process_info(0, PARENT_ID);
47 // this is not supposed to return an error value
51 pid_t
52 getsid(pid_t process)
54 pid_t session = _kern_process_info(process, SESSION_ID);
56 RETURN_AND_SET_ERRNO(session);
60 pid_t
61 getpgid(pid_t process)
63 pid_t group = _kern_process_info(process, GROUP_ID);
65 RETURN_AND_SET_ERRNO(group);
69 int
70 setpgid(pid_t process, pid_t group)
72 pid_t result = _kern_setpgid(process, group);
73 if (result >= 0)
74 return 0;
76 RETURN_AND_SET_ERRNO(result);
80 pid_t
81 setpgrp(void)
83 // setpgrp() never fails -- setpgid() fails when the process is a session
84 // leader, though.
85 pid_t result = _kern_setpgid(0, 0);
86 return result >= 0 ? result : getpgrp();
90 pid_t
91 setsid(void)
93 status_t status = _kern_setsid();
95 RETURN_AND_SET_ERRNO(status);