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