2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 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 #include "libudev-private.h"
25 static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
28 struct udev *udev = udev_device_get_udev(dev);
29 char filename[UTIL_PATH_SIZE];
31 id = udev_device_get_id_filename(dev);
34 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/tags/", tag, "/", id, NULL);
39 util_create_path(udev, filename);
40 fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
48 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
50 struct udev_list_entry *list_entry;
53 if (add && dev_old != NULL) {
54 /* delete possible left-over tags */
55 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
56 const char *tag_old = udev_list_entry_get_name(list_entry);
57 struct udev_list_entry *list_entry_current;
60 udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
61 const char *tag = udev_list_entry_get_name(list_entry_current);
63 if (strcmp(tag, tag_old) == 0) {
69 udev_device_tag(dev_old, tag_old, false);
73 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
74 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
79 static bool device_has_info(struct udev_device *udev_device)
81 struct udev *udev = udev_device_get_udev(udev_device);
82 struct udev_list_entry *list_entry;
84 if (udev_device_get_devlinks_list_entry(udev_device) != NULL)
86 if (udev_device_get_devlink_priority(udev_device) != 0)
88 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
89 if (udev_list_entry_get_num(list_entry))
91 if (udev_device_get_tags_list_entry(udev_device) != NULL)
93 if (udev_device_get_devnode(udev_device) != NULL && udev_device_get_knodename(udev_device) != NULL) {
94 size_t devlen = strlen(udev_get_dev_path(udev))+1;
96 if (strcmp(&udev_device_get_devnode(udev_device)[devlen], udev_device_get_knodename(udev_device)) != 0)
99 if (udev_device_get_watch_handle(udev_device) >= 0)
104 int udev_device_update_db(struct udev_device *udev_device)
108 struct udev *udev = udev_device_get_udev(udev_device);
109 char filename[UTIL_PATH_SIZE];
110 char filename_tmp[UTIL_PATH_SIZE];
113 id = udev_device_get_id_filename(udev_device);
117 has_info = device_has_info(udev_device);
118 util_strscpyl(filename, sizeof(filename), udev_get_run_path(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 util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
130 util_create_path(udev, filename_tmp);
131 f = fopen(filename_tmp, "we");
133 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 size_t devlen = strlen(udev_get_dev_path(udev))+1;
146 struct udev_list_entry *list_entry;
148 if (udev_device_get_devnode(udev_device) != NULL) {
149 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
150 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
151 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
153 if (udev_device_get_devlink_priority(udev_device) != 0)
154 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
155 if (udev_device_get_watch_handle(udev_device) >= 0)
156 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
157 if (udev_device_get_usec_initialized(udev_device) > 0)
158 fprintf(f, "I:%llu\n", 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));
166 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
167 fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
171 rename(filename_tmp, filename);
172 info(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty",
173 filename, udev_device_get_devpath(udev_device));
177 int udev_device_delete_db(struct udev_device *udev_device)
180 struct udev *udev = udev_device_get_udev(udev_device);
181 char filename[UTIL_PATH_SIZE];
183 id = udev_device_get_id_filename(udev_device);
186 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/data/", id, NULL);