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