From: Martin Schwenke Date: Thu, 28 Jun 2018 10:35:56 +0000 (+1000) Subject: ctdb-common: Move ctdb_get_peer_pid() to system.[ch] X-Git-Tag: tevent-0.9.37~180 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1d7d80451bc4fea12d313b911924ae89901d99a7;p=thirdparty%2Fsamba.git ctdb-common: Move ctdb_get_peer_pid() to system.[ch] The system_.c files contain a lot of duplication, making maintenance difficult. These functions are being merged into system_socket.c and system.c. This function doesn't need ctdb_sock_addr so put it with general system utilities. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/common/system.c b/ctdb/common/system.c index 50054f0abc6..a95f314e66f 100644 --- a/ctdb/common/system.c +++ b/ctdb/common/system.c @@ -198,3 +198,52 @@ bool ctdb_sys_check_iface_exists(const char *iface) } #endif /* HAVE_AF_PACKET */ + +#ifdef HAVE_PEERCRED + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + struct ucred cr; + socklen_t crl = sizeof(struct ucred); + int ret; + + ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl); + if (ret == 0) { + *peer_pid = cr.pid; + } else { + *peer_pid = -1; + } + return ret; +} + +#else /* HAVE_PEERCRED */ + +#ifdef _AIX_ + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + struct peercred_struct cr; + socklen_t crl = sizeof(struct peercred_struct); + int ret; + + ret = getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl); + if (ret == 0) { + *peer_pid = cr.pid; + } else { + *peer_pid = -1; + } + return ret; +} + +#else /* _AIX_ */ + +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) +{ + /* Not implemented */ + *peer_pid = -1; + return ENOSYS; +} + +#endif /* _AIX_ */ + +#endif /* HAVE_PEERCRED */ diff --git a/ctdb/common/system.h b/ctdb/common/system.h index 908d29d2794..d63fb91db6b 100644 --- a/ctdb/common/system.h +++ b/ctdb/common/system.h @@ -34,7 +34,6 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, ctdb_sock_addr *src, ctdb_sock_addr *dst, uint32_t *ack_seq, uint32_t *seq, int *rst, uint16_t *window); -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid); /* From system_util.c */ @@ -48,5 +47,6 @@ void mkdir_p_or_die(const char *dir, int mode); void ctdb_wait_for_process_to_exit(pid_t pid); bool ctdb_sys_check_iface_exists(const char *iface); +int ctdb_get_peer_pid(const int fd, pid_t *peer_pid); #endif /* __CTDB_SYSTEM_H__ */ diff --git a/ctdb/common/system_aix.c b/ctdb/common/system_aix.c index f17125cbd39..b19e7569929 100644 --- a/ctdb/common/system_aix.c +++ b/ctdb/common/system_aix.c @@ -379,14 +379,3 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) -{ - struct peercred_struct cr; - socklen_t crl = sizeof(struct peercred_struct); - int ret; - if ((ret = getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl)) == 0) { - *peer_pid = cr.pid; - } - return ret; -} diff --git a/ctdb/common/system_freebsd.c b/ctdb/common/system_freebsd.c index de7bf2b3396..9632fb209fa 100644 --- a/ctdb/common/system_freebsd.c +++ b/ctdb/common/system_freebsd.c @@ -383,9 +383,3 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) -{ - /* FIXME FreeBSD: get_peer_pid not implemented */ - return 1; -} diff --git a/ctdb/common/system_gnu.c b/ctdb/common/system_gnu.c index 7f2654e61d7..c45c6e930d4 100644 --- a/ctdb/common/system_gnu.c +++ b/ctdb/common/system_gnu.c @@ -378,9 +378,3 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) -{ - /* FIXME GNU/Hurd: get_peer_pid not implemented */ - return 1; -} diff --git a/ctdb/common/system_kfreebsd.c b/ctdb/common/system_kfreebsd.c index ccad294615d..694ae58a37c 100644 --- a/ctdb/common/system_kfreebsd.c +++ b/ctdb/common/system_kfreebsd.c @@ -378,9 +378,3 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) -{ - /* FIXME kFreeBSD: get_peer_pid not implemented */ - return 1; -} diff --git a/ctdb/common/system_linux.c b/ctdb/common/system_linux.c index 3cff3b20c94..a95076dc5f3 100644 --- a/ctdb/common/system_linux.c +++ b/ctdb/common/system_linux.c @@ -603,14 +603,3 @@ int ctdb_sys_read_tcp_packet(int s, void *private_data, return -1; } - -int ctdb_get_peer_pid(const int fd, pid_t *peer_pid) -{ - struct ucred cr; - socklen_t crl = sizeof(struct ucred); - int ret; - if ((ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl)) == 0) { - *peer_pid = cr.pid; - } - return ret; -}