Skip to content

Commit 5498ad7

Browse files
committed
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.
1 parent fe9756d commit 5498ad7

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

systemd/_daemon.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,17 @@ static PyObject* is_socket_inet(PyObject *self, PyObject *args) {
317317
return PyBool_FromLong(r);
318318
}
319319

320-
#ifdef HAVE_IS_SOCKET_SOCKADDR
321320
PyDoc_STRVAR(is_socket_sockaddr__doc__,
322321
"_is_socket_sockaddr(fd, address, type=0, flowinfo=0, listening=-1) -> bool\n\n"
323322
"Wraps sd_is_socket_inet_sockaddr(3).\n"
323+
#ifdef HAVE_IS_SOCKET_SOCKADDR
324324
"`address` is a systemd-style numerical IPv4 or IPv6 address as used in\n"
325325
"ListenStream=. A port may be included after a colon (\":\"). See\n"
326326
"systemd.socket(5) for details.\n\n"
327327
"Constants for `family` are defined in the socket module."
328+
#else
329+
"NOT SUPPORTED: compiled without support sd_socket_sockaddr"
330+
#endif
328331
);
329332

330333
static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) {
@@ -357,13 +360,17 @@ static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) {
357360
addr.in6.sin6_flowinfo = flowinfo;
358361
}
359362

363+
#ifdef HAVE_IS_SOCKET_SOCKADDR
360364
r = sd_is_socket_sockaddr(fd, type, &addr.sa, addr_len, listening);
361365
if (set_error(r, NULL, NULL) < 0)
362366
return NULL;
363367

364368
return PyBool_FromLong(r);
365-
}
369+
#else
370+
set_error(-ENOSYS, NULL, "Compiled without support for sd_is_socket_sockaddr");
371+
return NULL;
366372
#endif
373+
}
367374

368375
PyDoc_STRVAR(is_socket_unix__doc__,
369376
"_is_socket_unix(fd, type, listening, path) -> bool\n\n"
@@ -408,9 +415,7 @@ static PyMethodDef methods[] = {
408415
{ "_is_mq", is_mq, METH_VARARGS, is_mq__doc__},
409416
{ "_is_socket", is_socket, METH_VARARGS, is_socket__doc__},
410417
{ "_is_socket_inet", is_socket_inet, METH_VARARGS, is_socket_inet__doc__},
411-
#ifdef HAVE_IS_SOCKET_SOCKADDR
412418
{ "_is_socket_sockaddr", is_socket_sockaddr, METH_VARARGS, is_socket_sockaddr__doc__},
413-
#endif
414419
{ "_is_socket_unix", is_socket_unix, METH_VARARGS, is_socket_unix__doc__},
415420
{} /* Sentinel */
416421
};

systemd/test/test_daemon.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,21 @@ def test_no_mismatch():
123123
assert not is_fifo(sock)
124124
assert not is_mq_wrapper(sock)
125125
assert not is_socket_inet(sock)
126-
assert not is_socket_sockaddr(sock, '127.0.0.1:2000')
126+
with skip_enosys():
127+
assert not is_socket_sockaddr(sock, '127.0.0.1:2000')
127128

128129
fd = sock.fileno()
129130
assert not is_fifo(fd)
130131
assert not is_mq_wrapper(fd)
131132
assert not is_socket_inet(fd)
132-
assert not is_socket_sockaddr(fd, '127.0.0.1:2000')
133+
with skip_enosys():
134+
assert not is_socket_sockaddr(fd, '127.0.0.1:2000')
133135

134136
assert not _is_fifo(fd)
135137
assert not _is_mq_wrapper(fd)
136138
assert not _is_socket_inet(fd)
137-
assert not _is_socket_sockaddr(fd, '127.0.0.1:2000')
139+
with skip_enosys():
140+
assert not _is_socket_sockaddr(fd, '127.0.0.1:2000')
138141

139142
def test_is_socket():
140143
with closing_socketpair(socket.AF_UNIX) as pair:
@@ -145,14 +148,16 @@ def test_is_socket():
145148
assert not is_socket(arg, socket.AF_INET)
146149
assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
147150
assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
148-
assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
151+
with skip_enosys():
152+
assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
149153

150154
assert _is_socket(arg)
151155
assert _is_socket(arg, socket.AF_UNIX)
152156
assert not _is_socket(arg, socket.AF_INET)
153157
assert _is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
154158
assert not _is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
155-
assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
159+
with skip_enosys():
160+
assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
156161

157162
def test_is_socket_sockaddr():
158163
with contextlib.closing(socket.socket(socket.AF_INET)) as sock:
@@ -162,24 +167,33 @@ def test_is_socket_sockaddr():
162167

163168
for listening in (0, 1):
164169
for arg in (sock, sock.fileno()):
165-
assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM)
166-
assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM)
167-
168-
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening)
169-
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1)
170-
assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening)
170+
with skip_enosys():
171+
assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM)
172+
with skip_enosys():
173+
assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM)
174+
175+
with skip_enosys():
176+
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening)
177+
with skip_enosys():
178+
assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1)
179+
with skip_enosys():
180+
assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening)
171181

172182
with pytest.raises(ValueError):
173183
is_socket_sockaddr(arg, '127.0.0.1', flowinfo=123456)
174184

175-
assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
176-
assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
185+
with skip_enosys():
186+
assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
187+
with skip_enosys():
188+
assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
177189

178190
with pytest.raises(ValueError):
179191
_is_socket_sockaddr(arg, '127.0.0.1', 0, 123456)
180192

181-
assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
182-
assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
193+
with skip_enosys():
194+
assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
195+
with skip_enosys():
196+
assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
183197

184198
sock.listen(11)
185199

0 commit comments

Comments
 (0)