X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fsocket.c;h=0a18716cd752ab145f02832e4de7b17f90bdda25;hb=ec2002f84928c0b5921a961cb2b8637563f29daa;hp=01af34c1052585736dcd89406bdb874f2663c077;hpb=a7444edaf768ae3dda13c42d8fe26f78b4e63e38;p=elogind.git diff --git a/src/socket.c b/src/socket.c index 01af34c10..0a18716cd 100644 --- a/src/socket.c +++ b/src/socket.c @@ -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, @@ -715,7 +716,7 @@ static int fifo_address_create( r = mkfifo(path, socket_mode); umask(old_mask); - if (r < 0) { + if (r < 0 && errno != EEXIST) { r = -errno; goto fail; } @@ -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) { @@ -1612,7 +1679,12 @@ static void socket_fd_event(Unit *u, int fd, uint32_t events, Watch *w) { log_debug("Incoming traffic on %s", u->meta.id); if (events != EPOLLIN) { - log_error("Got invalid poll event on socket."); + + if (events & EPOLLHUP) + log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u->meta.id); + else + log_error("%s: Got unexpected poll event (0x%x) on socket.", u->meta.id, events); + goto fail; }