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