]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/nscd-flush.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / shared / nscd-flush.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <poll.h>
5
6 #include "fd-util.h"
7 #include "io-util.h"
8 #include "nscd-flush.h"
9 #include "socket-util.h"
10 #include "strv.h"
11 #include "time-util.h"
12
13 #define NSCD_FLUSH_CACHE_TIMEOUT_USEC (5*USEC_PER_SEC)
14
15 struct nscdInvalidateRequest {
16 int32_t version;
17 int32_t type; /* in glibc this is an enum. We don't replicate this here 1:1. Also, wtf, how unportable is that
18 * even? */
19 int32_t key_len;
20 char dbname[];
21 };
22
23 static int nscd_flush_cache_one(const char *database, usec_t end) {
24 size_t req_size, has_written = 0, has_read = 0, l;
25 struct nscdInvalidateRequest *req;
26 _cleanup_close_ int fd = -EBADF;
27 int32_t resp;
28 int events, r;
29
30 assert(database);
31
32 l = strlen(database);
33 req_size = offsetof(struct nscdInvalidateRequest, dbname) + l + 1;
34
35 req = alloca_safe(req_size);
36 *req = (struct nscdInvalidateRequest) {
37 .version = 2,
38 .type = 10,
39 .key_len = l + 1,
40 };
41
42 strcpy(req->dbname, database);
43
44 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
45 if (fd < 0)
46 return log_debug_errno(errno, "Failed to allocate nscd socket: %m");
47
48 /* Note: connect() returns EINPROGRESS if O_NONBLOCK is set and establishing a connection takes time. The
49 * kernel lets us know this way that the connection is now being established, and we should watch with poll()
50 * to learn when it is fully established. That said, AF_UNIX on Linux never triggers this IRL (connect() is
51 * always instant on AF_UNIX), hence handling this is mostly just an exercise in defensive, protocol-agnostic
52 * programming.
53 *
54 * connect() returns EAGAIN if the socket's backlog limit has been reached. When we see this we give up right
55 * away, after all this entire function here is written in a defensive style so that a non-responding nscd
56 * doesn't stall us for good. (Even if we wanted to handle this better: the Linux kernel doesn't really have a
57 * nice way to connect() to a server synchronously with a time limit that would also cover dealing with the
58 * backlog limit. After all SO_RCVTIMEO and SR_SNDTIMEO don't apply to connect(), and alarm() is frickin' ugly
59 * and not really reasonably usable from threads-aware code.) */
60 r = connect_unix_path(fd, AT_FDCWD, "/run/nscd/socket");
61 if (r < 0) {
62 if (r == -EAGAIN)
63 return log_debug_errno(r, "nscd is overloaded (backlog limit reached) and refuses to take further connections: %m");
64 if (r != -EINPROGRESS)
65 return log_debug_errno(r, "Failed to connect to nscd socket: %m");
66
67 /* Continue in case of EINPROGRESS, but don't bother with send() or recv() until being notified that
68 * establishing the connection is complete. */
69 events = 0;
70 } else
71 events = POLLIN|POLLOUT; /* Let's assume initially that we can write and read to the fd, to suppress
72 * one poll() invocation */
73 for (;;) {
74 usec_t p;
75
76 if (events & POLLOUT) {
77 ssize_t m;
78
79 assert(has_written < req_size);
80
81 m = send(fd, (uint8_t*) req + has_written, req_size - has_written, MSG_NOSIGNAL);
82 if (m < 0) {
83 if (errno != EAGAIN) /* Note that EAGAIN is returned by the kernel whenever it can't
84 * take the data right now, and that includes if the connect() is
85 * asynchronous and we saw EINPROGRESS on it, and it hasn't
86 * completed yet. */
87 return log_debug_errno(errno, "Failed to write to nscd socket: %m");
88 } else
89 has_written += m;
90 }
91
92 if (events & (POLLIN|POLLERR|POLLHUP)) {
93 ssize_t m;
94
95 if (has_read >= sizeof(resp))
96 return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Response from nscd longer than expected: %m");
97
98 m = recv(fd, (uint8_t*) &resp + has_read, sizeof(resp) - has_read, 0);
99 if (m < 0) {
100 if (errno != EAGAIN)
101 return log_debug_errno(errno, "Failed to read from nscd socket: %m");
102 } else if (m == 0) { /* EOF */
103 if (has_read == 0 && has_written >= req_size) /* Older nscd immediately terminated the
104 * connection, accept that as OK */
105 return 1;
106
107 return log_debug_errno(SYNTHETIC_ERRNO(EIO), "nscd prematurely ended connection.");
108 } else
109 has_read += m;
110 }
111
112 if (has_written >= req_size && has_read >= sizeof(resp)) { /* done? */
113 if (resp < 0)
114 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "nscd sent us a negative error number: %i", resp);
115 if (resp > 0)
116 return log_debug_errno(resp, "nscd return failure code on invalidating '%s'.", database);
117 return 1;
118 }
119
120 p = now(CLOCK_MONOTONIC);
121 if (p >= end)
122 return -ETIMEDOUT;
123
124 events = fd_wait_for_event(fd, POLLIN | (has_written < req_size ? POLLOUT : 0), end - p);
125 if (events < 0)
126 return events;
127 }
128 }
129
130 int nscd_flush_cache(char **databases) {
131 usec_t end;
132 int r = 0;
133
134 /* Tries to invalidate the specified database in nscd. We do this carefully, with a 5s timeout, so that we
135 * don't block indefinitely on another service. */
136
137 end = usec_add(now(CLOCK_MONOTONIC), NSCD_FLUSH_CACHE_TIMEOUT_USEC);
138
139 STRV_FOREACH(i, databases) {
140 int k;
141
142 k = nscd_flush_cache_one(*i, end);
143 if (k < 0 && r >= 0)
144 r = k;
145 }
146
147 return r;
148 }