]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/af_unix.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/socket.h>
35 lxc_log_define(lxc_af_unix
, lxc
);
37 int lxc_af_unix_open(const char *path
, int type
, int flags
)
41 struct sockaddr_un addr
;
46 fd
= socket(PF_UNIX
, type
, 0);
50 memset(&addr
, 0, sizeof(addr
));
55 addr
.sun_family
= AF_UNIX
;
56 /* copy entire buffer in case of abstract socket */
57 len
= sizeof(addr
.sun_path
);
60 if (len
>= sizeof(addr
.sun_path
)) {
66 memcpy(addr
.sun_path
, path
, len
);
68 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
75 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
85 int lxc_af_unix_close(int fd
)
87 struct sockaddr_un addr
;
88 socklen_t addrlen
= sizeof(addr
);
90 if (!getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
) &&
92 unlink(addr
.sun_path
);
99 int lxc_af_unix_connect(const char *path
)
102 struct sockaddr_un addr
;
105 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
110 memset(&addr
, 0, sizeof(addr
));
112 addr
.sun_family
= AF_UNIX
;
113 /* copy entire buffer in case of abstract socket */
114 memcpy(addr
.sun_path
, path
,
115 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
117 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
129 int lxc_af_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
131 struct msghdr msg
= { 0 };
133 struct cmsghdr
*cmsg
;
134 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
138 msg
.msg_control
= cmsgbuf
;
139 msg
.msg_controllen
= sizeof(cmsgbuf
);
141 cmsg
= CMSG_FIRSTHDR(&msg
);
142 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
143 cmsg
->cmsg_level
= SOL_SOCKET
;
144 cmsg
->cmsg_type
= SCM_RIGHTS
;
145 val
= (int *)(CMSG_DATA(cmsg
));
151 iov
.iov_base
= data
? data
: buf
;
152 iov
.iov_len
= data
? size
: sizeof(buf
);
156 return sendmsg(fd
, &msg
, 0);
159 int lxc_af_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
161 struct msghdr msg
= { 0 };
163 struct cmsghdr
*cmsg
;
164 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
170 msg
.msg_control
= cmsgbuf
;
171 msg
.msg_controllen
= sizeof(cmsgbuf
);
173 iov
.iov_base
= data
? data
: buf
;
174 iov
.iov_len
= data
? size
: sizeof(buf
);
178 ret
= recvmsg(fd
, &msg
, 0);
182 cmsg
= CMSG_FIRSTHDR(&msg
);
184 /* if the message is wrong the variable will not be
185 * filled and the peer will notified about a problem */
188 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
189 cmsg
->cmsg_level
== SOL_SOCKET
&&
190 cmsg
->cmsg_type
== SCM_RIGHTS
) {
191 val
= (int *) CMSG_DATA(cmsg
);
198 int lxc_af_unix_send_credential(int fd
, void *data
, size_t size
)
200 struct msghdr msg
= { 0 };
202 struct cmsghdr
*cmsg
;
203 struct ucred cred
= {
208 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
211 msg
.msg_control
= cmsgbuf
;
212 msg
.msg_controllen
= sizeof(cmsgbuf
);
214 cmsg
= CMSG_FIRSTHDR(&msg
);
215 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
216 cmsg
->cmsg_level
= SOL_SOCKET
;
217 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
218 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
223 iov
.iov_base
= data
? data
: buf
;
224 iov
.iov_len
= data
? size
: sizeof(buf
);
228 return sendmsg(fd
, &msg
, 0);
231 int lxc_af_unix_rcv_credential(int fd
, void *data
, size_t size
)
233 struct msghdr msg
= { 0 };
235 struct cmsghdr
*cmsg
;
237 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
243 msg
.msg_control
= cmsgbuf
;
244 msg
.msg_controllen
= sizeof(cmsgbuf
);
246 iov
.iov_base
= data
? data
: buf
;
247 iov
.iov_len
= data
? size
: sizeof(buf
);
251 ret
= recvmsg(fd
, &msg
, 0);
255 cmsg
= CMSG_FIRSTHDR(&msg
);
257 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
258 cmsg
->cmsg_level
== SOL_SOCKET
&&
259 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
260 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
261 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
262 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);