libudev-device.c revision cd42b50d866c35c7ffbab07920dc1323dd978a23
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/*
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * libudev - interface to udev device information
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen * This program is free software: you can redistribute it and/or modify
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * it under the terms of the GNU General Public License as published by
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * the Free Software Foundation, either version 2 of the License, or
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * (at your option) any later version.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * This program is distributed in the hope that it will be useful,
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * but WITHOUT ANY WARRANTY; without even the implied warranty of
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * GNU General Public License for more details.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * You should have received a copy of the GNU General Public License
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * along with this program. If not, see <http://www.gnu.org/licenses/>.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <stdio.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <stdlib.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <stddef.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <unistd.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <errno.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <string.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <dirent.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <fcntl.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include <sys/stat.h>
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include "libudev.h"
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch#include "libudev-private.h"
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int refcount;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev *udev;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_device *parent_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *syspath;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch const char *devpath;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch const char *sysname;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *devname;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *subsystem;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct list_head link_list;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct list_head properties_list;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *action;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *driver;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *devpath_old;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *physdevpath;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int timeout;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch dev_t devnum;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch unsigned long long int seqnum;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int num_fake_partitions;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int devlink_priority;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch int ignore_remove;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch struct list_head attr_list;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch};
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch size_t start;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch /* translate to location of db file */
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch start = util_strlcat(filename, "/.udev/db/", len);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch util_strlcat(filename, udev_device->devpath, len);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch return util_path_encode(&filename[start], len - start);
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int device_read_uevent_file(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char filename[UTIL_PATH_SIZE];
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch FILE *f;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char line[UTIL_LINE_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int maj = 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int min = 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(filename, udev_device->syspath, sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcat(filename, "/uevent", sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch f = fopen(filename, "r");
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (f == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (fgets(line, sizeof(line), f)) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *pos;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos = strchr(line, '\n');
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (pos == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch continue;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos[0] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (strncmp(line, "MAJOR=", 6) == 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch maj = strtoull(&line[6], NULL, 10);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch else if (strncmp(line, "MINOR=", 6) == 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch min = strtoull(&line[6], NULL, 10);
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_add_property_from_string(udev_device, line);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->devnum = makedev(maj, min);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch fclose(f);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic int device_read_db(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen struct stat stats;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char filename[UTIL_PATH_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char line[UTIL_LINE_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch FILE *f;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch syspath_to_db_path(udev_device, filename, sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (lstat(filename, &stats) != 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if ((stats.st_mode & S_IFMT) == S_IFLNK) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char target[UTIL_PATH_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int target_len;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch target_len = readlink(filename, target, sizeof(target));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (target_len > 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch target[target_len] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch else {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devname);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch f = fopen(filename, "r");
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (f == NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (fgets(line, sizeof(line), f)) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch ssize_t len;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch const char *val;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch len = strlen(line);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (len < 4)
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch line[len-1] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch val = &line[2];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch switch(line[0]) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'N':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'S':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcat(filename, "/", sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcat(filename, val, sizeof(filename));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_add_devlink(udev_device, filename);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'L':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_set_devlink_priority(udev_device, atoi(val));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'T':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_set_timeout(udev_device, atoi(val));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'A':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_set_num_fake_partitions(udev_device, atoi(val));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'R':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_set_ignore_remove(udev_device, atoi(val));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch case 'E':
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_add_property_from_string(udev_device, val);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch fclose(f);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "device %p filled with db file data\n", udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device *device_init(struct udev *udev)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_device *udev_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device = malloc(sizeof(struct udev_device));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch memset(udev_device, 0x00, sizeof(struct udev_device));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->refcount = 1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->udev = udev;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch INIT_LIST_HEAD(&udev_device->link_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch INIT_LIST_HEAD(&udev_device->properties_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch INIT_LIST_HEAD(&udev_device->attr_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "udev_device: %p created\n", udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_new_from_syspath:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev: udev library context
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @syspath: sys device path including sys directory
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Create new udev device, and fill in information from the sys
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * device and the udev database entry. The sypath is the absolute
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * path to the device, including the sys mount point.
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * The initial refcount is 1, and needs to be decremented to
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * release the ressources of the udev device.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen * Returns: a new udev device, or #NULL, if it does not exist
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char path[UTIL_PATH_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct stat statbuf;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_device *udev_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (syspath == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(path, syspath, sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcat(path, "/uevent", sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (stat(path, &statbuf) != 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev, "not a device :%s\n", syspath);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device = device_init(udev);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch /* resolve possible symlink to real path */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(path, syspath, sizeof(path));
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen util_resolve_sys_link(udev, path, sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_set_syspath(udev_device, path);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_read_uevent_file(udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device_read_db(udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen char path[UTIL_PATH_SIZE];
4351efdddced25735d629496f6c68c9d0cfb896aTimo Sirainen const char *type_str;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_enumerate *enumerate;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_list *list;
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen struct udev_device *device = NULL;
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (type == 'b')
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch type_str = "block";
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch else if (type == 'c')
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch type_str = "char";
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch else
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
0e702716169a158b87d08e21d1a128eb99e23fa1Phil Carmody /* /sys/dev/{block,char}/<maj>:<min> links */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch type_str, major(devnum), minor(devnum));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device_new_from_syspath(udev, path);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* fallback to search all sys devices for the major/minor */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (enumerate == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list = udev_enumerate_get_list(enumerate);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (list != NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_device *device_loop;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen
0e702716169a158b87d08e21d1a128eb99e23fa1Phil Carmody device_loop = udev_device_new_from_syspath(udev, udev_list_get_name(list));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (device_loop != NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device_get_devnum(device_loop) == devnum) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch device = device_loop;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device_unref(device_loop);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list = udev_list_get_next(list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_enumerate_unref(enumerate);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstatic struct udev_device *device_new_from_parent(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch struct udev_device *udev_device_parent = NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char path[UTIL_PATH_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *pos;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(path, udev_device->syspath, sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (1) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos = strrchr(path, '/');
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (pos == path || pos == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch break;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos[0] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device_parent != NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device_parent;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* follow "device" link in deprecated sys /sys/class/ layout */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcpy(path, udev_device->syspath, sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_strlcat(path, "/device", sizeof(path));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device_parent != NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device_parent;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device *udev_device_get_parent(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->parent_device != NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->parent_device;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch udev_device->parent_device = device_new_from_parent(udev_device);
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return udev_device->parent_device;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_get_udev:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the udev library context the device was created with.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the udev library context
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev *udev_device_get_udev(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->udev;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_ref:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Take a reference of a udev device.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch * Returns: the passed udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_device *udev_device_ref(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->refcount++;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device;
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_unref:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch * Drop a reference of a udev device. If the refcount reaches zero,
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * the ressources of the device will be released.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Boschvoid udev_device_unref(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->refcount--;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->refcount > 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->parent_device != NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device_unref(udev_device->parent_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->syspath);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->devname);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->subsystem);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list_cleanup(udev_device->udev, &udev_device->link_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list_cleanup(udev_device->udev, &udev_device->properties_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->action);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->driver);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->devpath_old);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device->physdevpath);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list_cleanup(udev_device->udev, &udev_device->attr_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "udev_device: %p released\n", udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch free(udev_device);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_get_devpath:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the kernel devpath value of the udev device. The path
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * does not contain the sys mount point, and starts with a '/'.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the devpath of the udev device
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *udev_device_get_devpath(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return NULL;
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen return udev_device->devpath;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch * udev_device_get_syspath:
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the sys path of the udev device. The path is an
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * absolute path and starts with the sys mount point.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch * Returns: the sys path of the udev device
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch **/
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Boschconst char *udev_device_get_syspath(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->syspath;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *udev_device_get_sysname(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->sysname;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_get_devnode:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the device node file name belonging to the udev device.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * The path is an absolute path, and starts with the device directory.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the device node file name of the udev device, or #NULL if no device node exists
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *udev_device_get_devnode(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->devname;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * udev_device_get_subsystem:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the subsystem string of the udev device. The string does not
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * contain any "/".
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *udev_device_get_subsystem(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen char subsystem[UTIL_NAME_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->subsystem != NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->subsystem;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* read "subsytem" link */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->subsystem = strdup(subsystem);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->subsystem;
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch }
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch /* implicit names */
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch udev_device->subsystem = strdup("module");
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->subsystem;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (strstr(udev_device->devpath, "/drivers/") != NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->subsystem = strdup("drivers");
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return udev_device->subsystem;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch }
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch strncmp(udev_device->devpath, "/class/", 7) == 0 ||
009217abb57a24a4076092e8e4e165545747839eStephan Bosch strncmp(udev_device->devpath, "/bus/", 5) == 0) {
009217abb57a24a4076092e8e4e165545747839eStephan Bosch udev_device->subsystem = strdup("subsystem");
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->subsystem;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * udev_device_get_devlinks_list:
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * Retrieve the list of device links pointing to the device file of
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * the udev device. The next list entry can be retrieved with
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * udev_list_next(), which returns #NULL if no more entries exist.
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * The devlink path can be retrieved from the list entry by
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * udev_list_get_name(). The path is an absolute path, and starts with
009217abb57a24a4076092e8e4e165545747839eStephan Bosch * the device directory.
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the first entry of the device node link list
009217abb57a24a4076092e8e4e165545747839eStephan Bosch **/
009217abb57a24a4076092e8e4e165545747839eStephan Boschstruct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device)
009217abb57a24a4076092e8e4e165545747839eStephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return list_get_entry(&udev_device->link_list);
009217abb57a24a4076092e8e4e165545747839eStephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch/**
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch * udev_device_get_properties_list:
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch * @udev_device: udev device
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Retrieve the list of key/value device properties of the udev
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * device. The next list entry can be retrieved with udev_list_next(),
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * which returns #NULL if no more entries exist. The property name
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch * can be retrieved from the list entry by udev_list_get_name(),
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * the property value by udev_list_get_value().
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch *
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch * Returns: the first entry of the property list
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch **/
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschstruct udev_list *udev_device_get_properties_list(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return list_get_entry(&udev_device->properties_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *udev_device_get_driver(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char driver[UTIL_NAME_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->driver != NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->driver;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->driver = strdup(driver);
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen return udev_device->driver;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschdev_t udev_device_get_devnum(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch if (udev_device == NULL)
f3cf2f02155c4bac23fd50f0de96c0cae9c46478Stephan Bosch return makedev(0, 0);
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return udev_device->devnum;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschconst char *udev_device_get_action(struct udev_device *udev_device)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch{
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch if (udev_device == NULL)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return NULL;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return udev_device->action;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch}
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschunsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch{
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return udev_device->seqnum;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch}
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschconst char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch{
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch struct udev_list *list;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch char path[UTIL_PATH_SIZE];
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch char value[UTIL_NAME_SIZE];
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch struct stat statbuf;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch int fd;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch ssize_t size;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch const char *val = NULL;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* look for possibly already cached result */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list = list_get_entry(&udev_device->attr_list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch while (list != NULL) {
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch if (strcmp(udev_list_get_name(list), attr) == 0) {
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_get_value(list));
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch return udev_list_get_name(list);
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch }
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch list = udev_list_get_next(list);
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch }
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch util_strlcat(path, "/", sizeof(path));
3ec5378aadaa699c38b2e02be30aae1add36eb7cTimo Sirainen util_strlcat(path, attr, sizeof(path));
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch if (lstat(path, &statbuf) != 0) {
df70bf8c997cd91452cdb3a5c2e20605d30446d2Stephan Bosch info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch goto out;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (S_ISLNK(statbuf.st_mode)) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* links return the last element of the target path */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char target[UTIL_NAME_SIZE];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch int len;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch char *pos;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch len = readlink(path, target, sizeof(target));
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch if (len > 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch target[len] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos = strrchr(target, '/');
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (pos != NULL) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch pos = &pos[1];
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list = list_insert(udev_device->udev, &udev_device->attr_list, attr, pos, 0);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch val = udev_list_get_value(list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen goto out;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch }
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* skip directories */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (S_ISDIR(statbuf.st_mode))
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch goto out;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* skip non-readable files */
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if ((statbuf.st_mode & S_IRUSR) == 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch goto out;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* read attribute value */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch fd = open(path, O_RDONLY);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (fd < 0) {
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "attribute '%s' can not be opened\n", path);
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch goto out;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch }
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen size = read(fd, value, sizeof(value));
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch close(fd);
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch if (size < 0)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch goto out;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if (size == sizeof(value))
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch goto out;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch /* got a valid value, store it in cache and return it */
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch value[size] = '\0';
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch util_remove_trailing_chars(value, '\n');
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch list = list_insert(udev_device->udev, &udev_device->attr_list, attr, value, 0);
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch val = udev_list_get_value(list);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschout:
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return val;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_syspath(struct udev_device *udev_device, const char *syspath)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch const char *pos;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->syspath = strdup(syspath);
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if (udev_device->syspath == NULL)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch return -ENOMEM;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch pos = strrchr(udev_device->syspath, '/');
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if (pos == NULL)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch return -EINVAL;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch udev_device->sysname = &pos[1];
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return 0;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch}
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Boschint device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch{
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch udev_device->subsystem = strdup(subsystem);
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if (udev_device->subsystem == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_devname(struct udev_device *udev_device, const char *devname)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->devname = strdup(devname);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->devname == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch}
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Boschint device_add_devlink(struct udev_device *udev_device, const char *devlink)
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch{
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch if (list_insert(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_add_property(struct udev_device *udev_device, const char *key, const char *value)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (list_insert(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Boschint device_add_property_from_string(struct udev_device *udev_device, const char *property)
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch{
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch char name[UTIL_PATH_SIZE];
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch char *val;
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch strncpy(name, property, sizeof(name));
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch val = strchr(name, '=');
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch if (val == NULL)
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Bosch return -1;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch val[0] = '\0';
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch val = &val[1];
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch if (val[0] == '\0')
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch val = NULL;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch device_add_property(udev_device, name, val);
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch return 0;
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch}
7a545edbd1fca7a330bcb4a807002373ee18762aStephan Bosch
05262e3132642bbdc4a8087c17b0903cf2ff22d2Stephan Boschint device_set_action(struct udev_device *udev_device, const char *action)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch{
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch udev_device->action = strdup(action);
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch if (udev_device->action == NULL)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return -ENOMEM;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_driver(struct udev_device *udev_device, const char *driver)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->driver = strdup(driver);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->driver == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *device_get_devpath_old(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return NULL;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->devpath_old;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->devpath_old = strdup(devpath_old);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->devpath_old == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschconst char *device_get_physdevpath(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return NULL;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return udev_device->physdevpath;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch}
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschint device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen udev_device->physdevpath = strdup(physdevpath);
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device->physdevpath == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -ENOMEM;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_get_timeout(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->timeout;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_timeout(struct udev_device *udev_device, int timeout)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->timeout = timeout;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->seqnum = seqnum;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_devnum(struct udev_device *udev_device, dev_t devnum)
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->devnum = devnum;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
bcd6c13936df11167350ac86598a781dce9038c3Stephan Boschint device_get_num_fake_partitions(struct udev_device *udev_device)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch{
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch if (udev_device == NULL)
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return -1;
bcd6c13936df11167350ac86598a781dce9038c3Stephan Bosch return udev_device->num_fake_partitions;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_num_fake_partitions(struct udev_device *udev_device, int num)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->num_fake_partitions = num;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_get_devlink_priority(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch if (udev_device == NULL)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return -1;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch return udev_device->devlink_priority;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_set_devlink_priority(struct udev_device *udev_device, int prio)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch udev_device->devlink_priority = prio;
72fc989c43a0dc94ec2f114b5e221beeab45519bTimo Sirainen return 0;
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch}
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Boschint device_get_ignore_remove(struct udev_device *udev_device)
8fe8f97e688779add9cd042a9db4ddb7b117cce2Stephan Bosch{
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch if (udev_device == NULL)
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch return -1;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch return udev_device->ignore_remove;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch}
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Boschint device_set_ignore_remove(struct udev_device *udev_device, int ignore)
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch{
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch udev_device->ignore_remove = ignore;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch return 0;
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch}
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch
4d955db590c3d76a631dfc5d37bcdf578a43e55aStephan Bosch