From b17231c6950cb1c05d18cd005e7397e4c7fc763a Mon Sep 17 00:00:00 2001 From: David Goulet Date: Tue, 21 Feb 2012 13:12:55 -0500 Subject: [PATCH] Add FreeBSD compat layer for socket.h Signed-off-by: David Goulet --- src/common/compat/socket.h | 55 ++++++++++++++++++++++++ src/common/sessiond-comm/sessiond-comm.c | 25 ++++++----- src/common/sessiond-comm/sessiond-comm.h | 4 +- 3 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 src/common/compat/socket.h diff --git a/src/common/compat/socket.h b/src/common/compat/socket.h new file mode 100644 index 000000000..dcb6cdede --- /dev/null +++ b/src/common/compat/socket.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2011 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; only version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _COMPAT_SOCKET_H +#define _COMPAT_SOCKET_H + +#include +#include + +#include + +#ifdef __linux__ + +#define LTTNG_SOCK_CREDS SCM_CREDENTIALS +#define LTTNG_SOCK_FDS SCM_RIGHTS + +typedef struct ucred lttng_sock_cred; + +#define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u; +#define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g; +#define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p; + +#elif __FreeBSD__ + +#undef SO_PASSCRED +#define SO_PASSCRED 0 + +#define LTTNG_SOCK_CREDS SCM_CREDS +#define LTTNG_SOCK_FDS SCM_RIGHTS + +typedef struct cmsgcred lttng_sock_cred; + +#define LTTNG_SOCK_SET_UID_CRED(c, uid) LTTNG_REF(c)->cmcred_uid = uid; +#define LTTNG_SOCK_SET_GID_CRED(c, gid) LTTNG_REF(c)->cmcred_gid = gid; +#define LTTNG_SOCK_SET_PID_CRED(c, pid) LTTNG_REF(c)->cmcred_pid = pid; + +#else +#error "Please add support for your OS into lttng/ust-endian.h." +#endif /* __linux__ , __FreeBSD__ */ + +#endif /* _COMPAT_SOCKET_H */ diff --git a/src/common/sessiond-comm/sessiond-comm.c b/src/common/sessiond-comm/sessiond-comm.c index 0c29003a6..f133d5bfb 100644 --- a/src/common/sessiond-comm/sessiond-comm.c +++ b/src/common/sessiond-comm/sessiond-comm.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -412,7 +411,7 @@ ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) } if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", - cmsg->cmsg_len, CMSG_LEN(sizeof_fds)); + (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); ret = -1; goto end; } @@ -433,9 +432,9 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) struct cmsghdr *cmptr; struct iovec iov[1]; ssize_t ret = -1; - struct ucred *creds; - size_t sizeof_cred = sizeof(struct ucred); + size_t sizeof_cred = sizeof(lttng_sock_cred); char anc_buf[CMSG_SPACE(sizeof_cred)]; + lttng_sock_cred *creds; memset(&msg, 0, sizeof(msg)); @@ -449,14 +448,14 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) cmptr = CMSG_FIRSTHDR(&msg); cmptr->cmsg_level = SOL_SOCKET; - cmptr->cmsg_type = SCM_CREDENTIALS; + cmptr->cmsg_type = LTTNG_SOCK_CREDS; cmptr->cmsg_len = CMSG_LEN(sizeof_cred); - creds = (struct ucred *) CMSG_DATA(cmptr); + creds = (lttng_sock_cred*) CMSG_DATA(cmptr); - creds->uid = geteuid(); - creds->gid = getegid(); - creds->pid = getpid(); + LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); + LTTNG_SOCK_SET_GID_CRED(creds, getegid()); + LTTNG_SOCK_SET_PID_CRED(creds, getpid()); ret = sendmsg(sock, &msg, 0); if (ret < 0) { @@ -472,13 +471,13 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) * Returns the size of received data, or negative error value. */ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, - struct ucred *creds) + lttng_sock_cred *creds) { struct msghdr msg; struct cmsghdr *cmptr; struct iovec iov[1]; ssize_t ret; - size_t sizeof_cred = sizeof(struct ucred); + size_t sizeof_cred = sizeof(lttng_sock_cred); char anc_buf[CMSG_SPACE(sizeof_cred)]; memset(&msg, 0, sizeof(msg)); @@ -518,7 +517,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, } if (cmptr->cmsg_level != SOL_SOCKET || - cmptr->cmsg_type != SCM_CREDENTIALS) { + cmptr->cmsg_type != LTTNG_SOCK_CREDS) { fprintf(stderr, "Didn't received any credentials\n"); ret = -1; goto end; @@ -526,7 +525,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", - cmptr->cmsg_len, CMSG_LEN(sizeof_cred)); + (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); ret = -1; goto end; } diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 31b7fe466..e4d81f287 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -28,7 +28,7 @@ #define _GNU_SOURCE #include #include -#include +#include /* Queue size of listen(2) */ #define LTTNG_SESSIOND_COMM_MAX_LISTEN 64 @@ -291,7 +291,7 @@ extern ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len); extern ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len); extern ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, - struct ucred *creds); + lttng_sock_cred *creds); extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code); extern int lttcomm_setsockopt_creds_unix_sock(int sock); -- 2.34.1