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