From: Christian Brauner Date: Thu, 25 Apr 2019 12:51:51 +0000 (+0200) Subject: af_unix: backport helper functions X-Git-Tag: lxc-3.0.4~63 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=548b3229d8d3bc18ddc11291aaafa32e0f479f64;p=thirdparty%2Flxc.git af_unix: backport helper functions This backports various helpers associated with seccomp notify to make maintenance easier. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/af_unix.c b/src/lxc/af_unix.c index 06700fac1..328f891f0 100644 --- a/src/lxc/af_unix.c +++ b/src/lxc/af_unix.c @@ -316,3 +316,73 @@ int lxc_abstract_unix_rcv_credential(int fd, void *data, size_t size) out: 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; +} diff --git a/src/lxc/af_unix.h b/src/lxc/af_unix.h index f2c2fdcc6..3ae595498 100644 --- a/src/lxc/af_unix.h +++ b/src/lxc/af_unix.h @@ -25,6 +25,8 @@ #define __LXC_AF_UNIX_H #include +#include +#include /* does not enforce \0-termination */ 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, 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_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 */