Commit | Line | Data |
---|---|---|
b17231c6 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
b17231c6 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
b17231c6 DG |
16 | */ |
17 | ||
18 | #ifndef _COMPAT_SOCKET_H | |
19 | #define _COMPAT_SOCKET_H | |
20 | ||
21 | #include <sys/socket.h> | |
22 | #include <sys/un.h> | |
23 | ||
24 | #include <common/macros.h> | |
25 | ||
26 | #ifdef __linux__ | |
27 | ||
28 | #define LTTNG_SOCK_CREDS SCM_CREDENTIALS | |
b17231c6 DG |
29 | |
30 | typedef struct ucred lttng_sock_cred; | |
31 | ||
1268b9d6 DG |
32 | #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u |
33 | #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g | |
34 | #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p | |
35 | ||
36 | #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid | |
37 | #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid | |
38 | #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid | |
b17231c6 | 39 | |
b1325eff | 40 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__)) |
b17231c6 | 41 | |
d27c42b8 MD |
42 | struct lttng_sock_cred { |
43 | uid_t uid; | |
44 | gid_t gid; | |
45 | }; | |
b17231c6 | 46 | |
d27c42b8 | 47 | typedef struct lttng_sock_cred lttng_sock_cred; |
b17231c6 | 48 | |
d27c42b8 MD |
49 | #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid |
50 | #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid | |
51 | #define LTTNG_SOCK_GET_PID_CRED(c) -1 | |
b17231c6 DG |
52 | |
53 | #else | |
7657ae75 | 54 | #error "Please add support for your OS." |
b17231c6 DG |
55 | #endif /* __linux__ , __FreeBSD__ */ |
56 | ||
28105b32 MJ |
57 | |
58 | #ifdef __sun__ | |
59 | ||
60 | # ifndef CMSG_ALIGN | |
61 | # ifdef _CMSG_DATA_ALIGN | |
62 | # define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len) | |
63 | # else | |
64 | /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */ | |
65 | # define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1)) | |
66 | # endif | |
67 | # ifndef CMSG_SPACE | |
68 | # define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len)) | |
69 | # endif | |
70 | # ifndef CMSG_LEN | |
71 | # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) | |
72 | # endif | |
73 | # endif | |
74 | ||
75 | ||
76 | #include <ucred.h> | |
11a2fd46 MJ |
77 | |
78 | static inline | |
79 | int getpeereid(int s, uid_t *euid, gid_t *gid) | |
28105b32 MJ |
80 | { |
81 | int ret = 0; | |
82 | ucred_t *ucred = NULL; | |
83 | ||
84 | ret = getpeerucred(s, &ucred); | |
85 | if (ret == -1) { | |
86 | goto end; | |
87 | } | |
88 | ||
89 | ret = ucred_geteuid(ucred); | |
90 | if (ret == -1) { | |
91 | goto free; | |
92 | } | |
93 | *euid = ret; | |
94 | ||
95 | ret = ucred_getrgid(ucred); | |
96 | if (ret == -1) { | |
97 | goto free; | |
98 | } | |
99 | *gid = ret; | |
100 | ret = 0; | |
101 | free: | |
102 | ucred_free(ucred); | |
103 | end: | |
104 | return ret; | |
105 | } | |
106 | ||
107 | #endif /* __sun__ */ | |
108 | ||
b17231c6 | 109 | #endif /* _COMPAT_SOCKET_H */ |