1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <libudev.h>
#include <fnmatch.h>
#include <limits.h>
#include "checkers.h"
#include "vector.h"
#include "structs.h"
#include "sysfs.h"
#include "list.h"
#include "util.h"
#include "debug.h"
#include "devmapper.h"
#include "config.h"
/*
* When we modify an attribute value we cannot rely on libudev for now,
* as libudev lacks the capability to update an attribute value.
* So for modified attributes we need to implement our own function.
*/
static ssize_t sysfs_attr_get_value__(struct udev_device *dev, const char *attr_name,
char *value, size_t value_len, bool binary)
{
const char *syspath;
char devpath[PATH_MAX];
int fd = -1;
ssize_t size = -1;
if (!dev || !attr_name || !value || !value_len) {
condlog(1, "%s: invalid parameters", __func__);
return -EINVAL;
}
syspath = udev_device_get_syspath(dev);
if (!syspath) {
condlog(3, "%s: invalid udevice", __func__);
return -EINVAL;
}
if (safe_sprintf(devpath, "%s/%s", syspath, attr_name)) {
condlog(3, "%s: devpath overflow", __func__);
return -EOVERFLOW;
}
condlog(4, "open '%s'", devpath);
/* read attribute value */
fd = open(devpath, O_RDONLY);
if (fd < 0) {
condlog(3, "%s: attribute '%s' cannot be opened: %s",
__func__, devpath, strerror(errno));
return -errno;
}
pthread_cleanup_push(cleanup_fd_ptr, &fd);
size = read(fd, value, value_len);
if (size < 0) {
size = -errno;
condlog(3, "%s: read from %s failed: %s", __func__, devpath,
strerror(errno));
if (!binary)
value[0] = '\0';
} else if (!binary && size == (ssize_t)value_len) {
condlog(3, "%s: overflow reading from %s (required len: %zu)",
__func__, devpath, size);
value[size - 1] = '\0';
} else if (!binary) {
value[size] = '\0';
size = strchop(value);
}
pthread_cleanup_pop(1);
return size;
}
ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name,
char *value, size_t value_len)
{
return sysfs_attr_get_value__(dev, attr_name, value, value_len, false);
}
ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name,
unsigned char *value, size_t value_len)
{
return sysfs_attr_get_value__(dev, attr_name, (char *)value,
value_len, true);
}
ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,
const char * value, size_t value_len)
{
const char *syspath;
char devpath[PATH_MAX];
int fd = -1;
ssize_t size = -1;
if (!dev || !attr_name || !value || !value_len) {
condlog(1, "%s: invalid parameters", __func__);
return -EINVAL;
}
syspath = udev_device_get_syspath(dev);
if (!syspath) {
condlog(3, "%s: invalid udevice", __func__);
return -EINVAL;
}
if (safe_sprintf(devpath, "%s/%s", syspath, attr_name)) {
condlog(3, "%s: devpath overflow", __func__);
return -EOVERFLOW;
}
condlog(4, "open '%s'", devpath);
/* write attribute value */
fd = open(devpath, O_WRONLY);
if (fd < 0) {
condlog(3, "%s: attribute '%s' cannot be opened: %s",
__func__, devpath, strerror(errno));
return -errno;
}
pthread_cleanup_push(cleanup_fd_ptr, &fd);
size = write(fd, value, value_len);
if (size < 0) {
size = -errno;
condlog(3, "%s: write to %s failed: %s", __func__,
devpath, strerror(errno));
} else if (size < (ssize_t)value_len)
condlog(3, "%s: underflow writing %zu bytes to %s. Wrote %zd bytes",
__func__, value_len, devpath, size);
pthread_cleanup_pop(1);
return size;
}
int
sysfs_get_size (struct path *pp, unsigned long long * size)
{
char attr[255];
int r;
if (!pp->udev || !size)
return 1;
attr[0] = '\0';
if (!sysfs_attr_get_value_ok(pp->udev, "size", attr, sizeof(attr))) {
condlog(3, "%s: No size attribute in sysfs", pp->dev);
return 1;
}
r = sscanf(attr, "%llu\n", size);
if (r != 1) {
condlog(3, "%s: Cannot parse size attribute", pp->dev);
return 1;
}
return 0;
}
int devt2devname(char *devname, int devname_len, const char *devt)
{
struct udev_device *u_dev;
const char * dev_name;
int r;
if (!devname || !devname_len || !devt)
return 1;
u_dev = udev_device_new_from_devnum(udev, 'b', parse_devt(devt));
if (!u_dev) {
condlog(0, "\"%s\": invalid major/minor numbers, not found in sysfs", devt);
return 1;
}
dev_name = udev_device_get_sysname(u_dev);
if (!dev_name) {
udev_device_unref(u_dev);
return 1;
}
r = strlcpy(devname, dev_name, devname_len);
udev_device_unref(u_dev);
return !(r < devname_len);
}
int sysfs_check_holders(char * check_devt, char * new_devt)
{
unsigned int major, new_minor, table_minor;
char path[PATH_MAX], check_dev[FILE_NAME_SIZE];
char * table_name;
DIR *dirfd;
struct dirent *holder;
if (sscanf(new_devt,"%d:%d", &major, &new_minor) != 2) {
condlog(1, "invalid device number %s", new_devt);
return 0;
}
if (devt2devname(check_dev, sizeof(check_dev), check_devt)) {
condlog(1, "can't get devname for %s", check_devt);
return 0;
}
condlog(3, "%s: checking holder", check_dev);
snprintf(path, sizeof(path), "/sys/block/%s/holders", check_dev);
dirfd = opendir(path);
if (dirfd == NULL) {
condlog(3, "%s: failed to open directory %s (%d)",
check_dev, path, errno);
return 0;
}
while ((holder = readdir(dirfd)) != NULL) {
if ((strcmp(holder->d_name,".") == 0) ||
(strcmp(holder->d_name,"..") == 0))
continue;
if (sscanf(holder->d_name, "dm-%d", &table_minor) != 1) {
condlog(3, "%s: %s is not a dm-device",
check_dev, holder->d_name);
continue;
}
if (table_minor == new_minor) {
condlog(3, "%s: holder already correct", check_dev);
continue;
}
table_name = dm_mapname(major, table_minor);
if (!table_name) {
condlog(2, "%s: mapname not found for %d:%d", check_dev,
major, table_minor);
continue;
}
condlog(0, "%s: reassign table %s old %s new %s", check_dev,
table_name, check_devt, new_devt);
dm_reassign_table(table_name, check_devt, new_devt);
free(table_name);
}
closedir(dirfd);
return 0;
}
static int select_dm_devs(const struct dirent *di)
{
return fnmatch("dm-*", di->d_name, FNM_FILE_NAME) == 0;
}
bool sysfs_is_multipathed(struct path *pp, bool set_wwid)
{
char pathbuf[PATH_MAX];
struct scandir_result sr;
struct dirent **di;
int n, r, i;
bool found = false;
n = snprintf(pathbuf, sizeof(pathbuf), "/sys/block/%s/holders",
pp->dev);
if (n < 0 || (size_t)n >= sizeof(pathbuf)) {
condlog(1, "%s: pathname overflow", __func__);
return false;
}
r = scandir(pathbuf, &di, select_dm_devs, alphasort);
if (r == 0)
return false;
else if (r < 0) {
condlog(1, "%s: error scanning %s", __func__, pathbuf);
return false;
}
sr.di = di;
sr.n = r;
pthread_cleanup_push_cast(free_scandir_result, &sr);
for (i = 0; i < r && !found; i++) {
int fd = -1;
int nr;
char uuid[WWID_SIZE + UUID_PREFIX_LEN];
if (safe_snprintf(pathbuf + n, sizeof(pathbuf) - n,
"/%s/dm/uuid", di[i]->d_name))
continue;
fd = open(pathbuf, O_RDONLY);
if (fd == -1) {
condlog(1, "%s: error opening %s", __func__, pathbuf);
continue;
}
pthread_cleanup_push(cleanup_fd_ptr, &fd);
nr = read(fd, uuid, sizeof(uuid));
if (nr > (int)UUID_PREFIX_LEN &&
!memcmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN)) {
found = true;
if (set_wwid) {
nr -= UUID_PREFIX_LEN;
memcpy(pp->wwid, uuid + UUID_PREFIX_LEN, nr);
if (nr == WWID_SIZE) {
condlog(4, "%s: overflow while reading from %s",
__func__, pathbuf);
pp->wwid[0] = '\0';
} else {
pp->wwid[nr] = '\0';
strchop(pp->wwid);
}
}
} else if (nr < 0)
condlog(1, "%s: error reading from %s: %m",
__func__, pathbuf);
pthread_cleanup_pop(1);
}
pthread_cleanup_pop(1);
return found;
}
struct udev_device *get_udev_for_mpp(const struct multipath *mpp)
{
dev_t devnum;
struct udev_device *udd;
if (!mpp || !has_dm_info(mpp)) {
condlog(1, "%s called with empty mpp", __func__);
return NULL;
}
devnum = makedev(mpp->dmi.major, mpp->dmi.minor);
udd = udev_device_new_from_devnum(udev, 'b', devnum);
if (!udd) {
condlog(1, "failed to get udev device for %s", mpp->alias);
return NULL;
}
return udd;
}
|