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