commit da4000da5e0253b10d59fdc04baafdadd43ca4fd
parent 7afd50239da5420d0163621b12fdf5e3214ec297
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Sat, 10 Sep 2016 14:39:33 +0000
Allow s6-ipcserver-socketbinder to create SOCK_DGRAM sockets and to not listen() (suggested by Daniel Kahn Gillmor)
Diffstat:
2 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/doc/s6-ipcserver-socketbinder.html b/doc/s6-ipcserver-socketbinder.html
@@ -26,11 +26,11 @@ socket, then executes a program.
<h2> Interface </h2>
<pre>
- s6-ipcserver-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] <em>path</em> <em>prog...</em>
+ s6-ipcserver-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] <em>path</em> <em>prog...</em>
</pre>
<ul>
- <li> s6-ipcserver-socketbinder creates a Unix domain socket of type SOCK_STREAM
+ <li> s6-ipcserver-socketbinder creates a Unix domain socket
and binds it to <em>path</em>. It prepares the socket to accept
connections by calling
<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html">listen()</a>. </li>
@@ -48,8 +48,17 @@ and is generally used with server programs. This is the default. Note that
<em>path</em> will be deleted if it already exists at program start time. </li>
<li> <tt>-D</tt> : disallow instant rebinding to the same path. </li>
<li> <tt>-b <em>backlog</em></tt> : set a maximum of
-<em>backlog</em> backlog connections on the socket. Extra
-connection attempts will rejected by the kernel. </li>
+<em>backlog</em> backlog connections on the socket - extra
+connection attempts will rejected by the kernel. The default is SOMAXCONN,
+i.e. the maximum number allowed by the system. If <em>backlog</em>
+is 0, then the socket will be created, but it <strong>will not be
+listening</em>. </li>
+ <li> <tt>-M</tt> : the type of the socket will be SOCK_STREAM. This is
+the default. </li>
+ <li> <tt>-m</tt> : the type of the socket will be SOCK_DGRAM. Note
+that by default SOCK_DGRAM sockets are not connection-mode, and <tt>listen()</tt>
+will fail - so you should always give the <tt>-b0</tt> option to
+s6-ipcserver-socketbinder along with <tt>-m</tt>. </li>
</ul>
<h2> Notes </h2>
diff --git a/src/conn-tools/s6-ipcserver-socketbinder.c b/src/conn-tools/s6-ipcserver-socketbinder.c
@@ -2,6 +2,7 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/socket.h>
#include <unistd.h>
#include <skalibs/uint.h>
#include <skalibs/sgetopt.h>
@@ -9,24 +10,27 @@
#include <skalibs/djbunix.h>
#include <skalibs/webipc.h>
-#define USAGE "s6-ipcserver-socketbinder [ -d | -D ] [ -b backlog ] path prog..."
+#define USAGE "s6-ipcserver-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] path prog..."
#define dieusage() strerr_dieusage(100, USAGE)
int main (int argc, char const *const *argv, char const *const *envp)
{
- unsigned int backlog = 20 ;
+ unsigned int backlog = SOMAXCONN ;
int flagreuse = 1 ;
+ int flagdgram = 0 ;
PROG = "s6-ipcserver-socketbinder" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
- register int opt = subgetopt_r(argc, argv, "Ddb:", &l) ;
+ register int opt = subgetopt_r(argc, argv, "DdMmb:", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'D' : flagreuse = 0 ; break ;
case 'd' : flagreuse = 1 ; break ;
+ case 'M' : flagdgram = 0 ; break ;
+ case 'm' : flagdgram = 1 ; break ;
case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ;
default : dieusage() ;
}
@@ -35,14 +39,15 @@ int main (int argc, char const *const *argv, char const *const *envp)
}
if (argc < 2) dieusage() ;
close(0) ;
- if (ipc_stream()) strerr_diefu1sys(111, "create socket") ;
+ if (flagdgram ? ipc_datagram() : ipc_stream()) strerr_diefu1sys(111, "create socket") ;
{
mode_t m = umask(0) ;
if ((flagreuse ? ipc_bind_reuse(0, argv[0]) : ipc_bind(0, argv[0])) < 0)
strerr_diefu2sys(111, "bind to ", argv[0]) ;
umask(m) ;
}
- if (ipc_listen(0, backlog) < 0) strerr_diefu2sys(111, "listen to ", argv[0]) ;
+ if (backlog && ipc_listen(0, backlog) < 0)
+ strerr_diefu2sys(111, "listen to ", argv[0]) ;
pathexec_run(argv[1], argv + 1, envp) ;
strerr_dieexec(111, argv[1]) ;