chiark / gitweb /
b0a0bfd885e0101f95ee91cef2de8cb94697b9ab
[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 /* find smallest number of instances of <syspath>/<name><number> */
93 static int base_number(const char *syspath, const char *name)
94 {
95         char *base;
96         char *pos;
97         DIR *dir;
98         struct dirent *dent;
99         size_t len;
100         int number = -1;
101
102         base = strdup(syspath);
103         if (base == NULL)
104                 goto out;
105
106         pos = strrchr(base, '/');
107         if (pos == NULL)
108                 goto out;
109         pos[0] = '\0';
110
111         len = strlen(name);
112         dir = opendir(base);
113         if (dir == NULL)
114                 goto out;
115         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
116                 char *rest;
117                 int i;
118
119                 if (dent->d_name[0] == '.')
120                         continue;
121                 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
122                         continue;
123                 if (strncmp(dent->d_name, name, len) != 0)
124                         continue;
125                 i = strtoul(&dent->d_name[len], &rest, 10);
126                 if (rest[0] != '\0')
127                         continue;
128                 if (number == -1 || i < number)
129                         number = i;
130         }
131         closedir(dir);
132 out:
133         free(base);
134         return number;
135 }
136
137 static struct udev_device *handle_fc(struct udev_device *parent, char **path)
138 {
139         struct udev *udev  = udev_device_get_udev(parent);
140         struct udev_device *targetdev;
141         struct udev_device *fcdev = NULL;
142         const char *port;
143         unsigned int lun;
144
145         targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
146         if (targetdev == NULL)
147                 return NULL;
148
149         fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
150         if (fcdev == NULL)
151                 return NULL;
152         port = udev_device_get_sysattr_value(fcdev, "port_name");
153         if (port == NULL) {
154                 parent = NULL;
155                 goto out;
156         }
157
158         lun = strtoul(udev_device_get_sysnum(parent), NULL, 10);
159         path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff);
160 out:
161         udev_device_unref(fcdev);
162         return parent;
163 }
164
165 static struct udev_device *handle_sas(struct udev_device *parent, char **path)
166 {
167         return NULL;
168 }
169
170 static struct udev_device *handle_iscsi(struct udev_device *parent, char **path)
171 {
172         struct udev *udev  = udev_device_get_udev(parent);
173         struct udev_device *transportdev;
174         struct udev_device *sessiondev = NULL;
175         const char *target;
176         char *connname;
177         struct udev_device *conndev = NULL;
178         const char *addr;
179         const char *port;
180
181         /* find iscsi session */        
182         transportdev = parent;
183         while (1) {
184                 transportdev = udev_device_get_parent(transportdev);
185                 if (transportdev == NULL)
186                         return NULL;
187                 if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
188                         break;
189         }
190         if (transportdev == NULL)
191                 return NULL;
192
193         /* find iscsi session device */
194         sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
195         if (sessiondev == NULL)
196                 return NULL;
197         target = udev_device_get_sysattr_value(sessiondev, "targetname");
198         if (target == NULL) {
199                 parent = NULL;
200                 goto out;
201         }
202
203         if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
204                 parent = NULL;
205                 goto out;
206         }
207         conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
208         free(connname);
209         if (conndev == NULL) {
210                 parent = NULL;
211                 goto out;
212         }
213         addr = udev_device_get_sysattr_value(conndev, "persistent_address");
214         port = udev_device_get_sysattr_value(conndev, "persistent_port");
215         if (addr == NULL || port == NULL) {
216                 parent = NULL;
217                 goto out;
218         }
219
220         path_prepend(path, "ip-%s:%s-iscsi-%s-lun-%s", addr, port, target, udev_device_get_sysnum(parent));
221 out:
222         udev_device_unref(sessiondev);
223         udev_device_unref(conndev);
224         return parent;
225 }
226
227 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
228 {
229         struct udev_device *hostdev;
230         int host, bus, target, lun;
231         const char *name;
232         int base;
233
234         hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
235         if (hostdev == NULL)
236                 return parent;
237
238         name = udev_device_get_sysname(parent);
239         if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
240                 return parent;
241
242         /* rebase host offset to get the local relative number */
243         base = base_number(udev_device_get_syspath(hostdev), "host");
244         if (base < 0)
245                 return parent;
246         host -= base;
247
248         path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
249         return hostdev;
250 }
251
252 static struct udev_device *handle_scsi_lun(struct udev_device *parent, char **path)
253 {
254         const char *devtype;
255         const char *name;
256         const char *id;
257
258         devtype = udev_device_get_devtype(parent);
259         if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
260                 return parent;
261
262         /* firewire */
263         id = udev_device_get_sysattr_value(parent, "ieee1394_id");
264         if (id != NULL) {
265                 parent = skip_subsystem(parent, "scsi");
266                 path_prepend(path, "ieee1394-0x%s", id);
267                 goto out;
268         }
269
270         /* broken scsi transport devices would need a subsystem */
271         name = udev_device_get_syspath(parent);
272
273         /* fibre channel */
274         if (strstr(name, "/rport-") != NULL) {
275                 parent = handle_fc(parent, path);
276                 goto out;
277         }
278
279         /* sas */
280         if (strstr(name, "/end_device-") != NULL) {
281                 parent = handle_sas(parent, path);
282                 goto out;
283         }
284
285         /* iSCSI */
286         if (strstr(name, "/session") != NULL) {
287                 parent = handle_iscsi(parent, path);
288                 goto out;
289         }
290
291         /* default */
292         parent = handle_scsi(parent, path);
293 out:
294         return parent;
295 }
296
297 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
298 {
299         const char *name;
300
301         name = udev_device_get_sysname(dev);
302         if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
303                 asprintf(suffix, "nst%c", name[3]);
304         else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
305                 asprintf(suffix, "st%c", name[2]);
306 }
307
308 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
309 {
310         const char *devtype;
311         const char *str;
312         const char *port;
313
314         devtype = udev_device_get_devtype(parent);
315         if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
316                 return parent;
317
318         str = udev_device_get_sysname(parent);
319         port = strchr(str, '-');
320         if (port == NULL)
321                 return parent;
322         port++;
323
324         parent = skip_subsystem(parent, "usb");
325         path_prepend(path, "usb-0:%s", port);
326         return parent;
327 }
328
329 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
330 {
331         return NULL;
332 }
333
334 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
335 {
336         struct udev_device *scsi_dev;
337
338         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
339         if (scsi_dev != NULL) {
340                 const char *wwpn;
341                 const char *lun;
342                 const char *hba_id;
343
344                 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
345                 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
346                 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
347                 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
348                         path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
349                         goto out;
350                 }
351         }
352
353         path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
354 out:
355         parent = skip_subsystem(parent, "ccw");
356         return parent;
357 }
358
359 int main(int argc, char **argv)
360 {
361         static const struct option options[] = {
362                 { "debug", no_argument, NULL, 'd' },
363                 { "help", no_argument, NULL, 'h' },
364                 {}
365         };
366         struct udev *udev;
367         struct udev_device *dev;
368         struct udev_device *parent;
369         char syspath[UTIL_PATH_SIZE];
370         const char *devpath;
371         char *path;
372         char *path_suffix;
373         int rc = 1;
374
375         udev = udev_new();
376         if (udev == NULL)
377                 goto exit;
378
379         logging_init("usb_id");
380         udev_set_log_fn(udev, log_fn);
381
382         while (1) {
383                 int option;
384
385                 option = getopt_long(argc, argv, "dh", options, NULL);
386                 if (option == -1)
387                         break;
388
389                 switch (option) {
390                 case 'd':
391                         debug = 1;
392                         if (udev_get_log_priority(udev) < LOG_INFO)
393                                 udev_set_log_priority(udev, LOG_INFO);
394                         break;
395                 case 'h':
396                         printf("Usage: path_id [--debug] [--help] <devpath>\n"
397                                "  --debug    print debug information\n"
398                                "  --help      print this help text\n\n");
399                 default:
400                         rc = 1;
401                         goto exit;
402                 }
403         }
404
405         devpath = argv[optind];
406         if (devpath == NULL) {
407                 fprintf(stderr, "No device specified\n");
408                 rc = 2;
409                 goto exit;
410         }
411
412         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
413         dev = udev_device_new_from_syspath(udev, syspath);
414         if (dev == NULL) {
415                 fprintf(stderr, "unable to access '%s'\n", devpath);
416                 rc = 3;
417                 goto exit;
418         }
419
420         path = NULL;
421         path_suffix = NULL;
422
423         /* S390 ccw bus */
424         parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
425         if (parent != NULL) {
426                 handle_ccw(parent, dev, &path);
427                 goto out;
428         }
429
430         /* walk up the chain of devices and compose path */
431         parent = dev;
432         while (parent != NULL) {
433                 const char *subsys;
434
435                 subsys = udev_device_get_subsystem(parent);
436
437                 if (subsys == NULL) {
438                         ;
439                 } else if (strcmp(subsys, "scsi_tape") == 0) {
440                         handle_scsi_tape(parent, &path_suffix);
441                 } else if (strcmp(subsys, "scsi") == 0) {
442                         parent = handle_scsi_lun(parent, &path);
443                 } else if (strcmp(subsys, "cciss") == 0) {
444                         handle_cciss(parent, &path);
445                 } else if (strcmp(subsys, "usb") == 0) {
446                         parent = handle_usb(parent, &path);
447                 } else if (strcmp(subsys, "serio") == 0) {
448                         path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
449                         parent = skip_subsystem(parent, "serio");
450                 } else if (strcmp(subsys, "pci") == 0) {
451                         path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
452                         parent = skip_subsystem(parent, "pci");
453                 } else if (strcmp(subsys, "platform") == 0) {
454                         path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
455                         parent = skip_subsystem(parent, "platform");
456                 } else if (strcmp(subsys, "xen") == 0) {
457                         path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
458                         parent = skip_subsystem(parent, "xen");
459                 }
460
461                 parent = udev_device_get_parent(parent);
462         }
463 out:
464         if (path != NULL) {
465                 if (path_suffix != NULL) {
466                         printf("ID_PATH=%s%s\n", path, path_suffix);
467                         free(path_suffix);
468                 } else {
469                         printf("ID_PATH=%s\n", path);
470                 }
471                 free(path);
472                 rc = 0;
473         }
474
475         udev_device_unref(dev);
476 exit:
477         udev_unref(udev);
478         logging_close();
479         return rc;
480 }