chiark / gitweb /
08b7e150c9dacfd37ad17111a7ec7b0e58c90dd2
[elogind.git] / extras / path_id / path_id.c
1 /*
2  * compose persisistent device path
3  *
4  * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <getopt.h>
30
31 #include <../../udev/lib/libudev.h>
32 #include <../../udev/udev.h>
33
34 int debug;
35
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)
39 {
40         if (debug) {
41                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
42                 vfprintf(stderr, format, args);
43         } else {
44                 vsyslog(priority, format, args);
45         }
46 }
47
48 static int path_prepend(char **path, const char *fmt, ...)
49 {
50         va_list va;
51         char *old;
52         char *pre;
53         int err;
54
55         old = *path;
56
57         va_start(va, fmt);
58         err = vasprintf(&pre, fmt, va);
59         va_end(va);
60         if (err < 0)
61                 return err;
62
63         if (old != NULL) {
64                 err = asprintf(path, "%s-%s", pre, old);
65                 if (err < 0)
66                         return err;
67                 free(pre);
68         } else {
69                 *path = pre;
70         }
71
72         free(old);
73         return 0;
74 }
75
76 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
77 {
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 || strcmp(subsystem, subsys) != 0)
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 {
94         struct udev *udev  = udev_device_get_udev(parent);
95         struct udev_device *targetdev;
96         struct udev_device *fcdev = NULL;
97         const char *port;
98         unsigned int lun;
99
100         targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
101         if (targetdev == NULL)
102                 return NULL;
103
104         fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
105         if (fcdev == NULL)
106                 return NULL;
107         port = udev_device_get_sysattr_value(fcdev, "port_name");
108         if (port == NULL) {
109                 parent = NULL;
110                 goto out;
111         }
112
113         lun = strtoul(udev_device_get_sysnum(parent), NULL, 10);
114         path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff);
115 out:
116         udev_device_unref(fcdev);
117         return parent;
118 }
119
120 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
121 {
122         return NULL;
123 }
124
125 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
126 {
127         struct udev *udev  = udev_device_get_udev(parent);
128         struct udev_device *transportdev;
129         struct udev_device *sessiondev = NULL;
130         const char *target;
131         char *connname;
132         struct udev_device *conndev = NULL;
133         const char *addr;
134         const char *port;
135
136         /* find iscsi session */
137         transportdev = parent;
138         while (1) {
139                 transportdev = udev_device_get_parent(transportdev);
140                 if (transportdev == NULL)
141                         return NULL;
142                 if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
143                         break;
144         }
145         if (transportdev == NULL)
146                 return NULL;
147
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)
151                 return NULL;
152         target = udev_device_get_sysattr_value(sessiondev, "targetname");
153         if (target == NULL) {
154                 parent = NULL;
155                 goto out;
156         }
157
158         if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
159                 parent = NULL;
160                 goto out;
161         }
162         conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
163         free(connname);
164         if (conndev == NULL) {
165                 parent = NULL;
166                 goto out;
167         }
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) {
171                 parent = NULL;
172                 goto out;
173         }
174
175         path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target, udev_device_get_sysnum(parent));
176 out:
177         udev_device_unref(sessiondev);
178         udev_device_unref(conndev);
179         return parent;
180 }
181
182 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
183 {
184         struct udev_device *hostdev;
185         int host, bus, target, lun;
186         const char *name;
187         char *base;
188         char *pos;
189         DIR *dir;
190         struct dirent *dent;
191         int basenum;
192
193         hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
194         if (hostdev == NULL)
195                 return NULL;
196
197         name = udev_device_get_sysname(parent);
198         if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
199                 return NULL;
200
201         /* rebase host offset to get the local relative number */
202         basenum = -1;
203         base = strdup(udev_device_get_syspath(hostdev));
204         if (base == NULL)
205                 return NULL;
206         pos = strrchr(base, '/');
207         if (pos == NULL) {
208                 parent = NULL;
209                 goto out;
210         }
211         pos[0] = '\0';
212         dir = opendir(base);
213         if (dir == NULL) {
214                 parent = NULL;
215                 goto out;
216         }
217         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
218                 char *rest;
219                 int i;
220
221                 if (dent->d_name[0] == '.')
222                         continue;
223                 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
224                         continue;
225                 if (strncmp(dent->d_name, "host", 4) != 0)
226                         continue;
227                 i = strtoul(&dent->d_name[4], &rest, 10);
228                 if (rest[0] != '\0')
229                         continue;
230                 if (basenum == -1 || i < basenum)
231                         basenum = i;
232         }
233         closedir(dir);
234         if (basenum == -1) {
235                 parent = NULL;
236                 goto out;
237         }
238         host -= basenum;
239
240         path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
241 out:
242         free(base);
243         return hostdev;
244 }
245
246 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
247 {
248         const char *devtype;
249         const char *name;
250         const char *id;
251
252         devtype = udev_device_get_devtype(parent);
253         if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
254                 return parent;
255
256         /* firewire */
257         id = udev_device_get_sysattr_value(parent, "ieee1394_id");
258         if (id != NULL) {
259                 parent = skip_subsystem(parent, "scsi");
260                 path_prepend(path, "ieee1394-0x%s", id);
261                 goto out;
262         }
263
264         /* lousy scsi sysfs does not have a "subsystem" for the transport */
265         name = udev_device_get_syspath(parent);
266
267         if (strstr(name, "/rport-") != NULL) {
268                 parent = handle_scsi_fibre_channel(parent, path);
269                 goto out;
270         }
271
272         if (strstr(name, "/end_device-") != NULL) {
273                 parent = handle_scsi_sas(parent, path);
274                 goto out;
275         }
276
277         if (strstr(name, "/session") != NULL) {
278                 parent = handle_scsi_iscsi(parent, path);
279                 goto out;
280         }
281
282         parent = handle_scsi_default(parent, path);
283 out:
284         return parent;
285 }
286
287 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
288 {
289         const char *name;
290
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]);
296 }
297
298 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
299 {
300         const char *devtype;
301         const char *str;
302         const char *port;
303
304         devtype = udev_device_get_devtype(parent);
305         if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
306                 return parent;
307
308         str = udev_device_get_sysname(parent);
309         port = strchr(str, '-');
310         if (port == NULL)
311                 return parent;
312         port++;
313
314         parent = skip_subsystem(parent, "usb");
315         path_prepend(path, "usb-0:%s", port);
316         return parent;
317 }
318
319 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
320 {
321         return NULL;
322 }
323
324 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
325 {
326         struct udev_device *scsi_dev;
327
328         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
329         if (scsi_dev != NULL) {
330                 const char *wwpn;
331                 const char *lun;
332                 const char *hba_id;
333
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);
339                         goto out;
340                 }
341         }
342
343         path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
344 out:
345         parent = skip_subsystem(parent, "ccw");
346         return parent;
347 }
348
349 int main(int argc, char **argv)
350 {
351         static const struct option options[] = {
352                 { "debug", no_argument, NULL, 'd' },
353                 { "help", no_argument, NULL, 'h' },
354                 {}
355         };
356         struct udev *udev;
357         struct udev_device *dev;
358         struct udev_device *parent;
359         char syspath[UTIL_PATH_SIZE];
360         const char *devpath;
361         char *path;
362         char *path_suffix;
363         int rc = 1;
364
365         udev = udev_new();
366         if (udev == NULL)
367                 goto exit;
368
369         logging_init("usb_id");
370         udev_set_log_fn(udev, log_fn);
371
372         while (1) {
373                 int option;
374
375                 option = getopt_long(argc, argv, "dh", options, NULL);
376                 if (option == -1)
377                         break;
378
379                 switch (option) {
380                 case 'd':
381                         debug = 1;
382                         if (udev_get_log_priority(udev) < LOG_INFO)
383                                 udev_set_log_priority(udev, LOG_INFO);
384                         break;
385                 case 'h':
386                         printf("Usage: path_id [--debug] [--help] <devpath>\n"
387                                "  --debug    print debug information\n"
388                                "  --help      print this help text\n\n");
389                 default:
390                         rc = 1;
391                         goto exit;
392                 }
393         }
394
395         devpath = argv[optind];
396         if (devpath == NULL) {
397                 fprintf(stderr, "No device specified\n");
398                 rc = 2;
399                 goto exit;
400         }
401
402         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
403         dev = udev_device_new_from_syspath(udev, syspath);
404         if (dev == NULL) {
405                 fprintf(stderr, "unable to access '%s'\n", devpath);
406                 rc = 3;
407                 goto exit;
408         }
409
410         path = NULL;
411         path_suffix = NULL;
412
413         /* S390 ccw bus */
414         parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
415         if (parent != NULL) {
416                 handle_ccw(parent, dev, &path);
417                 goto out;
418         }
419
420         /* walk up the chain of devices and compose path */
421         parent = dev;
422         while (parent != NULL) {
423                 const char *subsys;
424
425                 subsys = udev_device_get_subsystem(parent);
426
427                 if (subsys == NULL) {
428                         ;
429                 } else if (strcmp(subsys, "scsi_tape") == 0) {
430                         handle_scsi_tape(parent, &path_suffix);
431                 } else if (strcmp(subsys, "scsi") == 0) {
432                         parent = handle_scsi(parent, &path);
433                 } else if (strcmp(subsys, "cciss") == 0) {
434                         handle_cciss(parent, &path);
435                 } else if (strcmp(subsys, "usb") == 0) {
436                         parent = handle_usb(parent, &path);
437                 } else if (strcmp(subsys, "serio") == 0) {
438                         path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
439                         parent = skip_subsystem(parent, "serio");
440                 } else if (strcmp(subsys, "pci") == 0) {
441                         path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
442                         parent = skip_subsystem(parent, "pci");
443                 } else if (strcmp(subsys, "platform") == 0) {
444                         path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
445                         parent = skip_subsystem(parent, "platform");
446                 } else if (strcmp(subsys, "xen") == 0) {
447                         path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
448                         parent = skip_subsystem(parent, "xen");
449                 }
450
451                 parent = udev_device_get_parent(parent);
452         }
453 out:
454         if (path != NULL) {
455                 if (path_suffix != NULL) {
456                         printf("ID_PATH=%s%s\n", path, path_suffix);
457                         free(path_suffix);
458                 } else {
459                         printf("ID_PATH=%s\n", path);
460                 }
461                 free(path);
462                 rc = 0;
463         }
464
465         udev_device_unref(dev);
466 exit:
467         udev_unref(udev);
468         logging_close();
469         return rc;
470 }