chiark / gitweb /
hwdb: OUI - use ID_OUI_FROM_DATABASE=
[elogind.git] / src / udev / udev-builtin.c
1 /*
2  * Copyright (C) 2007-2012 Kay Sievers <kay.sievers@vrfy.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 #include "udev.h"
27
28 static bool initialized;
29
30 static const struct udev_builtin *builtins[] = {
31         [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
32         [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs,
33         [UDEV_BUILTIN_FIRMWARE] = &udev_builtin_firmware,
34         [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb,
35         [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
36         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
37         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
38         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
39 #ifdef HAVE_ACL
40         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
41 #endif
42 };
43
44 void udev_builtin_init(struct udev *udev)
45 {
46         unsigned int i;
47
48         if (initialized)
49                 return;
50
51         for (i = 0; i < ELEMENTSOF(builtins); i++)
52                 if (builtins[i]->init)
53                         builtins[i]->init(udev);
54
55         initialized = true;
56 }
57
58 void udev_builtin_exit(struct udev *udev)
59 {
60         unsigned int i;
61
62         if (!initialized)
63                 return;
64
65         for (i = 0; i < ELEMENTSOF(builtins); i++)
66                 if (builtins[i]->exit)
67                         builtins[i]->exit(udev);
68
69         initialized = false;
70 }
71
72 bool udev_builtin_validate(struct udev *udev)
73 {
74         unsigned int i;
75
76         for (i = 0; i < ELEMENTSOF(builtins); i++)
77                 if (builtins[i]->validate && builtins[i]->validate(udev))
78                         return true;
79         return false;
80 }
81
82 void udev_builtin_list(struct udev *udev)
83 {
84         unsigned int i;
85
86         for (i = 0; i < ELEMENTSOF(builtins); i++)
87                 fprintf(stderr, "  %-12s %s\n", builtins[i]->name, builtins[i]->help);
88 }
89
90 const char *udev_builtin_name(enum udev_builtin_cmd cmd)
91 {
92         return builtins[cmd]->name;
93 }
94
95 bool udev_builtin_run_once(enum udev_builtin_cmd cmd)
96 {
97         return builtins[cmd]->run_once;
98 }
99
100 enum udev_builtin_cmd udev_builtin_lookup(const char *command)
101 {
102         char name[UTIL_PATH_SIZE];
103         enum udev_builtin_cmd i;
104         char *pos;
105
106         util_strscpy(name, sizeof(name), command);
107         pos = strchr(name, ' ');
108         if (pos)
109                 pos[0] = '\0';
110         for (i = 0; i < ELEMENTSOF(builtins); i++)
111                 if (strcmp(builtins[i]->name, name) == 0)
112                         return i;
113         return UDEV_BUILTIN_MAX;
114 }
115
116 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
117 {
118         char arg[UTIL_PATH_SIZE];
119         int argc;
120         char *argv[128];
121
122         /* we need '0' here to reset the internal state */
123         optind = 0;
124         util_strscpy(arg, sizeof(arg), command);
125         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
126         return builtins[cmd]->cmd(dev, argc, argv, test);
127 }
128
129 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val)
130 {
131         struct udev_list_entry *entry;
132
133         entry = udev_device_add_property(dev, key, val);
134         /* store in db, skip private keys */
135         if (key[0] != '.')
136                 udev_list_entry_set_num(entry, true);
137
138         if (test)
139                 printf("%s=%s\n", key, val);
140         return 0;
141 }