]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev-builtin-path_id.c
fa4d6fb5fdfec6425d268713a1f265adc880b310
[thirdparty/systemd.git] / src / udev-builtin-path_id.c
1 /*
2 * compose persistent device path
3 *
4 * Copyright (C) 2009-2011 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 "udev.h"
34
35 static int path_prepend(char **path, const char *fmt, ...)
36 {
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 }
59 out:
60 return err;
61 }
62
63 /*
64 ** Linux only supports 32 bit luns.
65 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
66 */
67 static int format_lun_number(struct udev_device *dev, char **path)
68 {
69 unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
70
71 /* address method 0, peripheral device addressing with bus id of zero */
72 if (lun < 256)
73 return path_prepend(path, "lun-%d", lun);
74 /* handle all other lun addressing methods by using a variant of the original lun format */
75 return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
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 char *lun = NULL;;
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 format_lun_number(parent, &lun);
116 path_prepend(path, "fc-%s-%s", port, lun);
117 if (lun)
118 free(lun);
119 out:
120 udev_device_unref(fcdev);
121 return parent;
122 }
123
124 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
125 {
126 struct udev *udev = udev_device_get_udev(parent);
127 struct udev_device *targetdev;
128 struct udev_device *target_parent;
129 struct udev_device *sasdev;
130 const char *sas_address;
131 char *lun = NULL;
132
133 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
134 if (targetdev == NULL)
135 return NULL;
136
137 target_parent = udev_device_get_parent(targetdev);
138 if (target_parent == NULL)
139 return NULL;
140
141 sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
142 udev_device_get_sysname(target_parent));
143 if (sasdev == NULL)
144 return NULL;
145
146 sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
147 if (sas_address == NULL) {
148 parent = NULL;
149 goto out;
150 }
151
152 format_lun_number(parent, &lun);
153 path_prepend(path, "sas-%s-%s", sas_address, lun);
154 if (lun)
155 free(lun);
156 out:
157 udev_device_unref(sasdev);
158 return parent;
159 }
160
161 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
162 {
163 struct udev *udev = udev_device_get_udev(parent);
164 struct udev_device *transportdev;
165 struct udev_device *sessiondev = NULL;
166 const char *target;
167 char *connname;
168 struct udev_device *conndev = NULL;
169 const char *addr;
170 const char *port;
171 char *lun = NULL;
172
173 /* find iscsi session */
174 transportdev = parent;
175 for (;;) {
176 transportdev = udev_device_get_parent(transportdev);
177 if (transportdev == NULL)
178 return NULL;
179 if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
180 break;
181 }
182
183 /* find iscsi session device */
184 sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
185 if (sessiondev == NULL)
186 return NULL;
187 target = udev_device_get_sysattr_value(sessiondev, "targetname");
188 if (target == NULL) {
189 parent = NULL;
190 goto out;
191 }
192
193 if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
194 parent = NULL;
195 goto out;
196 }
197 conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
198 free(connname);
199 if (conndev == NULL) {
200 parent = NULL;
201 goto out;
202 }
203 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
204 port = udev_device_get_sysattr_value(conndev, "persistent_port");
205 if (addr == NULL || port == NULL) {
206 parent = NULL;
207 goto out;
208 }
209
210 format_lun_number(parent, &lun);
211 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
212 if (lun)
213 free(lun);
214 out:
215 udev_device_unref(sessiondev);
216 udev_device_unref(conndev);
217 return parent;
218 }
219
220 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
221 {
222 struct udev_device *hostdev;
223 int host, bus, target, lun;
224 const char *name;
225 char *base;
226 char *pos;
227 DIR *dir;
228 struct dirent *dent;
229 int basenum;
230
231 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
232 if (hostdev == NULL)
233 return NULL;
234
235 name = udev_device_get_sysname(parent);
236 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
237 return NULL;
238
239 /* rebase host offset to get the local relative number */
240 basenum = -1;
241 base = strdup(udev_device_get_syspath(hostdev));
242 if (base == NULL)
243 return NULL;
244 pos = strrchr(base, '/');
245 if (pos == NULL) {
246 parent = NULL;
247 goto out;
248 }
249 pos[0] = '\0';
250 dir = opendir(base);
251 if (dir == NULL) {
252 parent = NULL;
253 goto out;
254 }
255 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
256 char *rest;
257 int i;
258
259 if (dent->d_name[0] == '.')
260 continue;
261 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
262 continue;
263 if (strncmp(dent->d_name, "host", 4) != 0)
264 continue;
265 i = strtoul(&dent->d_name[4], &rest, 10);
266 if (rest[0] != '\0')
267 continue;
268 if (basenum == -1 || i < basenum)
269 basenum = i;
270 }
271 closedir(dir);
272 if (basenum == -1) {
273 parent = NULL;
274 goto out;
275 }
276 host -= basenum;
277
278 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
279 out:
280 free(base);
281 return hostdev;
282 }
283
284 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
285 {
286 const char *devtype;
287 const char *name;
288 const char *id;
289
290 devtype = udev_device_get_devtype(parent);
291 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
292 return parent;
293
294 /* firewire */
295 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
296 if (id != NULL) {
297 parent = skip_subsystem(parent, "scsi");
298 path_prepend(path, "ieee1394-0x%s", id);
299 goto out;
300 }
301
302 /* lousy scsi sysfs does not have a "subsystem" for the transport */
303 name = udev_device_get_syspath(parent);
304
305 if (strstr(name, "/rport-") != NULL) {
306 parent = handle_scsi_fibre_channel(parent, path);
307 goto out;
308 }
309
310 if (strstr(name, "/end_device-") != NULL) {
311 parent = handle_scsi_sas(parent, path);
312 goto out;
313 }
314
315 if (strstr(name, "/session") != NULL) {
316 parent = handle_scsi_iscsi(parent, path);
317 goto out;
318 }
319
320 parent = handle_scsi_default(parent, path);
321 out:
322 return parent;
323 }
324
325 static void handle_scsi_tape(struct udev_device *dev, char **path)
326 {
327 const char *name;
328
329 /* must be the last device in the syspath */
330 if (*path != NULL)
331 return;
332
333 name = udev_device_get_sysname(dev);
334 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
335 path_prepend(path, "nst%c", name[3]);
336 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
337 path_prepend(path, "st%c", name[2]);
338 }
339
340 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
341 {
342 const char *devtype;
343 const char *str;
344 const char *port;
345
346 devtype = udev_device_get_devtype(parent);
347 if (devtype == NULL)
348 return parent;
349 if (strcmp(devtype, "usb_interface") != 0 && strcmp(devtype, "usb_device") != 0)
350 return parent;
351
352 str = udev_device_get_sysname(parent);
353 port = strchr(str, '-');
354 if (port == NULL)
355 return parent;
356 port++;
357
358 parent = skip_subsystem(parent, "usb");
359 path_prepend(path, "usb-0:%s", port);
360 return parent;
361 }
362
363 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
364 {
365 return NULL;
366 }
367
368 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
369 {
370 struct udev_device *scsi_dev;
371
372 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
373 if (scsi_dev != NULL) {
374 const char *wwpn;
375 const char *lun;
376 const char *hba_id;
377
378 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
379 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
380 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
381 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
382 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
383 goto out;
384 }
385 }
386
387 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
388 out:
389 parent = skip_subsystem(parent, "ccw");
390 return parent;
391 }
392
393 static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test)
394 {
395 struct udev_device *parent;
396 char *path = NULL;
397
398 /* S390 ccw bus */
399 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
400 if (parent != NULL) {
401 handle_ccw(parent, dev, &path);
402 goto out;
403 }
404
405 /* walk up the chain of devices and compose path */
406 parent = dev;
407 while (parent != NULL) {
408 const char *subsys;
409
410 subsys = udev_device_get_subsystem(parent);
411 if (subsys == NULL) {
412 ;
413 } else if (strcmp(subsys, "scsi_tape") == 0) {
414 handle_scsi_tape(parent, &path);
415 } else if (strcmp(subsys, "scsi") == 0) {
416 parent = handle_scsi(parent, &path);
417 } else if (strcmp(subsys, "cciss") == 0) {
418 handle_cciss(parent, &path);
419 } else if (strcmp(subsys, "usb") == 0) {
420 parent = handle_usb(parent, &path);
421 } else if (strcmp(subsys, "serio") == 0) {
422 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
423 parent = skip_subsystem(parent, "serio");
424 } else if (strcmp(subsys, "pci") == 0) {
425 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
426 parent = skip_subsystem(parent, "pci");
427 } else if (strcmp(subsys, "platform") == 0) {
428 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
429 parent = skip_subsystem(parent, "platform");
430 } else if (strcmp(subsys, "acpi") == 0) {
431 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
432 parent = skip_subsystem(parent, "acpi");
433 } else if (strcmp(subsys, "xen") == 0) {
434 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
435 parent = skip_subsystem(parent, "xen");
436 } else if (strcmp(subsys, "virtio") == 0) {
437 path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
438 parent = skip_subsystem(parent, "virtio");
439 }
440
441 parent = udev_device_get_parent(parent);
442 }
443 out:
444 if (path != NULL) {
445 char tag[UTIL_NAME_SIZE];
446 size_t i;
447 const char *p;
448
449 /* compose valid udev tag name */
450 for (p = path, i = 0; *p; p++) {
451 if ((*p >= '0' && *p <= '9') ||
452 (*p >= 'A' && *p <= 'Z') ||
453 (*p >= 'a' && *p <= 'z') ||
454 *p == '-') {
455 tag[i++] = *p;
456 continue;
457 }
458
459 /* skip all leading '_' */
460 if (i == 0)
461 continue;
462
463 /* avoid second '_' */
464 if (tag[i-1] == '_')
465 continue;
466
467 tag[i++] = '_';
468 }
469 /* strip trailing '_' */
470 while (i > 0 && tag[i-1] == '_')
471 i--;
472 tag[i] = '\0';
473
474 udev_builtin_add_property(dev, test, "ID_PATH", path);
475 udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
476 free(path);
477 return EXIT_SUCCESS;
478 }
479 return EXIT_FAILURE;
480 }
481
482 const struct udev_builtin udev_builtin_path_id = {
483 .name = "path_id",
484 .cmd = builtin_path_id,
485 .help = "compose persistent device path",
486 .run_once = true,
487 };