]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevdb.c
3d0a9ea3c2b6cc2a646ebe6b3bf7f5fc88ffd7bc
[thirdparty/systemd.git] / udevdb.c
1 /*
2 * udevdb.c - udev database library
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
24 #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stddef.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <signal.h>
34
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev.h"
37 #include "udev_lib.h"
38 #include "udev_version.h"
39 #include "logging.h"
40 #include "namedev.h"
41 #include "udevdb.h"
42 #include "tdb/tdb.h"
43
44 static TDB_CONTEXT *udevdb;
45 sig_atomic_t gotalarm;
46
47 int udevdb_add_dev(struct udevice *udev)
48 {
49 TDB_DATA key, data;
50 char keystr[SYSFS_PATH_MAX];
51
52 if (udevdb == NULL)
53 return -1;
54
55 memset(keystr, 0x00, SYSFS_PATH_MAX);
56 strfieldcpy(keystr, udev->devpath);
57 key.dptr = keystr;
58 key.dsize = strlen(keystr) + 1;
59
60 data.dptr = (void *) udev;
61 data.dsize = UDEVICE_DB_LEN;
62 dbg("store key '%s' for device '%s'", keystr, udev->name);
63
64 return tdb_store(udevdb, key, data, TDB_REPLACE);
65 }
66
67 int udevdb_get_dev(const char *path, struct udevice *udev)
68 {
69 TDB_DATA key, data;
70
71 if (udevdb == NULL)
72 return -1;
73
74 if (path == NULL)
75 return -ENODEV;
76
77 key.dptr = (void *)path;
78 key.dsize = strlen(path) + 1;
79
80 data = tdb_fetch(udevdb, key);
81 if (data.dptr == NULL || data.dsize == 0)
82 return -ENODEV;
83
84 memset(udev, 0x00, sizeof(struct udevice));
85 memcpy(udev, data.dptr, UDEVICE_DB_LEN);
86
87 return 0;
88 }
89
90 int udevdb_delete_dev(const char *path)
91 {
92 TDB_DATA key;
93 char keystr[SYSFS_PATH_MAX];
94
95 if (udevdb == NULL)
96 return -1;
97
98 if (path == NULL)
99 return -EINVAL;
100
101 memset(keystr, 0, sizeof(keystr));
102 strfieldcpy(keystr, path);
103
104 key.dptr = keystr;
105 key.dsize = strlen(keystr) + 1;
106
107 return tdb_delete(udevdb, key);
108 }
109
110 /**
111 * udevdb_exit: closes database
112 */
113 void udevdb_exit(void)
114 {
115 if (udevdb != NULL) {
116 tdb_close(udevdb);
117 udevdb = NULL;
118 }
119 }
120
121 /**
122 * udevdb_init: initializes database
123 * @init_flag: UDEVDB_INTERNAL - database stays in memory
124 * UDEVDB_DEFAULT - database is written to a file
125 */
126 int udevdb_init(int init_flag)
127 {
128 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
129 return -EINVAL;
130
131 tdb_set_lock_alarm(&gotalarm);
132
133 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
134 if (udevdb == NULL) {
135 if (init_flag == UDEVDB_INTERNAL)
136 dbg("unable to initialize in-memory database");
137 else
138 dbg("unable to initialize database at '%s'", udev_db_filename);
139 return -EACCES;
140 }
141 return 0;
142 }
143
144 /**
145 * udevdb_open_ro: open database for reading
146 */
147 int udevdb_open_ro(void)
148 {
149 udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0);
150 if (udevdb == NULL) {
151 dbg("unable to open database at '%s'", udev_db_filename);
152 return -EACCES;
153 }
154 return 0;
155 }
156
157 static int (*user_record_callback) (const char *path, struct udevice *dev);
158
159 static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
160 {
161 return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
162 }
163
164 /**
165 * udevdb_call_foreach: dumps whole database by passing record data to user function
166 * @user_record_handler: user function called for every record in the database
167 */
168 int udevdb_call_foreach(int (*user_record_handler) (const char *path, struct udevice *dev))
169 {
170 int retval = 0;
171
172 if (udevdb == NULL)
173 return -1;
174
175 if (user_record_handler == NULL) {
176 dbg("invalid user record handling function");
177 return -EINVAL;
178 }
179 user_record_callback = user_record_handler;
180 retval = tdb_traverse(udevdb, traverse_callback, NULL);
181 if (retval < 0)
182 return -ENODEV;
183 else
184 return 0;
185 }
186
187 static struct udevice *find_dev;
188 static char *find_path;
189 static const char *find_name;
190 static int find_found;
191
192 static int find_device_by_name(const char *path, struct udevice *udev)
193 {
194 char *pos;
195 int len;
196
197 if (strncmp(udev->name, find_name, sizeof(udev->name)) == 0) {
198 memcpy(find_dev, udev, sizeof(struct udevice));
199 strfieldcpymax(find_path, path, NAME_SIZE);
200 find_found = 1;
201 /* stop search */
202 return 1;
203 }
204 /* look for matching symlink*/
205 foreach_strpart(udev->symlink, " ", pos, len) {
206 if (strncmp(pos, find_name, len) != 0)
207 continue;
208
209 if (len != strlen(find_name))
210 continue;
211
212 memcpy(find_dev, udev, sizeof(struct udevice));
213 strfieldcpymax(find_path, path, NAME_SIZE);
214 find_found = 1;
215 return 1;
216 }
217 return 0;
218 }
219
220 /**
221 * udevdb_get_dev_byname: search device with given name by traversing the whole database
222 */
223 int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev)
224 {
225 find_found = 0;
226 find_path = path;
227 find_dev = dev;
228 find_name = name;
229 udevdb_call_foreach(find_device_by_name);
230 if (find_found == 1)
231 return 0;
232 else
233 return -1;
234 }