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