]>
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
30 #include <sys/socket.h>
35 lxc_log_define(lxc_af_unix
, lxc
);
37 int lxc_abstract_unix_open(const char *path
, int type
, int flags
)
41 struct sockaddr_un addr
;
46 fd
= socket(PF_UNIX
, type
, 0);
50 /* Clear address structure */
51 memset(&addr
, 0, sizeof(addr
));
56 addr
.sun_family
= AF_UNIX
;
58 len
= strlen(&path
[1]);
59 /* do not enforce \0-termination */
60 if (len
>= sizeof(addr
.sun_path
)) {
65 /* addr.sun_path[0] has already been set to 0 by memset() */
66 strncpy(&addr
.sun_path
[1], &path
[1], strlen(&path
[1]));
68 if (bind(fd
, (struct sockaddr
*)&addr
, offsetof(struct sockaddr_un
, sun_path
) + len
+ 1)) {
75 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
85 int lxc_abstract_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_abstract_unix_connect(const char *path
)
103 struct sockaddr_un addr
;
105 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
109 memset(&addr
, 0, sizeof(addr
));
111 addr
.sun_family
= AF_UNIX
;
113 len
= strlen(&path
[1]);
114 /* do not enforce \0-termination */
115 if (len
>= sizeof(addr
.sun_path
)) {
117 errno
= ENAMETOOLONG
;
120 /* addr.sun_path[0] has already been set to 0 by memset() */
121 strncpy(&addr
.sun_path
[1], &path
[1], strlen(&path
[1]));
123 if (connect(fd
, (struct sockaddr
*)&addr
, offsetof(struct sockaddr_un
, sun_path
) + len
+ 1)) {
125 /* special case to connect to older containers */
126 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == 0)
136 int lxc_abstract_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
138 struct msghdr msg
= { 0 };
140 struct cmsghdr
*cmsg
;
141 char cmsgbuf
[CMSG_SPACE(sizeof(int))] = {0};
145 msg
.msg_control
= cmsgbuf
;
146 msg
.msg_controllen
= sizeof(cmsgbuf
);
148 cmsg
= CMSG_FIRSTHDR(&msg
);
149 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
150 cmsg
->cmsg_level
= SOL_SOCKET
;
151 cmsg
->cmsg_type
= SCM_RIGHTS
;
152 val
= (int *)(CMSG_DATA(cmsg
));
158 iov
.iov_base
= data
? data
: buf
;
159 iov
.iov_len
= data
? size
: sizeof(buf
);
163 return sendmsg(fd
, &msg
, MSG_NOSIGNAL
);
166 int lxc_abstract_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
168 struct msghdr msg
= { 0 };
170 struct cmsghdr
*cmsg
;
172 char cmsgbuf
[CMSG_SPACE(sizeof(int))] = {0};
177 msg
.msg_control
= cmsgbuf
;
178 msg
.msg_controllen
= sizeof(cmsgbuf
);
180 iov
.iov_base
= data
? data
: buf
;
181 iov
.iov_len
= data
? size
: sizeof(buf
);
185 ret
= recvmsg(fd
, &msg
, 0);
189 cmsg
= CMSG_FIRSTHDR(&msg
);
191 /* if the message is wrong the variable will not be
192 * filled and the peer will notified about a problem */
195 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
196 cmsg
->cmsg_level
== SOL_SOCKET
&&
197 cmsg
->cmsg_type
== SCM_RIGHTS
) {
198 val
= (int *) CMSG_DATA(cmsg
);
205 int lxc_abstract_unix_send_credential(int fd
, void *data
, size_t size
)
207 struct msghdr msg
= { 0 };
209 struct cmsghdr
*cmsg
;
210 struct ucred cred
= {
215 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))] = {0};
218 msg
.msg_control
= cmsgbuf
;
219 msg
.msg_controllen
= sizeof(cmsgbuf
);
221 cmsg
= CMSG_FIRSTHDR(&msg
);
222 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
223 cmsg
->cmsg_level
= SOL_SOCKET
;
224 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
225 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
230 iov
.iov_base
= data
? data
: buf
;
231 iov
.iov_len
= data
? size
: sizeof(buf
);
235 return sendmsg(fd
, &msg
, MSG_NOSIGNAL
);
238 int lxc_abstract_unix_rcv_credential(int fd
, void *data
, size_t size
)
240 struct msghdr msg
= { 0 };
242 struct cmsghdr
*cmsg
;
245 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))] = {0};
250 msg
.msg_control
= cmsgbuf
;
251 msg
.msg_controllen
= sizeof(cmsgbuf
);
253 iov
.iov_base
= data
? data
: buf
;
254 iov
.iov_len
= data
? size
: sizeof(buf
);
258 ret
= recvmsg(fd
, &msg
, 0);
262 cmsg
= CMSG_FIRSTHDR(&msg
);
264 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
265 cmsg
->cmsg_level
== SOL_SOCKET
&&
266 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
267 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
268 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
269 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);