udev-builtin-path_id.c revision dc4ebc0787a299f064553e6d28a7f35574ce1c2a
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * compose persistent device path
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Logic based on Hannes Reinecke's shell script.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is free software: you can redistribute it and/or modify
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * it under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is distributed in the hope that it will be useful,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * GNU General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering#include <stdio.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdlib.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdarg.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <ctype.h>
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include <fcntl.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <errno.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <dirent.h>
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include <getopt.h>
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering#include "udev.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_printf_(2,3)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int path_prepend(char **path, const char *fmt, ...) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_list va;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *pre;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering int err = 0;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen va_start(va, fmt);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = vasprintf(&pre, fmt, va);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_end(va);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (err < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (*path != NULL) {
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering char *new;
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering err = asprintf(&new, "%s-%s", pre, *path);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering free(pre);
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering if (err < 0)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering goto out;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering free(*path);
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier *path = new;
288a74cce597f81d3ba01d8a5ca7d2ba5b654b7eRonny Chevalier } else {
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering *path = pre;
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering }
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return err;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering** Linux only supports 32 bit luns.
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int format_lun_number(struct udev_device *dev, char **path) {
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering /* address method 0, peripheral device addressing with bus id of zero */
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering if (lun < 256)
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering return path_prepend(path, "lun-%lu", lun);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering /* handle all other lun addressing methods by using a variant of the original lun format */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poetteringstatic struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering struct udev_device *parent = dev;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering assert(dev);
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering assert(subsys);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while (parent != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *subsystem;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsystem = udev_device_get_subsystem(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (subsystem == NULL || !streq(subsystem, subsys))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering break;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dev = parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = udev_device_get_parent(parent);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering }
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return dev;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering}
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering struct udev *udev = udev_device_get_udev(parent);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering struct udev_device *targetdev;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering struct udev_device *fcdev = NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering const char *port;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering char *lun = NULL;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering assert(parent);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering assert(path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (targetdev == NULL)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (fcdev == NULL)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering port = udev_device_get_sysattr_value(fcdev, "port_name");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (port == NULL) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering parent = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering goto out;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_lun_number(parent, &lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering path_prepend(path, "fc-%s-%s", port, lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering free(lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringout:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering udev_device_unref(fcdev);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return parent;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringstatic struct udev_device *handle_scsi_sas_wide_port(struct udev_device *parent, char **path) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev *udev = udev_device_get_udev(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev_device *targetdev;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *target_parent;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *sasdev;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *sas_address;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering char *lun = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(path);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (targetdev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen target_parent = udev_device_get_parent(targetdev);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (target_parent == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen udev_device_get_sysname(target_parent));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sasdev == NULL)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sas_address == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering format_lun_number(parent, &lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "sas-%s-%s", sas_address, lun);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringout:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering udev_device_unref(sasdev);
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering return parent;
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering}
fee6d013d859bc66f5c993530898fece53fab06dLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering{
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *targetdev;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *target_parent;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *port;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *expander;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *target_sasdev = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *expander_sasdev = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering struct udev_device *port_sasdev = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *sas_address = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *phy_id;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering const char *phy_count;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering char *lun = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(parent);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(path);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (targetdev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering target_parent = udev_device_get_parent(targetdev);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (target_parent == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering /* Get sas device */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering target_sasdev = udev_device_new_from_subsystem_sysname(udev,
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "sas_device", udev_device_get_sysname(target_parent));
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (target_sasdev == NULL)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* The next parent is sas port */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering port = udev_device_get_parent(target_parent);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (port == NULL) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering parent = NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering goto out;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* Get port device */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering port_sasdev = udev_device_new_from_subsystem_sysname(udev,
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "sas_port", udev_device_get_sysname(port));
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering phy_count = udev_device_get_sysattr_value(port_sasdev, "num_phys");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (phy_count == NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Check if we are simple disk */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (strncmp(phy_count, "1", 2) != 0) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = handle_scsi_sas_wide_port(parent, path);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering goto out;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Get connected phy */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering phy_id = udev_device_get_sysattr_value(target_sasdev, "phy_identifier");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (phy_id == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* The port's parent is either hba or expander */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering expander = udev_device_get_parent(port);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (expander == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Get expander device */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering expander_sasdev = udev_device_new_from_subsystem_sysname(udev,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "sas_device", udev_device_get_sysname(expander));
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (expander_sasdev != NULL) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* Get expander's address */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sas_address = udev_device_get_sysattr_value(expander_sasdev,
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering "sas_address");
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (sas_address == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering format_lun_number(parent, &lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (sas_address)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering path_prepend(path, "sas-exp%s-phy%s-%s", sas_address, phy_id, lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering else
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering path_prepend(path, "sas-phy%s-%s", phy_id, lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering free(lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringout:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_device_unref(target_sasdev);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_device_unref(expander_sasdev);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_device_unref(port_sasdev);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return parent;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering struct udev_device *transportdev;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering struct udev_device *sessiondev = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *target;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering char *connname;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev_device *conndev = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *addr;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *port;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering char *lun = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(parent);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* find iscsi session */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering transportdev = parent;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering for (;;) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering transportdev = udev_device_get_parent(transportdev);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (transportdev == NULL)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (startswith(udev_device_get_sysname(transportdev), "session"))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering break;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* find iscsi session device */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (sessiondev == NULL)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering target = udev_device_get_sysattr_value(sessiondev, "targetname");
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (target == NULL) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering parent = NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering goto out;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering parent = NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering free(connname);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (conndev == NULL) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering parent = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering goto out;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering addr = udev_device_get_sysattr_value(conndev, "persistent_address");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering port = udev_device_get_sysattr_value(conndev, "persistent_port");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (addr == NULL || port == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering format_lun_number(parent, &lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering free(lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringout:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_device_unref(sessiondev);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering udev_device_unref(conndev);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return parent;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering}
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringstatic struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering struct udev_device *hostdev;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering int host, bus, target, lun;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *name;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering char *base;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering char *pos;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering DIR *dir;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering struct dirent *dent;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering int basenum;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poettering assert(parent);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(path);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (hostdev == NULL)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering name = udev_device_get_sysname(parent);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /*
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering * Rebase host offset to get the local relative number
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen *
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Note: This is by definition racy, unreliable and too simple.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * Please do not copy this model anywhere. It's just a left-over
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * from the time we had no idea how things should look like in
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering * the end.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering *
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * Making assumptions about a global in-kernel counter and use
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * that to calculate a local offset is a very broken concept. It
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * can only work as long as things are in strict order.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering *
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering * The kernel needs to export the instance/port number of a
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * controller directly, without the need for rebase magic like
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * this. Manual driver unbind/bind, parallel hotplug/unplug will
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * get into the way of this "I hope it works" logic.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering basenum = -1;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering base = strdup(udev_device_get_syspath(hostdev));
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (base == NULL)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering pos = strrchr(base, '/');
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (pos == NULL) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering parent = NULL;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering goto out;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering pos[0] = '\0';
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen dir = opendir(base);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering if (dir == NULL) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering parent = NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto out;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char *rest;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering int i;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (dent->d_name[0] == '.')
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering continue;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering continue;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (!startswith(dent->d_name, "host"))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering continue;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering i = strtoul(&dent->d_name[4], &rest, 10);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (rest[0] != '\0')
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering continue;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /*
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * find the smallest number; the host really needs to export its
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering * own instance number per parent device; relying on the global host
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * enumeration and plainly rebasing the numbers sounds unreliable
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (basenum == -1 || i < basenum)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering basenum = i;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering closedir(dir);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (basenum == -1) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering parent = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering goto out;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering host -= basenum;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringout:
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(base);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return hostdev;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering struct udev_device *hostdev;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering struct udev_device *vmbusdev;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const char *guid_str;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char *lun = NULL;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering char guid[38];
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering size_t i, k;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(parent);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering assert(path);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!hostdev)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering vmbusdev = udev_device_get_parent(hostdev);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!vmbusdev)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!guid_str)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering for (i = 1, k = 0; i < 36; i++) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (guid_str[i] == '-')
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering continue;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering guid[k++] = guid_str[i];
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering guid[k] = '\0';
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering format_lun_number(parent, &lun);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering path_prepend(path, "vmbus-%s-%s", guid, lun);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(lun);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return parent;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poetteringstatic struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const char *devtype;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const char *name;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const char *id;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering devtype = udev_device_get_devtype(parent);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (devtype == NULL || !streq(devtype, "scsi_device"))
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return parent;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* firewire */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering id = udev_device_get_sysattr_value(parent, "ieee1394_id");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (id != NULL) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering parent = skip_subsystem(parent, "scsi");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering path_prepend(path, "ieee1394-0x%s", id);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *supported_parent = true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering goto out;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* scsi sysfs does not have a "subsystem" for the transport */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering name = udev_device_get_syspath(parent);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (strstr(name, "/rport-") != NULL) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering parent = handle_scsi_fibre_channel(parent, path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *supported_parent = true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering goto out;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (strstr(name, "/end_device-") != NULL) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering parent = handle_scsi_sas(parent, path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *supported_parent = true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering goto out;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (strstr(name, "/session") != NULL) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering parent = handle_scsi_iscsi(parent, path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *supported_parent = true;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering goto out;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /*
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * We do not support the ATA transport class, it uses global counters
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * to name the ata devices which numbers spread across multiple
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * controllers.
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * The real link numbers are not exported. Also, possible chains of ports
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * behind port multipliers cannot be composed that way.
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering *
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * Until all that is solved at the kernel level, there are no by-path/
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * links for ATA devices.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (strstr(name, "/ata") != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers goto out;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (strstr(name, "/vmbus_") != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_hyperv(parent, path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering goto out;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_default(parent, path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann return parent;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann}
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmannstatic struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann const char *str;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann unsigned int controller, disk;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann str = udev_device_get_sysname(parent);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann return NULL;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann path_prepend(path, "cciss-disk%u", disk);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = skip_subsystem(parent, "cciss");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic void handle_scsi_tape(struct udev_device *dev, char **path) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering const char *name;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* must be the last device in the syspath */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (*path != NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name = udev_device_get_sysname(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "nst%c", name[3]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(path, "st%c", name[2]);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_usb(struct udev_device *parent, char **path) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *devtype;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *port;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering devtype = udev_device_get_devtype(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (devtype == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering str = udev_device_get_sysname(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering port = strchr(str, '-');
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (port == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering port++;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "usb");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "usb-0:%s", port);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *sysname;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned int core;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sysname = udev_device_get_sysname(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering path_prepend(path, "bcma-%u", core);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering}
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering struct udev_device *scsi_dev;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(dev);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(path);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (scsi_dev != NULL) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering const char *wwpn;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering const char *lun;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering const char *hba_id;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (hba_id != NULL && lun != NULL && wwpn != NULL) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering goto out;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringout:
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering parent = skip_subsystem(parent, "ccw");
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return parent;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringstatic int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering struct udev_device *parent;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering char *path = NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering bool supported_transport = false;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering bool supported_parent = false;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(dev);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering /* S390 ccw bus */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (parent != NULL) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering handle_ccw(parent, dev, &path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering goto out;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering /* walk up the chain of devices and compose path */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while (parent != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *subsys;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering subsys = udev_device_get_subsystem(parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (subsys == NULL) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering ;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "scsi_tape")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering handle_scsi_tape(parent, &path);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "scsi")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = handle_scsi(parent, &path, &supported_parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering supported_transport = true;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "cciss")) {
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek parent = handle_cciss(parent, &path);
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek supported_transport = true;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "usb")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = handle_usb(parent, &path);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering supported_transport = true;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "bcma")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = handle_bcma(parent, &path);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering supported_transport = true;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering } else if (streq(subsys, "serio")) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = skip_subsystem(parent, "serio");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering } else if (streq(subsys, "pci")) {
a6c616024db23fef34152c1432892824a07799ccLennart Poettering path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers parent = skip_subsystem(parent, "pci");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_parent = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering } else if (streq(subsys, "platform")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = skip_subsystem(parent, "platform");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_transport = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_parent = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering } else if (streq(subsys, "acpi")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = skip_subsystem(parent, "acpi");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_parent = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (streq(subsys, "xen")) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
a6c616024db23fef34152c1432892824a07799ccLennart Poettering parent = skip_subsystem(parent, "xen");
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann supported_parent = true;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen } else if (streq(subsys, "scm")) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering parent = skip_subsystem(parent, "scm");
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering supported_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_parent = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers if (parent)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers parent = udev_device_get_parent(parent);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /*
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt * Do not return devices with an unknown parent device type. They
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt * might produce conflicting IDs if the parent does not provide a
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * unique and predictable name.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!supported_parent)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path = mfree(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Do not return block devices without a well-known transport. Some
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers * devices do not expose their buses and do not provide a unique
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers * and predictable name that way.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path = mfree(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringout:
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (path != NULL) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering char tag[UTIL_NAME_SIZE];
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering size_t i;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers const char *p;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* compose valid udev tag name */
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers for (p = path, i = 0; *p; p++) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if ((*p >= '0' && *p <= '9') ||
27e72d6b22890ba4a8cbc05c49667cd1cccf1461Simon Peeters (*p >= 'A' && *p <= 'Z') ||
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen (*p >= 'a' && *p <= 'z') ||
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt *p == '-') {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i++] = *p;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering /* skip all leading '_' */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (i == 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen continue;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /* avoid second '_' */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (tag[i-1] == '_')
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering continue;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i++] = '_';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* strip trailing '_' */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering while (i > 0 && tag[i-1] == '_')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering i--;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i] = '\0';
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen udev_builtin_add_property(dev, test, "ID_PATH", path);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return EXIT_SUCCESS;
8c841f21f5042b11acc91cc1b039cb162cbbe8f4Djalal Harouni }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers return EXIT_FAILURE;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringconst struct udev_builtin udev_builtin_path_id = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .name = "path_id",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .cmd = builtin_path_id,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen .help = "Compose persistent device path",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen .run_once = true,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen};
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen