From 1e4117f1e1830723ae5d15cc7876370de14bf645 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 1 Jan 2022 16:19:51 +0100 Subject: [PATCH] LibCore: Add syscall wrapper for getgroups() This wrapper does all the grunt work of figuring out how many extra GIDs there are, and then returning them nicely wrapped in a Vector. --- Userland/Libraries/LibCore/System.cpp | 17 ++++++++++++++++- Userland/Libraries/LibCore/System.h | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 92ab111dcd..37cca47260 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2022, Andreas Kling * Copyright (c) 2021, Kenneth Myhra * Copyright (c) 2021, Sam Atkins * @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -742,4 +743,18 @@ ErrorOr> pipe2([[maybe_unused]] int flags) return fds; } +ErrorOr> getgroups() +{ + int count = ::getgroups(0, nullptr); + if (count < 0) + return Error::from_syscall("getgroups"sv, -errno); + if (count == 0) + return Vector {}; + Vector groups; + TRY(groups.try_resize(count)); + if (::getgroups(count, groups.data()) < 0) + return Error::from_syscall("getgroups"sv, -errno); + return groups; +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index d30457b495..1353108885 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -105,5 +105,5 @@ ErrorOr setsockopt(int sockfd, int level, int option, void const* value, s ErrorOr getsockname(int sockfd, struct sockaddr*, socklen_t*); ErrorOr getpeername(int sockfd, struct sockaddr*, socklen_t*); ErrorOr socketpair(int domain, int type, int protocol, int sv[2]); - +ErrorOr> getgroups(); }