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