From 5498ad7513e1a703e7881b28c78a47ffd8810838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 19 Mar 2017 12:38:27 -0400 Subject: daemon: properly skip sd_is_socket_sockaddr calls if not available As with other functions, the wrapper is always present, but returns OSError: [Errno 38] Function not implemented. --- systemd/_daemon.c | 13 +++++++++---- systemd/test/test_daemon.py | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/systemd/_daemon.c b/systemd/_daemon.c index 90cf205..fe6d890 100644 --- a/systemd/_daemon.c +++ b/systemd/_daemon.c @@ -317,14 +317,17 @@ static PyObject* is_socket_inet(PyObject *self, PyObject *args) { return PyBool_FromLong(r); } -#ifdef HAVE_IS_SOCKET_SOCKADDR PyDoc_STRVAR(is_socket_sockaddr__doc__, "_is_socket_sockaddr(fd, address, type=0, flowinfo=0, listening=-1) -> bool\n\n" "Wraps sd_is_socket_inet_sockaddr(3).\n" +#ifdef HAVE_IS_SOCKET_SOCKADDR "`address` is a systemd-style numerical IPv4 or IPv6 address as used in\n" "ListenStream=. A port may be included after a colon (\":\"). See\n" "systemd.socket(5) for details.\n\n" "Constants for `family` are defined in the socket module." +#else + "NOT SUPPORTED: compiled without support sd_socket_sockaddr" +#endif ); static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) { @@ -357,13 +360,17 @@ static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) { addr.in6.sin6_flowinfo = flowinfo; } +#ifdef HAVE_IS_SOCKET_SOCKADDR r = sd_is_socket_sockaddr(fd, type, &addr.sa, addr_len, listening); if (set_error(r, NULL, NULL) < 0) return NULL; return PyBool_FromLong(r); -} +#else + set_error(-ENOSYS, NULL, "Compiled without support for sd_is_socket_sockaddr"); + return NULL; #endif +} PyDoc_STRVAR(is_socket_unix__doc__, "_is_socket_unix(fd, type, listening, path) -> bool\n\n" @@ -408,9 +415,7 @@ static PyMethodDef methods[] = { { "_is_mq", is_mq, METH_VARARGS, is_mq__doc__}, { "_is_socket", is_socket, METH_VARARGS, is_socket__doc__}, { "_is_socket_inet", is_socket_inet, METH_VARARGS, is_socket_inet__doc__}, -#ifdef HAVE_IS_SOCKET_SOCKADDR { "_is_socket_sockaddr", is_socket_sockaddr, METH_VARARGS, is_socket_sockaddr__doc__}, -#endif { "_is_socket_unix", is_socket_unix, METH_VARARGS, is_socket_unix__doc__}, {} /* Sentinel */ }; diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py index ae76e54..1ddb55e 100644 --- a/systemd/test/test_daemon.py +++ b/systemd/test/test_daemon.py @@ -123,18 +123,21 @@ def test_no_mismatch(): assert not is_fifo(sock) assert not is_mq_wrapper(sock) assert not is_socket_inet(sock) - assert not is_socket_sockaddr(sock, '127.0.0.1:2000') + with skip_enosys(): + assert not is_socket_sockaddr(sock, '127.0.0.1:2000') fd = sock.fileno() assert not is_fifo(fd) assert not is_mq_wrapper(fd) assert not is_socket_inet(fd) - assert not is_socket_sockaddr(fd, '127.0.0.1:2000') + with skip_enosys(): + assert not is_socket_sockaddr(fd, '127.0.0.1:2000') assert not _is_fifo(fd) assert not _is_mq_wrapper(fd) assert not _is_socket_inet(fd) - assert not _is_socket_sockaddr(fd, '127.0.0.1:2000') + with skip_enosys(): + assert not _is_socket_sockaddr(fd, '127.0.0.1:2000') def test_is_socket(): with closing_socketpair(socket.AF_UNIX) as pair: @@ -145,14 +148,16 @@ def test_is_socket(): assert not is_socket(arg, socket.AF_INET) assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM) assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM) - assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) + with skip_enosys(): + assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) assert _is_socket(arg) assert _is_socket(arg, socket.AF_UNIX) assert not _is_socket(arg, socket.AF_INET) assert _is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM) assert not _is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM) - assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) + with skip_enosys(): + assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0) def test_is_socket_sockaddr(): with contextlib.closing(socket.socket(socket.AF_INET)) as sock: @@ -162,24 +167,33 @@ def test_is_socket_sockaddr(): for listening in (0, 1): for arg in (sock, sock.fileno()): - assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM) - assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM) - - assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening) - assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1) - assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening) + with skip_enosys(): + assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM) + with skip_enosys(): + assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM) + + with skip_enosys(): + assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening) + with skip_enosys(): + assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1) + with skip_enosys(): + assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening) with pytest.raises(ValueError): is_socket_sockaddr(arg, '127.0.0.1', flowinfo=123456) - assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) - assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) + with skip_enosys(): + assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) + with skip_enosys(): + assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) with pytest.raises(ValueError): _is_socket_sockaddr(arg, '127.0.0.1', 0, 123456) - assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) - assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) + with skip_enosys(): + assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM) + with skip_enosys(): + assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM) sock.listen(11) -- cgit v1.2.1