]>
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>
34 lxc_log_define(lxc_af_unix
, lxc
);
36 int lxc_af_unix_open(const char *path
, int type
, int flags
)
40 struct sockaddr_un addr
;
45 fd
= socket(PF_UNIX
, type
, 0);
49 memset(&addr
, 0, sizeof(addr
));
54 addr
.sun_family
= AF_UNIX
;
55 /* copy entire buffer in case of abstract socket */
56 len
= sizeof(addr
.sun_path
);
59 if (len
>= sizeof(addr
.sun_path
)) {
65 memcpy(addr
.sun_path
, path
, len
);
67 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
74 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
84 int lxc_af_unix_close(int fd
)
86 struct sockaddr_un addr
;
87 socklen_t addrlen
= sizeof(addr
);
89 if (!getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
) &&
91 unlink(addr
.sun_path
);
98 int lxc_af_unix_connect(const char *path
)
101 struct sockaddr_un addr
;
103 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
107 memset(&addr
, 0, sizeof(addr
));
109 addr
.sun_family
= AF_UNIX
;
110 /* copy entire buffer in case of abstract socket */
111 memcpy(addr
.sun_path
, path
,
112 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
114 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
124 int lxc_af_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
126 struct msghdr msg
= { 0 };
128 struct cmsghdr
*cmsg
;
129 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
133 msg
.msg_control
= cmsgbuf
;
134 msg
.msg_controllen
= sizeof(cmsgbuf
);
136 cmsg
= CMSG_FIRSTHDR(&msg
);
137 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
138 cmsg
->cmsg_level
= SOL_SOCKET
;
139 cmsg
->cmsg_type
= SCM_RIGHTS
;
140 val
= (int *)(CMSG_DATA(cmsg
));
146 iov
.iov_base
= data
? data
: buf
;
147 iov
.iov_len
= data
? size
: sizeof(buf
);
151 return sendmsg(fd
, &msg
, 0);
154 int lxc_af_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
156 struct msghdr msg
= { 0 };
158 struct cmsghdr
*cmsg
;
159 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
165 msg
.msg_control
= cmsgbuf
;
166 msg
.msg_controllen
= sizeof(cmsgbuf
);
168 iov
.iov_base
= data
? data
: buf
;
169 iov
.iov_len
= data
? size
: sizeof(buf
);
173 ret
= recvmsg(fd
, &msg
, 0);
177 cmsg
= CMSG_FIRSTHDR(&msg
);
179 /* if the message is wrong the variable will not be
180 * filled and the peer will notified about a problem */
183 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
184 cmsg
->cmsg_level
== SOL_SOCKET
&&
185 cmsg
->cmsg_type
== SCM_RIGHTS
) {
186 val
= (int *) CMSG_DATA(cmsg
);
193 int lxc_af_unix_send_credential(int fd
, void *data
, size_t size
)
195 struct msghdr msg
= { 0 };
197 struct cmsghdr
*cmsg
;
198 struct ucred cred
= {
203 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
206 msg
.msg_control
= cmsgbuf
;
207 msg
.msg_controllen
= sizeof(cmsgbuf
);
209 cmsg
= CMSG_FIRSTHDR(&msg
);
210 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
211 cmsg
->cmsg_level
= SOL_SOCKET
;
212 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
213 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
218 iov
.iov_base
= data
? data
: buf
;
219 iov
.iov_len
= data
? size
: sizeof(buf
);
223 return sendmsg(fd
, &msg
, 0);
226 int lxc_af_unix_rcv_credential(int fd
, void *data
, size_t size
)
228 struct msghdr msg
= { 0 };
230 struct cmsghdr
*cmsg
;
232 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
238 msg
.msg_control
= cmsgbuf
;
239 msg
.msg_controllen
= sizeof(cmsgbuf
);
241 iov
.iov_base
= data
? data
: buf
;
242 iov
.iov_len
= data
? size
: sizeof(buf
);
246 ret
= recvmsg(fd
, &msg
, 0);
250 cmsg
= CMSG_FIRSTHDR(&msg
);
252 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
253 cmsg
->cmsg_level
== SOL_SOCKET
&&
254 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
255 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
256 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
257 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);