]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-path_id.c
Merge pull request #30591 from yuwata/device-util
[thirdparty/systemd.git] / src / udev / udev-builtin-path_id.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * compose persistent device path
4 *
5 * Logic based on Hannes Reinecke's shell script.
6 */
7
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <linux/usb/ch11.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <unistd.h>
16
17 #include "alloc-util.h"
18 #include "device-util.h"
19 #include "dirent-util.h"
20 #include "fd-util.h"
21 #include "parse-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "sysexits.h"
25 #include "udev-builtin.h"
26 #include "udev-util.h"
27
28 _printf_(2,3)
29 static void path_prepend(char **path, const char *fmt, ...) {
30 va_list va;
31 _cleanup_free_ char *pre = NULL;
32 int r;
33
34 va_start(va, fmt);
35 r = vasprintf(&pre, fmt, va);
36 va_end(va);
37 if (r < 0) {
38 log_oom();
39 exit(EX_OSERR);
40 }
41
42 if (*path) {
43 char *new;
44
45 new = strjoin(pre, "-", *path);
46 if (!new) {
47 log_oom();
48 exit(EX_OSERR);
49 }
50
51 free_and_replace(*path, new);
52 } else
53 *path = TAKE_PTR(pre);
54 }
55
56 /*
57 ** Linux only supports 32 bit luns.
58 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
59 */
60 static int format_lun_number(sd_device *dev, char **path) {
61 const char *sysnum;
62 unsigned long lun;
63 int r;
64
65 r = sd_device_get_sysnum(dev, &sysnum);
66 if (r < 0)
67 return r;
68 if (!sysnum)
69 return -ENOENT;
70
71 r = safe_atolu_full(sysnum, 10, &lun);
72 if (r < 0)
73 return r;
74 if (lun < 256)
75 /* address method 0, peripheral device addressing with bus id of zero */
76 path_prepend(path, "lun-%lu", lun);
77 else
78 /* handle all other lun addressing methods by using a variant of the original lun format */
79 path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
80
81 return 0;
82 }
83
84 static sd_device *skip_subsystem(sd_device *dev, const char *subsys) {
85 sd_device *parent;
86
87 assert(dev);
88 assert(subsys);
89
90 /* Unlike the function name, this drops multiple parent devices EXCEPT FOR THE LAST ONE.
91 * The last one will be dropped at the end of the loop in builtin_path_id().
92 * E.g.
93 * Input: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:1.0
94 * Output: /sys/devices/pci0000:00/0000:00:14.0/usb1
95 */
96
97 for (parent = dev; ; ) {
98 if (!device_in_subsystem(parent, subsys))
99 break;
100
101 dev = parent;
102 if (sd_device_get_parent(dev, &parent) < 0)
103 break;
104 }
105
106 return dev;
107 }
108
109 static sd_device *handle_scsi_fibre_channel(sd_device *parent, char **path) {
110 sd_device *targetdev;
111 _cleanup_(sd_device_unrefp) sd_device *fcdev = NULL;
112 const char *port, *sysname;
113 _cleanup_free_ char *lun = NULL;
114
115 assert(parent);
116 assert(path);
117
118 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
119 return NULL;
120 if (sd_device_get_sysname(targetdev, &sysname) < 0)
121 return NULL;
122 if (sd_device_new_from_subsystem_sysname(&fcdev, "fc_transport", sysname) < 0)
123 return NULL;
124 if (sd_device_get_sysattr_value(fcdev, "port_name", &port) < 0)
125 return NULL;
126
127 format_lun_number(parent, &lun);
128 path_prepend(path, "fc-%s-%s", port, lun);
129 return parent;
130 }
131
132 static sd_device *handle_scsi_sas_wide_port(sd_device *parent, char **path) {
133 sd_device *targetdev, *target_parent;
134 _cleanup_(sd_device_unrefp) sd_device *sasdev = NULL;
135 const char *sas_address, *sysname;
136 _cleanup_free_ char *lun = NULL;
137
138 assert(parent);
139 assert(path);
140
141 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
142 return NULL;
143 if (sd_device_get_parent(targetdev, &target_parent) < 0)
144 return NULL;
145 if (sd_device_get_sysname(target_parent, &sysname) < 0)
146 return NULL;
147 if (sd_device_new_from_subsystem_sysname(&sasdev, "sas_device", sysname) < 0)
148 return NULL;
149 if (sd_device_get_sysattr_value(sasdev, "sas_address", &sas_address) < 0)
150 return NULL;
151
152 format_lun_number(parent, &lun);
153 path_prepend(path, "sas-%s-%s", sas_address, lun);
154 return parent;
155 }
156
157 static sd_device *handle_scsi_sas(sd_device *parent, char **path) {
158 sd_device *targetdev, *target_parent, *port, *expander;
159 _cleanup_(sd_device_unrefp) sd_device *target_sasdev = NULL, *expander_sasdev = NULL, *port_sasdev = NULL;
160 const char *sas_address = NULL;
161 const char *phy_id;
162 const char *phy_count, *sysname;
163 _cleanup_free_ char *lun = NULL;
164
165 assert(parent);
166 assert(path);
167
168 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target", &targetdev) < 0)
169 return NULL;
170 if (sd_device_get_parent(targetdev, &target_parent) < 0)
171 return NULL;
172 if (sd_device_get_sysname(target_parent, &sysname) < 0)
173 return NULL;
174 /* Get sas device */
175 if (sd_device_new_from_subsystem_sysname(&target_sasdev, "sas_device", sysname) < 0)
176 return NULL;
177 /* The next parent is sas port */
178 if (sd_device_get_parent(target_parent, &port) < 0)
179 return NULL;
180 if (sd_device_get_sysname(port, &sysname) < 0)
181 return NULL;
182 /* Get port device */
183 if (sd_device_new_from_subsystem_sysname(&port_sasdev, "sas_port", sysname) < 0)
184 return NULL;
185 if (sd_device_get_sysattr_value(port_sasdev, "num_phys", &phy_count) < 0)
186 return NULL;
187
188 /* Check if we are simple disk */
189 if (strncmp(phy_count, "1", 2) != 0)
190 return handle_scsi_sas_wide_port(parent, path);
191
192 /* Get connected phy */
193 if (sd_device_get_sysattr_value(target_sasdev, "phy_identifier", &phy_id) < 0)
194 return NULL;
195
196 /* The port's parent is either hba or expander */
197 if (sd_device_get_parent(port, &expander) < 0)
198 return NULL;
199
200 if (sd_device_get_sysname(expander, &sysname) < 0)
201 return NULL;
202 /* Get expander device */
203 if (sd_device_new_from_subsystem_sysname(&expander_sasdev, "sas_device", sysname) >= 0) {
204 /* Get expander's address */
205 if (sd_device_get_sysattr_value(expander_sasdev, "sas_address", &sas_address) < 0)
206 return NULL;
207 }
208
209 format_lun_number(parent, &lun);
210 if (sas_address)
211 path_prepend(path, "sas-exp%s-phy%s-%s", sas_address, phy_id, lun);
212 else
213 path_prepend(path, "sas-phy%s-%s", phy_id, lun);
214
215 return parent;
216 }
217
218 static sd_device *handle_scsi_iscsi(sd_device *parent, char **path) {
219 sd_device *transportdev;
220 _cleanup_(sd_device_unrefp) sd_device *sessiondev = NULL, *conndev = NULL;
221 const char *target, *connname, *addr, *port;
222 _cleanup_free_ char *lun = NULL;
223 const char *sysname, *sysnum;
224
225 assert(parent);
226 assert(path);
227
228 /* find iscsi session */
229 for (transportdev = parent; ; ) {
230
231 if (sd_device_get_parent(transportdev, &transportdev) < 0)
232 return NULL;
233 if (sd_device_get_sysname(transportdev, &sysname) < 0)
234 return NULL;
235 if (startswith(sysname, "session"))
236 break;
237 }
238
239 /* find iscsi session device */
240 if (sd_device_new_from_subsystem_sysname(&sessiondev, "iscsi_session", sysname) < 0)
241 return NULL;
242
243 if (sd_device_get_sysattr_value(sessiondev, "targetname", &target) < 0)
244 return NULL;
245
246 if (sd_device_get_sysnum(transportdev, &sysnum) < 0 || !sysnum)
247 return NULL;
248 connname = strjoina("connection", sysnum, ":0");
249 if (sd_device_new_from_subsystem_sysname(&conndev, "iscsi_connection", connname) < 0)
250 return NULL;
251
252 if (sd_device_get_sysattr_value(conndev, "persistent_address", &addr) < 0)
253 return NULL;
254 if (sd_device_get_sysattr_value(conndev, "persistent_port", &port) < 0)
255 return NULL;
256
257 format_lun_number(parent, &lun);
258 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
259 return parent;
260 }
261
262 static sd_device *handle_scsi_ata(sd_device *parent, char **path, char **compat_path) {
263 sd_device *targetdev, *target_parent;
264 _cleanup_(sd_device_unrefp) sd_device *atadev = NULL;
265 const char *port_no, *sysname, *name;
266 unsigned host, bus, target, lun;
267
268 assert(parent);
269 assert(path);
270
271 if (sd_device_get_sysname(parent, &name) < 0)
272 return NULL;
273 if (sscanf(name, "%u:%u:%u:%u", &host, &bus, &target, &lun) != 4)
274 return NULL;
275
276 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &targetdev) < 0)
277 return NULL;
278
279 if (sd_device_get_parent(targetdev, &target_parent) < 0)
280 return NULL;
281
282 if (sd_device_get_sysname(target_parent, &sysname) < 0)
283 return NULL;
284 if (sd_device_new_from_subsystem_sysname(&atadev, "ata_port", sysname) < 0)
285 return NULL;
286
287 if (sd_device_get_sysattr_value(atadev, "port_no", &port_no) < 0)
288 return NULL;
289
290 if (bus != 0)
291 /* Devices behind port multiplier have a bus != 0 */
292 path_prepend(path, "ata-%s.%u.0", port_no, bus);
293 else
294 /* Master/slave are distinguished by target id */
295 path_prepend(path, "ata-%s.%u", port_no, target);
296
297 /* old compatible persistent link for ATA devices */
298 if (compat_path)
299 path_prepend(compat_path, "ata-%s", port_no);
300
301 return parent;
302 }
303
304 static sd_device *handle_scsi_default(sd_device *parent, char **path) {
305 sd_device *hostdev;
306 int host, bus, target, lun;
307 const char *name, *base, *pos;
308 _cleanup_closedir_ DIR *dir = NULL;
309 int basenum = -1;
310
311 assert(parent);
312 assert(path);
313
314 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
315 return NULL;
316
317 if (sd_device_get_sysname(parent, &name) < 0)
318 return NULL;
319 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
320 return NULL;
321
322 /*
323 * Rebase host offset to get the local relative number
324 *
325 * Note: This is by definition racy, unreliable and too simple.
326 * Please do not copy this model anywhere. It's just a left-over
327 * from the time we had no idea how things should look like in
328 * the end.
329 *
330 * Making assumptions about a global in-kernel counter and use
331 * that to calculate a local offset is a very broken concept. It
332 * can only work as long as things are in strict order.
333 *
334 * The kernel needs to export the instance/port number of a
335 * controller directly, without the need for rebase magic like
336 * this. Manual driver unbind/bind, parallel hotplug/unplug will
337 * get into the way of this "I hope it works" logic.
338 */
339
340 if (sd_device_get_syspath(hostdev, &base) < 0)
341 return NULL;
342 pos = strrchr(base, '/');
343 if (!pos)
344 return NULL;
345
346 base = strndupa_safe(base, pos - base);
347 dir = opendir(base);
348 if (!dir)
349 return NULL;
350
351 FOREACH_DIRENT_ALL(de, dir, break) {
352 unsigned i;
353
354 if (de->d_name[0] == '.')
355 continue;
356 if (!IN_SET(de->d_type, DT_DIR, DT_LNK))
357 continue;
358 if (!startswith(de->d_name, "host"))
359 continue;
360 if (safe_atou_full(&de->d_name[4], 10, &i) < 0)
361 continue;
362 /*
363 * find the smallest number; the host really needs to export its
364 * own instance number per parent device; relying on the global host
365 * enumeration and plainly rebasing the numbers sounds unreliable
366 */
367 if (basenum == -1 || (int) i < basenum)
368 basenum = i;
369 }
370 if (basenum == -1)
371 return hostdev;
372 host -= basenum;
373
374 path_prepend(path, "scsi-%i:%i:%i:%i", host, bus, target, lun);
375 return hostdev;
376 }
377
378 static sd_device *handle_scsi_hyperv(sd_device *parent, char **path, size_t guid_str_len) {
379 sd_device *hostdev;
380 sd_device *vmbusdev;
381 const char *guid_str;
382 _cleanup_free_ char *lun = NULL;
383 char guid[39];
384
385 assert(parent);
386 assert(path);
387 assert(guid_str_len < sizeof(guid));
388
389 if (sd_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host", &hostdev) < 0)
390 return NULL;
391
392 if (sd_device_get_parent(hostdev, &vmbusdev) < 0)
393 return NULL;
394
395 if (sd_device_get_sysattr_value(vmbusdev, "device_id", &guid_str) < 0)
396 return NULL;
397
398 if (strlen(guid_str) < guid_str_len || guid_str[0] != '{' || guid_str[guid_str_len-1] != '}')
399 return NULL;
400
401 size_t k = 0;
402 for (size_t i = 1; i < guid_str_len-1; i++) {
403 if (guid_str[i] == '-')
404 continue;
405 guid[k++] = guid_str[i];
406 }
407 guid[k] = '\0';
408
409 format_lun_number(parent, &lun);
410 path_prepend(path, "vmbus-%s-%s", guid, lun);
411 return parent;
412 }
413
414 static sd_device *handle_scsi(sd_device *parent, char **path, char **compat_path, bool *supported_parent) {
415 const char *id, *name;
416
417 if (!device_is_devtype(parent, "scsi_device"))
418 return parent;
419
420 /* firewire */
421 if (sd_device_get_sysattr_value(parent, "ieee1394_id", &id) >= 0) {
422 path_prepend(path, "ieee1394-0x%s", id);
423 *supported_parent = true;
424 return skip_subsystem(parent, "scsi");
425 }
426
427 /* scsi sysfs does not have a "subsystem" for the transport */
428 if (sd_device_get_syspath(parent, &name) < 0)
429 return NULL;
430
431 if (strstr(name, "/rport-")) {
432 *supported_parent = true;
433 return handle_scsi_fibre_channel(parent, path);
434 }
435
436 if (strstr(name, "/end_device-")) {
437 *supported_parent = true;
438 return handle_scsi_sas(parent, path);
439 }
440
441 if (strstr(name, "/session")) {
442 *supported_parent = true;
443 return handle_scsi_iscsi(parent, path);
444 }
445
446 if (strstr(name, "/ata"))
447 return handle_scsi_ata(parent, path, compat_path);
448
449 if (strstr(name, "/vmbus_"))
450 return handle_scsi_hyperv(parent, path, 37);
451 else if (strstr(name, "/VMBUS"))
452 return handle_scsi_hyperv(parent, path, 38);
453
454 return handle_scsi_default(parent, path);
455 }
456
457 static sd_device *handle_cciss(sd_device *parent, char **path) {
458 const char *str;
459 unsigned controller, disk;
460
461 if (sd_device_get_sysname(parent, &str) < 0)
462 return NULL;
463 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
464 return NULL;
465
466 path_prepend(path, "cciss-disk%u", disk);
467 return skip_subsystem(parent, "cciss");
468 }
469
470 static void handle_scsi_tape(sd_device *dev, char **path) {
471 const char *name;
472
473 /* must be the last device in the syspath */
474 if (*path)
475 return;
476
477 if (sd_device_get_sysname(dev, &name) < 0)
478 return;
479
480 if (startswith(name, "nst") && strchr("lma", name[3]))
481 path_prepend(path, "nst%c", name[3]);
482 else if (startswith(name, "st") && strchr("lma", name[2]))
483 path_prepend(path, "st%c", name[2]);
484 }
485
486 static int get_usb_revision(sd_device *dev) {
487 uint8_t protocol;
488 const char *s;
489 int r;
490
491 assert(dev);
492
493 /* Returns usb revision 1, 2, or 3. */
494
495 r = sd_device_get_sysattr_value(dev, "bDeviceProtocol", &s);
496 if (r < 0)
497 return r;
498
499 r = safe_atou8_full(s, 16, &protocol);
500 if (r < 0)
501 return r;
502
503 switch (protocol) {
504 case USB_HUB_PR_HS_NO_TT: /* Full speed hub (USB1) or Hi-speed hub without TT (USB2) */
505
506 /* See speed_show() in drivers/usb/core/sysfs.c of the kernel. */
507 r = sd_device_get_sysattr_value(dev, "speed", &s);
508 if (r < 0)
509 return r;
510
511 if (streq(s, "480"))
512 return 2;
513
514 return 1;
515
516 case USB_HUB_PR_HS_SINGLE_TT: /* Hi-speed hub with single TT */
517 case USB_HUB_PR_HS_MULTI_TT: /* Hi-speed hub with multiple TT */
518 return 2;
519
520 case USB_HUB_PR_SS: /* Super speed hub */
521 return 3;
522
523 default:
524 return -EPROTONOSUPPORT;
525 }
526 }
527
528 static sd_device *handle_usb(sd_device *parent, char **path) {
529 const char *str, *port;
530 int r;
531
532 if (!device_is_devtype(parent, "usb_interface") && !device_is_devtype(parent, "usb_device"))
533 return parent;
534
535 if (sd_device_get_sysname(parent, &str) < 0)
536 return parent;
537 port = strchr(str, '-');
538 if (!port)
539 return parent;
540 port++;
541
542 parent = skip_subsystem(parent, "usb");
543 if (!parent)
544 return NULL;
545
546 /* USB host number may change across reboots (and probably even without reboot). The part after USB
547 * host number is determined by device topology and so does not change. Hence, drop the host number
548 * and always use '0' instead.
549 *
550 * xHCI host controllers may register two (or more?) USB root hubs for USB 2.0 and USB 3.0, and the
551 * sysname, whose host number replaced with 0, of a device under the hubs may conflict with others.
552 * To avoid the conflict, let's include the USB revision of the root hub to the PATH_ID.
553 * See issue https://github.com/systemd/systemd/issues/19406 for more details. */
554 r = get_usb_revision(parent);
555 if (r < 0) {
556 log_device_debug_errno(parent, r, "Failed to get the USB revision number, ignoring: %m");
557 path_prepend(path, "usb-0:%s", port);
558 } else {
559 assert(r > 0);
560 path_prepend(path, "usbv%i-0:%s", r, port);
561 }
562
563 return parent;
564 }
565
566 static sd_device *handle_bcma(sd_device *parent, char **path) {
567 const char *sysname;
568 unsigned core;
569
570 if (sd_device_get_sysname(parent, &sysname) < 0)
571 return NULL;
572 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
573 return NULL;
574
575 path_prepend(path, "bcma-%u", core);
576 return parent;
577 }
578
579 /* Handle devices of AP bus in System z platform. */
580 static sd_device *handle_ap(sd_device *parent, char **path) {
581 const char *type, *func;
582
583 assert(parent);
584 assert(path);
585
586 if (sd_device_get_sysattr_value(parent, "type", &type) >= 0 &&
587 sd_device_get_sysattr_value(parent, "ap_functions", &func) >= 0)
588 path_prepend(path, "ap-%s-%s", type, func);
589 else {
590 const char *sysname;
591
592 if (sd_device_get_sysname(parent, &sysname) >= 0)
593 path_prepend(path, "ap-%s", sysname);
594 }
595
596 return skip_subsystem(parent, "ap");
597 }
598
599 static int find_real_nvme_parent(sd_device *dev, sd_device **ret) {
600 _cleanup_(sd_device_unrefp) sd_device *nvme = NULL;
601 const char *sysname, *end, *devpath;
602 int r;
603
604 /* If the device belongs to "nvme-subsystem" (not to be confused with "nvme"), which happens when
605 * NVMe multipathing is enabled in the kernel (/sys/module/nvme_core/parameters/multipath is Y),
606 * then the syspath is something like the following:
607 * /sys/devices/virtual/nvme-subsystem/nvme-subsys0/nvme0n1
608 * Hence, we need to find the 'real parent' in "nvme" subsystem, e.g,
609 * /sys/devices/pci0000:00/0000:00:1c.4/0000:3c:00.0/nvme/nvme0 */
610
611 assert(dev);
612 assert(ret);
613
614 r = sd_device_get_sysname(dev, &sysname);
615 if (r < 0)
616 return r;
617
618 /* The sysname format of nvme block device is nvme%d[c%d]n%d[p%d], e.g. nvme0n1p2 or nvme0c1n2.
619 * (Note, nvme device with 'c' can be ignored, as they are hidden. )
620 * The sysname format of nvme subsystem device is nvme%d.
621 * See nvme_alloc_ns() and nvme_init_ctrl() in drivers/nvme/host/core.c for more details. */
622 end = startswith(sysname, "nvme");
623 if (!end)
624 return -ENXIO;
625
626 end += strspn(end, DIGITS);
627 sysname = strndupa_safe(sysname, end - sysname);
628
629 r = sd_device_new_from_subsystem_sysname(&nvme, "nvme", sysname);
630 if (r < 0)
631 return r;
632
633 r = sd_device_get_devpath(nvme, &devpath);
634 if (r < 0)
635 return r;
636
637 /* If the 'real parent' is (still) virtual, e.g. for nvmf disks, refuse to set ID_PATH. */
638 if (path_startswith(devpath, "/devices/virtual/"))
639 return -ENXIO;
640
641 *ret = TAKE_PTR(nvme);
642 return 0;
643 }
644
645 static void add_id_with_usb_revision(sd_device *dev, bool test, char *path) {
646 char *p;
647 int r;
648
649 assert(dev);
650 assert(path);
651
652 /* When the path contains the USB revision, let's adds ID_PATH_WITH_USB_REVISION property and
653 * drop the version specifier for later use. */
654
655 p = strstrafter(path, "-usbv");
656 if (!p)
657 return;
658 if (!ascii_isdigit(p[0]))
659 return;
660 if (p[1] != '-')
661 return;
662
663 r = udev_builtin_add_property(dev, test, "ID_PATH_WITH_USB_REVISION", path);
664 if (r < 0)
665 log_device_debug_errno(dev, r, "Failed to add ID_PATH_WITH_USB_REVISION property, ignoring: %m");
666
667 /* Drop the USB revision specifier for backward compatibility. */
668 memmove(p - 1, p + 1, strlen(p + 1) + 1);
669 }
670
671 static void add_id_tag(sd_device *dev, bool test, const char *path) {
672 char tag[UDEV_NAME_SIZE];
673 size_t i = 0;
674 int r;
675
676 /* compose valid udev tag name */
677 for (const char *p = path; *p; p++) {
678 if (ascii_isdigit(*p) ||
679 ascii_isalpha(*p) ||
680 *p == '-') {
681 tag[i++] = *p;
682 continue;
683 }
684
685 /* skip all leading '_' */
686 if (i == 0)
687 continue;
688
689 /* avoid second '_' */
690 if (tag[i-1] == '_')
691 continue;
692
693 tag[i++] = '_';
694 }
695 /* strip trailing '_' */
696 while (i > 0 && tag[i-1] == '_')
697 i--;
698 tag[i] = '\0';
699
700 r = udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
701 if (r < 0)
702 log_device_debug_errno(dev, r, "Failed to add ID_PATH_TAG property, ignoring: %m");
703 }
704
705 static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test) {
706 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
707 _cleanup_(sd_device_unrefp) sd_device *dev_other_branch = NULL;
708 _cleanup_free_ char *path = NULL, *compat_path = NULL;
709 bool supported_transport = false, supported_parent = false;
710 int r;
711
712 /* walk up the chain of devices and compose path */
713 for (sd_device *parent = dev; parent; ) {
714 const char *sysname;
715
716 if (sd_device_get_sysname(parent, &sysname) < 0) {
717 ;
718 } else if (device_in_subsystem(parent, "scsi_tape")) {
719 handle_scsi_tape(parent, &path);
720 } else if (device_in_subsystem(parent, "scsi")) {
721 parent = handle_scsi(parent, &path, &compat_path, &supported_parent);
722 supported_transport = true;
723 } else if (device_in_subsystem(parent, "cciss")) {
724 parent = handle_cciss(parent, &path);
725 supported_transport = true;
726 } else if (device_in_subsystem(parent, "usb")) {
727 parent = handle_usb(parent, &path);
728 supported_transport = true;
729 } else if (device_in_subsystem(parent, "bcma")) {
730 parent = handle_bcma(parent, &path);
731 supported_transport = true;
732 } else if (device_in_subsystem(parent, "serio")) {
733 const char *sysnum;
734
735 if (sd_device_get_sysnum(parent, &sysnum) >= 0 && sysnum) {
736 path_prepend(&path, "serio-%s", sysnum);
737 parent = skip_subsystem(parent, "serio");
738 }
739 } else if (device_in_subsystem(parent, "pci")) {
740 path_prepend(&path, "pci-%s", sysname);
741 if (compat_path)
742 path_prepend(&compat_path, "pci-%s", sysname);
743 parent = skip_subsystem(parent, "pci");
744 supported_parent = true;
745 } else if (device_in_subsystem(parent, "platform")) {
746 path_prepend(&path, "platform-%s", sysname);
747 if (compat_path)
748 path_prepend(&compat_path, "platform-%s", sysname);
749 parent = skip_subsystem(parent, "platform");
750 supported_transport = true;
751 supported_parent = true;
752 } else if (device_in_subsystem(parent, "amba")) {
753 path_prepend(&path, "amba-%s", sysname);
754 if (compat_path)
755 path_prepend(&compat_path, "amba-%s", sysname);
756 parent = skip_subsystem(parent, "amba");
757 supported_transport = true;
758 supported_parent = true;
759 } else if (device_in_subsystem(parent, "acpi")) {
760 path_prepend(&path, "acpi-%s", sysname);
761 if (compat_path)
762 path_prepend(&compat_path, "acpi-%s", sysname);
763 parent = skip_subsystem(parent, "acpi");
764 supported_parent = true;
765 } else if (device_in_subsystem(parent, "xen")) {
766 path_prepend(&path, "xen-%s", sysname);
767 if (compat_path)
768 path_prepend(&compat_path, "xen-%s", sysname);
769 parent = skip_subsystem(parent, "xen");
770 supported_parent = true;
771 } else if (device_in_subsystem(parent, "virtio")) {
772 parent = skip_subsystem(parent, "virtio");
773 supported_transport = true;
774 } else if (device_in_subsystem(parent, "scm")) {
775 path_prepend(&path, "scm-%s", sysname);
776 if (compat_path)
777 path_prepend(&compat_path, "scm-%s", sysname);
778 parent = skip_subsystem(parent, "scm");
779 supported_transport = true;
780 supported_parent = true;
781 } else if (device_in_subsystem(parent, "ccw")) {
782 path_prepend(&path, "ccw-%s", sysname);
783 if (compat_path)
784 path_prepend(&compat_path, "ccw-%s", sysname);
785 parent = skip_subsystem(parent, "ccw");
786 supported_transport = true;
787 supported_parent = true;
788 } else if (device_in_subsystem(parent, "ccwgroup")) {
789 path_prepend(&path, "ccwgroup-%s", sysname);
790 if (compat_path)
791 path_prepend(&compat_path, "ccwgroup-%s", sysname);
792 parent = skip_subsystem(parent, "ccwgroup");
793 supported_transport = true;
794 supported_parent = true;
795 } else if (device_in_subsystem(parent, "ap")) {
796 parent = handle_ap(parent, &path);
797 supported_transport = true;
798 supported_parent = true;
799 } else if (device_in_subsystem(parent, "iucv")) {
800 path_prepend(&path, "iucv-%s", sysname);
801 if (compat_path)
802 path_prepend(&compat_path, "iucv-%s", sysname);
803 parent = skip_subsystem(parent, "iucv");
804 supported_transport = true;
805 supported_parent = true;
806 } else if (device_in_subsystem(parent, "nvme") || device_in_subsystem(parent, "nvme-subsystem")) {
807 const char *nsid;
808
809 if (sd_device_get_sysattr_value(dev, "nsid", &nsid) >= 0) {
810 path_prepend(&path, "nvme-%s", nsid);
811 if (compat_path)
812 path_prepend(&compat_path, "nvme-%s", nsid);
813
814 if (device_in_subsystem(parent, "nvme-subsystem")) {
815 r = find_real_nvme_parent(dev, &dev_other_branch);
816 if (r < 0)
817 return r;
818
819 parent = dev_other_branch;
820 }
821
822 parent = skip_subsystem(parent, "nvme");
823 supported_parent = true;
824 supported_transport = true;
825 }
826 } else if (device_in_subsystem(parent, "spi")) {
827 const char *sysnum;
828
829 if (sd_device_get_sysnum(parent, &sysnum) >= 0 && sysnum) {
830 path_prepend(&path, "cs-%s", sysnum);
831 parent = skip_subsystem(parent, "spi");
832 }
833 }
834
835 if (!parent)
836 break;
837 if (sd_device_get_parent(parent, &parent) < 0)
838 break;
839 }
840
841 if (!path)
842 return -ENOENT;
843
844 /*
845 * Do not return devices with an unknown parent device type. They
846 * might produce conflicting IDs if the parent does not provide a
847 * unique and predictable name.
848 */
849 if (!supported_parent)
850 return -ENOENT;
851
852 /*
853 * Do not return block devices without a well-known transport. Some
854 * devices do not expose their buses and do not provide a unique
855 * and predictable name that way.
856 */
857 if (device_in_subsystem(dev, "block") && !supported_transport)
858 return -ENOENT;
859
860 add_id_with_usb_revision(dev, test, path);
861
862 r = udev_builtin_add_property(dev, test, "ID_PATH", path);
863 if (r < 0)
864 log_device_debug_errno(dev, r, "Failed to add ID_PATH property, ignoring: %m");
865
866 add_id_tag(dev, test, path);
867
868 /*
869 * Compatible link generation for ATA devices
870 * we assign compat_link to the env variable
871 * ID_PATH_ATA_COMPAT
872 */
873 if (compat_path)
874 udev_builtin_add_property(dev, test, "ID_PATH_ATA_COMPAT", compat_path);
875
876 return 0;
877 }
878
879 const UdevBuiltin udev_builtin_path_id = {
880 .name = "path_id",
881 .cmd = builtin_path_id,
882 .help = "Compose persistent device path",
883 .run_once = true,
884 };