Example #1
0
int main(int argc, char *argv[])
{
    struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
    char *devname;
    dev_t devno;
    char path[PATH_MAX];
    int i;
    uint64_t u64;
    ssize_t len;

    if (argc != 2)
        errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]);

    devname = argv[1];
    devno = sysfs_devname_to_devno(devname, NULL);

    if (!devno)
        err(EXIT_FAILURE, "failed to read devno");

    printf("NAME: %s\n", devname);
    printf("DEVNO: %u\n", (unsigned int) devno);
    printf("DEVNOPATH: %s\n", sysfs_devno_path(devno, path, sizeof(path)));
    printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno, path, sizeof(path)));
    printf("PARTITION: %s\n",
           sysfs_devno_has_attribute(devno, "partition") ? "YES" : "NOT");

    if (sysfs_init(&cxt, devno, NULL))
        return EXIT_FAILURE;

    len = sysfs_readlink(&cxt, NULL, path, sizeof(path) - 1);
    if (len > 0) {
        path[len] = '\0';
        printf("DEVNOLINK: %s\n", path);
    }

    printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves"));

    if (sysfs_read_u64(&cxt, "size", &u64))
        printf("read SIZE failed\n");
    else
        printf("SIZE: %jd\n", u64);

    if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i))
        printf("read SECTOR failed\n");
    else
        printf("SECTOR: %d\n", i);

    printf("DEVNAME: %s\n", sysfs_get_devname(&cxt, path, sizeof(path)));

    sysfs_deinit(&cxt);
    return EXIT_SUCCESS;
}
Example #2
0
char *next_proc_partition(FILE **f)
{
	char line[128 + 1];

	if (!*f) {
		*f = fopen(_PATH_PROC_PARTITIONS, "r");
		if (!*f) {
			warn(_("cannot open %s"), _PATH_PROC_PARTITIONS);
			return NULL;
		}
	}

	while (fgets(line, sizeof(line), *f)) {
		char buf[PATH_MAX], *cn;
		dev_t devno;

		if (sscanf(line, " %*d %*d %*d %128[^\n ]", buf) != 1)
			continue;

		devno = sysfs_devname_to_devno(buf, NULL);
		if (devno <= 0)
			continue;

		if (sysfs_devno_is_lvm_private(devno) ||
		    sysfs_devno_is_wholedisk(devno) <= 0)
			continue;

		if (!sysfs_devno_to_devpath(devno, buf, sizeof(buf)))
			continue;

		cn = canonicalize_path(buf);
		if (!cn)
			continue;

		if (!is_ide_cdrom_or_tape(cn))
			return cn;
	}
	fclose(*f);
	*f = NULL;

	return NULL;
}
Example #3
0
/**
 * blkid_devno_to_devname:
 * @devno: device number
 *
 * This function finds the pathname to a block device with a given
 * device number.
 *
 * Returns: a pointer to allocated memory to the pathname on success,
 * and NULL on failure.
 */
char *blkid_devno_to_devname(dev_t devno)
{
	char *path = NULL;
	char buf[PATH_MAX];

	path = sysfs_devno_to_devpath(devno, buf, sizeof(buf));
	if (path)
		path = strdup(path);
	if (!path)
		path = scandev_devno_to_devpath(devno);

	if (!path) {
		DBG(DEVNO, ul_debug("blkid: couldn't find devno 0x%04lx",
			   (unsigned long) devno));
	} else {
		DBG(DEVNO, ul_debug("found devno 0x%04llx as %s", (long long)devno, path));
	}

	return path;
}