xref: /haiku/src/system/libroot/posix/unistd/process.c (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
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  */
5 
6 
7 #include <syscalls.h>
8 #include <syscall_process_info.h>
9 
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 
14 
15 #define RETURN_AND_SET_ERRNO(err) \
16 	if (err < 0) { \
17 		errno = err; \
18 		return -1; \
19 	} \
20 	return err;
21 
22 
23 extern thread_id __main_thread_id;
24 
25 
26 pid_t
27 getpgrp(void)
28 {
29 	return getpgid(__main_thread_id);
30 }
31 
32 
33 pid_t
34 getpid(void)
35 {
36 	// this one returns the ID of the main thread
37 	return __main_thread_id;
38 }
39 
40 
41 pid_t
42 getppid(void)
43 {
44 	return _kern_process_info(0, PARENT_ID);
45 		// this is not supposed to return an error value
46 }
47 
48 
49 pid_t
50 getsid(pid_t process)
51 {
52 	pid_t session = _kern_process_info(process, SESSION_ID);
53 
54 	RETURN_AND_SET_ERRNO(session);
55 }
56 
57 
58 pid_t
59 getpgid(pid_t process)
60 {
61 	pid_t group = _kern_process_info(process, GROUP_ID);
62 
63 	RETURN_AND_SET_ERRNO(group);
64 }
65 
66 
67 int
68 setpgid(pid_t process, pid_t group)
69 {
70 	pid_t result = _kern_setpgid(process, group);
71 	if (result >= 0)
72 		return 0;
73 
74 	RETURN_AND_SET_ERRNO(result);
75 }
76 
77 
78 pid_t
79 setpgrp(void)
80 {
81 	// setpgrp() never fails -- setpgid() fails when the process is a session
82 	// leader, though.
83 	pid_t result = _kern_setpgid(0, 0);
84 	return result >= 0 ? result : getpgrp();
85 }
86 
87 
88 pid_t
89 setsid(void)
90 {
91 	status_t status = _kern_setsid();
92 
93 	RETURN_AND_SET_ERRNO(status);
94 }
95 
96