-
-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
280 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ | ||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ | ||
╞══════════════════════════════════════════════════════════════════════════════╡ | ||
│ Copyright 2020 Justine Alexandra Roberts Tunney │ | ||
│ │ | ||
│ Permission to use, copy, modify, and/or distribute this software for │ | ||
│ any purpose with or without fee is hereby granted, provided that the │ | ||
│ above copyright notice and this permission notice appear in all copies. │ | ||
│ │ | ||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ | ||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ | ||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ | ||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ | ||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ | ||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ | ||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ | ||
│ PERFORMANCE OF THIS SOFTWARE. │ | ||
╚─────────────────────────────────────────────────────────────────────────────*/ | ||
#include "libc/calls/calls.h" | ||
#include "libc/calls/groups.internal.h" | ||
#include "libc/dce.h" | ||
#include "libc/intrin/asan.internal.h" | ||
#include "libc/intrin/describeflags.internal.h" | ||
#include "libc/intrin/strace.internal.h" | ||
#include "libc/sysv/errfuns.h" | ||
|
||
/** | ||
* Get list of supplementary group IDs | ||
* | ||
* @param size - maximum number of items that can be stored in list | ||
* @param list - buffer to store output gid_t | ||
* @return -1 w/ EFAULT | ||
*/ | ||
int getgroups(int size, uint32_t list[]) { | ||
int rc; | ||
if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) { | ||
rc = efault(); | ||
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { | ||
rc = sys_getgroups(size, list); | ||
} else { | ||
rc = enosys(); | ||
} | ||
STRACE("getgroups(%d, %s) → %d% m", size, DescribeGidList(rc, rc, list), rc); | ||
return rc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_ | ||
#define COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_ | ||
|
||
#if !(__ASSEMBLER__ + __LINKER__ + 0) | ||
COSMOPOLITAN_C_START_ | ||
|
||
int sys_getgroups(int size, uint32_t list[]); | ||
int sys_setgroups(size_t size, uint32_t list[]); | ||
|
||
const char *DescribeGidList(char[128], int, int, const uint32_t list[]); | ||
#define DescribeGidList(rc, length, gidlist) \ | ||
DescribeGidList(alloca(128), rc, length, gidlist) | ||
|
||
COSMOPOLITAN_C_END_ | ||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ | ||
#endif /* COSMOPOLITAN_LIBC_CALLS_GROUPS_INTERNAL_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ | ||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ | ||
╞══════════════════════════════════════════════════════════════════════════════╡ | ||
│ Copyright 2020 Justine Alexandra Roberts Tunney │ | ||
│ │ | ||
│ Permission to use, copy, modify, and/or distribute this software for │ | ||
│ any purpose with or without fee is hereby granted, provided that the │ | ||
│ above copyright notice and this permission notice appear in all copies. │ | ||
│ │ | ||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ | ||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ | ||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ | ||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ | ||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ | ||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ | ||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ | ||
│ PERFORMANCE OF THIS SOFTWARE. │ | ||
╚─────────────────────────────────────────────────────────────────────────────*/ | ||
#include "libc/calls/calls.h" | ||
#include "libc/calls/groups.internal.h" | ||
#include "libc/dce.h" | ||
#include "libc/intrin/asan.internal.h" | ||
#include "libc/intrin/describeflags.internal.h" | ||
#include "libc/intrin/strace.internal.h" | ||
#include "libc/sysv/errfuns.h" | ||
|
||
/** | ||
* Set list of supplementary group IDs | ||
* | ||
* @param size - number of items in list | ||
* @param list - input set of gid_t to set | ||
* @return -1 w/ EFAULT | ||
*/ | ||
int setgroups(size_t size, const uint32_t list[]) { | ||
int rc; | ||
if (IsAsan() && size && !__asan_is_valid(list, size * sizeof(list[0]))) { | ||
rc = efault(); | ||
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { | ||
rc = sys_setgroups(size, list); | ||
} else { | ||
rc = enosys(); | ||
} | ||
STRACE("setgroups(%u, %s) → %d% m", size, DescribeGidList(rc, size, list), | ||
rc); | ||
return rc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ | ||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ | ||
╞══════════════════════════════════════════════════════════════════════════════╡ | ||
│ Copyright 2021 Justine Alexandra Roberts Tunney │ | ||
│ │ | ||
│ Permission to use, copy, modify, and/or distribute this software for │ | ||
│ any purpose with or without fee is hereby granted, provided that the │ | ||
│ above copyright notice and this permission notice appear in all copies. │ | ||
│ │ | ||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ | ||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ | ||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ | ||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ | ||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ | ||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ | ||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ | ||
│ PERFORMANCE OF THIS SOFTWARE. │ | ||
╚─────────────────────────────────────────────────────────────────────────────*/ | ||
#include "libc/calls/groups.internal.h" | ||
#include "libc/dce.h" | ||
#include "libc/intrin/asan.internal.h" | ||
#include "libc/intrin/kprintf.h" | ||
#include "libc/intrin/popcnt.h" | ||
#include "libc/macros.internal.h" | ||
#include "libc/str/str.h" | ||
|
||
#define N 128 | ||
|
||
const char *(DescribeGidList)(char buf[N], int rc, int size, | ||
const uint32_t list[]) { | ||
if ((rc == -1) || (size < 0)) return "n/a"; | ||
if (!size) return "{}"; | ||
if (!list) return "NULL"; | ||
if ((!IsAsan() && kisdangerous(list)) || | ||
(IsAsan() && !__asan_is_valid(list, size * sizeof(list[0])))) { | ||
ksnprintf(buf, N, "%p", list); | ||
return buf; | ||
} | ||
int i = 0, n = N; | ||
i += ksnprintf(buf + i, MAX(0, n - i), "{"); | ||
unsigned c; | ||
for (c = 0; c < size && MAX(0, n - i) > 0; c++) { | ||
i += ksnprintf(buf + i, MAX(0, n - i), "%u, ", list[c]); | ||
} | ||
if (c == size) { | ||
if (buf[i - 1] == ' ') i--; | ||
if (buf[i - 1] == ',') i--; | ||
i += ksnprintf(buf + i, MAX(0, n - i), "}"); | ||
} | ||
return buf; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#ifndef LIBC_ISYSTEM_GRP_H_ | ||
#define LIBC_ISYSTEM_GRP_H_ | ||
#include "libc/calls/calls.h" | ||
#include "third_party/musl/passwd.h" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.include "o/libc/sysv/macros.internal.inc" | ||
.scall sys_getgroups,0x04f04f04f204f073,globl | ||
.scall sys_getgroups,0x04f04f04f204f073,globl,hidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.include "o/libc/sysv/macros.internal.inc" | ||
.scall sys_setgroups,0x0500500502050074,globl | ||
.scall sys_setgroups,0x0500500502050074,globl,hidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ | ||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ | ||
╞══════════════════════════════════════════════════════════════════════════════╡ | ||
│ Copyright 2022 Gavin Arthur Hayes │ | ||
│ │ | ||
│ Permission to use, copy, modify, and/or distribute this software for │ | ||
│ any purpose with or without fee is hereby granted, provided that the │ | ||
│ above copyright notice and this permission notice appear in all copies. │ | ||
│ │ | ||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ | ||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ | ||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ | ||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ | ||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ | ||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ | ||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ | ||
│ PERFORMANCE OF THIS SOFTWARE. │ | ||
╚─────────────────────────────────────────────────────────────────────────────*/ | ||
#include "libc/calls/calls.h" | ||
#include "libc/dce.h" | ||
#include "libc/errno.h" | ||
#include "libc/testlib/testlib.h" | ||
|
||
TEST(getgroups, test) { | ||
int rc; | ||
uint32_t res[1000]; | ||
rc = getgroups(0, NULL); | ||
if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { | ||
EXPECT_NE(-1, rc); | ||
EXPECT_NE(-1, getgroups(sizeof(res) / sizeof(res[0]), res)); | ||
} else { | ||
EXPECT_EQ(-1, rc); | ||
EXPECT_EQ(ENOSYS, errno); | ||
} | ||
} | ||
|
||
TEST(setgroups, test) { | ||
int rc; | ||
uint32_t src[5]; | ||
EXPECT_EQ(-1, setgroups(0, NULL)); | ||
if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) { | ||
EXPECT_EQ(EPERM, errno); | ||
EXPECT_EQ(-1, setgroups(sizeof(src) / sizeof(src[0]), src)); | ||
} else { | ||
EXPECT_EQ(ENOSYS, errno); | ||
} | ||
} |
Oops, something went wrong.