]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-path_id.c
tree-wide: replace 'unsigned int' with 'unsigned'
[thirdparty/systemd.git] / src / udev / udev-builtin-path_id.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
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 <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "alloc-util.h"
19 #include "dirent-util.h"
20 #include "fd-util.h"
21 #include "libudev-private.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "sysexits.h"
25 #include "udev-builtin.h"
26
27 _printf_(2,3)
28 static void path_prepend(char **path, const char *fmt, ...) {
29 va_list va;
30 _cleanup_free_ char *pre = NULL;
31 int r;
32
33 va_start(va, fmt);
34 r = vasprintf(&pre, fmt, va);
35 va_end(va);
36 if (r < 0) {
37 log_oom();
38 exit(EX_OSERR);
39 }
40
41 if (*path) {
42 char *new;
43
44 new = strjoin(pre, "-", *path);
45 if (!new) {
46 log_oom();
47 exit(EX_OSERR);
48 }
49
50 free_and_replace(*path, new);
51 } else
52 *path = TAKE_PTR(pre);
53 }
54
55 /*
56 ** Linux only supports 32 bit luns.
57 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
58 */
59 static void format_lun_number(struct udev_device *dev, char **path) {
60 unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
61
62 if (lun < 256)
63 /* address method 0, peripheral device addressing with bus id of zero */
64 path_prepend(path, "lun-%lu", lun);
65 else
66 /* handle all other lun addressing methods by using a variant of the original lun format */
67 path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
68 }
69
70 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
71 struct udev_device *parent = dev;
72
73 assert(dev);
74 assert(subsys);
75
76 while (parent) {
77 const char *subsystem;
78
79 subsystem = udev_device_get_subsystem(parent);
80 if (!streq_ptr(subsystem, subsys))
81 break;
82
83 dev = parent;
84 parent = udev_device_get_parent(parent);
85 }
86
87 return dev;
88 }
89
90 static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) {
91 struct udev_device *targetdev;
92 _cleanup_(udev_device_unrefp) struct udev_device *fcdev = NULL;
93 const char *port;
94 _cleanup_free_ char *lun = NULL;
95
96 assert(parent);
97 assert(path);
98
99
100 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
101 if (!targetdev)
102 return NULL;
103
104 fcdev = udev_device_new_from_subsystem_sysname(NULL, "fc_transport", udev_device_get_sysname(targetdev));
105 if (!fcdev)
106 return NULL;
107
108 port = udev_device_get_sysattr_value(fcdev, "port_name");
109 if (!port)
110 return NULL;
111
112 format_lun_number(parent, &lun);
113 path_prepend(path, "fc-%s-%s", port, lun);
114 return parent;
115 }
116
117 static struct udev_device *handle_scsi_sas_wide_port(struct udev_device *parent, char **path) {
118 struct udev_device *targetdev, *target_parent;
119 _cleanup_(udev_device_unrefp) struct udev_device *sasdev = NULL;
120 const char *sas_address;
121 _cleanup_free_ char *lun = NULL;
122
123 assert(parent);
124 assert(path);
125
126
127 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
128 if (!targetdev)
129 return NULL;
130
131 target_parent = udev_device_get_parent(targetdev);
132 if (!target_parent)
133 return NULL;
134
135 sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device",
136 udev_device_get_sysname(target_parent));
137 if (!sasdev)
138 return NULL;
139
140 sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
141 if (!sas_address)
142 return NULL;
143
144 format_lun_number(parent, &lun);
145 path_prepend(path, "sas-%s-%s", sas_address, lun);
146 return parent;
147 }
148
149 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
150 {
151 struct udev_device *targetdev, *target_parent, *port, *expander;
152 _cleanup_(udev_device_unrefp) struct udev_device
153 *target_sasdev = NULL, *expander_sasdev = NULL, *port_sasdev = NULL;
154 const char *sas_address = NULL;
155 const char *phy_id;
156 const char *phy_count;
157 _cleanup_free_ char *lun = NULL;
158
159 assert(parent);
160 assert(path);
161
162
163 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
164 if (!targetdev)
165 return NULL;
166
167 target_parent = udev_device_get_parent(targetdev);
168 if (!target_parent)
169 return NULL;
170
171 /* Get sas device */
172 target_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device", udev_device_get_sysname(target_parent));
173 if (!target_sasdev)
174 return NULL;
175
176 /* The next parent is sas port */
177 port = udev_device_get_parent(target_parent);
178 if (!port)
179 return NULL;
180
181 /* Get port device */
182 port_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_port", udev_device_get_sysname(port));
183
184 phy_count = udev_device_get_sysattr_value(port_sasdev, "num_phys");
185 if (!phy_count)
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 phy_id = udev_device_get_sysattr_value(target_sasdev, "phy_identifier");
194 if (!phy_id)
195 return NULL;
196
197 /* The port's parent is either hba or expander */
198 expander = udev_device_get_parent(port);
199 if (!expander)
200 return NULL;
201
202 /* Get expander device */
203 expander_sasdev = udev_device_new_from_subsystem_sysname(NULL, "sas_device", udev_device_get_sysname(expander));
204 if (expander_sasdev) {
205 /* Get expander's address */
206 sas_address = udev_device_get_sysattr_value(expander_sasdev, "sas_address");
207 if (!sas_address)
208 return NULL;
209 }
210
211 format_lun_number(parent, &lun);
212 if (sas_address)
213 path_prepend(path, "sas-exp%s-phy%s-%s", sas_address, phy_id, lun);
214 else
215 path_prepend(path, "sas-phy%s-%s", phy_id, lun);
216
217 return parent;
218 }
219
220 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
221 struct udev_device *transportdev;
222 _cleanup_(udev_device_unrefp) struct udev_device
223 *sessiondev = NULL, *conndev = NULL;
224 const char *target, *connname, *addr, *port;
225 _cleanup_free_ char *lun = NULL;
226
227 assert(parent);
228 assert(path);
229
230
231 /* find iscsi session */
232 transportdev = parent;
233 for (;;) {
234 transportdev = udev_device_get_parent(transportdev);
235 if (!transportdev)
236 return NULL;
237 if (startswith(udev_device_get_sysname(transportdev), "session"))
238 break;
239 }
240
241 /* find iscsi session device */
242 sessiondev = udev_device_new_from_subsystem_sysname(NULL, "iscsi_session", udev_device_get_sysname(transportdev));
243 if (!sessiondev)
244 return NULL;
245
246 target = udev_device_get_sysattr_value(sessiondev, "targetname");
247 if (!target)
248 return NULL;
249
250 connname = strjoina("connection", udev_device_get_sysnum(transportdev), ":0");
251 conndev = udev_device_new_from_subsystem_sysname(NULL, "iscsi_connection", connname);
252 if (!conndev)
253 return NULL;
254
255 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
256 port = udev_device_get_sysattr_value(conndev, "persistent_port");
257 if (!addr || !port)
258 return NULL;
259
260 format_lun_number(parent, &lun);
261 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
262 return parent;
263 }
264
265 static struct udev_device *handle_scsi_ata(struct udev_device *parent, char **path) {
266 struct udev_device *targetdev, *target_parent;
267 _cleanup_(udev_device_unrefp) struct udev_device *atadev = NULL;
268 const char *port_no;
269
270 assert(parent);
271 assert(path);
272
273
274 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
275 if (!targetdev)
276 return NULL;
277
278 target_parent = udev_device_get_parent(targetdev);
279 if (!target_parent)
280 return NULL;
281
282 atadev = udev_device_new_from_subsystem_sysname(NULL, "ata_port", udev_device_get_sysname(target_parent));
283 if (!atadev)
284 return NULL;
285
286 port_no = udev_device_get_sysattr_value(atadev, "port_no");
287 if (!port_no)
288 return NULL;
289
290 path_prepend(path, "ata-%s", port_no);
291 return parent;
292 }
293
294 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
295 struct udev_device *hostdev;
296 int host, bus, target, lun;
297 const char *name, *base, *pos;
298 _cleanup_closedir_ DIR *dir = NULL;
299 struct dirent *dent;
300 int basenum = -1;
301
302 assert(parent);
303 assert(path);
304
305 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
306 if (!hostdev)
307 return NULL;
308
309 name = udev_device_get_sysname(parent);
310 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
311 return NULL;
312
313 /*
314 * Rebase host offset to get the local relative number
315 *
316 * Note: This is by definition racy, unreliable and too simple.
317 * Please do not copy this model anywhere. It's just a left-over
318 * from the time we had no idea how things should look like in
319 * the end.
320 *
321 * Making assumptions about a global in-kernel counter and use
322 * that to calculate a local offset is a very broken concept. It
323 * can only work as long as things are in strict order.
324 *
325 * The kernel needs to export the instance/port number of a
326 * controller directly, without the need for rebase magic like
327 * this. Manual driver unbind/bind, parallel hotplug/unplug will
328 * get into the way of this "I hope it works" logic.
329 */
330
331 base = udev_device_get_syspath(hostdev);
332 pos = strrchr(base, '/');
333 if (!pos)
334 return NULL;
335
336 base = strndupa(base, pos - base);
337 dir = opendir(base);
338 if (!dir)
339 return NULL;
340
341 FOREACH_DIRENT_ALL(dent, dir, break) {
342 char *rest;
343 int i;
344
345 if (dent->d_name[0] == '.')
346 continue;
347 if (!IN_SET(dent->d_type, DT_DIR, DT_LNK))
348 continue;
349 if (!startswith(dent->d_name, "host"))
350 continue;
351 i = strtoul(&dent->d_name[4], &rest, 10);
352 if (rest[0] != '\0')
353 continue;
354 /*
355 * find the smallest number; the host really needs to export its
356 * own instance number per parent device; relying on the global host
357 * enumeration and plainly rebasing the numbers sounds unreliable
358 */
359 if (basenum == -1 || i < basenum)
360 basenum = i;
361 }
362 if (basenum == -1)
363 return hostdev;
364 host -= basenum;
365
366 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
367 return hostdev;
368 }
369
370 static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path, size_t guid_str_len) {
371 struct udev_device *hostdev;
372 struct udev_device *vmbusdev;
373 const char *guid_str;
374 _cleanup_free_ char *lun = NULL;
375 char guid[39];
376 size_t i, k;
377
378 assert(parent);
379 assert(path);
380 assert(guid_str_len < sizeof(guid));
381
382 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
383 if (!hostdev)
384 return NULL;
385
386 vmbusdev = udev_device_get_parent(hostdev);
387 if (!vmbusdev)
388 return NULL;
389
390 guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
391 if (!guid_str)
392 return NULL;
393
394 if (strlen(guid_str) < guid_str_len || guid_str[0] != '{' || guid_str[guid_str_len-1] != '}')
395 return NULL;
396
397 for (i = 1, k = 0; i < guid_str_len-1; i++) {
398 if (guid_str[i] == '-')
399 continue;
400 guid[k++] = guid_str[i];
401 }
402 guid[k] = '\0';
403
404 format_lun_number(parent, &lun);
405 path_prepend(path, "vmbus-%s-%s", guid, lun);
406 return parent;
407 }
408
409 static struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) {
410 const char *devtype, *id, *name;
411
412 devtype = udev_device_get_devtype(parent);
413 if (!streq_ptr(devtype, "scsi_device"))
414 return parent;
415
416 /* firewire */
417 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
418 if (id) {
419 path_prepend(path, "ieee1394-0x%s", id);
420 *supported_parent = true;
421 return skip_subsystem(parent, "scsi");
422 }
423
424 /* scsi sysfs does not have a "subsystem" for the transport */
425 name = udev_device_get_syspath(parent);
426
427 if (strstr(name, "/rport-")) {
428 *supported_parent = true;
429 return handle_scsi_fibre_channel(parent, path);
430 }
431
432 if (strstr(name, "/end_device-")) {
433 *supported_parent = true;
434 return handle_scsi_sas(parent, path);
435 }
436
437 if (strstr(name, "/session")) {
438 *supported_parent = true;
439 return handle_scsi_iscsi(parent, path);
440 }
441
442 if (strstr(name, "/ata"))
443 return handle_scsi_ata(parent, path);
444
445 if (strstr(name, "/vmbus_"))
446 return handle_scsi_hyperv(parent, path, 37);
447 else if (strstr(name, "/VMBUS"))
448 return handle_scsi_hyperv(parent, path, 38);
449
450 return handle_scsi_default(parent, path);
451 }
452
453 static struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
454 const char *str;
455 unsigned controller, disk;
456
457 str = udev_device_get_sysname(parent);
458 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
459 return NULL;
460
461 path_prepend(path, "cciss-disk%u", disk);
462 return skip_subsystem(parent, "cciss");
463 }
464
465 static void handle_scsi_tape(struct udev_device *dev, char **path) {
466 const char *name;
467
468 /* must be the last device in the syspath */
469 if (*path)
470 return;
471
472 name = udev_device_get_sysname(dev);
473 if (startswith(name, "nst") && strchr("lma", name[3]))
474 path_prepend(path, "nst%c", name[3]);
475 else if (startswith(name, "st") && strchr("lma", name[2]))
476 path_prepend(path, "st%c", name[2]);
477 }
478
479 static struct udev_device *handle_usb(struct udev_device *parent, char **path) {
480 const char *devtype, *str, *port;
481
482 devtype = udev_device_get_devtype(parent);
483 if (!devtype)
484 return parent;
485 if (!STR_IN_SET(devtype, "usb_interface", "usb_device"))
486 return parent;
487
488 str = udev_device_get_sysname(parent);
489 port = strchr(str, '-');
490 if (!port)
491 return parent;
492 port++;
493
494 path_prepend(path, "usb-0:%s", port);
495 return skip_subsystem(parent, "usb");
496 }
497
498 static struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
499 const char *sysname;
500 unsigned core;
501
502 sysname = udev_device_get_sysname(parent);
503 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
504 return NULL;
505
506 path_prepend(path, "bcma-%u", core);
507 return parent;
508 }
509
510 /* Handle devices of AP bus in System z platform. */
511 static struct udev_device *handle_ap(struct udev_device *parent, char **path) {
512 const char *type, *func;
513
514 assert(parent);
515 assert(path);
516
517 type = udev_device_get_sysattr_value(parent, "type");
518 func = udev_device_get_sysattr_value(parent, "ap_functions");
519
520 if (type && func)
521 path_prepend(path, "ap-%s-%s", type, func);
522 else
523 path_prepend(path, "ap-%s", udev_device_get_sysname(parent));
524
525 return skip_subsystem(parent, "ap");
526 }
527
528 static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
529 struct udev_device *parent;
530 _cleanup_free_ char *path = NULL;
531 bool supported_transport = false;
532 bool supported_parent = false;
533
534 assert(dev);
535
536 /* walk up the chain of devices and compose path */
537 parent = dev;
538 while (parent) {
539 const char *subsys;
540
541 subsys = udev_device_get_subsystem(parent);
542 if (!subsys) {
543 ;
544 } else if (streq(subsys, "scsi_tape")) {
545 handle_scsi_tape(parent, &path);
546 } else if (streq(subsys, "scsi")) {
547 parent = handle_scsi(parent, &path, &supported_parent);
548 supported_transport = true;
549 } else if (streq(subsys, "cciss")) {
550 parent = handle_cciss(parent, &path);
551 supported_transport = true;
552 } else if (streq(subsys, "usb")) {
553 parent = handle_usb(parent, &path);
554 supported_transport = true;
555 } else if (streq(subsys, "bcma")) {
556 parent = handle_bcma(parent, &path);
557 supported_transport = true;
558 } else if (streq(subsys, "serio")) {
559 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
560 parent = skip_subsystem(parent, "serio");
561 } else if (streq(subsys, "pci")) {
562 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
563 parent = skip_subsystem(parent, "pci");
564 supported_parent = true;
565 } else if (streq(subsys, "platform")) {
566 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
567 parent = skip_subsystem(parent, "platform");
568 supported_transport = true;
569 supported_parent = true;
570 } else if (streq(subsys, "acpi")) {
571 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
572 parent = skip_subsystem(parent, "acpi");
573 supported_parent = true;
574 } else if (streq(subsys, "xen")) {
575 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
576 parent = skip_subsystem(parent, "xen");
577 supported_parent = true;
578 } else if (streq(subsys, "virtio")) {
579 parent = skip_subsystem(parent, "virtio");
580 supported_transport = true;
581 } else if (streq(subsys, "scm")) {
582 path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
583 parent = skip_subsystem(parent, "scm");
584 supported_transport = true;
585 supported_parent = true;
586 } else if (streq(subsys, "ccw")) {
587 path_prepend(&path, "ccw-%s", udev_device_get_sysname(parent));
588 parent = skip_subsystem(parent, "ccw");
589 supported_transport = true;
590 supported_parent = true;
591 } else if (streq(subsys, "ccwgroup")) {
592 path_prepend(&path, "ccwgroup-%s", udev_device_get_sysname(parent));
593 parent = skip_subsystem(parent, "ccwgroup");
594 supported_transport = true;
595 supported_parent = true;
596 } else if (streq(subsys, "ap")) {
597 parent = handle_ap(parent, &path);
598 supported_transport = true;
599 supported_parent = true;
600 } else if (streq(subsys, "iucv")) {
601 path_prepend(&path, "iucv-%s", udev_device_get_sysname(parent));
602 parent = skip_subsystem(parent, "iucv");
603 supported_transport = true;
604 supported_parent = true;
605 } else if (streq(subsys, "nvme")) {
606 const char *nsid = udev_device_get_sysattr_value(dev, "nsid");
607
608 if (nsid) {
609 path_prepend(&path, "nvme-%s", nsid);
610 parent = skip_subsystem(parent, "nvme");
611 supported_parent = true;
612 supported_transport = true;
613 }
614 }
615
616 if (parent)
617 parent = udev_device_get_parent(parent);
618 }
619
620 if (!path)
621 return EXIT_FAILURE;
622
623 /*
624 * Do not return devices with an unknown parent device type. They
625 * might produce conflicting IDs if the parent does not provide a
626 * unique and predictable name.
627 */
628 if (!supported_parent)
629 return EXIT_FAILURE;
630
631 /*
632 * Do not return block devices without a well-known transport. Some
633 * devices do not expose their buses and do not provide a unique
634 * and predictable name that way.
635 */
636 if (streq_ptr(udev_device_get_subsystem(dev), "block") && !supported_transport)
637 return EXIT_FAILURE;
638
639 {
640 char tag[UTIL_NAME_SIZE];
641 size_t i;
642 const char *p;
643
644 /* compose valid udev tag name */
645 for (p = path, i = 0; *p; p++) {
646 if ((*p >= '0' && *p <= '9') ||
647 (*p >= 'A' && *p <= 'Z') ||
648 (*p >= 'a' && *p <= 'z') ||
649 *p == '-') {
650 tag[i++] = *p;
651 continue;
652 }
653
654 /* skip all leading '_' */
655 if (i == 0)
656 continue;
657
658 /* avoid second '_' */
659 if (tag[i-1] == '_')
660 continue;
661
662 tag[i++] = '_';
663 }
664 /* strip trailing '_' */
665 while (i > 0 && tag[i-1] == '_')
666 i--;
667 tag[i] = '\0';
668
669 udev_builtin_add_property(dev, test, "ID_PATH", path);
670 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
671 }
672
673 return EXIT_SUCCESS;
674 }
675
676 const struct udev_builtin udev_builtin_path_id = {
677 .name = "path_id",
678 .cmd = builtin_path_id,
679 .help = "Compose persistent device path",
680 .run_once = true,
681 };