vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / unistd / usergroup.cpp
blob97f31cff129a250afbcd035cf205d332218f169f
1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
8 #include <syscalls.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <pwd.h>
20 #include <errno_private.h>
23 template<typename T>
24 static inline T
25 set_errno_if_necessary(const T& result)
27 if (result < 0) {
28 __set_errno(result);
29 return -1;
32 return result;
36 // #pragma mark -
39 gid_t
40 getegid(void)
42 return _kern_getgid(true);
46 uid_t
47 geteuid(void)
49 return _kern_getuid(true);
53 gid_t
54 getgid(void)
56 return _kern_getgid(false);
60 uid_t
61 getuid(void)
63 return _kern_getuid(false);
67 int
68 setgid(gid_t gid)
70 return set_errno_if_necessary(_kern_setregid(gid, (gid_t)-1, true));
74 int
75 setuid(uid_t uid)
77 return set_errno_if_necessary(_kern_setreuid(uid, (uid_t)-1, true));
81 int
82 setegid(gid_t gid)
84 return set_errno_if_necessary(_kern_setregid((gid_t)-1, gid, false));
88 int
89 seteuid(uid_t uid)
91 return set_errno_if_necessary(_kern_setreuid((uid_t)-1, uid, false));
95 int
96 setregid(gid_t rgid, gid_t egid)
98 return set_errno_if_necessary(_kern_setregid(rgid, egid, false));
103 setreuid(uid_t ruid, uid_t euid)
105 return set_errno_if_necessary(_kern_setreuid(ruid, euid, false));
110 getgroups(int groupCount, gid_t groupList[])
112 return set_errno_if_necessary(_kern_getgroups(groupCount, groupList));
117 setgroups(int groupCount, const gid_t* groupList)
119 return set_errno_if_necessary(_kern_setgroups(groupCount, groupList));
122 // NOTE: getgrouplist() and initgroups() are implemented in grp.cpp.