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