chiark / gitweb /
path_id: handle fibre channel
[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 = NULL;
141         char *syspath = NULL;
142         struct udev_device *fcdev;
143         const char *port;
144         unsigned int lun;
145
146         targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
147         if (targetdev == NULL) {
148                 parent = NULL;
149                 goto out;
150         }
151
152         if (asprintf(&syspath, "%s/fc_transport/%s", udev_device_get_syspath(targetdev), udev_device_get_sysname(targetdev)) < 0) {
153                 parent = NULL;
154                 goto out;
155         }
156         fcdev = udev_device_new_from_syspath(udev, syspath);
157         if (fcdev == NULL) {
158                 parent = NULL;
159                 goto out;
160         }
161
162         port = udev_device_get_sysattr_value(fcdev, "port_name");
163         if (port == NULL) {
164                 parent = NULL;
165                 goto out;
166         }
167
168         lun = strtoul(udev_device_get_sysnum(parent), NULL, 10);
169         path_prepend(path, "fc-%s:0x%04x%04x00000000", port, lun & 0xffff, (lun >> 16) & 0xffff);
170 out:
171         free(syspath);
172         udev_device_unref(fcdev);
173         return parent;
174 }
175
176 static struct udev_device *handle_sas(struct udev_device *parent, char **path)
177 {
178         path_prepend(path, "sas-PATH_ID_NOT_IMPLEMENTED");
179         return parent;
180 }
181
182 static struct udev_device *handle_iscsi(struct udev_device *parent, char **path)
183 {
184         path_prepend(path, "ip-PATH_ID_NOT_IMPLEMENTED");
185         return parent;
186 }
187
188 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
189 {
190         struct udev_device *hostdev;
191         int host, bus, target, lun;
192         const char *name;
193         int base;
194
195         hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
196         if (hostdev == NULL)
197                 return parent;
198
199         name = udev_device_get_sysname(parent);
200         if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
201                 return parent;
202
203         /* rebase host offset to get the local relative number */
204         base = base_number(udev_device_get_syspath(hostdev), "host");
205         if (base < 0)
206                 return parent;
207         host -= base;
208
209         path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
210         return hostdev;
211 }
212
213 static struct udev_device *handle_scsi_lun(struct udev_device *parent, char **path)
214 {
215         const char *devtype;
216         const char *name;
217         const char *id;
218
219         devtype = udev_device_get_devtype(parent);
220         if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
221                 return parent;
222
223         /* firewire */
224         id = udev_device_get_sysattr_value(parent, "ieee1394_id");
225         if (id != NULL) {
226                 parent = skip_subsystem(parent, "scsi");
227                 path_prepend(path, "ieee1394-0x%s", id);
228                 goto out;
229         }
230
231         name = udev_device_get_syspath(parent);
232
233         /* fibre channel */
234         if (strstr(name, "/rport-") != NULL) {
235                 parent = handle_fc(parent, path);
236                 goto out;
237         }
238
239         /* sas */
240         if (strstr(name, "/end_device-") != NULL) {
241                 parent = handle_sas(parent, path);
242                 goto out;
243         }
244
245         /* iSCSI */
246         if (strstr(name, "/session") != NULL) {
247                 parent = handle_iscsi(parent, path);
248                 goto out;
249         }
250
251         /* default */
252         parent = handle_scsi(parent, path);
253 out:
254         return parent;
255 }
256
257 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
258 {
259         const char *name;
260
261         name = udev_device_get_sysname(dev);
262         if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
263                 asprintf(suffix, "nst%c", name[3]);
264         else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
265                 asprintf(suffix, "st%c", name[2]);
266 }
267
268 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
269 {
270         const char *devtype;
271         const char *str;
272         const char *port;
273
274         devtype = udev_device_get_devtype(parent);
275         if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
276                 return parent;
277
278         str = udev_device_get_sysname(parent);
279         port = strchr(str, '-');
280         if (port == NULL)
281                 return parent;
282         port++;
283
284         parent = skip_subsystem(parent, "usb");
285         path_prepend(path, "usb-0:%s", port);
286         return parent;
287 }
288
289 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
290 {
291         path_prepend(path, "cciss-PATH_ID_NOT_IMPLEMENTED");
292         return parent;
293 }
294
295 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
296 {
297         struct udev_device *scsi_dev;
298
299         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
300         if (scsi_dev != NULL) {
301                 const char *wwpn;
302                 const char *lun;
303                 const char *hba_id;
304
305                 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
306                 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
307                 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
308                 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
309                         path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
310                         goto out;
311                 }
312         }
313
314         path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
315 out:
316         parent = skip_subsystem(parent, "ccw");
317         return parent;
318 }
319
320 int main(int argc, char **argv)
321 {
322         static const struct option options[] = {
323                 { "debug", no_argument, NULL, 'd' },
324                 { "help", no_argument, NULL, 'h' },
325                 {}
326         };
327         struct udev *udev;
328         struct udev_device *dev;
329         struct udev_device *parent;
330         char syspath[UTIL_PATH_SIZE];
331         const char *devpath;
332         char *path;
333         char *path_suffix;
334         int rc = 1;
335
336         udev = udev_new();
337         if (udev == NULL)
338                 goto exit;
339
340         logging_init("usb_id");
341         udev_set_log_fn(udev, log_fn);
342
343         while (1) {
344                 int option;
345
346                 option = getopt_long(argc, argv, "dh", options, NULL);
347                 if (option == -1)
348                         break;
349
350                 switch (option) {
351                 case 'd':
352                         debug = 1;
353                         if (udev_get_log_priority(udev) < LOG_INFO)
354                                 udev_set_log_priority(udev, LOG_INFO);
355                         break;
356                 case 'h':
357                         printf("Usage: path_id [--debug] [--help] <devpath>\n"
358                                "  --debug    print debug information\n"
359                                "  --help      print this help text\n\n");
360                 default:
361                         rc = 1;
362                         goto exit;
363                 }
364         }
365
366         devpath = argv[optind];
367         if (devpath == NULL) {
368                 fprintf(stderr, "No device specified\n");
369                 rc = 2;
370                 goto exit;
371         }
372
373         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
374         dev = udev_device_new_from_syspath(udev, syspath);
375         if (dev == NULL) {
376                 fprintf(stderr, "unable to access '%s'\n", devpath);
377                 rc = 3;
378                 goto exit;
379         }
380
381         path = NULL;
382         path_suffix = NULL;
383
384         /* S390 ccw bus */
385         parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
386         if (parent != NULL) {
387                 handle_ccw(parent, dev, &path);
388                 goto out;
389         }
390
391         /* walk up the chain of devices and compose path */
392         parent = dev;
393         while (parent != NULL) {
394                 const char *subsys;
395
396                 subsys = udev_device_get_subsystem(parent);
397
398                 if (subsys == NULL) {
399                         ;
400                 } else if (strcmp(subsys, "scsi_tape") == 0) {
401                         handle_scsi_tape(parent, &path_suffix);
402                 } else if (strcmp(subsys, "scsi") == 0) {
403                         parent = handle_scsi_lun(parent, &path);
404                 } else if (strcmp(subsys, "cciss") == 0) {
405                         handle_cciss(parent, &path);
406                 } else if (strcmp(subsys, "usb") == 0) {
407                         parent = handle_usb(parent, &path);
408                 } else if (strcmp(subsys, "serio") == 0) {
409                         path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
410                         parent = skip_subsystem(parent, "serio");
411                 } else if (strcmp(subsys, "pci") == 0) {
412                         path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
413                         parent = skip_subsystem(parent, "pci");
414                 } else if (strcmp(subsys, "platform") == 0) {
415                         path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
416                         parent = skip_subsystem(parent, "platform");
417                 } else if (strcmp(subsys, "xen") == 0) {
418                         path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
419                         parent = skip_subsystem(parent, "xen");
420                 }
421
422                 parent = udev_device_get_parent(parent);
423         }
424 out:
425         if (path != NULL) {
426                 if (path_suffix != NULL) {
427                         printf("ID_PATH=%s%s\n", path, path_suffix);
428                         free(path_suffix);
429                 } else {
430                         printf("ID_PATH=%s\n", path);
431                 }
432                 free(path);
433                 rc = 0;
434         }
435
436         udev_device_unref(dev);
437 exit:
438         udev_unref(udev);
439         logging_close();
440         return rc;
441 }