Add FreeBSD compat layer for socket.h
authorDavid Goulet <dgoulet@efficios.com>
Tue, 21 Feb 2012 18:12:55 +0000 (13:12 -0500)
committerDavid Goulet <dgoulet@efficios.com>
Wed, 22 Feb 2012 22:25:30 +0000 (17:25 -0500)
Signed-off-by: David Goulet <dgoulet@efficios.com>
src/common/compat/socket.h [new file with mode: 0644]
src/common/sessiond-comm/sessiond-comm.c
src/common/sessiond-comm/sessiond-comm.h

diff --git a/src/common/compat/socket.h b/src/common/compat/socket.h
new file mode 100644 (file)
index 0000000..dcb6cde
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
+ *
+ * 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 <sys/socket.h>
+#include <sys/un.h>
+
+#include <common/macros.h>
+
+#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 */
index 0c29003a6267a382b204a6f277a9d0a1468ad182..f133d5bfbabe1892c868c6ccb7d6784cc707ac2f 100644 (file)
@@ -24,7 +24,6 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/un.h>
 #include <unistd.h>
 #include <errno.h>
 
@@ -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;
        }
index 31b7fe4661cbd1db37acb52013d31048b873df2e..e4d81f28718f7adf4e8cb73d50252e4a7cd29304 100644 (file)
@@ -28,7 +28,7 @@
 #define _GNU_SOURCE
 #include <limits.h>
 #include <lttng/lttng.h>
-#include <sys/socket.h>
+#include <common/compat/socket.h>
 
 /* 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);
This page took 0.03874 seconds and 4 git commands to generate.