]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-builtin-path_id.c
networkd: add and expose per-link LLMNR config option
[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
9ec6e95b 335static struct udev_device *handle_scsi(struct udev_device *parent, char **path) {
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);
349 goto out;
350 }
351
59d86149 352 /* scsi sysfs does not have a "subsystem" for the transport */
912541b0
KS
353 name = udev_device_get_syspath(parent);
354
355 if (strstr(name, "/rport-") != NULL) {
356 parent = handle_scsi_fibre_channel(parent, path);
357 goto out;
358 }
359
360 if (strstr(name, "/end_device-") != NULL) {
361 parent = handle_scsi_sas(parent, path);
362 goto out;
363 }
364
365 if (strstr(name, "/session") != NULL) {
366 parent = handle_scsi_iscsi(parent, path);
367 goto out;
368 }
369
481dcf7c 370 /*
59d86149
KS
371 * We do not support the ATA transport class, it uses global counters
372 * to name the ata devices which numbers spread across multiple
373 * controllers.
374 *
375 * The real link numbers are not exported. Also, possible chains of ports
376 * behind port multipliers cannot be composed that way.
377 *
378 * Until all that is solved at the kernel level, there are no by-path/
379 * links for ATA devices.
481dcf7c
KS
380 */
381 if (strstr(name, "/ata") != NULL) {
382 parent = NULL;
383 goto out;
384 }
385
a24d03b8
HR
386 if (strstr(name, "/vmbus_") != NULL) {
387 parent = handle_scsi_hyperv(parent, path);
388 goto out;
389 }
390
912541b0 391 parent = handle_scsi_default(parent, path);
d7867b31 392out:
912541b0 393 return parent;
d7867b31
KS
394}
395
9ec6e95b 396static struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
68acb21d
HR
397 const char *str;
398 unsigned int controller, disk;
399
400 str = udev_device_get_sysname(parent);
401 if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
402 return NULL;
403
404 path_prepend(path, "cciss-disk%u", disk);
405 parent = skip_subsystem(parent, "cciss");
406 return parent;
407}
408
9ec6e95b 409static void handle_scsi_tape(struct udev_device *dev, char **path) {
912541b0 410 const char *name;
d7867b31 411
912541b0
KS
412 /* must be the last device in the syspath */
413 if (*path != NULL)
414 return;
d7867b31 415
912541b0 416 name = udev_device_get_sysname(dev);
33502ffe 417 if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
912541b0 418 path_prepend(path, "nst%c", name[3]);
33502ffe 419 else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
912541b0 420 path_prepend(path, "st%c", name[2]);
d7867b31
KS
421}
422
9ec6e95b 423static struct udev_device *handle_usb(struct udev_device *parent, char **path) {
912541b0
KS
424 const char *devtype;
425 const char *str;
426 const char *port;
427
428 devtype = udev_device_get_devtype(parent);
429 if (devtype == NULL)
430 return parent;
090be865 431 if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
912541b0
KS
432 return parent;
433
434 str = udev_device_get_sysname(parent);
435 port = strchr(str, '-');
436 if (port == NULL)
437 return parent;
438 port++;
439
440 parent = skip_subsystem(parent, "usb");
441 path_prepend(path, "usb-0:%s", port);
442 return parent;
d7867b31
KS
443}
444
9ec6e95b 445static struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
89f17d4f
TG
446 const char *sysname;
447 unsigned int core;
448
449 sysname = udev_device_get_sysname(parent);
450 if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
451 return NULL;
452
453 path_prepend(path, "bcma-%u", core);
454 return parent;
455}
456
9ec6e95b 457static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path) {
912541b0
KS
458 struct udev_device *scsi_dev;
459
460 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
461 if (scsi_dev != NULL) {
462 const char *wwpn;
463 const char *lun;
464 const char *hba_id;
465
466 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
467 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
468 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
469 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
470 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
471 goto out;
472 }
473 }
474
475 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
d7867b31 476out:
912541b0
KS
477 parent = skip_subsystem(parent, "ccw");
478 return parent;
d7867b31
KS
479}
480
9ec6e95b 481static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
912541b0
KS
482 struct udev_device *parent;
483 char *path = NULL;
7fdd367e 484 bool some_transport = false;
912541b0
KS
485
486 /* S390 ccw bus */
487 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
488 if (parent != NULL) {
489 handle_ccw(parent, dev, &path);
490 goto out;
491 }
492
493 /* walk up the chain of devices and compose path */
494 parent = dev;
495 while (parent != NULL) {
496 const char *subsys;
497
498 subsys = udev_device_get_subsystem(parent);
499 if (subsys == NULL) {
500 ;
090be865 501 } else if (streq(subsys, "scsi_tape")) {
912541b0 502 handle_scsi_tape(parent, &path);
090be865 503 } else if (streq(subsys, "scsi")) {
912541b0 504 parent = handle_scsi(parent, &path);
7fdd367e 505 some_transport = true;
090be865 506 } else if (streq(subsys, "cciss")) {
68acb21d 507 parent = handle_cciss(parent, &path);
9e055fb8 508 some_transport = true;
090be865 509 } else if (streq(subsys, "usb")) {
912541b0 510 parent = handle_usb(parent, &path);
7fdd367e 511 some_transport = true;
89f17d4f
TG
512 } else if (streq(subsys, "bcma")) {
513 parent = handle_bcma(parent, &path);
514 some_transport = true;
090be865 515 } else if (streq(subsys, "serio")) {
912541b0
KS
516 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
517 parent = skip_subsystem(parent, "serio");
090be865 518 } else if (streq(subsys, "pci")) {
912541b0
KS
519 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
520 parent = skip_subsystem(parent, "pci");
090be865 521 } else if (streq(subsys, "platform")) {
912541b0
KS
522 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
523 parent = skip_subsystem(parent, "platform");
2a3fe9a7 524 some_transport = true;
090be865 525 } else if (streq(subsys, "acpi")) {
912541b0
KS
526 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
527 parent = skip_subsystem(parent, "acpi");
090be865 528 } else if (streq(subsys, "xen")) {
912541b0
KS
529 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
530 parent = skip_subsystem(parent, "xen");
090be865 531 } else if (streq(subsys, "scm")) {
4ecc1318
SO
532 path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
533 parent = skip_subsystem(parent, "scm");
dd8b2bf4 534 some_transport = true;
912541b0
KS
535 }
536
537 parent = udev_device_get_parent(parent);
538 }
7fdd367e
KS
539
540 /*
541 * Do not return a single-parent-device-only for block
542 * devices, they might have entire buses behind it which
543 * do not get unique IDs only by using the parent device.
544 */
545 if (!some_transport && streq(udev_device_get_subsystem(dev), "block")) {
546 free(path);
547 path = NULL;
548 }
549
d7867b31 550out:
912541b0
KS
551 if (path != NULL) {
552 char tag[UTIL_NAME_SIZE];
553 size_t i;
554 const char *p;
555
556 /* compose valid udev tag name */
557 for (p = path, i = 0; *p; p++) {
558 if ((*p >= '0' && *p <= '9') ||
559 (*p >= 'A' && *p <= 'Z') ||
560 (*p >= 'a' && *p <= 'z') ||
561 *p == '-') {
562 tag[i++] = *p;
563 continue;
564 }
565
566 /* skip all leading '_' */
567 if (i == 0)
568 continue;
569
570 /* avoid second '_' */
571 if (tag[i-1] == '_')
572 continue;
573
574 tag[i++] = '_';
575 }
576 /* strip trailing '_' */
577 while (i > 0 && tag[i-1] == '_')
578 i--;
579 tag[i] = '\0';
580
581 udev_builtin_add_property(dev, test, "ID_PATH", path);
582 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
583 free(path);
584 return EXIT_SUCCESS;
585 }
586 return EXIT_FAILURE;
d7867b31
KS
587}
588
589const struct udev_builtin udev_builtin_path_id = {
912541b0
KS
590 .name = "path_id",
591 .cmd = builtin_path_id,
592 .help = "compose persistent device path",
593 .run_once = true,
d7867b31 594};