2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "libudev-private.h"
32 static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
35 char filename[UTIL_PATH_SIZE];
37 id = udev_device_get_id_filename(dev);
40 strscpyl(filename, sizeof(filename), "/run/udev/tags/", tag, "/", id, NULL);
45 mkdir_parents(filename, 0755);
46 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
54 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
56 struct udev_list_entry *list_entry;
59 if (add && dev_old != NULL) {
60 /* delete possible left-over tags */
61 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
62 const char *tag_old = udev_list_entry_get_name(list_entry);
63 struct udev_list_entry *list_entry_current;
66 udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
67 const char *tag = udev_list_entry_get_name(list_entry_current);
69 if (streq(tag, tag_old)) {
75 udev_device_tag(dev_old, tag_old, false);
79 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
80 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
85 static bool device_has_info(struct udev_device *udev_device)
87 struct udev_list_entry *list_entry;
89 if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
91 if (udev_device_get_devlink_priority(udev_device) != 0)
93 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
94 if (udev_list_entry_get_num(list_entry))
96 if (udev_device_get_tags_list_entry(udev_device) != NULL)
98 if (udev_device_get_watch_handle(udev_device) >= 0)
103 int udev_device_update_db(struct udev_device *udev_device)
105 struct udev *udev = udev_device_get_udev(udev_device);
108 char filename[UTIL_PATH_SIZE];
109 char filename_tmp[UTIL_PATH_SIZE];
113 id = udev_device_get_id_filename(udev_device);
117 has_info = device_has_info(udev_device);
118 strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);
120 /* do not store anything for otherwise empty devices */
122 major(udev_device_get_devnum(udev_device)) == 0 &&
123 udev_device_get_ifindex(udev_device) == 0) {
128 /* write a database file */
129 strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
130 mkdir_parents(filename_tmp, 0755);
131 f = fopen(filename_tmp, "we");
133 udev_err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
138 * set 'sticky' bit to indicate that we should not clean the
139 * database when we transition from initramfs to the real root
141 if (udev_device_get_db_persist(udev_device))
142 fchmod(fileno(f), 01644);
145 struct udev_list_entry *list_entry;
147 if (major(udev_device_get_devnum(udev_device)) > 0) {
148 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
149 fprintf(f, "S:%s\n", udev_list_entry_get_name(list_entry) + strlen("/dev/"));
150 if (udev_device_get_devlink_priority(udev_device) != 0)
151 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
152 if (udev_device_get_watch_handle(udev_device) >= 0)
153 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
156 if (udev_device_get_usec_initialized(udev_device) > 0)
157 fprintf(f, "I:%llu\n", (unsigned long long)udev_device_get_usec_initialized(udev_device));
159 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
160 if (!udev_list_entry_get_num(list_entry))
162 fprintf(f, "E:%s=%s\n",
163 udev_list_entry_get_name(list_entry),
164 udev_list_entry_get_value(list_entry));
167 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
168 fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
172 r = rename(filename_tmp, filename);
175 udev_dbg(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty",
176 filename, udev_device_get_devpath(udev_device));
180 int udev_device_delete_db(struct udev_device *udev_device)
183 char filename[UTIL_PATH_SIZE];
185 id = udev_device_get_id_filename(udev_device);
188 strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL);