summaryrefslogtreecommitdiff
path: root/src/basic/socket-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-12-30 14:02:36 +0100
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:49:52 +0200
commitdfecc42944e40ab1763b16ed5ef54e8a88fb3590 (patch)
tree2f47111e01293e173bb3ed56e2d8d4c0444523da /src/basic/socket-util.c
parent2ad22ba29e9400833f977061a79e776f81e2b4e1 (diff)
socket-util: add new getpeergroups() call
It's a wrapper around the new SO_PEERGROUPS sockopt, similar in style as getpeersec() and getpeercred().
Diffstat (limited to 'src/basic/socket-util.c')
-rw-r--r--src/basic/socket-util.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index 5969aa761..ea4647776 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -1027,6 +1027,39 @@ int getpeersec(int fd, char **ret) {
return 0;
}
+int getpeergroups(int fd, gid_t **ret) {
+ socklen_t n = sizeof(gid_t) * 64;
+ _cleanup_free_ gid_t *d = NULL;
+
+ assert(fd);
+ assert(ret);
+
+ for (;;) {
+ d = malloc(n);
+ if (!d)
+ return -ENOMEM;
+
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
+ break;
+
+ if (errno != ERANGE)
+ return -errno;
+
+ d = mfree(d);
+ }
+
+ assert_se(n % sizeof(gid_t) == 0);
+ n /= sizeof(gid_t);
+
+ if ((socklen_t) (int) n != n)
+ return -E2BIG;
+
+ *ret = d;
+ d = NULL;
+
+ return (int) n;
+}
+
int send_one_fd_sa(
int transport_fd,
int fd,