@@ -61,7 +61,21 @@ struct candidate_args {
6161};
6262typedef bool (* ctrl_match_t )(struct nvme_ctrl * c , struct candidate_args * candidate );
6363
64- const char * nvme_slots_sysfs_dir = "/sys/bus/pci/slots" ;
64+ #define PATH_SYSFS_SLOTS "/sys/bus/pci/slots"
65+
66+ static char * nvme_slots_sysfs_dir (void )
67+ {
68+ char * basepath = getenv ("LIBNVME_SYSFS_PATH" );
69+ char * str ;
70+
71+ if (!basepath )
72+ return strdup (PATH_SYSFS_SLOTS );
73+
74+ if (!asprintf (& str , "%s" PATH_SYSFS_SLOTS , basepath ))
75+ return NULL ;
76+
77+ return str ;
78+ }
6579
6680static struct nvme_host * default_host ;
6781
@@ -658,9 +672,10 @@ static int nvme_subsystem_scan_namespaces(nvme_root_t r, nvme_subsystem_t s,
658672
659673static int nvme_init_subsystem (nvme_subsystem_t s , const char * name )
660674{
675+ _cleanup_free_ char * subsys_dir = nvme_subsys_sysfs_dir ();
661676 char * path ;
662677
663- if (asprintf (& path , "%s/%s" , nvme_subsys_sysfs_dir , name ) < 0 )
678+ if (asprintf (& path , "%s/%s" , subsys_dir , name ) < 0 )
664679 return -1 ;
665680
666681 s -> model = nvme_get_attr (path , "model" );
@@ -701,11 +716,12 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
701716{
702717 struct nvme_subsystem * s = NULL , * _s ;
703718 _cleanup_free_ char * path = NULL , * subsysnqn = NULL ;
719+ _cleanup_free_ char * subsys_dir = nvme_subsys_sysfs_dir ();
704720 nvme_host_t h = NULL ;
705721 int ret ;
706722
707723 nvme_msg (r , LOG_DEBUG , "scan subsystem %s\n" , name );
708- ret = asprintf (& path , "%s/%s" , nvme_subsys_sysfs_dir , name );
724+ ret = asprintf (& path , "%s/%s" , subsys_dir , name );
709725 if (ret < 0 )
710726 return ret ;
711727
@@ -1687,6 +1703,7 @@ static int nvme_ctrl_scan_namespaces(nvme_root_t r, struct nvme_ctrl *c)
16871703static char * nvme_ctrl_lookup_subsystem_name (nvme_root_t r ,
16881704 const char * ctrl_name )
16891705{
1706+ _cleanup_free_ char * subsys_dir = nvme_subsys_sysfs_dir ();
16901707 _cleanup_dirents_ struct dirents subsys = {};
16911708 int i ;
16921709
@@ -1697,7 +1714,7 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r,
16971714 struct stat st ;
16981715 _cleanup_free_ char * path = NULL ;
16991716
1700- if (asprintf (& path , "%s/%s/%s" , nvme_subsys_sysfs_dir ,
1717+ if (asprintf (& path , "%s/%s/%s" , subsys_dir ,
17011718 subsys .ents [i ]-> d_name , ctrl_name ) < 0 ) {
17021719 errno = ENOMEM ;
17031720 return NULL ;
@@ -1713,18 +1730,19 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r,
17131730
17141731static char * nvme_ctrl_lookup_phy_slot (nvme_root_t r , const char * address )
17151732{
1733+ _cleanup_free_ char * slots_sysfs_dir = nvme_slots_sysfs_dir ();
17161734 _cleanup_free_ char * target_addr = NULL ;
1717- int ret ;
17181735 _cleanup_dir_ DIR * slots_dir = NULL ;
1736+ int ret ;
17191737 struct dirent * entry ;
17201738
17211739 if (!address )
17221740 return NULL ;
17231741
1724- slots_dir = opendir (nvme_slots_sysfs_dir );
1742+ slots_dir = opendir (slots_sysfs_dir );
17251743 if (!slots_dir ) {
17261744 nvme_msg (r , LOG_WARNING , "failed to open slots dir %s\n" ,
1727- nvme_slots_sysfs_dir );
1745+ slots_sysfs_dir );
17281746 return NULL ;
17291747 }
17301748
@@ -1737,7 +1755,7 @@ static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address)
17371755 _cleanup_free_ char * addr = NULL ;
17381756
17391757 ret = asprintf (& path , "%s/%s" ,
1740- nvme_slots_sysfs_dir , entry -> d_name );
1758+ slots_sysfs_dir , entry -> d_name );
17411759 if (ret < 0 ) {
17421760 errno = ENOMEM ;
17431761 return NULL ;
@@ -1799,18 +1817,19 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
17991817
18001818int nvme_init_ctrl (nvme_host_t h , nvme_ctrl_t c , int instance )
18011819{
1802- nvme_subsystem_t s ;
1820+ _cleanup_free_ char * ctrl_dir = nvme_ctrl_sysfs_dir () ;
18031821 _cleanup_free_ char * subsys_name = NULL ;
1804- char * path ;
18051822 _cleanup_free_ char * name = NULL ;
1823+ nvme_subsystem_t s ;
1824+ char * path ;
18061825 int ret ;
18071826
18081827 ret = asprintf (& name , "nvme%d" , instance );
18091828 if (ret < 0 ) {
18101829 errno = ENOMEM ;
18111830 return -1 ;
18121831 }
1813- ret = asprintf (& path , "%s/nvme%d" , nvme_ctrl_sysfs_dir , instance );
1832+ ret = asprintf (& path , "%s/nvme%d" , ctrl_dir , instance );
18141833 if (ret < 0 ) {
18151834 errno = ENOMEM ;
18161835 return ret ;
@@ -1954,10 +1973,11 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
19541973 _cleanup_free_ char * path = NULL ;
19551974 _cleanup_free_ char * hostnqn = NULL , * hostid = NULL ;
19561975 _cleanup_free_ char * subsysnqn = NULL , * subsysname = NULL ;
1976+ _cleanup_free_ char * ctrl_dir = nvme_ctrl_sysfs_dir ();
19571977 int ret ;
19581978
19591979 nvme_msg (r , LOG_DEBUG , "scan controller %s\n" , name );
1960- ret = asprintf (& path , "%s/%s" , nvme_ctrl_sysfs_dir , name );
1980+ ret = asprintf (& path , "%s/%s" , ctrl_dir , name );
19611981 if (ret < 0 ) {
19621982 errno = ENOMEM ;
19631983 return NULL ;
@@ -2585,7 +2605,9 @@ static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *
25852605
25862606nvme_ns_t nvme_scan_namespace (const char * name )
25872607{
2588- return __nvme_scan_namespace (nvme_ns_sysfs_dir , name );
2608+ _cleanup_free_ char * ns_dir = nvme_ns_sysfs_dir ();
2609+
2610+ return __nvme_scan_namespace (ns_dir , name );
25892611}
25902612
25912613static int nvme_ctrl_scan_namespace (nvme_root_t r , struct nvme_ctrl * c ,
0 commit comments