2 * compose persistent device path
4 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
6 * Logic based on Hannes Reinecke's shell script.
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.
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.
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/>.
34 #include "libudev-private.h"
38 static void log_fn(struct udev *udev, int priority,
39 const char *file, int line, const char *fn,
40 const char *format, va_list args)
43 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
44 vfprintf(stderr, format, args);
46 vsyslog(priority, format, args);
50 static int path_prepend(char **path, const char *fmt, ...)
60 err = vasprintf(&pre, fmt, va);
66 err = asprintf(path, "%s-%s", pre, old);
78 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
80 struct udev_device *parent = dev;
82 while (parent != NULL) {
83 const char *subsystem;
85 subsystem = udev_device_get_subsystem(parent);
86 if (subsystem == NULL || strcmp(subsystem, subsys) != 0)
89 parent = udev_device_get_parent(parent);
94 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;
102 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
103 if (targetdev == NULL)
106 fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
109 port = udev_device_get_sysattr_value(fcdev, "port_name");
115 lun = strtoul(udev_device_get_sysnum(parent), NULL, 10);
116 path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff);
118 udev_device_unref(fcdev);
122 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
127 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
129 struct udev *udev = udev_device_get_udev(parent);
130 struct udev_device *transportdev;
131 struct udev_device *sessiondev = NULL;
134 struct udev_device *conndev = NULL;
138 /* find iscsi session */
139 transportdev = parent;
141 transportdev = udev_device_get_parent(transportdev);
142 if (transportdev == NULL)
144 if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
148 /* find iscsi session device */
149 sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
150 if (sessiondev == NULL)
152 target = udev_device_get_sysattr_value(sessiondev, "targetname");
153 if (target == NULL) {
158 if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
162 conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
164 if (conndev == NULL) {
168 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
169 port = udev_device_get_sysattr_value(conndev, "persistent_port");
170 if (addr == NULL || port == NULL) {
175 path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target, udev_device_get_sysnum(parent));
177 udev_device_unref(sessiondev);
178 udev_device_unref(conndev);
182 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
184 struct udev_device *hostdev;
185 int host, bus, target, lun;
193 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
197 name = udev_device_get_sysname(parent);
198 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
201 /* rebase host offset to get the local relative number */
203 base = strdup(udev_device_get_syspath(hostdev));
206 pos = strrchr(base, '/');
217 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
221 if (dent->d_name[0] == '.')
223 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
225 if (strncmp(dent->d_name, "host", 4) != 0)
227 i = strtoul(&dent->d_name[4], &rest, 10);
230 if (basenum == -1 || i < basenum)
240 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
246 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
252 devtype = udev_device_get_devtype(parent);
253 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
257 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
259 parent = skip_subsystem(parent, "scsi");
260 path_prepend(path, "ieee1394-0x%s", id);
264 /* lousy scsi sysfs does not have a "subsystem" for the transport */
265 name = udev_device_get_syspath(parent);
267 if (strstr(name, "/rport-") != NULL) {
268 parent = handle_scsi_fibre_channel(parent, path);
272 if (strstr(name, "/end_device-") != NULL) {
273 parent = handle_scsi_sas(parent, path);
277 if (strstr(name, "/session") != NULL) {
278 parent = handle_scsi_iscsi(parent, path);
282 parent = handle_scsi_default(parent, path);
287 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
291 name = udev_device_get_sysname(dev);
292 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
293 asprintf(suffix, "nst%c", name[3]);
294 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
295 asprintf(suffix, "st%c", name[2]);
298 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
304 devtype = udev_device_get_devtype(parent);
305 if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
308 str = udev_device_get_sysname(parent);
309 port = strchr(str, '-');
314 parent = skip_subsystem(parent, "usb");
315 path_prepend(path, "usb-0:%s", port);
319 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
324 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
326 struct udev_device *scsi_dev;
328 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
329 if (scsi_dev != NULL) {
334 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
335 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
336 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
337 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
338 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
343 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
345 parent = skip_subsystem(parent, "ccw");
349 int main(int argc, char **argv)
351 static const struct option options[] = {
352 { "debug", no_argument, NULL, 'd' },
353 { "help", no_argument, NULL, 'h' },
357 struct udev_device *dev;
358 struct udev_device *parent;
359 char syspath[UTIL_PATH_SIZE];
369 udev_log_init("path_id");
370 udev_set_log_fn(udev, log_fn);
375 option = getopt_long(argc, argv, "dh", options, NULL);
382 if (udev_get_log_priority(udev) < LOG_INFO)
383 udev_set_log_priority(udev, LOG_INFO);
386 printf("Usage: path_id [--debug] [--help] <devpath>\n"
387 " --debug print debug information\n"
388 " --help print this help text\n\n");
393 devpath = argv[optind];
394 if (devpath == NULL) {
395 fprintf(stderr, "No device specified\n");
400 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
401 dev = udev_device_new_from_syspath(udev, syspath);
403 fprintf(stderr, "unable to access '%s'\n", devpath);
412 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
413 if (parent != NULL) {
414 handle_ccw(parent, dev, &path);
418 /* walk up the chain of devices and compose path */
420 while (parent != NULL) {
423 subsys = udev_device_get_subsystem(parent);
425 if (subsys == NULL) {
427 } else if (strcmp(subsys, "scsi_tape") == 0) {
428 handle_scsi_tape(parent, &path_suffix);
429 } else if (strcmp(subsys, "scsi") == 0) {
430 parent = handle_scsi(parent, &path);
431 } else if (strcmp(subsys, "cciss") == 0) {
432 handle_cciss(parent, &path);
433 } else if (strcmp(subsys, "usb") == 0) {
434 parent = handle_usb(parent, &path);
435 } else if (strcmp(subsys, "serio") == 0) {
436 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
437 parent = skip_subsystem(parent, "serio");
438 } else if (strcmp(subsys, "pci") == 0) {
439 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
440 parent = skip_subsystem(parent, "pci");
441 } else if (strcmp(subsys, "platform") == 0) {
442 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
443 parent = skip_subsystem(parent, "platform");
444 } else if (strcmp(subsys, "xen") == 0) {
445 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
446 parent = skip_subsystem(parent, "xen");
447 } else if (strcmp(subsys, "virtio") == 0) {
448 path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
449 parent = skip_subsystem(parent, "virtio");
452 parent = udev_device_get_parent(parent);
456 if (path_suffix != NULL) {
457 printf("ID_PATH=%s%s\n", path, path_suffix);
460 printf("ID_PATH=%s\n", path);
466 udev_device_unref(dev);