]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-path_id.c
networkd: fix compile
[thirdparty/systemd.git] / src / udev / udev-builtin-path_id.c
CommitLineData
d7867b31
KS
1/*
2 * compose persistent device path
3 *
1298001e 4 * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
d7867b31
KS
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 "udev.h"
34
9091e686 35_printf_(2,3)
9ec6e95b 36static int path_prepend(char **path, const char *fmt, ...) {
912541b0
KS
37 va_list va;
38 char *pre;
39 int err = 0;
40
41 va_start(va, fmt);
42 err = vasprintf(&pre, fmt, va);
43 va_end(va);
44 if (err < 0)
45 goto out;
46
47 if (*path != NULL) {
48 char *new;
49
50 err = asprintf(&new, "%s-%s", pre, *path);
51 free(pre);
52 if (err < 0)
53 goto out;
54 free(*path);
55 *path = new;
56 } else {
57 *path = pre;
58 }
d7867b31 59out:
912541b0 60 return err;
d7867b31
KS
61}
62
63/*
64** Linux only supports 32 bit luns.
65** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
66*/
9ec6e95b 67static int format_lun_number(struct udev_device *dev, char **path) {
912541b0 68 unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
d7867b31 69
912541b0
KS
70 /* address method 0, peripheral device addressing with bus id of zero */
71 if (lun < 256)
d9de321f 72 return path_prepend(path, "lun-%lu", lun);
912541b0 73 /* handle all other lun addressing methods by using a variant of the original lun format */
d9de321f 74 return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
d7867b31
KS
75}
76
9ec6e95b 77static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
912541b0
KS
78 struct udev_device *parent = dev;
79
80 while (parent != NULL) {
81 const char *subsystem;
82
83 subsystem = udev_device_get_subsystem(parent);
090be865 84 if (subsystem == NULL || !streq(subsystem, subsys))
912541b0
KS
85 break;
86 dev = parent;
87 parent = udev_device_get_parent(parent);
88 }
89 return dev;
d7867b31
KS
90}
91
9ec6e95b 92static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) {
912541b0
KS
93 struct udev *udev = udev_device_get_udev(parent);
94 struct udev_device *targetdev;
95 struct udev_device *fcdev = NULL;
96 const char *port;
33b40551 97 char *lun = NULL;
912541b0
KS
98
99 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
100 if (targetdev == NULL)
101 return NULL;
102
103 fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
104 if (fcdev == NULL)
105 return NULL;
106 port = udev_device_get_sysattr_value(fcdev, "port_name");
107 if (port == NULL) {
108 parent = NULL;
109 goto out;
110 }
111
112 format_lun_number(parent, &lun);
113 path_prepend(path, "fc-%s-%s", port, lun);
114 if (lun)
115 free(lun);
d7867b31 116out:
912541b0
KS
117 udev_device_unref(fcdev);
118 return parent;
d7867b31
KS
119}
120
9ec6e95b 121static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path) {
912541b0
KS
122 struct udev *udev = udev_device_get_udev(parent);
123 struct udev_device *targetdev;
124 struct udev_device *target_parent;
125 struct udev_device *sasdev;
126 const char *sas_address;
127 char *lun = NULL;
128
129 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
130 if (targetdev == NULL)
131 return NULL;
132
133 target_parent = udev_device_get_parent(targetdev);
134 if (target_parent == NULL)
135 return NULL;
136
137 sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
138 udev_device_get_sysname(target_parent));
139 if (sasdev == NULL)
140 return NULL;
141
142 sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
143 if (sas_address == NULL) {
144 parent = NULL;
145 goto out;
146 }
147
148 format_lun_number(parent, &lun);
149 path_prepend(path, "sas-%s-%s", sas_address, lun);
150 if (lun)
151 free(lun);
d7867b31 152out:
912541b0
KS
153 udev_device_unref(sasdev);
154 return parent;
d7867b31
KS
155}
156
9ec6e95b 157static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
912541b0
KS
158 struct udev *udev = udev_device_get_udev(parent);
159 struct udev_device *transportdev;
160 struct udev_device *sessiondev = NULL;
161 const char *target;
162 char *connname;
163 struct udev_device *conndev = NULL;
164 const char *addr;
165 const char *port;
166 char *lun = NULL;
167
168 /* find iscsi session */
169 transportdev = parent;
170 for (;;) {
171 transportdev = udev_device_get_parent(transportdev);
172 if (transportdev == NULL)
173 return NULL;
33502ffe 174 if (startswith(udev_device_get_sysname(transportdev), "session"))
912541b0
KS
175 break;
176 }
177
178 /* find iscsi session device */
179 sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
180 if (sessiondev == NULL)
181 return NULL;
182 target = udev_device_get_sysattr_value(sessiondev, "targetname");
183 if (target == NULL) {
184 parent = NULL;
185 goto out;
186 }
187
188 if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
189 parent = NULL;
190 goto out;
191 }
192 conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
193 free(connname);
194 if (conndev == NULL) {
195 parent = NULL;
196 goto out;
197 }
198 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
199 port = udev_device_get_sysattr_value(conndev, "persistent_port");
200 if (addr == NULL || port == NULL) {
201 parent = NULL;
202 goto out;
203 }
204
205 format_lun_number(parent, &lun);
206 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
207 if (lun)
208 free(lun);
d7867b31 209out:
912541b0
KS
210 udev_device_unref(sessiondev);
211 udev_device_unref(conndev);
212 return parent;
d7867b31
KS
213}
214
9ec6e95b 215static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
912541b0
KS
216 struct udev_device *hostdev;
217 int host, bus, target, lun;
218 const char *name;
219 char *base;
220 char *pos;
221 DIR *dir;
222 struct dirent *dent;
223 int basenum;
224
225 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
226 if (hostdev == NULL)
227 return NULL;
228
229 name = udev_device_get_sysname(parent);
230 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
231 return NULL;
232
1c7dfbf2
KS
233 /*
234 * Rebase host offset to get the local relative number
235 *
236 * Note: This is by definition racy, unreliable and too simple.
237 * Please do not copy this model anywhere. It's just a left-over
238 * from the time we had no idea how things should look like in
239 * the end.
240 *
241 * Making assumptions about a global in-kernel counter and use
242 * that to calculate a local offset is a very broken concept. It
243 * can only work as long as things are in strict order.
244 *
245 * The kernel needs to export the instance/port number of a
246 * controller directly, without the need for rebase magic like
247 * this. Manual driver unbind/bind, parallel hotplug/unplug will
248 * get into the way of this "I hope it works" logic.
249 */
912541b0
KS
250 basenum = -1;
251 base = strdup(udev_device_get_syspath(hostdev));
252 if (base == NULL)
253 return NULL;
254 pos = strrchr(base, '/');
255 if (pos == NULL) {
256 parent = NULL;
257 goto out;
258 }
259 pos[0] = '\0';
260 dir = opendir(base);
261 if (dir == NULL) {
262 parent = NULL;
263 goto out;
264 }
265 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
266 char *rest;
267 int i;
268
269 if (dent->d_name[0] == '.')
270 continue;
271 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
272 continue;
33502ffe 273 if (!startswith(dent->d_name, "host"))
912541b0
KS
274 continue;
275 i = strtoul(&dent->d_name[4], &rest, 10);
276 if (rest[0] != '\0')
277 continue;
746b5152
KS
278 /*
279 * find the smallest number; the host really needs to export its
280 * own instance number per parent device; relying on the global host
281 * enumeration and plainly rebasing the numbers sounds unreliable
282 */
912541b0
KS
283 if (basenum == -1 || i < basenum)
284 basenum = i;
285 }
286 closedir(dir);
287 if (basenum == -1) {
288 parent = NULL;
289 goto out;
290 }
291 host -= basenum;
292
293 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
d7867b31 294out:
912541b0
KS
295 free(base);
296 return hostdev;
d7867b31
KS
297}
298
a24d03b8
HR
299static struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
300 struct udev_device *hostdev;
301 struct udev_device *vmbusdev;
302 const char *guid_str;
303 char *lun = NULL;
304 char guid[38];
305 size_t i, k;
306
307 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
308 if (!hostdev)
309 return NULL;
310
311 vmbusdev = udev_device_get_parent(hostdev);
312 if (!vmbusdev)
313 return NULL;
314
315 guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
316 if (!guid_str)
317 return NULL;
318
319 if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
320 return NULL;
321
322 for (i = 1, k = 0; i < 36; i++) {
323 if (guid_str[i] == '-')
324 continue;
325 guid[k++] = guid_str[i];
326 }
327 guid[k] = '\0';
328
329 format_lun_number(parent, &lun);
330 path_prepend(path, "vmbus-%s-%s", guid, lun);
331 free(lun);
332 return parent;
333}
334
cc821d02 335static struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) {
912541b0
KS
336 const char *devtype;
337 const char *name;
338 const char *id;
339
340 devtype = udev_device_get_devtype(parent);
090be865 341 if (devtype == NULL || !streq(devtype, "scsi_device"))
912541b0
KS
342 return parent;
343
344 /* firewire */
345 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
346 if (id != NULL) {
347 parent = skip_subsystem(parent, "scsi");
348 path_prepend(path, "ieee1394-0x%s", id);
cc821d02 349 *supported_parent = true;
912541b0
KS
350 goto out;
351 }
352
59d86149 353 /* scsi sysfs does not have a "subsystem" for the transport */
912541b0
KS
354 name = udev_device_get_syspath(parent);
355
356 if (strstr(name, "/rport-") != NULL) {
357 parent = handle_scsi_fibre_channel(parent, path);
cc821d02 358 *supported_parent = true;
912541b0
KS
359 goto out;
360 }
361
362 if (strstr(name, "/end_device-") != NULL) {
363 parent = handle_scsi_sas(parent, path);
cc821d02 364 *supported_parent = true;
912541b0
KS
365 goto out;
366 }
367
368 if (strstr(name, "/session") != NULL) {
369 parent = handle_scsi_iscsi(parent, path);
cc821d02 370 *supported_parent = true;
912541b0
KS
371 goto out;
372 }
373
481dcf7c 374 /*
59d86149
KS
375 * We do not support the ATA transport class, it uses global counters
376 * to name the ata devices which numbers spread across multiple
377 * controllers.
378 *
379 * The real link numbers are not exported. Also, possible chains of ports
380 * behind port multipliers cannot be composed that way.
381 *
382 * Until all that is solved at the kernel level, there are no by-path/
383 * links for ATA devices.
481dcf7c
KS
384 */
385 if (strstr(name, "/ata") != NULL) {
386 parent = NULL;
387 goto out;
388 }
389
a24d03b8
HR
390 if (strstr(name, "/vmbus_") != NULL) {
391 parent = handle_scsi_hyperv(parent, path);
392 goto out;
393 }
394
912541b0 395 parent = handle_scsi_default(parent, path);
d7867b31 396out:
912541b0 397 return parent;
d7867b31
KS
398}
399
9ec6e95b 400static struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
68acb21d
HR
401 const char *str;
402 unsigned int controller, disk;
403
404 str = udev_device_get_sysname(parent);
405 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
406 return NULL;
407
408 path_prepend(path, "cciss-disk%u", disk);
409 parent = skip_subsystem(parent, "cciss");
410 return parent;
411}
412
9ec6e95b 413static void handle_scsi_tape(struct udev_device *dev, char **path) {
912541b0 414 const char *name;
d7867b31 415
912541b0
KS
416 /* must be the last device in the syspath */
417 if (*path != NULL)
418 return;
d7867b31 419
912541b0 420 name = udev_device_get_sysname(dev);
33502ffe 421 if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
912541b0 422 path_prepend(path, "nst%c", name[3]);
33502ffe 423 else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
912541b0 424 path_prepend(path, "st%c", name[2]);
d7867b31
KS
425}
426
9ec6e95b 427static struct udev_device *handle_usb(struct udev_device *parent, char **path) {
912541b0
KS
428 const char *devtype;
429 const char *str;
430 const char *port;
431
432 devtype = udev_device_get_devtype(parent);
433 if (devtype == NULL)
434 return parent;
090be865 435 if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
912541b0
KS
436 return parent;
437
438 str = udev_device_get_sysname(parent);
439 port = strchr(str, '-');
440 if (port == NULL)
441 return parent;
442 port++;
443
444 parent = skip_subsystem(parent, "usb");
445 path_prepend(path, "usb-0:%s", port);
446 return parent;
d7867b31
KS
447}
448
9ec6e95b 449static struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
89f17d4f
TG
450 const char *sysname;
451 unsigned int core;
452
453 sysname = udev_device_get_sysname(parent);
454 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
455 return NULL;
456
457 path_prepend(path, "bcma-%u", core);
458 return parent;
459}
460
9ec6e95b 461static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path) {
912541b0
KS
462 struct udev_device *scsi_dev;
463
464 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
465 if (scsi_dev != NULL) {
466 const char *wwpn;
467 const char *lun;
468 const char *hba_id;
469
470 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
471 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
472 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
473 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
474 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
475 goto out;
476 }
477 }
478
479 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
d7867b31 480out:
912541b0
KS
481 parent = skip_subsystem(parent, "ccw");
482 return parent;
d7867b31
KS
483}
484
9ec6e95b 485static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
912541b0
KS
486 struct udev_device *parent;
487 char *path = NULL;
e98bbfd2
KS
488 bool supported_transport = false;
489 bool supported_parent = false;
912541b0
KS
490
491 /* S390 ccw bus */
492 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
493 if (parent != NULL) {
494 handle_ccw(parent, dev, &path);
495 goto out;
496 }
497
498 /* walk up the chain of devices and compose path */
499 parent = dev;
500 while (parent != NULL) {
501 const char *subsys;
502
503 subsys = udev_device_get_subsystem(parent);
504 if (subsys == NULL) {
505 ;
090be865 506 } else if (streq(subsys, "scsi_tape")) {
912541b0 507 handle_scsi_tape(parent, &path);
090be865 508 } else if (streq(subsys, "scsi")) {
cc821d02 509 parent = handle_scsi(parent, &path, &supported_parent);
e98bbfd2 510 supported_transport = true;
090be865 511 } else if (streq(subsys, "cciss")) {
68acb21d 512 parent = handle_cciss(parent, &path);
e98bbfd2 513 supported_transport = true;
090be865 514 } else if (streq(subsys, "usb")) {
912541b0 515 parent = handle_usb(parent, &path);
e98bbfd2 516 supported_transport = true;
89f17d4f
TG
517 } else if (streq(subsys, "bcma")) {
518 parent = handle_bcma(parent, &path);
e98bbfd2 519 supported_transport = true;
090be865 520 } else if (streq(subsys, "serio")) {
912541b0
KS
521 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
522 parent = skip_subsystem(parent, "serio");
090be865 523 } else if (streq(subsys, "pci")) {
912541b0
KS
524 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
525 parent = skip_subsystem(parent, "pci");
e98bbfd2 526 supported_parent = true;
090be865 527 } else if (streq(subsys, "platform")) {
912541b0
KS
528 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
529 parent = skip_subsystem(parent, "platform");
e98bbfd2
KS
530 supported_transport = true;
531 supported_parent = true;
090be865 532 } else if (streq(subsys, "acpi")) {
912541b0
KS
533 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
534 parent = skip_subsystem(parent, "acpi");
e98bbfd2 535 supported_parent = true;
090be865 536 } else if (streq(subsys, "xen")) {
912541b0
KS
537 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
538 parent = skip_subsystem(parent, "xen");
e98bbfd2 539 supported_parent = true;
090be865 540 } else if (streq(subsys, "scm")) {
4ecc1318
SO
541 path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
542 parent = skip_subsystem(parent, "scm");
e98bbfd2
KS
543 supported_transport = true;
544 supported_parent = true;
912541b0
KS
545 }
546
547 parent = udev_device_get_parent(parent);
548 }
7fdd367e
KS
549
550 /*
a42cdff1
KS
551 * Do not return devices with an unknown parent device type. They
552 * might produce conflicting IDs if the parent does not provide a
553 * unique and predictable name.
7fdd367e 554 */
e98bbfd2
KS
555 if (!supported_parent) {
556 free(path);
557 path = NULL;
558 }
559
560 /*
a42cdff1
KS
561 * Do not return block devices without a well-known transport. Some
562 * devices do not expose their buses and do not provide a unique
563 * and predictable name that way.
e98bbfd2
KS
564 */
565 if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) {
7fdd367e
KS
566 free(path);
567 path = NULL;
568 }
569
d7867b31 570out:
912541b0
KS
571 if (path != NULL) {
572 char tag[UTIL_NAME_SIZE];
573 size_t i;
574 const char *p;
575
576 /* compose valid udev tag name */
577 for (p = path, i = 0; *p; p++) {
578 if ((*p >= '0' && *p <= '9') ||
579 (*p >= 'A' && *p <= 'Z') ||
580 (*p >= 'a' && *p <= 'z') ||
581 *p == '-') {
582 tag[i++] = *p;
583 continue;
584 }
585
586 /* skip all leading '_' */
587 if (i == 0)
588 continue;
589
590 /* avoid second '_' */
591 if (tag[i-1] == '_')
592 continue;
593
594 tag[i++] = '_';
595 }
596 /* strip trailing '_' */
597 while (i > 0 && tag[i-1] == '_')
598 i--;
599 tag[i] = '\0';
600
601 udev_builtin_add_property(dev, test, "ID_PATH", path);
602 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
603 free(path);
604 return EXIT_SUCCESS;
605 }
606 return EXIT_FAILURE;
d7867b31
KS
607}
608
609const struct udev_builtin udev_builtin_path_id = {
912541b0
KS
610 .name = "path_id",
611 .cmd = builtin_path_id,
612 .help = "compose persistent device path",
613 .run_once = true,
d7867b31 614};