]>
Commit | Line | Data |
---|---|---|
8dfc8dbe | 1 | /* |
4061ab9f | 2 | * libudev - interface to udev device information |
8dfc8dbe | 3 | * |
28460195 | 4 | * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org> |
55e9959b | 5 | * |
4061ab9f KS |
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. | |
8dfc8dbe GKH |
10 | */ |
11 | ||
8e41d35d DS |
12 | #include <stdlib.h> |
13 | #include <stdio.h> | |
8ea84a8a | 14 | #include <string.h> |
c81b35c0 | 15 | #include <stddef.h> |
28460195 | 16 | #include <stdbool.h> |
845d4751 | 17 | #include <unistd.h> |
8e41d35d DS |
18 | #include <fcntl.h> |
19 | #include <string.h> | |
34bb5d05 | 20 | #include <sys/stat.h> |
8e41d35d | 21 | |
44b49d37 KS |
22 | #include "libudev.h" |
23 | #include "libudev-private.h" | |
2b41e68a | 24 | |
c1dbe11d | 25 | static void udev_device_tag(struct udev_device *dev, const char *tag, bool add) |
28460195 | 26 | { |
912541b0 KS |
27 | const char *id; |
28 | struct udev *udev = udev_device_get_udev(dev); | |
29 | char filename[UTIL_PATH_SIZE]; | |
30 | ||
31 | id = udev_device_get_id_filename(dev); | |
32 | if (id == NULL) | |
33 | return; | |
6ada823a | 34 | util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/run/udev/tags/", tag, "/", id, NULL); |
912541b0 KS |
35 | |
36 | if (add) { | |
37 | int fd; | |
38 | ||
39 | util_create_path(udev, filename); | |
40 | fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); | |
41 | if (fd >= 0) | |
42 | close(fd); | |
43 | } else { | |
44 | unlink(filename); | |
45 | } | |
c1dbe11d | 46 | } |
28460195 | 47 | |
c1dbe11d KS |
48 | int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add) |
49 | { | |
912541b0 KS |
50 | struct udev_list_entry *list_entry; |
51 | bool found; | |
52 | ||
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; | |
58 | ||
59 | found = false; | |
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); | |
62 | ||
63 | if (strcmp(tag, tag_old) == 0) { | |
64 | found = true; | |
65 | break; | |
66 | } | |
67 | } | |
68 | if (!found) | |
69 | udev_device_tag(dev_old, tag_old, false); | |
70 | } | |
71 | } | |
72 | ||
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); | |
75 | ||
76 | return 0; | |
28460195 KS |
77 | } |
78 | ||
24d10766 KS |
79 | static bool device_has_info(struct udev_device *udev_device) |
80 | { | |
912541b0 KS |
81 | struct udev_list_entry *list_entry; |
82 | ||
83 | if (udev_device_get_devlinks_list_entry(udev_device) != NULL) | |
84 | return true; | |
85 | if (udev_device_get_devlink_priority(udev_device) != 0) | |
86 | return true; | |
87 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) | |
88 | if (udev_list_entry_get_num(list_entry)) | |
89 | return true; | |
90 | if (udev_device_get_tags_list_entry(udev_device) != NULL) | |
91 | return true; | |
92 | if (udev_device_get_watch_handle(udev_device) >= 0) | |
93 | return true; | |
94 | return false; | |
24d10766 KS |
95 | } |
96 | ||
aa8734ff | 97 | int udev_device_update_db(struct udev_device *udev_device) |
ca999860 | 98 | { |
912541b0 KS |
99 | bool has_info; |
100 | const char *id; | |
101 | struct udev *udev = udev_device_get_udev(udev_device); | |
102 | char filename[UTIL_PATH_SIZE]; | |
103 | char filename_tmp[UTIL_PATH_SIZE]; | |
104 | FILE *f; | |
105 | ||
106 | id = udev_device_get_id_filename(udev_device); | |
107 | if (id == NULL) | |
108 | return -1; | |
109 | ||
110 | has_info = device_has_info(udev_device); | |
6ada823a | 111 | util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/run/udev/data/", id, NULL); |
912541b0 KS |
112 | |
113 | /* do not store anything for otherwise empty devices */ | |
114 | if (!has_info && | |
115 | major(udev_device_get_devnum(udev_device)) == 0 && | |
116 | udev_device_get_ifindex(udev_device) == 0) { | |
117 | unlink(filename); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | /* write a database file */ | |
122 | util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL); | |
123 | util_create_path(udev, filename_tmp); | |
124 | f = fopen(filename_tmp, "we"); | |
125 | if (f == NULL) { | |
126 | err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp); | |
127 | return -1; | |
128 | } | |
129 | ||
130 | /* | |
131 | * set 'sticky' bit to indicate that we should not clean the | |
132 | * database when we transition from initramfs to the real root | |
133 | */ | |
134 | if (udev_device_get_db_persist(udev_device)) | |
135 | fchmod(fileno(f), 01644); | |
136 | ||
137 | if (has_info) { | |
138 | struct udev_list_entry *list_entry; | |
139 | ||
140 | if (major(udev_device_get_devnum(udev_device)) > 0) { | |
912541b0 | 141 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) |
6ada823a | 142 | fprintf(f, "S:%s\n", udev_list_entry_get_name(list_entry) + strlen(TEST_PREFIX "/dev/")); |
912541b0 KS |
143 | if (udev_device_get_devlink_priority(udev_device) != 0) |
144 | fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device)); | |
145 | if (udev_device_get_watch_handle(udev_device) >= 0) | |
146 | fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device)); | |
147 | } | |
148 | ||
149 | if (udev_device_get_usec_initialized(udev_device) > 0) | |
150 | fprintf(f, "I:%llu\n", udev_device_get_usec_initialized(udev_device)); | |
151 | ||
152 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { | |
153 | if (!udev_list_entry_get_num(list_entry)) | |
154 | continue; | |
155 | fprintf(f, "E:%s=%s\n", | |
156 | udev_list_entry_get_name(list_entry), | |
157 | udev_list_entry_get_value(list_entry)); | |
158 | } | |
159 | ||
160 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) | |
161 | fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry)); | |
162 | } | |
163 | ||
164 | fclose(f); | |
165 | rename(filename_tmp, filename); | |
baa30fbc | 166 | dbg(udev, "created %s file '%s' for '%s'\n", has_info ? "db" : "empty", |
912541b0 KS |
167 | filename, udev_device_get_devpath(udev_device)); |
168 | return 0; | |
ca999860 GKH |
169 | } |
170 | ||
aa8734ff | 171 | int udev_device_delete_db(struct udev_device *udev_device) |
8e41d35d | 172 | { |
912541b0 | 173 | const char *id; |
912541b0 KS |
174 | char filename[UTIL_PATH_SIZE]; |
175 | ||
176 | id = udev_device_get_id_filename(udev_device); | |
177 | if (id == NULL) | |
178 | return -1; | |
6ada823a | 179 | util_strscpyl(filename, sizeof(filename), TEST_PREFIX "/run/udev/data/", id, NULL); |
912541b0 KS |
180 | unlink(filename); |
181 | return 0; | |
8e41d35d | 182 | } |