chiark / gitweb /
872f0c932e82a27ad1cd9666fbcf8fd799568e90
[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_scsi(struct udev_device *dev, char **path)
138 {
139         const char *devtype;
140         struct udev_device *hostdev;
141         const char *name;
142         int host, bus, target, lun;
143         int base;
144
145         devtype = udev_device_get_devtype(dev);
146         if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
147                 return dev;
148
149         hostdev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_host");
150         if (hostdev == NULL)
151                 return dev;
152
153         name = udev_device_get_sysname(dev);
154         if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
155                 return dev;
156
157         /* rebase host offset to get the local relative number */
158         base = base_number(udev_device_get_syspath(hostdev), "host");
159         if (base < 0)
160                 return dev;
161         host -= base;
162
163         path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
164         dev = skip_subsystem(dev, "scsi");
165         return dev;
166 }
167
168 static void handle_scsi_tape(struct udev_device *dev, char **suffix)
169 {
170         const char *name;
171
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]);
177 }
178
179 static struct udev_device *handle_usb(struct udev_device *dev, char **path)
180 {
181         const char *devtype;
182         const char *str;
183         const char *port;
184
185         devtype = udev_device_get_devtype(dev);
186         if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
187                 return dev;
188
189         str = udev_device_get_sysname(dev);
190         port = strchr(str, '-');
191         if (port == NULL)
192                 return dev;
193         port++;
194
195         dev = skip_subsystem(dev, "usb");
196         path_prepend(path, "usb-0:%s", port);
197         return dev;
198 }
199
200 static struct udev_device *handle_firewire(struct udev_device *parent, struct udev_device *dev, char **path)
201 {
202         struct udev_device *scsi_dev;
203
204         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
205         if (scsi_dev != NULL) {
206                 const char *id;
207
208                 id = udev_device_get_sysattr_value(scsi_dev, "ieee1394_id");
209                 if (id != NULL)
210                         path_prepend(path, "ieee1394-0x%s", id);
211         }
212
213         parent = skip_subsystem(parent, "firewire");
214         return parent;
215 }
216
217 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
218 {
219         struct udev_device *scsi_dev;
220
221         scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
222         if (scsi_dev != NULL) {
223                 const char *wwpn;
224                 const char *lun;
225                 const char *hba_id;
226
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);
232                         goto out;
233                 }
234         }
235
236         path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
237 out:
238         parent = skip_subsystem(parent, "ccw");
239         return parent;
240 }
241
242 int main(int argc, char **argv)
243 {
244         static const struct option options[] = {
245                 { "debug", no_argument, NULL, 'd' },
246                 { "help", no_argument, NULL, 'h' },
247                 {}
248         };
249         struct udev *udev;
250         struct udev_device *dev;
251         struct udev_device *parent;
252         char syspath[UTIL_PATH_SIZE];
253         const char *devpath;
254         char *path;
255         char *path_suffix;
256         int rc = 1;
257
258         udev = udev_new();
259         if (udev == NULL)
260                 goto exit;
261
262         logging_init("usb_id");
263         udev_set_log_fn(udev, log_fn);
264
265         while (1) {
266                 int option;
267
268                 option = getopt_long(argc, argv, "dh", options, NULL);
269                 if (option == -1)
270                         break;
271
272                 switch (option) {
273                 case 'd':
274                         debug = 1;
275                         if (udev_get_log_priority(udev) < LOG_INFO)
276                                 udev_set_log_priority(udev, LOG_INFO);
277                         break;
278                 case 'h':
279                         printf("Usage: path_id [--debug] [--help] <devpath>\n"
280                                "  --debug    print debug information\n"
281                                "  --help      print this help text\n\n");
282                 default:
283                         rc = 1;
284                         goto exit;
285                 }
286         }
287
288         devpath = argv[optind];
289         if (devpath == NULL) {
290                 fprintf(stderr, "No device specified\n");
291                 rc = 2;
292                 goto exit;
293         }
294
295         util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
296         dev = udev_device_new_from_syspath(udev, syspath);
297         if (dev == NULL) {
298                 fprintf(stderr, "unable to access '%s'\n", devpath);
299                 rc = 3;
300                 goto exit;
301         }
302
303         path = NULL;
304         path_suffix = NULL;
305
306         parent = dev;
307         while (parent != NULL) {
308                 const char *subsys;
309
310                 subsys = udev_device_get_subsystem(parent);
311
312                 if (subsys == NULL) {
313                         ;
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) {
319                         ; //handle_fc();
320                 } else if (strcmp(subsys, "sas_end_device") == 0) {
321                         ; //handle_sas();
322                 } else if (strcmp(subsys, "iscsi_session") == 0) {
323                         ; //handle_iscsi()
324                 } else if (strcmp(subsys, "ccw") == 0) {
325                         handle_ccw(parent, dev, &path);
326                 } else if (strcmp(subsys, "cciss") == 0) {
327                         ; //handle_cciss();
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");
344                 }
345
346                 parent = udev_device_get_parent(parent);
347         }
348
349         if (path != NULL) {
350                 if (path_suffix != NULL) {
351                         printf("ID_PATH=%s%s\n", path, path_suffix);
352                         free(path_suffix);
353                 } else {
354                         printf("ID_PATH=%s\n", path);
355                 }
356                 free(path);
357                 rc = 0;
358         }
359
360         udev_device_unref(dev);
361 exit:
362         udev_unref(udev);
363         logging_close();
364         return rc;
365 }