]>
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
;
47 fd
= socket(PF_UNIX
, type
, 0);
52 memset(&addr
, 0, sizeof(addr
));
57 addr
.sun_family
= AF_UNIX
;
58 /* copy entire buffer in case of abstract socket */
59 len
= sizeof(addr
.sun_path
);
62 if (len
>= sizeof(addr
.sun_path
)) {
70 memcpy(addr
.sun_path
, path
, len
);
72 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
81 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
93 int lxc_af_unix_close(int fd
)
95 struct sockaddr_un addr
;
96 socklen_t addrlen
= sizeof(addr
);
98 if (!getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
) &&
100 unlink(addr
.sun_path
);
109 int lxc_af_unix_connect(const char *path
)
112 struct sockaddr_un addr
;
115 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
120 memset(&addr
, 0, sizeof(addr
));
122 addr
.sun_family
= AF_UNIX
;
123 /* copy entire buffer in case of abstract socket */
124 memcpy(addr
.sun_path
, path
,
125 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
127 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
139 int lxc_af_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
141 struct msghdr msg
= { 0 };
143 struct cmsghdr
*cmsg
;
144 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
148 msg
.msg_control
= cmsgbuf
;
149 msg
.msg_controllen
= sizeof(cmsgbuf
);
151 cmsg
= CMSG_FIRSTHDR(&msg
);
152 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
153 cmsg
->cmsg_level
= SOL_SOCKET
;
154 cmsg
->cmsg_type
= SCM_RIGHTS
;
155 val
= (int *)(CMSG_DATA(cmsg
));
161 iov
.iov_base
= data
? data
: buf
;
162 iov
.iov_len
= data
? size
: sizeof(buf
);
166 return sendmsg(fd
, &msg
, 0);
169 int lxc_af_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
171 struct msghdr msg
= { 0 };
173 struct cmsghdr
*cmsg
;
174 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
180 msg
.msg_control
= cmsgbuf
;
181 msg
.msg_controllen
= sizeof(cmsgbuf
);
183 iov
.iov_base
= data
? data
: buf
;
184 iov
.iov_len
= data
? size
: sizeof(buf
);
188 ret
= recvmsg(fd
, &msg
, 0);
192 cmsg
= CMSG_FIRSTHDR(&msg
);
194 /* if the message is wrong the variable will not be
195 * filled and the peer will notified about a problem */
198 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
199 cmsg
->cmsg_level
== SOL_SOCKET
&&
200 cmsg
->cmsg_type
== SCM_RIGHTS
) {
201 val
= (int *) CMSG_DATA(cmsg
);
208 int lxc_af_unix_send_credential(int fd
, void *data
, size_t size
)
210 struct msghdr msg
= { 0 };
212 struct cmsghdr
*cmsg
;
213 struct ucred cred
= {
218 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
221 msg
.msg_control
= cmsgbuf
;
222 msg
.msg_controllen
= sizeof(cmsgbuf
);
224 cmsg
= CMSG_FIRSTHDR(&msg
);
225 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
226 cmsg
->cmsg_level
= SOL_SOCKET
;
227 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
228 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
233 iov
.iov_base
= data
? data
: buf
;
234 iov
.iov_len
= data
? size
: sizeof(buf
);
238 return sendmsg(fd
, &msg
, 0);
241 int lxc_af_unix_rcv_credential(int fd
, void *data
, size_t size
)
243 struct msghdr msg
= { 0 };
245 struct cmsghdr
*cmsg
;
247 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
253 msg
.msg_control
= cmsgbuf
;
254 msg
.msg_controllen
= sizeof(cmsgbuf
);
256 iov
.iov_base
= data
? data
: buf
;
257 iov
.iov_len
= data
? size
: sizeof(buf
);
261 ret
= recvmsg(fd
, &msg
, 0);
265 cmsg
= CMSG_FIRSTHDR(&msg
);
267 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
268 cmsg
->cmsg_level
== SOL_SOCKET
&&
269 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
270 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
271 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
272 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);