chiark / gitweb /
[PATCH] udev - man page update
[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 "libsysfs/sysfs/libsysfs.h"
37 #include "udev_version.h"
38 #include "udev.h"
39 #include "logging.h"
40 #include "namedev.h"
41 #include "udevdb.h"
42 #include "tdb/tdb.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         strfieldcpy(keystr, path);
57         key.dptr = keystr;
58         key.dsize = strlen(keystr) + 1;
59
60         data.dptr = (void *)dev;
61         data.dsize = UDEVICE_LEN;
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         memset(dev, 0, sizeof(struct udevice));
81         memcpy(dev, data.dptr, UDEVICE_LEN);
82         return 0;
83 }
84
85 int udevdb_delete_dev(const char *path)
86 {
87         TDB_DATA key;
88         char keystr[SYSFS_PATH_MAX];
89
90         if (path == NULL)
91                 return -EINVAL;
92
93         memset(keystr, 0, sizeof(keystr));
94         strfieldcpy(keystr, path);
95
96         key.dptr = keystr;
97         key.dsize = strlen(keystr) + 1;
98
99         return tdb_delete(udevdb, key);
100 }
101
102 /**
103  * udevdb_exit: closes database
104  */
105 void udevdb_exit(void)
106 {
107         if (udevdb != NULL) {
108                 tdb_close(udevdb);
109                 udevdb = NULL;
110         }
111 }
112
113 /**
114  * udevdb_init: initializes database
115  * @init_flag: UDEVDB_INTERNAL - database stays in memory
116  *             UDEVDB_DEFAULT - database is written to a file
117  */
118 int udevdb_init(int init_flag)
119 {
120         if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
121                 return -EINVAL;
122
123         udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
124         if (udevdb == NULL) {
125                 if (init_flag == UDEVDB_INTERNAL)
126                         dbg("unable to initialize in-memory database");
127                 else
128                         dbg("unable to initialize database at '%s'", udev_db_filename);
129                 return -EACCES;
130         }
131         return 0;
132 }
133
134 /**
135  * udevdb_open_ro: open database for reading
136  */
137 int 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);
142                 return -EACCES;
143         }
144         return 0;
145 }
146
147 static int (*user_record_callback) (char *path, struct udevice *dev);
148
149 static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
150 {
151         return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
152 }
153
154 /**
155  * udevdb_call_foreach: dumps whole database by passing record data to user function
156  * @user_record_handler: user function called for every record in the database
157  */
158 int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev))
159 {
160         int retval = 0;
161
162         if (user_record_handler == NULL) {
163                 dbg("invalid user record handling function");
164                 return -EINVAL;
165         }
166         user_record_callback = user_record_handler;
167         retval = tdb_traverse(udevdb, traverse_callback, NULL);
168         if (retval < 0)
169                 return -ENODEV;
170         else
171                 return 0;
172 }
173
174 static struct udevice *find_dev;
175 static char *find_path;
176 static const char *find_name;
177 static int find_found;
178
179 static int find_device_by_name(char *path, struct udevice *dev)
180 {
181         if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
182                 memcpy(find_dev, dev, sizeof(*find_dev));
183                 strfieldcpy(find_path, path);
184                 find_found = 1;
185                 /* stop search */
186                 return 1;
187         }
188         return 0;
189 }
190
191 /**
192  * udevdb_get_dev_byname: search device with given name by traversing the whole database
193  */
194 int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev)
195 {
196         find_found = 0;
197         find_path = path;
198         find_dev = dev;
199         find_name = name;
200         udevdb_call_foreach(find_device_by_name);
201         if (find_found == 1)
202                 return 0;
203         else
204                 return -1;
205 }