]>
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 <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/socket.h>
34 lxc_log_define(lxc_af_unix
, lxc
);
36 int lxc_af_unix_open(const char *path
, int type
, int flags
)
39 struct sockaddr_un addr
;
44 fd
= socket(PF_UNIX
, type
, 0);
48 memset(&addr
, 0, sizeof(addr
));
53 addr
.sun_family
= AF_UNIX
;
54 /* copy entire buffer in case of abstract socket */
55 memcpy(addr
.sun_path
, path
,
56 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
58 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
63 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
71 int lxc_af_unix_close(int fd
)
73 struct sockaddr_un addr
;
76 if (!getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
) &&
78 unlink(addr
.sun_path
);
85 int lxc_af_unix_connect(const char *path
)
88 struct sockaddr_un addr
;
90 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
94 memset(&addr
, 0, sizeof(addr
));
96 addr
.sun_family
= AF_UNIX
;
97 /* copy entire buffer in case of abstract socket */
98 memcpy(addr
.sun_path
, path
,
99 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
101 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
109 int lxc_af_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
111 struct msghdr msg
= { 0 };
113 struct cmsghdr
*cmsg
;
114 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
118 msg
.msg_control
= cmsgbuf
;
119 msg
.msg_controllen
= sizeof(cmsgbuf
);
121 cmsg
= CMSG_FIRSTHDR(&msg
);
122 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
123 cmsg
->cmsg_level
= SOL_SOCKET
;
124 cmsg
->cmsg_type
= SCM_RIGHTS
;
125 val
= (int *)(CMSG_DATA(cmsg
));
131 iov
.iov_base
= data
? data
: buf
;
132 iov
.iov_len
= data
? size
: sizeof(buf
);
136 return sendmsg(fd
, &msg
, 0);
139 int lxc_af_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
141 struct msghdr msg
= { 0 };
143 struct cmsghdr
*cmsg
;
144 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
150 msg
.msg_control
= cmsgbuf
;
151 msg
.msg_controllen
= sizeof(cmsgbuf
);
153 iov
.iov_base
= data
? data
: buf
;
154 iov
.iov_len
= data
? size
: sizeof(buf
);
158 ret
= recvmsg(fd
, &msg
, 0);
162 cmsg
= CMSG_FIRSTHDR(&msg
);
164 /* if the message is wrong the variable will not be
165 * filled and the peer will notified about a problem */
168 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
169 cmsg
->cmsg_level
== SOL_SOCKET
&&
170 cmsg
->cmsg_type
== SCM_RIGHTS
) {
171 val
= (int *) CMSG_DATA(cmsg
);
178 int lxc_af_unix_send_credential(int fd
, void *data
, size_t size
)
180 struct msghdr msg
= { 0 };
182 struct cmsghdr
*cmsg
;
183 struct ucred cred
= {
188 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
191 msg
.msg_control
= cmsgbuf
;
192 msg
.msg_controllen
= sizeof(cmsgbuf
);
194 cmsg
= CMSG_FIRSTHDR(&msg
);
195 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
196 cmsg
->cmsg_level
= SOL_SOCKET
;
197 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
198 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
203 iov
.iov_base
= data
? data
: buf
;
204 iov
.iov_len
= data
? size
: sizeof(buf
);
208 return sendmsg(fd
, &msg
, 0);
211 int lxc_af_unix_rcv_credential(int fd
, void *data
, size_t size
)
213 struct msghdr msg
= { 0 };
215 struct cmsghdr
*cmsg
;
217 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
223 msg
.msg_control
= cmsgbuf
;
224 msg
.msg_controllen
= sizeof(cmsgbuf
);
226 iov
.iov_base
= data
? data
: buf
;
227 iov
.iov_len
= data
? size
: sizeof(buf
);
231 ret
= recvmsg(fd
, &msg
, 0);
235 cmsg
= CMSG_FIRSTHDR(&msg
);
237 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
238 cmsg
->cmsg_level
== SOL_SOCKET
&&
239 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
240 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
241 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
242 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);