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