]>
Commit | Line | Data |
---|---|---|
8dfc8dbe GKH |
1 | /* |
2 | * udevdb.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
6 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
7 | * Copyright (C) 2003 IBM Corp. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | */ | |
23 | ||
8e41d35d DS |
24 | /* |
25 | * udev database library | |
26 | */ | |
d546791d | 27 | #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T |
8e41d35d DS |
28 | #include <stdlib.h> |
29 | #include <stdio.h> | |
8ea84a8a | 30 | #include <string.h> |
8e41d35d DS |
31 | #include <fcntl.h> |
32 | #include <string.h> | |
33 | #include <sys/stat.h> | |
34 | #include <errno.h> | |
a2822451 | 35 | #include <signal.h> |
8e41d35d | 36 | |
c80da508 | 37 | #include "libsysfs/sysfs/libsysfs.h" |
6739707d GKH |
38 | #include "udev_version.h" |
39 | #include "udev.h" | |
54988802 | 40 | #include "logging.h" |
6739707d | 41 | #include "namedev.h" |
8e41d35d | 42 | #include "udevdb.h" |
a2822451 | 43 | #include "tdb/tdb.h" |
8e41d35d | 44 | |
dbfc520c | 45 | static TDB_CONTEXT *udevdb; |
8e41d35d | 46 | |
ca999860 | 47 | |
5840bc63 | 48 | int udevdb_add_dev(const char *path, const struct udevice *dev) |
ca999860 GKH |
49 | { |
50 | TDB_DATA key, data; | |
5840bc63 | 51 | char keystr[SYSFS_PATH_MAX]; |
ca999860 | 52 | |
5840bc63 GKH |
53 | if ((path == NULL) || (dev == NULL)) |
54 | return -ENODEV; | |
ca999860 | 55 | |
5840bc63 | 56 | memset(keystr, 0, NAME_SIZE); |
c472e3c8 | 57 | strfieldcpy(keystr, path); |
5840bc63 | 58 | key.dptr = keystr; |
ca999860 | 59 | key.dsize = strlen(keystr) + 1; |
f7b4eca4 | 60 | |
5840bc63 | 61 | data.dptr = (void *)dev; |
00866ed2 KS |
62 | data.dsize = UDEVICE_LEN; |
63 | ||
5840bc63 | 64 | return tdb_store(udevdb, key, data, TDB_REPLACE); |
ca999860 GKH |
65 | } |
66 | ||
a56ef382 | 67 | int udevdb_get_dev(const char *path, struct udevice *dev) |
8e41d35d DS |
68 | { |
69 | TDB_DATA key, data; | |
8e41d35d | 70 | |
5840bc63 | 71 | if (path == NULL) |
a56ef382 | 72 | return -ENODEV; |
8e41d35d | 73 | |
5840bc63 GKH |
74 | key.dptr = (void *)path; |
75 | key.dsize = strlen(path) + 1; | |
8e41d35d | 76 | |
dbfc520c | 77 | data = tdb_fetch(udevdb, key); |
8e41d35d | 78 | if (data.dptr == NULL || data.dsize == 0) |
a56ef382 | 79 | return -ENODEV; |
f7b4eca4 | 80 | |
00866ed2 KS |
81 | memset(dev, 0, sizeof(struct udevice)); |
82 | memcpy(dev, data.dptr, UDEVICE_LEN); | |
a56ef382 | 83 | return 0; |
8e41d35d DS |
84 | } |
85 | ||
5840bc63 | 86 | int udevdb_delete_dev(const char *path) |
8e41d35d DS |
87 | { |
88 | TDB_DATA key; | |
5840bc63 | 89 | char keystr[SYSFS_PATH_MAX]; |
8e41d35d | 90 | |
5840bc63 GKH |
91 | if (path == NULL) |
92 | return -EINVAL; | |
a9ce0a41 GKH |
93 | |
94 | memset(keystr, 0, sizeof(keystr)); | |
c472e3c8 | 95 | strfieldcpy(keystr, path); |
a9ce0a41 GKH |
96 | |
97 | key.dptr = keystr; | |
98 | key.dsize = strlen(keystr) + 1; | |
f7b4eca4 | 99 | |
a9ce0a41 GKH |
100 | return tdb_delete(udevdb, key); |
101 | } | |
102 | ||
dbfc520c DS |
103 | /** |
104 | * udevdb_exit: closes database | |
105 | */ | |
106 | void udevdb_exit(void) | |
107 | { | |
5840bc63 GKH |
108 | if (udevdb != NULL) { |
109 | tdb_close(udevdb); | |
110 | udevdb = NULL; | |
111 | } | |
dbfc520c DS |
112 | } |
113 | ||
114 | /** | |
115 | * udevdb_init: initializes database | |
f7b4eca4 KS |
116 | * @init_flag: UDEVDB_INTERNAL - database stays in memory |
117 | * UDEVDB_DEFAULT - database is written to a file | |
dbfc520c DS |
118 | */ |
119 | int udevdb_init(int init_flag) | |
120 | { | |
121 | if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL) | |
5840bc63 | 122 | return -EINVAL; |
dbfc520c | 123 | |
c056c514 | 124 | udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644); |
5840bc63 GKH |
125 | if (udevdb == NULL) { |
126 | if (init_flag == UDEVDB_INTERNAL) | |
f7b4eca4 | 127 | dbg("unable to initialize in-memory database"); |
5840bc63 | 128 | else |
f7b4eca4 | 129 | dbg("unable to initialize database at '%s'", udev_db_filename); |
ee1db00d | 130 | return -EACCES; |
5840bc63 GKH |
131 | } |
132 | return 0; | |
dbfc520c | 133 | } |
f4dc8d11 KS |
134 | |
135 | /** | |
4070d2fe | 136 | * udevdb_open_ro: open database for reading |
f4dc8d11 KS |
137 | */ |
138 | int udevdb_open_ro(void) | |
139 | { | |
140 | udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0); | |
141 | if (udevdb == NULL) { | |
142 | dbg("unable to open database at '%s'", udev_db_filename); | |
ee1db00d KS |
143 | return -EACCES; |
144 | } | |
145 | return 0; | |
146 | } | |
147 | ||
b2a21a35 | 148 | static int (*user_record_callback) (char *path, struct udevice *dev); |
ee1db00d KS |
149 | |
150 | static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state) | |
151 | { | |
b2a21a35 | 152 | return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr); |
ee1db00d KS |
153 | } |
154 | ||
155 | /** | |
b2a21a35 | 156 | * udevdb_call_foreach: dumps whole database by passing record data to user function |
ee1db00d KS |
157 | * @user_record_handler: user function called for every record in the database |
158 | */ | |
b2a21a35 | 159 | int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev)) |
ee1db00d | 160 | { |
b2a21a35 KS |
161 | int retval = 0; |
162 | ||
ee1db00d KS |
163 | if (user_record_handler == NULL) { |
164 | dbg("invalid user record handling function"); | |
f4dc8d11 KS |
165 | return -EINVAL; |
166 | } | |
ee1db00d | 167 | user_record_callback = user_record_handler; |
b2a21a35 KS |
168 | retval = tdb_traverse(udevdb, traverse_callback, NULL); |
169 | if (retval < 0) | |
170 | return -ENODEV; | |
171 | else | |
172 | return 0; | |
173 | } | |
174 | ||
175 | static struct udevice *find_dev; | |
176 | static char *find_path; | |
177 | static const char *find_name; | |
178 | static int find_found; | |
179 | ||
180 | static int find_device_by_name(char *path, struct udevice *dev) | |
181 | { | |
ef672b3d KS |
182 | char *pos; |
183 | int len; | |
9fe3f9a9 | 184 | |
b2a21a35 | 185 | if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) { |
8ea84a8a | 186 | memcpy(find_dev, dev, sizeof(struct udevice)); |
17794d77 | 187 | strfieldcpymax(find_path, path, NAME_SIZE); |
b2a21a35 KS |
188 | find_found = 1; |
189 | /* stop search */ | |
190 | return 1; | |
191 | } | |
8ea84a8a | 192 | /* look for matching symlink*/ |
9fe3f9a9 | 193 | foreach_strpart(dev->symlink, " ", pos, len) { |
ef672b3d | 194 | if (strncmp(pos, find_name, len) != 0) |
9fe3f9a9 KS |
195 | continue; |
196 | ||
197 | if (len != strlen(find_name)) | |
198 | continue; | |
199 | ||
200 | memcpy(find_dev, dev, sizeof(struct udevice)); | |
17794d77 | 201 | strfieldcpymax(find_path, path, NAME_SIZE); |
9fe3f9a9 KS |
202 | find_found = 1; |
203 | return 1; | |
204 | } | |
f4dc8d11 KS |
205 | return 0; |
206 | } | |
b2a21a35 KS |
207 | |
208 | /** | |
209 | * udevdb_get_dev_byname: search device with given name by traversing the whole database | |
210 | */ | |
211 | int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev) | |
212 | { | |
213 | find_found = 0; | |
214 | find_path = path; | |
215 | find_dev = dev; | |
216 | find_name = name; | |
217 | udevdb_call_foreach(find_device_by_name); | |
218 | if (find_found == 1) | |
219 | return 0; | |
220 | else | |
221 | return -1; | |
222 | } |