]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevdb.c
[PATCH] udev - kill %D from udev-test.pl
[thirdparty/systemd.git] / udevdb.c
CommitLineData
8dfc8dbe
GKH
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
8e41d35d
DS
24/*
25 * udev database library
26 */
d546791d 27#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
8e41d35d
DS
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>
a2822451 34#include <signal.h>
8e41d35d 35
6739707d
GKH
36#include "udev_version.h"
37#include "udev.h"
54988802 38#include "logging.h"
6739707d 39#include "namedev.h"
8e41d35d 40#include "udevdb.h"
a2822451 41#include "tdb/tdb.h"
ca999860 42#include "libsysfs/libsysfs.h"
8e41d35d 43
dbfc520c 44static TDB_CONTEXT *udevdb;
8e41d35d 45
ca999860 46
5840bc63 47int udevdb_add_dev(const char *path, const struct udevice *dev)
ca999860
GKH
48{
49 TDB_DATA key, data;
5840bc63 50 char keystr[SYSFS_PATH_MAX];
ca999860 51
5840bc63
GKH
52 if ((path == NULL) || (dev == NULL))
53 return -ENODEV;
ca999860 54
5840bc63 55 memset(keystr, 0, NAME_SIZE);
ca999860 56 strcpy(keystr, path);
5840bc63 57 key.dptr = keystr;
ca999860 58 key.dsize = strlen(keystr) + 1;
f7b4eca4 59
5840bc63
GKH
60 data.dptr = (void *)dev;
61 data.dsize = sizeof(*dev);
ca999860 62
5840bc63 63 return tdb_store(udevdb, key, data, TDB_REPLACE);
ca999860
GKH
64}
65
a56ef382 66int udevdb_get_dev(const char *path, struct udevice *dev)
8e41d35d
DS
67{
68 TDB_DATA key, data;
8e41d35d 69
5840bc63 70 if (path == NULL)
a56ef382 71 return -ENODEV;
8e41d35d 72
5840bc63
GKH
73 key.dptr = (void *)path;
74 key.dsize = strlen(path) + 1;
8e41d35d 75
dbfc520c 76 data = tdb_fetch(udevdb, key);
8e41d35d 77 if (data.dptr == NULL || data.dsize == 0)
a56ef382 78 return -ENODEV;
f7b4eca4 79
5840bc63 80 memcpy(dev, data.dptr, sizeof(*dev));
a56ef382 81 return 0;
8e41d35d
DS
82}
83
5840bc63 84int udevdb_delete_dev(const char *path)
8e41d35d
DS
85{
86 TDB_DATA key;
5840bc63 87 char keystr[SYSFS_PATH_MAX];
8e41d35d 88
5840bc63
GKH
89 if (path == NULL)
90 return -EINVAL;
a9ce0a41
GKH
91
92 memset(keystr, 0, sizeof(keystr));
93 strcpy(keystr, path);
94
95 key.dptr = keystr;
96 key.dsize = strlen(keystr) + 1;
f7b4eca4 97
a9ce0a41
GKH
98 return tdb_delete(udevdb, key);
99}
100
dbfc520c
DS
101/**
102 * udevdb_exit: closes database
103 */
104void udevdb_exit(void)
105{
5840bc63
GKH
106 if (udevdb != NULL) {
107 tdb_close(udevdb);
108 udevdb = NULL;
109 }
dbfc520c
DS
110}
111
112/**
113 * udevdb_init: initializes database
f7b4eca4
KS
114 * @init_flag: UDEVDB_INTERNAL - database stays in memory
115 * UDEVDB_DEFAULT - database is written to a file
dbfc520c
DS
116 */
117int udevdb_init(int init_flag)
118{
119 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
5840bc63 120 return -EINVAL;
dbfc520c 121
c056c514 122 udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
5840bc63
GKH
123 if (udevdb == NULL) {
124 if (init_flag == UDEVDB_INTERNAL)
f7b4eca4 125 dbg("unable to initialize in-memory database");
5840bc63 126 else
f7b4eca4 127 dbg("unable to initialize database at '%s'", udev_db_filename);
ee1db00d 128 return -EACCES;
5840bc63
GKH
129 }
130 return 0;
dbfc520c 131}
f4dc8d11
KS
132
133/**
4070d2fe 134 * udevdb_open_ro: open database for reading
f4dc8d11
KS
135 */
136int 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);
ee1db00d
KS
141 return -EACCES;
142 }
143 return 0;
144}
145
146void (*user_record_callback) (char *path, struct udevice *dev);
147
148static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
149{
150 user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
151 return 0;
152}
153
154/**
155 * udevdb_dump: dumps whole database by passing record data to user function
156 * @user_record_handler: user function called for every record in the database
157 */
158int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev))
159{
160 if (user_record_handler == NULL) {
161 dbg("invalid user record handling function");
f4dc8d11
KS
162 return -EINVAL;
163 }
ee1db00d
KS
164 user_record_callback = user_record_handler;
165 tdb_traverse(udevdb, traverse_callback, NULL);
f4dc8d11
KS
166 return 0;
167}