Debian Codegraph @

src/tls/wsinstance-factory.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <err.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <systemd/sd-bus.h>
14 #include <systemd/sd-daemon.h>
15
16 #include "socket-io.h"
17 #include "utils.h"
18
19 #define UNIT_MAX 256
20
21 static int
22 match_job_removed (sd_bus_message *message,
23 void *user_data,
24 sd_bus_error *error)
25 {
26 const char **job_path = user_data;
27 const char *result;
28 const char *path;
29
30 debug (FACTORY, "Received JobRemoved signal:");
31
32 if (sd_bus_message_read (message, "uoss", NULL, &path, NULL, &result) < 0)
33 return 0;
34
35 debug (FACTORY, " -> path: %s, result: %s", path, result);
36
37 if (!*job_path || strcmp (path, *job_path) != 0)
38 return 0;
39
40 /* This is our job. */
41 debug (FACTORY, " -> sending result.");
42 send_all (SD_LISTEN_FDS_START, result, strlen (result), 5 * 1000000);
43 *job_path = NULL;
44
45 return 0;
46 }
47
48 int
49 main (void)
50 {
51 char instance[WSINSTANCE_MAX];
52 sd_bus_error error = SD_BUS_ERROR_NULL;
53 sd_bus *bus = NULL;
54 char unit[UNIT_MAX + 1];
55 sd_bus_message *reply = NULL;
56 const char *job_path = NULL;
57 char **fdnames;
58 int r;
59
60 if (sd_listen_fds_with_names (false, &fdnames) != 1 || strcmp (fdnames[0], "connection") != 0)
61 errx (EXIT_FAILURE, "Must be spawned from a systemd service on a socket with Accept=yes %s", fdnames[0]);
62
63 if (!recv_alnum (SD_LISTEN_FDS_START, instance, sizeof instance, 10 * 1000000))
64 errx (EXIT_FAILURE, "Didn't receive fingerprint");
65
66 r = sd_bus_open_system (&bus);
67 if (r < 0)
68 errx (EXIT_FAILURE, "Failed to connect to system bus: %s", strerror (-r));
69
70 /* We use the job_path variable to communicate with the match function
71 * in two ways:
72 *
73 * - we set it to the path of the job that we're waiting to exit so
74 * that the match function knows which signal is for us
75 *
76 * - once the job is removed, the match function clears the variable
77 * back to NULL. that's how we know when to stop waiting.
78 *
79 * In effect, the duration of job_path being set to non-NULL is more
80 * or less equal to the duration of the existence of a job object at
81 * that path.
82 */
83 r = sd_bus_match_signal_async (bus, NULL,
84 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
85 "org.freedesktop.systemd1.Manager", "JobRemoved",
86 match_job_removed, NULL, &job_path);
87 if (r < 0)
88 errx (EXIT_FAILURE, "Failed to install match rule: %s", strerror (-r));
89
90 /* can't fail, because instance is small */
91 r = snprintf (unit, sizeof unit, "cockpit-wsinstance-https@%s.socket", instance);
92 assert (0 < r && r < sizeof unit);
93
94 debug (FACTORY, "Requesting start of unit %s", unit);
95 r = sd_bus_call_method (bus,
96 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
97 "org.freedesktop.systemd1.Manager", "StartUnit",
98 &error, &reply, "ss", unit, "replace");
99 if (r < 0)
100 errx (EXIT_FAILURE, "Method call failed: %s", error.message);
101
102 r = sd_bus_message_read (reply, "o", &job_path);
103 if (r < 0)
104 errx (EXIT_FAILURE, "Invalid message response: %s", strerror (-r));
105
106 debug (FACTORY, " -> job is %s", job_path);
107 debug (FACTORY, "Waiting for signal.");
108
109 do
110 r = sd_bus_process (bus, NULL);
111 while (r > 0);
112 if (r < 0)
113 errx (EXIT_FAILURE, "sd_bus_process() failed: %s", strerror (-r));
114
115 struct timespec start = { 0, 0 };
116 uint64_t remaining;
117 while (job_path && get_remaining_timeout (&start, &remaining, 20 * 1000000))
118 {
119 debug (FACTORY, "sd_bus_wait(%llu)", (long long) remaining);
120 r = sd_bus_wait (bus, remaining);
121 if (r < 0)
122 errx (EXIT_FAILURE, "Error while waiting for bus: %s", strerror (-r));
123
124 debug (FACTORY, "sd_bus_process():");
125 do
126 r = sd_bus_process (bus, NULL);
127 while (r > 0);
128 if (r < 0)
129 errx (EXIT_FAILURE, "sd_bus_process() failed: %s", strerror (-r));
130 debug (FACTORY, " -> done.");
131 }
132
133 sd_bus_message_unref (reply);
134 sd_bus_close (bus);
135 sd_bus_unref (bus);
136
137 return 0;
138 }