]>
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
32 #include <sys/socket.h>
37 lxc_log_define(lxc_af_unix
, lxc
);
39 int lxc_abstract_unix_open(const char *path
, int type
, int flags
)
43 struct sockaddr_un addr
;
45 fd
= socket(PF_UNIX
, type
, 0);
49 /* Clear address structure */
50 memset(&addr
, 0, sizeof(addr
));
55 addr
.sun_family
= AF_UNIX
;
57 len
= strlen(&path
[1]);
58 /* do not enforce \0-termination */
59 if (len
>= sizeof(addr
.sun_path
)) {
64 /* addr.sun_path[0] has already been set to 0 by memset() */
65 strncpy(&addr
.sun_path
[1], &path
[1], len
);
67 ret
= bind(fd
, (struct sockaddr
*)&addr
,
68 offsetof(struct sockaddr_un
, sun_path
) + len
+ 1);
76 if (type
== SOCK_STREAM
) {
77 ret
= listen(fd
, 100);
90 int lxc_abstract_unix_close(int fd
)
96 int lxc_abstract_unix_connect(const char *path
)
100 struct sockaddr_un addr
;
102 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
106 memset(&addr
, 0, sizeof(addr
));
108 addr
.sun_family
= AF_UNIX
;
110 len
= strlen(&path
[1]);
111 /* do not enforce \0-termination */
112 if (len
>= sizeof(addr
.sun_path
)) {
114 errno
= ENAMETOOLONG
;
117 /* addr.sun_path[0] has already been set to 0 by memset() */
118 strncpy(&addr
.sun_path
[1], &path
[1], strlen(&path
[1]));
120 ret
= connect(fd
, (struct sockaddr
*)&addr
,
121 offsetof(struct sockaddr_un
, sun_path
) + len
+ 1);
130 int lxc_abstract_unix_send_fds(int fd
, int *sendfds
, int num_sendfds
,
131 void *data
, size_t size
)
136 struct cmsghdr
*cmsg
= NULL
;
139 size_t cmsgbufsize
= CMSG_SPACE(num_sendfds
* sizeof(int));
141 memset(&msg
, 0, sizeof(msg
));
142 memset(&iov
, 0, sizeof(iov
));
144 cmsgbuf
= malloc(cmsgbufsize
);
148 msg
.msg_control
= cmsgbuf
;
149 msg
.msg_controllen
= cmsgbufsize
;
151 cmsg
= CMSG_FIRSTHDR(&msg
);
152 cmsg
->cmsg_level
= SOL_SOCKET
;
153 cmsg
->cmsg_type
= SCM_RIGHTS
;
154 cmsg
->cmsg_len
= CMSG_LEN(num_sendfds
* sizeof(int));
156 msg
.msg_controllen
= cmsg
->cmsg_len
;
158 memcpy(CMSG_DATA(cmsg
), sendfds
, num_sendfds
* sizeof(int));
160 iov
.iov_base
= data
? data
: buf
;
161 iov
.iov_len
= data
? size
: sizeof(buf
);
165 ret
= sendmsg(fd
, &msg
, MSG_NOSIGNAL
);
170 int lxc_abstract_unix_recv_fds(int fd
, int *recvfds
, int num_recvfds
,
171 void *data
, size_t size
)
176 struct cmsghdr
*cmsg
= NULL
;
179 size_t cmsgbufsize
= CMSG_SPACE(num_recvfds
* sizeof(int));
181 memset(&msg
, 0, sizeof(msg
));
182 memset(&iov
, 0, sizeof(iov
));
184 cmsgbuf
= malloc(cmsgbufsize
);
188 msg
.msg_control
= cmsgbuf
;
189 msg
.msg_controllen
= cmsgbufsize
;
191 iov
.iov_base
= data
? data
: buf
;
192 iov
.iov_len
= data
? size
: sizeof(buf
);
196 ret
= recvmsg(fd
, &msg
, 0);
200 cmsg
= CMSG_FIRSTHDR(&msg
);
202 memset(recvfds
, -1, num_recvfds
* sizeof(int));
203 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(num_recvfds
* sizeof(int)) &&
204 cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
== SCM_RIGHTS
) {
205 memcpy(recvfds
, CMSG_DATA(cmsg
), num_recvfds
* sizeof(int));
213 int lxc_abstract_unix_send_credential(int fd
, void *data
, size_t size
)
215 struct msghdr msg
= {0};
217 struct cmsghdr
*cmsg
;
218 struct ucred cred
= {
219 .pid
= getpid(), .uid
= getuid(), .gid
= getgid(),
221 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))] = {0};
224 msg
.msg_control
= cmsgbuf
;
225 msg
.msg_controllen
= sizeof(cmsgbuf
);
227 cmsg
= CMSG_FIRSTHDR(&msg
);
228 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
229 cmsg
->cmsg_level
= SOL_SOCKET
;
230 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
231 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
236 iov
.iov_base
= data
? data
: buf
;
237 iov
.iov_len
= data
? size
: sizeof(buf
);
241 return sendmsg(fd
, &msg
, MSG_NOSIGNAL
);
244 int lxc_abstract_unix_rcv_credential(int fd
, void *data
, size_t size
)
246 struct msghdr msg
= {0};
248 struct cmsghdr
*cmsg
;
251 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))] = {0};
256 msg
.msg_control
= cmsgbuf
;
257 msg
.msg_controllen
= sizeof(cmsgbuf
);
259 iov
.iov_base
= data
? data
: buf
;
260 iov
.iov_len
= data
? size
: sizeof(buf
);
264 ret
= recvmsg(fd
, &msg
, 0);
268 cmsg
= CMSG_FIRSTHDR(&msg
);
270 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
271 cmsg
->cmsg_level
== SOL_SOCKET
&&
272 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
273 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
275 (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
276 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);