]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevdb.c
[PATCH] use udevdir in udev.conf
[thirdparty/systemd.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 #include <stdlib.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <signal.h>
34
35 #include "udev_version.h"
36 #include "udev.h"
37 #include "namedev.h"
38 #include "udevdb.h"
39 #include "tdb/tdb.h"
40 #include "libsysfs/libsysfs.h"
41
42 static TDB_CONTEXT *udevdb;
43
44
45 int udevdb_add_dev(const char *path, const struct udevice *dev)
46 {
47 TDB_DATA key, data;
48 char keystr[SYSFS_PATH_MAX];
49
50 if ((path == NULL) || (dev == NULL))
51 return -ENODEV;
52
53 memset(keystr, 0, NAME_SIZE);
54 strcpy(keystr, path);
55 key.dptr = keystr;
56 key.dsize = strlen(keystr) + 1;
57
58 data.dptr = (void *)dev;
59 data.dsize = sizeof(*dev);
60
61 return tdb_store(udevdb, key, data, TDB_REPLACE);
62 }
63
64 struct udevice *udevdb_get_dev(const char *path)
65 {
66 TDB_DATA key, data;
67 struct udevice *dev;
68
69 if (path == NULL)
70 return NULL;
71
72 key.dptr = (void *)path;
73 key.dsize = strlen(path) + 1;
74
75 data = tdb_fetch(udevdb, key);
76 if (data.dptr == NULL || data.dsize == 0)
77 return NULL;
78
79 dev = malloc(sizeof(*dev));
80 if (dev == NULL)
81 goto exit;
82
83 memcpy(dev, data.dptr, sizeof(*dev));
84 exit:
85 free(data.dptr);
86 return dev;
87 }
88
89 int udevdb_delete_dev(const char *path)
90 {
91 TDB_DATA key;
92 char keystr[SYSFS_PATH_MAX];
93
94 if (path == NULL)
95 return -EINVAL;
96
97 memset(keystr, 0, sizeof(keystr));
98 strcpy(keystr, path);
99
100 key.dptr = keystr;
101 key.dsize = strlen(keystr) + 1;
102
103 return tdb_delete(udevdb, key);
104 }
105
106 /**
107 * udevdb_exit: closes database
108 */
109 void udevdb_exit(void)
110 {
111 if (udevdb != NULL) {
112 tdb_close(udevdb);
113 udevdb = NULL;
114 }
115 }
116
117 /**
118 * udevdb_init: initializes database
119 * @init_flag: UDEVDB_INTERNAL - database stays in memory
120 * UDEVDB_DEFAULT - database is written to a file
121 */
122 int udevdb_init(int init_flag)
123 {
124 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
125 return -EINVAL;
126
127 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
128 if (udevdb == NULL) {
129 if (init_flag == UDEVDB_INTERNAL)
130 dbg("unable to initialize in-memory database");
131 else
132 dbg("unable to initialize database at '%s'", udev_db_filename);
133 return -EINVAL;
134 }
135 return 0;
136 }