| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
#include "sd-device.h" |
| 7 |
#include "sd-event.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "bus-message-util.h" |
| 11 |
#include "device-util.h" |
| 12 |
#include "event-util.h" |
| 13 |
#include "format-util.h" |
| 14 |
#include "hash-funcs.h" |
| 15 |
#include "logind.h" |
| 16 |
#include "logind-brightness.h" |
| 17 |
#include "process-util.h" |
| 18 |
#include "set.h" |
| 19 |
#include "stdio-util.h" |
| 20 |
|
| 21 |
/* Brightness and LED devices tend to be very slow to write to (often being I2C and such). Writes to the |
| 22 |
* sysfs attributes are synchronous, and hence will freeze our process on access. We can't really have that, |
| 23 |
* hence we add some complexity: whenever we need to write to the brightness attribute, we do so in a forked |
| 24 |
* off process, which terminates when it is done. Watching that process allows us to watch completion of the |
| 25 |
* write operation. |
| 26 |
* |
| 27 |
* To make this even more complex: clients are likely to send us many write requests in a short time-frame |
| 28 |
* (because they implement reactive brightness sliders on screen). Let's coalesce writes to make this |
| 29 |
* efficient: whenever we get requests to change brightness while we are still writing to the brightness |
| 30 |
* attribute, let's remember the request and restart a new one when the initial operation finished. When we |
| 31 |
* get another request while one is ongoing and one is pending we'll replace the pending one with the new |
| 32 |
* one. |
| 33 |
* |
| 34 |
* The bus messages are answered when the first write operation finishes that started either due to the |
| 35 |
* request or due to a later request that overrode the requested one. |
| 36 |
* |
| 37 |
* Yes, this is complex, but I don't see an easier way if we want to be both efficient and still support |
| 38 |
* completion notification. */ |
| 39 |
|
| 40 |
typedef struct BrightnessWriter { |
| 41 |
Manager *manager; |
| 42 |
|
| 43 |
sd_device *device; |
| 44 |
char *path; |
| 45 |
|
| 46 |
uint32_t brightness; |
| 47 |
bool again; |
| 48 |
|
| 49 |
Set *current_messages; |
| 50 |
Set *pending_messages; |
| 51 |
|
| 52 |
sd_event_source* child_event_source; |
| 53 |
} BrightnessWriter; |
| 54 |
|
| 55 |
static BrightnessWriter* brightness_writer_free(BrightnessWriter *w) { |
| 56 |
if (!w) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
if (w->manager && w->path) |
| 60 |
(void) hashmap_remove_value(w->manager->brightness_writers, w->path, w); |
| 61 |
|
| 62 |
sd_device_unref(w->device); |
| 63 |
free(w->path); |
| 64 |
|
| 65 |
set_free(w->current_messages); |
| 66 |
set_free(w->pending_messages); |
| 67 |
|
| 68 |
w->child_event_source = sd_event_source_unref(w->child_event_source); |
| 69 |
|
| 70 |
return mfree(w); |
| 71 |
} |
| 72 |
|
| 73 |
DEFINE_TRIVIAL_CLEANUP_FUNC(BrightnessWriter*, brightness_writer_free); |
| 74 |
|
| 75 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 76 |
brightness_writer_hash_ops, |
| 77 |
char, |
| 78 |
string_hash_func, |
| 79 |
string_compare_func, |
| 80 |
BrightnessWriter, |
| 81 |
brightness_writer_free); |
| 82 |
|
| 83 |
static void brightness_writer_reply(BrightnessWriter *w, int error) { |
| 84 |
int r; |
| 85 |
|
| 86 |
assert(w); |
| 87 |
|
| 88 |
for (;;) { |
| 89 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 90 |
|
| 91 |
m = set_steal_first(w->current_messages); |
| 92 |
if (!m) |
| 93 |
break; |
| 94 |
|
| 95 |
if (error == 0) |
| 96 |
r = sd_bus_reply_method_return(m, NULL); |
| 97 |
else |
| 98 |
r = sd_bus_reply_method_errnof(m, error, "Failed to write to brightness device: %m"); |
| 99 |
if (r < 0) |
| 100 |
log_warning_errno(r, "Failed to send method reply, ignoring: %m"); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static int brightness_writer_fork(BrightnessWriter *w); |
| 105 |
|
| 106 |
static int on_brightness_writer_exit(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 107 |
BrightnessWriter *w = ASSERT_PTR(userdata); |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(s); |
| 111 |
assert(si); |
| 112 |
|
| 113 |
w->child_event_source = sd_event_source_unref(w->child_event_source); |
| 114 |
|
| 115 |
brightness_writer_reply(w, |
| 116 |
si->si_code == CLD_EXITED && |
| 117 |
si->si_status == EXIT_SUCCESS ? 0 : -EPROTO); |
| 118 |
|
| 119 |
if (w->again) { |
| 120 |
/* Another request to change the brightness has been queued. Act on it, but make the pending |
| 121 |
* messages the current ones. */ |
| 122 |
w->again = false; |
| 123 |
set_free(w->current_messages); |
| 124 |
w->current_messages = TAKE_PTR(w->pending_messages); |
| 125 |
|
| 126 |
r = brightness_writer_fork(w); |
| 127 |
if (r >= 0) |
| 128 |
return 0; |
| 129 |
|
| 130 |
brightness_writer_reply(w, r); |
| 131 |
} |
| 132 |
|
| 133 |
brightness_writer_free(w); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int brightness_writer_fork(BrightnessWriter *w) { |
| 138 |
int r; |
| 139 |
|
| 140 |
assert(w); |
| 141 |
assert(w->manager); |
| 142 |
assert(!w->child_event_source); |
| 143 |
|
| 144 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 145 |
r = pidref_safe_fork( |
| 146 |
"(sd-bright)", |
| 147 |
FORK_DEATHSIG_SIGKILL|FORK_REARRANGE_STDIO|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_REOPEN_LOG, |
| 148 |
&pidref); |
| 149 |
if (r < 0) |
| 150 |
return r; |
| 151 |
if (r == 0) { |
| 152 |
char brs[DECIMAL_STR_MAX(uint32_t)+1]; |
| 153 |
|
| 154 |
/* Child */ |
| 155 |
xsprintf(brs, "%" PRIu32, w->brightness); |
| 156 |
|
| 157 |
r = sd_device_set_sysattr_value(w->device, "brightness", brs); |
| 158 |
if (r < 0) { |
| 159 |
log_device_error_errno(w->device, r, "Failed to write brightness to device: %m"); |
| 160 |
_exit(EXIT_FAILURE); |
| 161 |
} |
| 162 |
|
| 163 |
_exit(EXIT_SUCCESS); |
| 164 |
} |
| 165 |
|
| 166 |
r = event_add_child_pidref(w->manager->event, &w->child_event_source, &pidref, WEXITED, on_brightness_writer_exit, w); |
| 167 |
if (r < 0) |
| 168 |
return log_error_errno(r, "Failed to watch brightness writer child " PID_FMT ": %m", pidref.pid); |
| 169 |
|
| 170 |
r = sd_event_source_set_child_process_own(w->child_event_source, true); |
| 171 |
if (r < 0) |
| 172 |
return log_error_errno(r, "Failed to take ownership of child process: %m"); |
| 173 |
|
| 174 |
pidref_done(&pidref); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static int set_add_message(Set **set, sd_bus_message *message) { |
| 180 |
int r; |
| 181 |
|
| 182 |
assert(set); |
| 183 |
|
| 184 |
if (!message) |
| 185 |
return 0; |
| 186 |
|
| 187 |
r = sd_bus_message_get_expect_reply(message); |
| 188 |
if (r <= 0) |
| 189 |
return r; |
| 190 |
|
| 191 |
r = set_ensure_consume(set, &bus_message_hash_ops, sd_bus_message_ref(message)); |
| 192 |
if (r <= 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
int manager_write_brightness( |
| 199 |
Manager *m, |
| 200 |
sd_device *device, |
| 201 |
uint32_t brightness, |
| 202 |
sd_bus_message *message) { |
| 203 |
|
| 204 |
_cleanup_(brightness_writer_freep) BrightnessWriter *w = NULL; |
| 205 |
BrightnessWriter *existing; |
| 206 |
const char *path; |
| 207 |
int r; |
| 208 |
|
| 209 |
assert(m); |
| 210 |
assert(device); |
| 211 |
|
| 212 |
r = sd_device_get_syspath(device, &path); |
| 213 |
if (r < 0) |
| 214 |
return log_device_error_errno(device, r, "Failed to get sysfs path for brightness device: %m"); |
| 215 |
|
| 216 |
existing = hashmap_get(m->brightness_writers, path); |
| 217 |
if (existing) { |
| 218 |
/* There's already a writer for this device. Let's update it with the new brightness, and add |
| 219 |
* our message to the set of message to reply when done. */ |
| 220 |
|
| 221 |
r = set_add_message(&existing->pending_messages, message); |
| 222 |
if (r < 0) |
| 223 |
return log_error_errno(r, "Failed to add message to set: %m"); |
| 224 |
|
| 225 |
/* We override any previously requested brightness here: we coalesce writes, and the newest |
| 226 |
* requested brightness is the one we'll put into effect. */ |
| 227 |
existing->brightness = brightness; |
| 228 |
existing->again = true; /* request another iteration of the writer when the current one is |
| 229 |
* complete */ |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
w = new(BrightnessWriter, 1); |
| 234 |
if (!w) |
| 235 |
return log_oom(); |
| 236 |
|
| 237 |
*w = (BrightnessWriter) { |
| 238 |
.device = sd_device_ref(device), |
| 239 |
.path = strdup(path), |
| 240 |
.brightness = brightness, |
| 241 |
}; |
| 242 |
|
| 243 |
if (!w->path) |
| 244 |
return log_oom(); |
| 245 |
|
| 246 |
r = hashmap_ensure_put(&m->brightness_writers, &brightness_writer_hash_ops, w->path, w); |
| 247 |
if (r == -ENOMEM) |
| 248 |
return log_oom(); |
| 249 |
if (r < 0) |
| 250 |
return log_error_errno(r, "Failed to add brightness writer to hashmap: %m"); |
| 251 |
|
| 252 |
w->manager = m; |
| 253 |
|
| 254 |
r = set_add_message(&w->current_messages, message); |
| 255 |
if (r < 0) |
| 256 |
return log_error_errno(r, "Failed to add message to set: %m"); |
| 257 |
|
| 258 |
r = brightness_writer_fork(w); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
TAKE_PTR(w); |
| 263 |
return 0; |
| 264 |
} |