]>
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
29 #include <sys/socket.h>
36 lxc_log_define(lxc_af_unix
, lxc
);
38 int lxc_abstract_unix_open(const char *path
, int type
, int flags
)
42 struct sockaddr_un addr
;
48 fd
= socket(PF_UNIX
, type
, 0);
53 /* Clear address structure */
54 memset(&addr
, 0, sizeof(addr
));
59 addr
.sun_family
= AF_UNIX
;
61 len
= strlen(&path
[1]) + 1;
62 if (len
>= sizeof(addr
.sun_path
) - 1) {
69 /* addr.sun_path[0] has already been set to 0 by memset() */
70 strncpy(&addr
.sun_path
[1], &path
[1], strlen(&path
[1]));
72 if (bind(fd
, (struct sockaddr
*)&addr
, offsetof(struct sockaddr_un
, sun_path
) + len
)) {
81 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
93 int lxc_abstract_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_abstract_unix_connect(const char *path
)
113 struct sockaddr_un addr
;
116 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
121 memset(&addr
, 0, sizeof(addr
));
123 addr
.sun_family
= AF_UNIX
;
125 len
= strlen(&path
[1]) + 1;
126 if (len
>= sizeof(addr
.sun_path
) - 1) {
130 errno
= ENAMETOOLONG
;
133 /* addr.sun_path[0] has already been set to 0 by memset() */
134 strncpy(&addr
.sun_path
[1], &path
[1], strlen(&path
[1]));
136 if (connect(fd
, (struct sockaddr
*)&addr
, offsetof(struct sockaddr_un
, sun_path
) + len
)) {
148 int lxc_abstract_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
150 struct msghdr msg
= { 0 };
152 struct cmsghdr
*cmsg
;
153 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
157 msg
.msg_control
= cmsgbuf
;
158 msg
.msg_controllen
= sizeof(cmsgbuf
);
160 cmsg
= CMSG_FIRSTHDR(&msg
);
161 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
162 cmsg
->cmsg_level
= SOL_SOCKET
;
163 cmsg
->cmsg_type
= SCM_RIGHTS
;
164 val
= (int *)(CMSG_DATA(cmsg
));
170 iov
.iov_base
= data
? data
: buf
;
171 iov
.iov_len
= data
? size
: sizeof(buf
);
175 return sendmsg(fd
, &msg
, 0);
178 int lxc_abstract_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
180 struct msghdr msg
= { 0 };
182 struct cmsghdr
*cmsg
;
183 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
189 msg
.msg_control
= cmsgbuf
;
190 msg
.msg_controllen
= sizeof(cmsgbuf
);
192 iov
.iov_base
= data
? data
: buf
;
193 iov
.iov_len
= data
? size
: sizeof(buf
);
197 ret
= recvmsg(fd
, &msg
, 0);
201 cmsg
= CMSG_FIRSTHDR(&msg
);
203 /* if the message is wrong the variable will not be
204 * filled and the peer will notified about a problem */
207 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
208 cmsg
->cmsg_level
== SOL_SOCKET
&&
209 cmsg
->cmsg_type
== SCM_RIGHTS
) {
210 val
= (int *) CMSG_DATA(cmsg
);
217 int lxc_abstract_unix_send_credential(int fd
, void *data
, size_t size
)
219 struct msghdr msg
= { 0 };
221 struct cmsghdr
*cmsg
;
222 struct ucred cred
= {
227 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
230 msg
.msg_control
= cmsgbuf
;
231 msg
.msg_controllen
= sizeof(cmsgbuf
);
233 cmsg
= CMSG_FIRSTHDR(&msg
);
234 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
235 cmsg
->cmsg_level
= SOL_SOCKET
;
236 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
237 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
242 iov
.iov_base
= data
? data
: buf
;
243 iov
.iov_len
= data
? size
: sizeof(buf
);
247 return sendmsg(fd
, &msg
, 0);
250 int lxc_abstract_unix_rcv_credential(int fd
, void *data
, size_t size
)
252 struct msghdr msg
= { 0 };
254 struct cmsghdr
*cmsg
;
256 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
262 msg
.msg_control
= cmsgbuf
;
263 msg
.msg_controllen
= sizeof(cmsgbuf
);
265 iov
.iov_base
= data
? data
: buf
;
266 iov
.iov_len
= data
? size
: sizeof(buf
);
270 ret
= recvmsg(fd
, &msg
, 0);
274 cmsg
= CMSG_FIRSTHDR(&msg
);
276 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
277 cmsg
->cmsg_level
== SOL_SOCKET
&&
278 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
279 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
280 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
281 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);