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>
This commit is contained in:
Jeremy Huddleston Sequoia 2023-01-18 12:19:05 -08:00
parent 165d5c1260
commit 8a4ab22873
2 changed files with 19 additions and 0 deletions

View File

@ -133,6 +133,7 @@ conf_data.set('HAVE_FNMATCH_H', cc.has_header('fnmatch.h') ? '1' : false)
conf_data.set('HAVE_LINUX_AGPGART_H', cc.has_header('linux/agpgart.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_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)

View File

@ -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