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