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