af_unix: backport helper functions

This backports various helpers associated with seccomp notify to make maintenance easier. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 85714791
...@@ -316,3 +316,73 @@ int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size) ...@@ -316,3 +316,73 @@ int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size)
out: out:
return ret; return ret;
} }
int lxc_unix_sockaddr(struct sockaddr_un *ret, const char *path)
{
size_t len;
len = strlen(path);
if (len == 0)
return minus_one_set_errno(EINVAL);
if (path[0] != '/' && path[0] != '@')
return minus_one_set_errno(EINVAL);
if (path[1] == '\0')
return minus_one_set_errno(EINVAL);
if (len + 1 > sizeof(ret->sun_path))
return minus_one_set_errno(EINVAL);
*ret = (struct sockaddr_un){
.sun_family = AF_UNIX,
};
if (path[0] == '@') {
memcpy(ret->sun_path + 1, path + 1, len);
return (int)(offsetof(struct sockaddr_un, sun_path) + len);
}
memcpy(ret->sun_path, path, len + 1);
return (int)(offsetof(struct sockaddr_un, sun_path) + len + 1);
}
int lxc_unix_connect(struct sockaddr_un *addr)
{
__do_close_prot_errno int fd = -EBADF;
int ret;
ssize_t len;
fd = socket(PF_UNIX, SOCK_STREAM, SOCK_CLOEXEC);
if (fd < 0)
return -1;
if (addr->sun_path[0] == '\0')
len = strlen(&addr->sun_path[1]);
else
len = strlen(&addr->sun_path[0]);
ret = connect(fd, (struct sockaddr *)&addr,
offsetof(struct sockaddr_un, sun_path) + len + 1);
if (ret < 0)
return -1;
return move_fd(fd);
}
int lxc_socket_set_timeout(int fd, int rcv_timeout, int snd_timeout)
{
struct timeval out = {0};
int ret;
out.tv_sec = snd_timeout;
ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&out,
sizeof(out));
if (ret < 0)
return -1;
out.tv_sec = rcv_timeout;
ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const void *)&out,
sizeof(out));
if (ret < 0)
return -1;
return 0;
}
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#define __LXC_AF_UNIX_H #define __LXC_AF_UNIX_H
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
/* does not enforce \0-termination */ /* does not enforce \0-termination */
extern int lxc_abstract_unix_open(const char *path, int type, int flags); extern int lxc_abstract_unix_open(const char *path, int type, int flags);
...@@ -37,5 +39,8 @@ extern int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds, ...@@ -37,5 +39,8 @@ extern int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds,
void *data, size_t size); void *data, size_t size);
extern int lxc_abstract_unix_send_credential(int fd, void *data, size_t size); extern int lxc_abstract_unix_send_credential(int fd, void *data, size_t size);
extern int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size); extern int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size);
extern int lxc_unix_sockaddr(struct sockaddr_un *ret, const char *path);
extern int lxc_unix_connect(struct sockaddr_un *addr);
extern int lxc_socket_set_timeout(int fd, int rcv_timeout, int snd_timeout);
#endif /* __LXC_AF_UNIX_H */ #endif /* __LXC_AF_UNIX_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment