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_scsi(struct udev_device *dev, char **path)
140 struct udev_device *hostdev;
142 int host, bus, target, lun;
145 devtype = udev_device_get_devtype(dev);
146 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
149 hostdev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_host");
153 name = udev_device_get_sysname(dev);
154 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
157 /* rebase host offset to get the local relative number */
158 base = base_number(udev_device_get_syspath(hostdev), "host");
163 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
164 dev = skip_subsystem(dev, "scsi");
168 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
172 name = udev_device_get_sysname(dev);
173 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
174 asprintf(suffix, "nst%c", name[3]);
175 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
176 asprintf(suffix, "st%c", name[2]);
179 static struct udev_device *handle_usb(struct udev_device *dev, char **path)
185 devtype = udev_device_get_devtype(dev);
186 if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
189 str = udev_device_get_sysname(dev);
190 port = strchr(str, '-');
195 dev = skip_subsystem(dev, "usb");
196 path_prepend(path, "usb-0:%s", port);
200 static struct udev_device *handle_firewire(struct udev_device *parent, struct udev_device *dev, char **path)
202 struct udev_device *scsi_dev;
204 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
205 if (scsi_dev != NULL) {
208 id = udev_device_get_sysattr_value(scsi_dev, "ieee1394_id");
210 path_prepend(path, "ieee1394-0x%s", id);
213 parent = skip_subsystem(parent, "firewire");
217 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
219 struct udev_device *scsi_dev;
221 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
222 if (scsi_dev != NULL) {
227 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
228 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
229 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
230 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
231 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
236 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
238 parent = skip_subsystem(parent, "ccw");
242 int main(int argc, char **argv)
244 static const struct option options[] = {
245 { "debug", no_argument, NULL, 'd' },
246 { "help", no_argument, NULL, 'h' },
250 struct udev_device *dev;
251 struct udev_device *parent;
252 char syspath[UTIL_PATH_SIZE];
262 logging_init("usb_id");
263 udev_set_log_fn(udev, log_fn);
268 option = getopt_long(argc, argv, "dh", options, NULL);
275 if (udev_get_log_priority(udev) < LOG_INFO)
276 udev_set_log_priority(udev, LOG_INFO);
279 printf("Usage: path_id [--debug] [--help] <devpath>\n"
280 " --debug print debug information\n"
281 " --help print this help text\n\n");
288 devpath = argv[optind];
289 if (devpath == NULL) {
290 fprintf(stderr, "No device specified\n");
295 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
296 dev = udev_device_new_from_syspath(udev, syspath);
298 fprintf(stderr, "unable to access '%s'\n", devpath);
307 while (parent != NULL) {
310 subsys = udev_device_get_subsystem(parent);
312 if (subsys == NULL) {
314 } else if (strcmp(subsys, "scsi_tape") == 0) {
315 handle_scsi_tape(parent, &path_suffix);
316 } else if (strcmp(subsys, "scsi") == 0) {
317 parent = handle_scsi(parent, &path);
318 } else if (strcmp(subsys, "fc_transport") == 0) {
320 } else if (strcmp(subsys, "sas_end_device") == 0) {
322 } else if (strcmp(subsys, "iscsi_session") == 0) {
324 } else if (strcmp(subsys, "ccw") == 0) {
325 handle_ccw(parent, dev, &path);
326 } else if (strcmp(subsys, "cciss") == 0) {
328 } else if (strcmp(subsys, "usb") == 0) {
329 parent = handle_usb(parent, &path);
330 } else if (strcmp(subsys, "serio") == 0) {
331 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
332 parent = skip_subsystem(parent, "serio");
333 } else if (strcmp(subsys, "firewire") == 0 || strcmp(subsys, "ieee1394") == 0) {
334 parent = handle_firewire(parent, dev, &path);
335 } else if (strcmp(subsys, "pci") == 0) {
336 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
337 parent = skip_subsystem(parent, "pci");
338 } else if (strcmp(subsys, "platform") == 0) {
339 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
340 parent = skip_subsystem(parent, "platform");
341 } else if (strcmp(subsys, "xen") == 0) {
342 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
343 parent = skip_subsystem(parent, "xen");
346 parent = udev_device_get_parent(parent);
350 if (path_suffix != NULL) {
351 printf("ID_PATH=%s%s\n", path, path_suffix);
354 printf("ID_PATH=%s\n", path);
360 udev_device_unref(dev);