| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <linux/fs.h> |
| 4 |
#include <sys/file.h> |
| 5 |
#include <sys/ioctl.h> |
| 6 |
|
| 7 |
#include "sd-daemon.h" |
| 8 |
#include "sd-event.h" |
| 9 |
#include "sd-netlink.h" |
| 10 |
|
| 11 |
#include "blockdev-util.h" |
| 12 |
#include "device-monitor-private.h" |
| 13 |
#include "device-private.h" |
| 14 |
#include "device-util.h" |
| 15 |
#include "errno-list.h" |
| 16 |
#include "errno-util.h" |
| 17 |
#include "fd-util.h" |
| 18 |
#include "hashmap.h" |
| 19 |
#include "process-util.h" |
| 20 |
#include "udev-error.h" |
| 21 |
#include "udev-event.h" |
| 22 |
#include "udev-rules.h" |
| 23 |
#include "udev-spawn.h" |
| 24 |
#include "udev-trace.h" |
| 25 |
#include "udev-util.h" |
| 26 |
#include "udev-watch.h" |
| 27 |
#include "udev-worker.h" |
| 28 |
|
| 29 |
void udev_worker_done(UdevWorker *worker) { |
| 30 |
assert(worker); |
| 31 |
|
| 32 |
sd_event_unref(worker->event); |
| 33 |
sd_netlink_unref(worker->rtnl); |
| 34 |
sd_device_monitor_unref(worker->monitor); |
| 35 |
hashmap_free(worker->properties); |
| 36 |
udev_rules_free(worker->rules); |
| 37 |
} |
| 38 |
|
| 39 |
int udev_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname) { |
| 40 |
const char *val; |
| 41 |
int r; |
| 42 |
|
| 43 |
assert(dev); |
| 44 |
|
| 45 |
if (device_for_action(dev, SD_DEVICE_REMOVE)) |
| 46 |
goto irrelevant; |
| 47 |
|
| 48 |
/* Exclude the following devices: |
| 49 |
* For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494. |
| 50 |
* For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0, |
| 51 |
* but not sure the assumption is still valid even when partitions are created on the md |
| 52 |
* devices, surprisingly which seems to be possible, see PR #22973. |
| 53 |
* For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */ |
| 54 |
r = device_sysname_startswith(dev, "dm-", "md", "drbd"); |
| 55 |
if (r < 0) |
| 56 |
return log_device_debug_errno(dev, r, "Failed to check sysname: %m"); |
| 57 |
if (r > 0) |
| 58 |
goto irrelevant; |
| 59 |
|
| 60 |
r = block_device_get_whole_disk(dev, &dev); |
| 61 |
if (IN_SET(r, |
| 62 |
-ENOTBLK, /* The device is not a block device. */ |
| 63 |
-ENODEV /* The whole disk device was not found, it may already be removed. */)) |
| 64 |
goto irrelevant; |
| 65 |
if (r < 0) |
| 66 |
return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); |
| 67 |
|
| 68 |
r = sd_device_get_devname(dev, &val); |
| 69 |
if (r < 0) |
| 70 |
return log_device_debug_errno(dev, r, "Failed to get devname: %m"); |
| 71 |
|
| 72 |
if (ret_device) |
| 73 |
*ret_device = dev; |
| 74 |
if (ret_devname) |
| 75 |
*ret_devname = val; |
| 76 |
return 1; |
| 77 |
|
| 78 |
irrelevant: |
| 79 |
if (ret_device) |
| 80 |
*ret_device = NULL; |
| 81 |
if (ret_devname) |
| 82 |
*ret_devname = NULL; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int worker_lock_whole_disk(UdevWorker *worker, sd_device *dev, int *ret_fd) { |
| 87 |
_cleanup_close_ int fd = -EBADF; |
| 88 |
sd_device *dev_whole_disk; |
| 89 |
const char *whole_disk; |
| 90 |
int r; |
| 91 |
|
| 92 |
assert(worker); |
| 93 |
assert(dev); |
| 94 |
assert(ret_fd); |
| 95 |
|
| 96 |
/* Take a shared lock on the device node; this establishes a concept of device "ownership" to |
| 97 |
* serialize device access. External processes holding an exclusive lock will cause udev to skip the |
| 98 |
* event handling; in the case udev acquired the lock, the external process can block until udev has |
| 99 |
* finished its event handling. */ |
| 100 |
|
| 101 |
/* Do not try to lock device on remove event, as the device node specified by DEVNAME= has already |
| 102 |
* been removed, and may already be assigned to another device. Consider the case e.g. a USB stick |
| 103 |
* memory was unplugged and then another one is plugged. */ |
| 104 |
if (device_for_action(dev, SD_DEVICE_REMOVE)) |
| 105 |
goto nolock; |
| 106 |
|
| 107 |
r = udev_get_whole_disk(dev, &dev_whole_disk, &whole_disk); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
if (r == 0) |
| 111 |
goto nolock; |
| 112 |
|
| 113 |
fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 114 |
if (fd < 0) { |
| 115 |
bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(fd); |
| 116 |
|
| 117 |
log_device_debug_errno(dev, fd, "Failed to open '%s'%s: %m", whole_disk, ignore ? ", ignoring" : ""); |
| 118 |
if (!ignore) |
| 119 |
return fd; |
| 120 |
|
| 121 |
goto nolock; |
| 122 |
} |
| 123 |
|
| 124 |
if (flock(fd, LOCK_SH|LOCK_NB) < 0) { |
| 125 |
if (errno != EAGAIN) |
| 126 |
return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", whole_disk); |
| 127 |
|
| 128 |
log_device_debug_errno(dev, errno, "Block device %s is currently locked, requeuing the event.", whole_disk); |
| 129 |
|
| 130 |
r = sd_notifyf(/* unset_environment= */ false, "TRY_AGAIN=1\nWHOLE_DISK=%s", whole_disk); |
| 131 |
if (r < 0) { |
| 132 |
log_device_warning_errno(dev, r, "Failed to send notification message to manager process: %m"); |
| 133 |
(void) sd_event_exit(worker->event, r); |
| 134 |
} |
| 135 |
|
| 136 |
return -EAGAIN; |
| 137 |
} |
| 138 |
|
| 139 |
log_device_debug(dev, "Successfully took flock(LOCK_SH) for %s, it will be released after the event has been processed.", whole_disk); |
| 140 |
*ret_fd = TAKE_FD(fd); |
| 141 |
return 1; |
| 142 |
|
| 143 |
nolock: |
| 144 |
*ret_fd = -EBADF; |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int worker_mark_block_device_read_only(sd_device *dev) { |
| 149 |
int r; |
| 150 |
|
| 151 |
assert(dev); |
| 152 |
|
| 153 |
/* Do this only once, when the block device is new. If the device is later retriggered let's not |
| 154 |
* toggle the bit again, so that people can boot up with full read-only mode and then unset the bit |
| 155 |
* for specific devices only. */ |
| 156 |
if (!device_for_action(dev, SD_DEVICE_ADD)) |
| 157 |
return 0; |
| 158 |
|
| 159 |
r = device_in_subsystem(dev, "block"); |
| 160 |
if (r <= 0) |
| 161 |
return r; |
| 162 |
|
| 163 |
/* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification |
| 164 |
* of physical devices, and what sits on top of those doesn't really matter if we don't allow the |
| 165 |
* underlying block devices to receive changes. */ |
| 166 |
r = device_sysname_startswith(dev, "dm-", "md", "drbd", "loop", "nbd", "zram"); |
| 167 |
if (r < 0) |
| 168 |
return log_device_debug_errno(dev, r, "Failed to check sysname: %m"); |
| 169 |
if (r > 0) |
| 170 |
return 0; |
| 171 |
|
| 172 |
const char *node; |
| 173 |
r = sd_device_get_devname(dev, &node); |
| 174 |
if (r < 0) |
| 175 |
return log_device_debug_errno(dev, r, "Failed to get device node: %m"); |
| 176 |
|
| 177 |
_cleanup_close_ int fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 178 |
if (fd < 0) { |
| 179 |
bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(fd); |
| 180 |
log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd, |
| 181 |
"Failed to open device node '%s'%s: %m", |
| 182 |
node, ignore ? ", ignoring" : ""); |
| 183 |
return ignore ? 0 : fd; |
| 184 |
} |
| 185 |
|
| 186 |
int state = 1; |
| 187 |
if (ioctl(fd, BLKROSET, &state) < 0) |
| 188 |
return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", node); |
| 189 |
|
| 190 |
log_device_info(dev, "Successfully marked block device '%s' read-only.", node); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
static int worker_process_device(UdevWorker *worker, sd_device *dev) { |
| 195 |
int r; |
| 196 |
|
| 197 |
assert(worker); |
| 198 |
assert(dev); |
| 199 |
|
| 200 |
log_device_uevent(dev, "Processing device"); |
| 201 |
|
| 202 |
/* If this is a block device and the device is locked currently via the BSD advisory locks, |
| 203 |
* someone else is using it exclusively. We don't run our udev rules now to not interfere. |
| 204 |
* Instead of processing the event, we requeue the event and will try again after a delay. |
| 205 |
* |
| 206 |
* The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */ |
| 207 |
_cleanup_close_ int fd_lock = -EBADF; |
| 208 |
r = worker_lock_whole_disk(worker, dev, &fd_lock); |
| 209 |
if (r == -EAGAIN) |
| 210 |
return 0; |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
|
| 214 |
if (worker->config.blockdev_read_only) |
| 215 |
(void) worker_mark_block_device_read_only(dev); |
| 216 |
|
| 217 |
/* Disable watch during event processing. */ |
| 218 |
r = udev_watch_end(worker, dev); |
| 219 |
if (r < 0) |
| 220 |
log_device_warning_errno(dev, r, "Failed to remove inotify watch, ignoring: %m"); |
| 221 |
|
| 222 |
_cleanup_(udev_event_unrefp) UdevEvent *udev_event = udev_event_new(dev, worker, EVENT_UDEV_WORKER); |
| 223 |
if (!udev_event) |
| 224 |
return -ENOMEM; |
| 225 |
udev_event->trace = worker->config.trace; |
| 226 |
|
| 227 |
/* apply rules, create node, symlinks */ |
| 228 |
r = udev_event_execute_rules(udev_event, worker->rules); |
| 229 |
if (r < 0) |
| 230 |
return r; |
| 231 |
|
| 232 |
/* Process RUN=. */ |
| 233 |
udev_event_execute_run(udev_event); |
| 234 |
|
| 235 |
if (!worker->rtnl) |
| 236 |
/* in case rtnl was initialized */ |
| 237 |
worker->rtnl = sd_netlink_ref(udev_event->rtnl); |
| 238 |
|
| 239 |
/* Enable watch if requested. */ |
| 240 |
if (udev_event->inotify_watch) { |
| 241 |
r = udev_watch_begin(worker, dev); |
| 242 |
if (r < 0) |
| 243 |
log_device_warning_errno(dev, r, "Failed to add inotify watch, ignoring: %m"); |
| 244 |
} |
| 245 |
|
| 246 |
/* Finalize database. But do not re-create database on remove, which has been already removed in |
| 247 |
* event_execute_rules_on_remove(). */ |
| 248 |
if (!device_for_action(dev, SD_DEVICE_REMOVE)) { |
| 249 |
r = device_add_property(dev, "ID_PROCESSING", NULL); |
| 250 |
if (r < 0) |
| 251 |
return log_device_warning_errno(dev, r, "Failed to remove 'ID_PROCESSING' property: %m"); |
| 252 |
|
| 253 |
r = device_update_db(dev); |
| 254 |
if (r < 0) |
| 255 |
return log_device_warning_errno(dev, r, "Failed to update database under /run/udev/data/: %m"); |
| 256 |
} |
| 257 |
|
| 258 |
log_device_uevent(dev, "Device processed"); |
| 259 |
|
| 260 |
/* send processed event to libudev listeners */ |
| 261 |
r = device_monitor_send(worker->monitor, NULL, dev); |
| 262 |
if (r < 0) { |
| 263 |
log_device_warning_errno(dev, r, "Failed to broadcast event to libudev listeners: %m"); |
| 264 |
(void) sd_event_exit(worker->event, r); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
r = sd_notify(/* unset_environment= */ false, "PROCESSED=1"); |
| 269 |
if (r < 0) { |
| 270 |
log_device_warning_errno(dev, r, "Failed to send notification message to manager process: %m"); |
| 271 |
(void) sd_event_exit(worker->event, r); |
| 272 |
} |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
| 278 |
UdevWorker *worker = ASSERT_PTR(userdata); |
| 279 |
int r; |
| 280 |
|
| 281 |
assert(monitor); |
| 282 |
assert(dev); |
| 283 |
|
| 284 |
r = worker_process_device(worker, dev); |
| 285 |
if (r < 0) { |
| 286 |
log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); |
| 287 |
(void) device_add_errno(dev, r); |
| 288 |
|
| 289 |
/* broadcast (possibly partially processed) event to libudev listeners */ |
| 290 |
int k = device_monitor_send(monitor, NULL, dev); |
| 291 |
if (k < 0) { |
| 292 |
log_device_warning_errno(dev, k, "Failed to broadcast event to libudev listeners: %m"); |
| 293 |
(void) sd_event_exit(worker->event, k); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
const char *e = errno_name_no_fallback(r); |
| 298 |
r = sd_notifyf(/* unset_environment= */ false, "ERRNO=%i%s%s", -r, e ? "\nERRNO_NAME=" : "", strempty(e)); |
| 299 |
if (r < 0) { |
| 300 |
log_device_warning_errno(dev, r, "Failed to send notification message to manager process, ignoring: %m"); |
| 301 |
(void) sd_event_exit(worker->event, r); |
| 302 |
return 0; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/* Reset the log level, as it might be changed by "OPTIONS=log_level=". */ |
| 307 |
log_set_max_level(worker->config.log_level); |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
int udev_worker_main(UdevWorker *worker, sd_device *dev) { |
| 313 |
int r; |
| 314 |
|
| 315 |
assert(worker); |
| 316 |
assert(worker->monitor); |
| 317 |
assert(dev); |
| 318 |
|
| 319 |
DEVICE_TRACE_POINT(worker_spawned, dev, getpid_cached()); |
| 320 |
|
| 321 |
/* Reset OOM score, we only protect the main daemon. */ |
| 322 |
r = set_oom_score_adjust(0); |
| 323 |
if (r < 0) |
| 324 |
log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); |
| 325 |
|
| 326 |
r = sd_event_new(&worker->event); |
| 327 |
if (r < 0) |
| 328 |
return log_error_errno(r, "Failed to allocate event loop: %m"); |
| 329 |
|
| 330 |
r = sd_event_add_signal(worker->event, NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, NULL, NULL); |
| 331 |
if (r < 0) |
| 332 |
return log_error_errno(r, "Failed to set SIGTERM event: %m"); |
| 333 |
|
| 334 |
r = sd_device_monitor_attach_event(worker->monitor, worker->event); |
| 335 |
if (r < 0) |
| 336 |
return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); |
| 337 |
|
| 338 |
r = sd_device_monitor_start(worker->monitor, worker_device_monitor_handler, worker); |
| 339 |
if (r < 0) |
| 340 |
return log_error_errno(r, "Failed to start device monitor: %m"); |
| 341 |
|
| 342 |
/* Process first device */ |
| 343 |
(void) worker_device_monitor_handler(worker->monitor, dev, worker); |
| 344 |
|
| 345 |
r = sd_event_loop(worker->event); |
| 346 |
if (r < 0) |
| 347 |
return log_error_errno(r, "Event loop failed: %m"); |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |