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)
27 /* translate to location of db file */
28 util_strlcpy(filename, udev_get_dev_path(udev), len);
29 start = util_strlcat(filename, "/.udev/db/", len);
30 util_strlcat(filename, devpath, len);
31 return util_path_encode(&filename[start], len - start);
34 int udev_device_update_db(struct udev_device *udev_device)
36 struct udev *udev = udev_device_get_udev(udev_device);
37 char filename[UTIL_PATH_SIZE];
39 char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
40 size_t devlen = strlen(udev_get_dev_path(udev))+1;
41 struct udev_list_entry *list_entry;
44 devpath_to_db_path(udev,
45 udev_device_get_devpath(udev_device),
46 filename, sizeof(filename));
47 util_create_path(udev, filename);
50 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
51 if (udev_list_entry_get_flag(list_entry))
53 if (udev_device_get_num_fake_partitions(udev_device) != 0)
55 if (udev_device_get_ignore_remove(udev_device))
57 if (udev_device_get_devlink_priority(udev_device) != 0)
59 if (udev_device_get_event_timeout(udev_device) >= 0)
61 if (udev_device_get_watch_handle(udev_device) >= 0)
63 if (udev_device_get_devnode(udev_device) == NULL)
67 * if we have only the node and symlinks to store, try not to waste
68 * tmpfs memory -- store values, if they fit, in a symlink target
70 util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
71 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
74 util_strlcat(target, " ", sizeof(target));
75 len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
76 if (len >= sizeof(target)) {
77 info(udev, "size of links too large, create file\n");
81 info(udev, "create db link (%s)\n", target);
82 udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
83 ret = symlink(target, filename);
84 udev_selinux_resetfscreatecon(udev);
88 f = fopen(filename, "w");
90 err(udev, "unable to create db file '%s': %m\n", filename);
93 info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
95 if (udev_device_get_devnode(udev_device) != NULL) {
96 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
97 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
98 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
100 if (udev_device_get_devlink_priority(udev_device) != 0)
101 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
102 if (udev_device_get_event_timeout(udev_device) >= 0)
103 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
104 if (udev_device_get_num_fake_partitions(udev_device) != 0)
105 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
106 if (udev_device_get_ignore_remove(udev_device))
107 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
108 if (udev_device_get_watch_handle(udev_device) >= 0)
109 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
110 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
111 if (!udev_list_entry_get_flag(list_entry))
113 fprintf(f, "E:%s=%s\n",
114 udev_list_entry_get_name(list_entry),
115 udev_list_entry_get_value(list_entry));
122 int udev_device_delete_db(struct udev_device *udev_device)
124 char filename[UTIL_PATH_SIZE];
126 devpath_to_db_path(udev_device_get_udev(udev_device),
127 udev_device_get_devpath(udev_device),
128 filename, sizeof(filename));
133 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
135 char filename_old[UTIL_PATH_SIZE];
136 char filename[UTIL_PATH_SIZE];
138 devpath_to_db_path(udev_device_get_udev(udev_device),
140 filename_old, sizeof(filename_old));
141 devpath_to_db_path(udev_device_get_udev(udev_device),
142 udev_device_get_devpath(udev_device),
143 filename, sizeof(filename));
144 return rename(filename_old, filename);