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