]>
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., 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
))) {
65 if (type
== SOCK_STREAM
&& listen(fd
, 100)) {
75 int lxc_af_unix_close(int fd
)
77 struct sockaddr_un addr
;
78 socklen_t addrlen
= sizeof(addr
);
80 if (!getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
) &&
82 unlink(addr
.sun_path
);
89 int lxc_af_unix_connect(const char *path
)
92 struct sockaddr_un addr
;
94 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
98 memset(&addr
, 0, sizeof(addr
));
100 addr
.sun_family
= AF_UNIX
;
101 /* copy entire buffer in case of abstract socket */
102 memcpy(addr
.sun_path
, path
,
103 path
[0]?strlen(path
):sizeof(addr
.sun_path
));
105 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
115 int lxc_af_unix_send_fd(int fd
, int sendfd
, void *data
, size_t size
)
117 struct msghdr msg
= { 0 };
119 struct cmsghdr
*cmsg
;
120 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
124 msg
.msg_control
= cmsgbuf
;
125 msg
.msg_controllen
= sizeof(cmsgbuf
);
127 cmsg
= CMSG_FIRSTHDR(&msg
);
128 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
129 cmsg
->cmsg_level
= SOL_SOCKET
;
130 cmsg
->cmsg_type
= SCM_RIGHTS
;
131 val
= (int *)(CMSG_DATA(cmsg
));
137 iov
.iov_base
= data
? data
: buf
;
138 iov
.iov_len
= data
? size
: sizeof(buf
);
142 return sendmsg(fd
, &msg
, 0);
145 int lxc_af_unix_recv_fd(int fd
, int *recvfd
, void *data
, size_t size
)
147 struct msghdr msg
= { 0 };
149 struct cmsghdr
*cmsg
;
150 char cmsgbuf
[CMSG_SPACE(sizeof(int))];
156 msg
.msg_control
= cmsgbuf
;
157 msg
.msg_controllen
= sizeof(cmsgbuf
);
159 iov
.iov_base
= data
? data
: buf
;
160 iov
.iov_len
= data
? size
: sizeof(buf
);
164 ret
= recvmsg(fd
, &msg
, 0);
168 cmsg
= CMSG_FIRSTHDR(&msg
);
170 /* if the message is wrong the variable will not be
171 * filled and the peer will notified about a problem */
174 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(int)) &&
175 cmsg
->cmsg_level
== SOL_SOCKET
&&
176 cmsg
->cmsg_type
== SCM_RIGHTS
) {
177 val
= (int *) CMSG_DATA(cmsg
);
184 int lxc_af_unix_send_credential(int fd
, void *data
, size_t size
)
186 struct msghdr msg
= { 0 };
188 struct cmsghdr
*cmsg
;
189 struct ucred cred
= {
194 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
197 msg
.msg_control
= cmsgbuf
;
198 msg
.msg_controllen
= sizeof(cmsgbuf
);
200 cmsg
= CMSG_FIRSTHDR(&msg
);
201 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
202 cmsg
->cmsg_level
= SOL_SOCKET
;
203 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
204 memcpy(CMSG_DATA(cmsg
), &cred
, sizeof(cred
));
209 iov
.iov_base
= data
? data
: buf
;
210 iov
.iov_len
= data
? size
: sizeof(buf
);
214 return sendmsg(fd
, &msg
, 0);
217 int lxc_af_unix_rcv_credential(int fd
, void *data
, size_t size
)
219 struct msghdr msg
= { 0 };
221 struct cmsghdr
*cmsg
;
223 char cmsgbuf
[CMSG_SPACE(sizeof(cred
))];
229 msg
.msg_control
= cmsgbuf
;
230 msg
.msg_controllen
= sizeof(cmsgbuf
);
232 iov
.iov_base
= data
? data
: buf
;
233 iov
.iov_len
= data
? size
: sizeof(buf
);
237 ret
= recvmsg(fd
, &msg
, 0);
241 cmsg
= CMSG_FIRSTHDR(&msg
);
243 if (cmsg
&& cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)) &&
244 cmsg
->cmsg_level
== SOL_SOCKET
&&
245 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
246 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(cred
));
247 if (cred
.uid
&& (cred
.uid
!= getuid() || cred
.gid
!= getgid())) {
248 INFO("message denied for '%d/%d'", cred
.uid
, cred
.gid
);