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