Debian Codegraph @

src/shared/varlink-serialize.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "extract-word.h"
5 #include "fd-util.h"
6 #include "fdset.h"
7 #include "log.h"
8 #include "serialize.h"
9 #include "socket-util.h"
10 #include "string-util.h"
11 #include "varlink-internal.h"
12 #include "varlink-serialize.h"
13
14 int varlink_server_serialize(sd_varlink_server *s, const char *name, FILE *f, FDSet *fds) {
15 assert(f);
16 assert(fds);
17
18 if (!s)
19 return 0;
20
21 const char *prefix = name ? strjoina("varlink-server-", name) : "varlink-server";
22
23 LIST_FOREACH(sockets, ss, s->sockets) {
24 assert(ss->address);
25 assert(ss->fd >= 0);
26
27 /* If we fail to serialize the fd, it will be considered an error during deserialization */
28 int copy = fdset_put_dup(fds, ss->fd);
29 if (copy < 0)
30 return copy;
31
32 fprintf(f, "%s-socket-address=%s varlink-server-socket-fd=%d\n", prefix, ss->address, copy);
33 }
34
35 return 0;
36 }
37
38 int varlink_server_deserialize_one(sd_varlink_server *s, const char *value, FDSet *fds) {
39 _cleanup_free_ char *address = NULL;
40 _cleanup_close_ int fd = -EBADF;
41 const char *v;
42 int r;
43
44 /* This function expects a serialization line with "varlink-server(-name)-" prefix stripped! */
45
46 assert(s);
47 assert(value);
48 assert(fds);
49
50 v = startswith(value, "socket-address=");
51 if (!v)
52 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
53 "Invalid varlink server serialization entry: %s", value);
54
55 r = extract_first_word(&v, &address, " ", /* flags = */ 0);
56 if (r <= 0)
57 return varlink_server_log_errno(s, r < 0 ? r : SYNTHETIC_ERRNO(ENODATA),
58 "Failed to extract socket address from varlink serialization: %s", value);
59 if (v)
60 v = startswith(v, "varlink-server-socket-fd=");
61 if (!v)
62 return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EBADF),
63 "Got varlink serialization without socket fd, refusing.");
64
65 fd = deserialize_fd(fds, v);
66 if (fd < 0)
67 return varlink_server_log_errno(s, fd, "Failed to deserialize varlink socket fd: %m");
68
69 /* NB: varlink_server_socket_free() does not close the fd! */
70 _cleanup_(varlink_server_socket_freep) VarlinkServerSocket *ss = NULL;
71 ss = new(VarlinkServerSocket, 1);
72 if (!ss)
73 return log_oom_debug();
74
75 *ss = (VarlinkServerSocket) {
76 .server = s,
77 .address = TAKE_PTR(address),
78 .fd = fd,
79 };
80
81 r = varlink_server_add_socket_event_source(s, ss);
82 if (r < 0)
83 return varlink_server_log_errno(s, r, "Failed to add VarlinkServerSocket event source to the event loop: %m");
84
85 LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss));
86 TAKE_FD(fd); /* ownership is now transferred to varlink server */
87
88 return 0;
89 }
90
91 bool varlink_server_contains_socket(sd_varlink_server *s, const char *address) {
92 int r;
93
94 assert(s);
95 assert(address);
96
97 LIST_FOREACH(sockets, ss, s->sockets) {
98 r = socket_address_equal_unix(ss->address, address);
99 if (r < 0)
100 log_debug_errno(r, "Failed to compare '%s' and '%s', ignoring: %m", ss->address, address);
101 if (r > 0)
102 return true;
103 }
104
105 return false;
106 }