chiark / gitweb /
socket: support ListeSpecial= sockets
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Apr 2011 03:02:23 +0000 (05:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 Apr 2011 03:02:23 +0000 (05:02 +0200)
TODO
man/systemd.socket.xml
src/load-fragment.c
src/socket.c
src/socket.h

diff --git a/TODO b/TODO
index f90e533..5d6b8c9 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,6 +2,13 @@ F15:
 
 * swap units that are activated by one name but shown in the kernel under another are semi-broken
 
+* Fix assert http://lists.freedesktop.org/archives/systemd-devel/2011-April/001910.html
+
+* 0595f9a1c182a84581749823ef47c5f292e545f9 is borked, freezes shutdown
+    (path: after installing inotify watches, recheck file again to fix race)
+
+F15 External:
+
 * NFS, networkmanager ordering issue (PENDING)
 
 * NM should pull in network.target (PENDING)
@@ -9,9 +16,6 @@ F15:
 
 * bluetooth should be possible to disable (PENDING)
 
-* 0595f9a1c182a84581749823ef47c5f292e545f9 is borked, freezes shutdown
-    (path: after installing inotify watches, recheck file again to fix race)
-
 * get writev() /dev/kmsg support into the F15 kernel
     https://lkml.org/lkml/2011/4/6/473
   patched merged into -mm
@@ -24,8 +28,6 @@ F15:
 
 * make anaconda write timeout=0 for encrypted devices
 
-* Fix assert http://lists.freedesktop.org/archives/systemd-devel/2011-April/001910.html
-
 Features:
 
 * maybe lower default timeout to 2min?
@@ -34,8 +36,6 @@ Features:
 
 * support wildcard expansion in ListeStream= and friends
 
-* Add ListenSpecial to .socket units for /proc/kmsg and similar friends?
-
 * avoid DefaultStandardOutput=syslog to have any effect on StandardInput=socket services
 
 * use pivot_root on shutdown so that we can unmount the root directory.
index fd4569e..d651c14 100644 (file)
                         </varlistentry>
 
                         <varlistentry>
+                                <term><varname>ListenSpecial=</varname></term>
+                                <listitem><para>Specifies a special
+                                file in the file system to listen
+                                on. This expects an absolute file
+                                system path as argument. Behaviour
+                                otherwise is very similar to the
+                                <varname>ListenFIFO=</varname>
+                                directive above. Use this to open
+                                character device nodes as well as
+                                special files in
+                                <filename>/proc</filename> and
+                                <filename>/sys</filename>.</para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
                                 <term><varname>ListenNetlink=</varname></term>
                                 <listitem><para>Specifies a Netlink
                                 family to create a socket for to
index c48d764..6ec5090 100644 (file)
@@ -220,6 +220,17 @@ static int config_parse_listen(
                 }
 
                 path_kill_slashes(p->path);
+
+        } else if (streq(lvalue, "ListenSpecial")) {
+                p->type = SOCKET_SPECIAL;
+
+                if (!(p->path = strdup(rvalue))) {
+                        free(p);
+                        return -ENOMEM;
+                }
+
+                path_kill_slashes(p->path);
+
         } else if (streq(lvalue, "ListenNetlink")) {
                 p->type = SOCKET_SOCKET;
 
@@ -1908,6 +1919,7 @@ static int load_from_path(Unit *u, const char *path) {
                 { "ListenSequentialPacket", config_parse_listen,          0, &u->socket,                                      "Socket"  },
                 { "ListenFIFO",             config_parse_listen,          0, &u->socket,                                      "Socket"  },
                 { "ListenNetlink",          config_parse_listen,          0, &u->socket,                                      "Socket"  },
+                { "ListenSpecial",          config_parse_listen,          0, &u->socket,                                      "Socket"  },
                 { "BindIPv6Only",           config_parse_socket_bind,     0, &u->socket,                                      "Socket"  },
                 { "Backlog",                config_parse_unsigned,        0, &u->socket.backlog,                              "Socket"  },
                 { "BindToDevice",           config_parse_bindtodevice,    0, &u->socket,                                      "Socket"  },
index 1f03c22..ad67215 100644 (file)
@@ -249,7 +249,7 @@ static bool socket_needs_mount(Socket *s, const char *prefix) {
                         if (socket_address_needs_mount(&p->address, prefix))
                                 return true;
                 } else {
-                        assert(p->type == SOCKET_FIFO);
+                        assert(p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL);
                         if (path_startswith(p->path, prefix))
                                 return true;
                 }
@@ -482,7 +482,9 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
 
                         fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
                         free(k);
-                } else
+                } else if (p->type == SOCKET_SPECIAL)
+                        fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
+                else
                         fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
         }
 
@@ -687,7 +689,6 @@ static void socket_apply_fifo_options(Socket *s, int fd) {
                         log_warning("F_SETPIPE_SZ: %m");
 }
 
-
 static int fifo_address_create(
                 const char *path,
                 mode_t directory_mode,
@@ -753,6 +754,42 @@ fail:
         return r;
 }
 
+static int special_address_create(
+                const char *path,
+                int *_fd) {
+
+        int fd = -1, r = 0;
+        struct stat st;
+
+        assert(path);
+        assert(_fd);
+
+        if ((fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW)) < 0) {
+                r = -errno;
+                goto fail;
+        }
+
+        if (fstat(fd, &st) < 0) {
+                r = -errno;
+                goto fail;
+        }
+
+        /* Check whether this is a /proc, /sys or /dev file or char device */
+        if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
+                r = -EEXIST;
+                goto fail;
+        }
+
+        *_fd = fd;
+        return 0;
+
+fail:
+        if (fd >= 0)
+                close_nointr_nofail(fd);
+
+        return r;
+}
+
 static int socket_open_fds(Socket *s) {
         SocketPort *p;
         int r;
@@ -796,6 +833,13 @@ static int socket_open_fds(Socket *s) {
 
                         socket_apply_socket_options(s, p->fd);
 
+                } else  if (p->type == SOCKET_SPECIAL) {
+
+                        if ((r = special_address_create(
+                                             p->path,
+                                             &p->fd)) < 0)
+                                goto rollback;
+
                 } else  if (p->type == SOCKET_FIFO) {
 
                         if ((r = fifo_address_create(
@@ -1461,7 +1505,9 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
                         else
                                 unit_serialize_item_format(u, f, "socket", "%i %i %s", copy, p->address.type, t);
                         free(t);
-                } else {
+                } else if (p->type == SOCKET_SPECIAL)
+                        unit_serialize_item_format(u, f, "special", "%i %s", copy, p->path);
+                else {
                         assert(p->type == SOCKET_FIFO);
                         unit_serialize_item_format(u, f, "fifo", "%i %s", copy, p->path);
                 }
@@ -1525,7 +1571,28 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                 else {
 
                         LIST_FOREACH(port, p, s->ports)
-                                if (streq(p->path, value+skip))
+                                if (p->type == SOCKET_FIFO &&
+                                    streq_ptr(p->path, value+skip))
+                                        break;
+
+                        if (p) {
+                                if (p->fd >= 0)
+                                        close_nointr_nofail(p->fd);
+                                p->fd = fdset_remove(fds, fd);
+                        }
+                }
+
+        } else if (streq(key, "special")) {
+                int fd, skip = 0;
+                SocketPort *p;
+
+                if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
+                        log_debug("Failed to parse special value %s", value);
+                else {
+
+                        LIST_FOREACH(port, p, s->ports)
+                                if (p->type == SOCKET_SPECIAL &&
+                                    streq_ptr(p->path, value+skip))
                                         break;
 
                         if (p) {
index 9dd9f55..b83c34c 100644 (file)
@@ -58,6 +58,7 @@ typedef enum SocketExecCommand {
 typedef enum SocketType {
         SOCKET_SOCKET,
         SOCKET_FIFO,
+        SOCKET_SPECIAL,
         _SOCKET_FIFO_MAX,
         _SOCKET_FIFO_INVALID = -1
 } SocketType;