2 * compose persisistent device path
4 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <../../udev/udev.h>
36 static void log_fn(struct udev *udev, int priority,
37 const char *file, int line, const char *fn,
38 const char *format, va_list args)
41 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
42 vfprintf(stderr, format, args);
44 vsyslog(priority, format, args);
48 static int path_prepend(char **path, const char *fmt, ...)
58 err = vasprintf(&pre, fmt, va);
64 err = asprintf(path, "%s-%s", pre, old);
76 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
78 struct udev_device *parent = dev;
80 while (parent != NULL) {
81 const char *subsystem;
83 subsystem = udev_device_get_subsystem(parent);
84 if (subsystem == NULL || strcmp(subsystem, subsys) != 0)
87 parent = udev_device_get_parent(parent);
92 /* find smallest number of instances of <syspath>/<name><number> */
93 static int base_number(const char *syspath, const char *name)
102 base = strdup(syspath);
106 pos = strrchr(base, '/');
115 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
119 if (dent->d_name[0] == '.')
121 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
123 if (strncmp(dent->d_name, name, len) != 0)
125 i = strtoul(&dent->d_name[len], &rest, 10);
128 if (number == -1 || i < number)
137 static struct udev_device *handle_fc(struct udev_device *parent, char **path)
139 path_prepend(path, "fc-PATH_ID_NOT_IMPLEMENTED");
143 static struct udev_device *handle_sas(struct udev_device *parent, char **path)
145 path_prepend(path, "sas-PATH_ID_NOT_IMPLEMENTED");
149 static struct udev_device *handle_iscsi(struct udev_device *parent, char **path)
151 path_prepend(path, "ip-PATH_ID_NOT_IMPLEMENTED");
155 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
157 struct udev_device *hostdev;
158 int host, bus, target, lun;
162 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
166 name = udev_device_get_sysname(parent);
167 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
170 /* rebase host offset to get the local relative number */
171 base = base_number(udev_device_get_syspath(hostdev), "host");
176 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
180 static struct udev_device *handle_scsi_lun(struct udev_device *parent, char **path)
186 devtype = udev_device_get_devtype(parent);
187 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
191 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
193 parent = skip_subsystem(parent, "scsi");
194 path_prepend(path, "ieee1394-0x%s", id);
198 name = udev_device_get_syspath(parent);
201 if (strstr(name, "/rport-") != NULL) {
202 parent = handle_fc(parent, path);
207 if (strstr(name, "/end_device-") != NULL) {
208 parent = handle_sas(parent, path);
213 if (strstr(name, "/session") != NULL) {
214 parent = handle_iscsi(parent, path);
219 parent = handle_scsi(parent, path);
224 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
228 name = udev_device_get_sysname(dev);
229 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
230 asprintf(suffix, "nst%c", name[3]);
231 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
232 asprintf(suffix, "st%c", name[2]);
235 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
241 devtype = udev_device_get_devtype(parent);
242 if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
245 str = udev_device_get_sysname(parent);
246 port = strchr(str, '-');
251 parent = skip_subsystem(parent, "usb");
252 path_prepend(path, "usb-0:%s", port);
256 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
258 path_prepend(path, "cciss-PATH_ID_NOT_IMPLEMENTED");
262 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
264 struct udev_device *scsi_dev;
266 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
267 if (scsi_dev != NULL) {
272 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
273 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
274 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
275 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
276 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
281 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
283 parent = skip_subsystem(parent, "ccw");
287 int main(int argc, char **argv)
289 static const struct option options[] = {
290 { "debug", no_argument, NULL, 'd' },
291 { "help", no_argument, NULL, 'h' },
295 struct udev_device *dev;
296 struct udev_device *parent;
297 char syspath[UTIL_PATH_SIZE];
307 logging_init("usb_id");
308 udev_set_log_fn(udev, log_fn);
313 option = getopt_long(argc, argv, "dh", options, NULL);
320 if (udev_get_log_priority(udev) < LOG_INFO)
321 udev_set_log_priority(udev, LOG_INFO);
324 printf("Usage: path_id [--debug] [--help] <devpath>\n"
325 " --debug print debug information\n"
326 " --help print this help text\n\n");
333 devpath = argv[optind];
334 if (devpath == NULL) {
335 fprintf(stderr, "No device specified\n");
340 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
341 dev = udev_device_new_from_syspath(udev, syspath);
343 fprintf(stderr, "unable to access '%s'\n", devpath);
352 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
353 if (parent != NULL) {
354 handle_ccw(parent, dev, &path);
358 /* walk up the chain of devices and compose path */
360 while (parent != NULL) {
363 subsys = udev_device_get_subsystem(parent);
365 if (subsys == NULL) {
367 } else if (strcmp(subsys, "scsi_tape") == 0) {
368 handle_scsi_tape(parent, &path_suffix);
369 } else if (strcmp(subsys, "scsi") == 0) {
370 parent = handle_scsi_lun(parent, &path);
371 } else if (strcmp(subsys, "cciss") == 0) {
372 handle_cciss(parent, &path);
373 } else if (strcmp(subsys, "usb") == 0) {
374 parent = handle_usb(parent, &path);
375 } else if (strcmp(subsys, "serio") == 0) {
376 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
377 parent = skip_subsystem(parent, "serio");
378 } else if (strcmp(subsys, "pci") == 0) {
379 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
380 parent = skip_subsystem(parent, "pci");
381 } else if (strcmp(subsys, "platform") == 0) {
382 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
383 parent = skip_subsystem(parent, "platform");
384 } else if (strcmp(subsys, "xen") == 0) {
385 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
386 parent = skip_subsystem(parent, "xen");
389 parent = udev_device_get_parent(parent);
393 if (path_suffix != NULL) {
394 printf("ID_PATH=%s%s\n", path, path_suffix);
397 printf("ID_PATH=%s\n", path);
403 udev_device_unref(dev);