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.
22 #include "libudev-private.h"
24 int udev_device_update_db(struct udev_device *udev_device)
26 struct udev *udev = udev_device_get_udev(udev_device);
27 char filename[UTIL_PATH_SIZE];
29 char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
30 size_t devlen = strlen(udev_get_dev_path(udev))+1;
33 struct udev_list_entry *list_entry;
36 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
37 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
40 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
41 if (udev_list_entry_get_flag(list_entry))
43 if (udev_device_get_num_fake_partitions(udev_device) != 0)
45 if (udev_device_get_ignore_remove(udev_device))
47 if (udev_device_get_devlink_priority(udev_device) != 0)
49 if (udev_device_get_event_timeout(udev_device) >= 0)
51 if (udev_device_get_watch_handle(udev_device) >= 0)
53 if (udev_device_get_devnode(udev_device) == NULL)
57 * if we have only the node and symlinks to store, try not to waste
58 * tmpfs memory -- store values, if they fit, in a symlink target
61 l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
62 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
63 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
65 info(udev, "size of links too large, create file\n");
69 info(udev, "create db link (%s)\n", target);
70 udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
71 util_create_path(udev, filename);
72 ret = symlink(target, filename);
73 udev_selinux_resetfscreatecon(udev);
77 util_create_path(udev, filename);
78 f = fopen(filename, "w");
80 err(udev, "unable to create db file '%s': %m\n", filename);
83 info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
85 if (udev_device_get_devnode(udev_device) != NULL) {
86 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
87 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
88 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
90 if (udev_device_get_devlink_priority(udev_device) != 0)
91 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
92 if (udev_device_get_event_timeout(udev_device) >= 0)
93 fprintf(f, "T:%i\n", udev_device_get_event_timeout(udev_device));
94 if (udev_device_get_num_fake_partitions(udev_device) != 0)
95 fprintf(f, "A:%i\n", udev_device_get_num_fake_partitions(udev_device));
96 if (udev_device_get_ignore_remove(udev_device))
97 fprintf(f, "R:%i\n", udev_device_get_ignore_remove(udev_device));
98 if (udev_device_get_watch_handle(udev_device) >= 0)
99 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
100 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
101 if (!udev_list_entry_get_flag(list_entry))
103 fprintf(f, "E:%s=%s\n",
104 udev_list_entry_get_name(list_entry),
105 udev_list_entry_get_value(list_entry));
112 int udev_device_delete_db(struct udev_device *udev_device)
114 struct udev *udev = udev_device_get_udev(udev_device);
115 char filename[UTIL_PATH_SIZE];
117 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
118 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
123 int udev_device_rename_db(struct udev_device *udev_device)
125 struct udev *udev = udev_device_get_udev(udev_device);
126 char filename_old[UTIL_PATH_SIZE];
127 char filename[UTIL_PATH_SIZE];
129 util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/db/",
130 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
131 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
132 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
133 return rename(filename_old, filename);