summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Huddleston Sequoia <jeremyhu@apple.com>2023-01-18 12:19:05 -0800
committerJeremy Huddleston Sequoia <jeremyhu@apple.com>2023-01-26 09:56:10 -0800
commit1317083fbc407dc9dbb04ba5b98187b75222a16f (patch)
tree335de02df37d1c5606b65f4d35359f984274f905
parenta6c49106cef36e84eb11d64e325465250b7678be (diff)
os: Use LOCAL_PEERPID from sys/un.h if it is available to detemine the pid when falling back on getpeereids()
This provides a way to determine the pid of a peer connection on systems like darwin that do not support getpeerucred() nor SO_PEERCRED. Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com> (cherry picked from commit 8a4ab2287398773a4868c220662d93bf84ec6241)
-rw-r--r--include/meson.build1
-rw-r--r--os/access.c18
2 files changed, 19 insertions, 0 deletions
diff --git a/include/meson.build b/include/meson.build
index 3521fa3ef..591bc25e0 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -135,6 +135,7 @@ conf_data.set('HAVE_STRING_H', cc.has_header('string.h') ? '1' : false)
conf_data.set('HAVE_STRINGS_H', cc.has_header('strings.h') ? '1' : false)
conf_data.set('HAVE_SYS_AGPGART_H', cc.has_header('sys/agpgart.h') ? '1' : false)
conf_data.set('HAVE_SYS_AGPIO_H', cc.has_header('sys/agpio.h') ? '1' : false)
+conf_data.set('HAVE_SYS_UN_H', cc.has_header('sys/un.h') ? '1' : false)
conf_data.set('HAVE_SYS_UTSNAME_H', cc.has_header('sys/utsname.h') ? '1' : false)
conf_data.set('HAVE_SYS_SYSMACROS_H', cc.has_header('sys/sysmacros.h') ? '1' : false)
conf_data.set('HAVE_UNISTD_H', cc.has_header('unistd.h') ? '1' : false)
diff --git a/os/access.c b/os/access.c
index debf4c741..61ee8e30f 100644
--- a/os/access.c
+++ b/os/access.c
@@ -116,6 +116,10 @@ SOFTWARE.
#endif
#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+
#if defined(SVR4) || (defined(SYSV) && defined(__i386__)) || defined(__GNU__)
#include <sys/utsname.h>
#endif
@@ -1176,6 +1180,10 @@ GetLocalClientCreds(ClientPtr client, LocalClientCredRec ** lccp)
#elif defined(HAVE_GETPEEREID)
uid_t uid;
gid_t gid;
+#if defined(LOCAL_PEERPID)
+ pid_t pid;
+ socklen_t so_len = sizeof(pid);
+#endif
#endif
if (client == NULL)
@@ -1253,6 +1261,16 @@ GetLocalClientCreds(ClientPtr client, LocalClientCredRec ** lccp)
lcc->euid = uid;
lcc->egid = gid;
lcc->fieldsSet = LCC_UID_SET | LCC_GID_SET;
+
+#if defined(LOCAL_PEERPID)
+ if (getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &pid, &so_len) != 0) {
+ ErrorF("getsockopt failed to determine pid of socket %d: %s\n", fd, strerror(errno));
+ } else {
+ lcc->pid = pid;
+ lcc->fieldsSet |= LCC_PID_SET;
+ }
+#endif
+
return 0;
#endif
#else