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