2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
23 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
29 l = util_strpcpyl(&s, len, udev_get_dev_path(udev), "/.udev/db/", NULL);
30 return util_path_encode(devpath, s, l);
33 int udev_device_update_db(struct udev_device *udev_device)
35 struct udev *udev = udev_device_get_udev(udev_device);
36 char filename[UTIL_PATH_SIZE];
38 char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
39 size_t devlen = strlen(udev_get_dev_path(udev))+1;
42 struct udev_list_entry *list_entry;
45 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
46 util_create_path(udev, filename);
49 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
50 if (udev_list_entry_get_flag(list_entry))
52 if (udev_device_get_num_fake_partitions(udev_device) != 0)
54 if (udev_device_get_ignore_remove(udev_device))
56 if (udev_device_get_devlink_priority(udev_device) != 0)
58 if (udev_device_get_event_timeout(udev_device) >= 0)
60 if (udev_device_get_watch_handle(udev_device) >= 0)
62 if (udev_device_get_devnode(udev_device) == NULL)
66 * if we have only the node and symlinks to store, try not to waste
67 * tmpfs memory -- store values, if they fit, in a symlink target
70 l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
71 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
72 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
74 info(udev, "size of links too large, create file\n");
78 info(udev, "create db link (%s)\n", target);
79 udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
80 ret = symlink(target, filename);
81 udev_selinux_resetfscreatecon(udev);
85 f = fopen(filename, "w");
87 err(udev, "unable to create db file '%s': %m\n", filename);
90 info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
92 if (udev_device_get_devnode(udev_device) != NULL) {
93 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
94 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
95 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
97 if (udev_device_get_devlink_priority(udev_device) != 0)
98 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
99 if (udev_device_get_event_timeout(udev_device) >= 0)
100 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
101 if (udev_device_get_num_fake_partitions(udev_device) != 0)
102 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
103 if (udev_device_get_ignore_remove(udev_device))
104 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
105 if (udev_device_get_watch_handle(udev_device) >= 0)
106 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
107 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
108 if (!udev_list_entry_get_flag(list_entry))
110 fprintf(f, "E:%s=%s\n",
111 udev_list_entry_get_name(list_entry),
112 udev_list_entry_get_value(list_entry));
119 int udev_device_delete_db(struct udev_device *udev_device)
121 struct udev *udev = udev_device_get_udev(udev_device);
122 char filename[UTIL_PATH_SIZE];
124 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
129 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
131 struct udev *udev = udev_device_get_udev(udev_device);
132 char filename_old[UTIL_PATH_SIZE];
133 char filename[UTIL_PATH_SIZE];
135 devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
136 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
137 return rename(filename_old, filename);