udev-builtin-path_id.c revision e7eb5a8d88367a755944fdda3023a308e5272953
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 <ctype.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <dirent.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <fcntl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <getopt.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <stdarg.h>
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include <stdio.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <stdlib.h>
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include <string.h>
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include <unistd.h>
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
eef46c372f64f40dd75415b2c504c73138719c8dLennart Poettering#include "alloc-util.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen#include "string-util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "udev.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering_printf_(2,3)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int path_prepend(char **path, const char *fmt, ...) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering va_list va;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char *pre;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int err = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_start(va, fmt);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering err = vasprintf(&pre, fmt, va);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering va_end(va);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (err < 0)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering goto out;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poettering if (*path != NULL) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering char *new;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering err = asprintf(&new, "%s-%s", pre, *path);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering free(pre);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (err < 0)
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier goto out;
288a74cce597f81d3ba01d8a5ca7d2ba5b654b7eRonny Chevalier free(*path);
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering *path = new;
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering } else {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *path = pre;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return err;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering** Linux only supports 32 bit luns.
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering*/
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poetteringstatic int format_lun_number(struct udev_device *dev, char **path) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering /* address method 0, peripheral device addressing with bus id of zero */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (lun < 256)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return path_prepend(path, "lun-%lu", lun);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering /* handle all other lun addressing methods by using a variant of the original lun format */
6e18cc9fa078d2a967251017ddb5baefb104b720Lennart Poettering return path_prepend(path, "lun-0x%04lx%04lx00000000", lun & 0xffff, (lun >> 16) & 0xffff);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poetteringstatic struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys) {
c454426c54c9beb274f415a80c64a4f1580700e7Lennart Poettering struct udev_device *parent = dev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(dev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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))
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering break;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering dev = parent;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart 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;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(parent);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart 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));
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (fcdev == NULL)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart 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:
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering udev_device_unref(fcdev);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return parent;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart 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;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering struct udev_device *sasdev;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *sas_address;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *lun = NULL;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering assert(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (targetdev == NULL)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen 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
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen udev_device_get_sysname(target_parent));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sasdev == NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering if (sas_address == NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = NULL;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering format_lun_number(parent, &lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering path_prepend(path, "sas-%s-%s", sas_address, lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering free(lun);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_device_unref(sasdev);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return parent;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering{
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart 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;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(parent);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering assert(path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (targetdev == NULL)
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering target_parent = udev_device_get_parent(targetdev);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (target_parent == NULL)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return NULL;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering /* Get sas device */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart 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;
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen goto out;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Get port device */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering port_sasdev = udev_device_new_from_subsystem_sysname(udev,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "sas_port", udev_device_get_sysname(port));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering phy_count = udev_device_get_sysattr_value(port_sasdev, "num_phys");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (phy_count == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering goto out;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart 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);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Get connected phy */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering phy_id = udev_device_get_sysattr_value(target_sasdev, "phy_identifier");
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (phy_id == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* The port's parent is either hba or expander */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering expander = udev_device_get_parent(port);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (expander == NULL) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering parent = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering goto out;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /* Get expander device */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering expander_sasdev = udev_device_new_from_subsystem_sysname(udev,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "sas_device", udev_device_get_sysname(expander));
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (expander_sasdev != NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering /* Get expander's address */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sas_address = udev_device_get_sysattr_value(expander_sasdev,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering "sas_address");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart 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);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering free(lun);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringout:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering udev_device_unref(target_sasdev);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering udev_device_unref(expander_sasdev);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering udev_device_unref(port_sasdev);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return parent;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev_device *transportdev;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart 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;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering const char *port;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering char *lun = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert(parent);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert(path);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* find iscsi session */
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering transportdev = parent;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering for (;;) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering transportdev = udev_device_get_parent(transportdev);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (transportdev == NULL)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (startswith(udev_device_get_sysname(transportdev), "session"))
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering break;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering /* find iscsi session device */
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart 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;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering parent = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering goto out;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering free(connname);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (conndev == NULL) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering parent = NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering goto out;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart 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 }
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering format_lun_number(parent, &lun);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering free(lun);
c19de71113f956809995fc68817e055e9f61f607Lennart Poetteringout:
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering udev_device_unref(sessiondev);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering udev_device_unref(conndev);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return parent;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poetteringstatic struct udev_device *handle_scsi_ata(struct udev_device *parent, char **path) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev_device *targetdev;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering struct udev_device *target_parent;
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poettering struct udev_device *atadev;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering const char *port_no;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering assert(parent);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(path);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!targetdev)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering target_parent = udev_device_get_parent(targetdev);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!target_parent)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen atadev = udev_device_new_from_subsystem_sysname(udev, "ata_port", udev_device_get_sysname(target_parent));
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (!atadev)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering port_no = udev_device_get_sysattr_value(atadev, "port_no");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (!port_no) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering parent = NULL;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering goto out;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering }
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering path_prepend(path, "ata-%s", port_no);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringout:
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering udev_device_unref(atadev);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return parent;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic struct udev_device *handle_scsi_default(struct udev_device *parent, char **path) {
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering struct udev_device *hostdev;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering int host, bus, target, lun;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *name;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char *base;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering char *pos;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering DIR *dir;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct dirent *dent;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int basenum;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering assert(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen assert(path);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (hostdev == NULL)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return NULL;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering name = udev_device_get_sysname(parent);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return NULL;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering /*
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering * Rebase host offset to get the local relative number
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering *
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering * 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
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart 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
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering * can only work as long as things are in strict order.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering *
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * The kernel needs to export the instance/port number of a
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering * controller directly, without the need for rebase magic like
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering * this. Manual driver unbind/bind, parallel hotplug/unplug will
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * get into the way of this "I hope it works" logic.
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering basenum = -1;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering base = strdup(udev_device_get_syspath(hostdev));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (base == NULL)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering pos = strrchr(base, '/');
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (pos == NULL) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering parent = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering goto out;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering pos[0] = '\0';
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering dir = opendir(base);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (dir == NULL) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering parent = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering goto out;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char *rest;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int i;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (dent->d_name[0] == '.')
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering continue;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering continue;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!startswith(dent->d_name, "host"))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering continue;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering i = strtoul(&dent->d_name[4], &rest, 10);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (rest[0] != '\0')
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering continue;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering /*
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * find the smallest number; the host really needs to export its
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * own instance number per parent device; relying on the global host
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering * enumeration and plainly rebasing the numbers sounds unreliable
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (basenum == -1 || i < basenum)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering basenum = i;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering closedir(dir);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (basenum == -1) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart 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;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char guid[38];
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering size_t i, k;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering assert(parent);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(path);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (!hostdev)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering vmbusdev = udev_device_get_parent(hostdev);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (!vmbusdev)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (!guid_str)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering for (i = 1, k = 0; i < 36; i++) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (guid_str[i] == '-')
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering continue;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering guid[k++] = guid_str[i];
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering guid[k] = '\0';
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering format_lun_number(parent, &lun);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering path_prepend(path, "vmbus-%s-%s", guid, lun);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering free(lun);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return parent;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic struct udev_device *handle_scsi(struct udev_device *parent, char **path, bool *supported_parent) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const char *devtype;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const char *name;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const char *id;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering devtype = udev_device_get_devtype(parent);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart 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) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "scsi");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path_prepend(path, "ieee1394-0x%s", id);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *supported_parent = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers goto out;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /* scsi sysfs does not have a "subsystem" for the transport */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering name = udev_device_get_syspath(parent);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (strstr(name, "/rport-") != NULL) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = handle_scsi_fibre_channel(parent, path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *supported_parent = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann }
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (strstr(name, "/end_device-") != NULL) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann parent = handle_scsi_sas(parent, path);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann *supported_parent = true;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann goto out;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann }
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (strstr(name, "/session") != NULL) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann parent = handle_scsi_iscsi(parent, path);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann *supported_parent = true;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann goto out;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strstr(name, "/ata") != NULL) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = handle_scsi_ata(parent, path);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering goto out;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strstr(name, "/vmbus_") != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_hyperv(parent, path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering goto out;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_default(parent, path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringout:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_cciss(struct udev_device *parent, char **path) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *str;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned int controller, disk;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering str = udev_device_get_sysname(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "cciss-disk%u", disk);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "cciss");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void handle_scsi_tape(struct udev_device *dev, char **path) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "st%c", name[2]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering devtype = udev_device_get_devtype(parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (devtype == NULL)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering str = udev_device_get_sysname(parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering port = strchr(str, '-');
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (port == NULL)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering port++;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = skip_subsystem(parent, "usb");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering path_prepend(path, "usb-0:%s", port);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering}
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic struct udev_device *handle_bcma(struct udev_device *parent, char **path) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering const char *sysname;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering unsigned int core;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering sysname = udev_device_get_sysname(parent);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering path_prepend(path, "bcma-%u", core);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return parent;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering/* Handle devices of AP bus in System z platform. */
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poetteringstatic struct udev_device *handle_ap(struct udev_device *parent, char **path) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering const char *type, *func;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(parent);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering type = udev_device_get_sysattr_value(parent, "type");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering func = udev_device_get_sysattr_value(parent, "ap_functions");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (type != NULL && func != NULL) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(path, "ap-%s-%s", type, func);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering goto out;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(path, "ap-%s", udev_device_get_sysname(parent));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringout:
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = skip_subsystem(parent, "ap");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return parent;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering struct udev_device *parent;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering char *path = NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering bool supported_transport = false;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering bool supported_parent = false;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(dev);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering /* walk up the chain of devices and compose path */
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering parent = dev;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering while (parent != NULL) {
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek const char *subsys;
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek
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")) {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering parent = handle_cciss(parent, &path);
a6c616024db23fef34152c1432892824a07799ccLennart Poettering supported_transport = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (streq(subsys, "usb")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = handle_usb(parent, &path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_transport = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering } else if (streq(subsys, "bcma")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = handle_bcma(parent, &path);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering supported_transport = true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering } else if (streq(subsys, "serio")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering parent = skip_subsystem(parent, "serio");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering } else if (streq(subsys, "pci")) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers parent = skip_subsystem(parent, "pci");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers supported_parent = true;
a6c616024db23fef34152c1432892824a07799ccLennart Poettering } else if (streq(subsys, "platform")) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = skip_subsystem(parent, "platform");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen supported_transport = true;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering supported_parent = true;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering } else if (streq(subsys, "acpi")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "acpi");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_parent = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (streq(subsys, "xen")) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers parent = skip_subsystem(parent, "xen");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers supported_parent = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (streq(subsys, "scm")) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt parent = skip_subsystem(parent, "scm");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_parent = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "ccw")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "ccw-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "ccw");
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers supported_transport = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_parent = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else if (streq(subsys, "ccwgroup")) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path_prepend(&path, "ccwgroup-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "ccwgroup");
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering supported_transport = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers supported_parent = true;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering } else if (streq(subsys, "ap")) {
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering parent = handle_ap(parent, &path);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering supported_transport = true;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering supported_parent = true;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering } else if (streq(subsys, "iucv")) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path_prepend(&path, "iucv-%s", udev_device_get_sysname(parent));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers parent = skip_subsystem(parent, "iucv");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering supported_transport = true;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers supported_parent = true;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
27e72d6b22890ba4a8cbc05c49667cd1cccf1461Simon Peeters
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (parent)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt parent = udev_device_get_parent(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Do not return devices with an unknown parent device type. They
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering * might produce conflicting IDs if the parent does not provide a
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering * unique and predictable name.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!supported_parent)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers path = mfree(path);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering /*
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Do not return block devices without a well-known transport. Some
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * devices do not expose their buses and do not provide a unique
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * and predictable name that way.
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path = mfree(path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (path != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen char tag[UTIL_NAME_SIZE];
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers size_t i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *p;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
8c841f21f5042b11acc91cc1b039cb162cbbe8f4Djalal Harouni /* compose valid udev tag name */
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers for (p = path, i = 0; *p; p++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if ((*p >= '0' && *p <= '9') ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (*p >= 'A' && *p <= 'Z') ||
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering (*p >= 'a' && *p <= 'z') ||
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *p == '-') {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering tag[i++] = *p;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* skip all leading '_' */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (i == 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* avoid second '_' */
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (tag[i-1] == '_')
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen continue;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen tag[i++] = '_';
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen }
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* strip trailing '_' */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen while (i > 0 && tag[i-1] == '_')
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering i--;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering tag[i] = '\0';
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers udev_builtin_add_property(dev, test, "ID_PATH", path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers free(path);
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return EXIT_SUCCESS;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering }
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering return EXIT_FAILURE;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering}
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poetteringconst struct udev_builtin udev_builtin_path_id = {
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .name = "path_id",
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .cmd = builtin_path_id,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .help = "Compose persistent device path",
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering .run_once = true,
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering};
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering