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