chiark / gitweb /
keymap: Tolerate invalid entries in keymaps
[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_INPUT_ID] = &udev_builtin_input_id,
35         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
36         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
37         [UDEV_BUILTIN_PCI_DB] = &udev_builtin_pci_db,
38         [UDEV_BUILTIN_USB_DB] = &udev_builtin_usb_db,
39         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
40 #ifdef HAVE_ACL
41         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
42 #endif
43 };
44
45 int udev_builtin_init(struct udev *udev)
46 {
47         unsigned int i;
48         int err = 0;
49
50         if (initialized)
51                 return 0;
52
53         for (i = 0; i < ELEMENTSOF(builtins); i++) {
54                 if (builtins[i]->init) {
55                         err = builtins[i]->init(udev);
56                         if (err < 0)
57                                 break;
58                 }
59         }
60
61         initialized = true;
62         return err;
63 }
64
65 void udev_builtin_exit(struct udev *udev)
66 {
67         unsigned int i;
68
69         if (!initialized)
70                 return;
71
72         for (i = 0; i < ELEMENTSOF(builtins); i++)
73                 if (builtins[i]->exit)
74                         builtins[i]->exit(udev);
75
76         initialized = false;
77 }
78
79 bool udev_builtin_validate(struct udev *udev)
80 {
81         unsigned int i;
82         bool change = false;
83
84         for (i = 0; i < ELEMENTSOF(builtins); i++)
85                 if (builtins[i]->validate)
86                         if (builtins[i]->validate(udev))
87                                 change = true;
88         return change;
89 }
90
91 void udev_builtin_list(struct udev *udev)
92 {
93         unsigned int i;
94
95         for (i = 0; i < ELEMENTSOF(builtins); i++)
96                 fprintf(stderr, "  %-12s %s\n", builtins[i]->name, builtins[i]->help);
97 }
98
99 const char *udev_builtin_name(enum udev_builtin_cmd cmd)
100 {
101         return builtins[cmd]->name;
102 }
103
104 bool udev_builtin_run_once(enum udev_builtin_cmd cmd)
105 {
106         return builtins[cmd]->run_once;
107 }
108
109 enum udev_builtin_cmd udev_builtin_lookup(const char *command)
110 {
111         char name[UTIL_PATH_SIZE];
112         enum udev_builtin_cmd i;
113         char *pos;
114
115         util_strscpy(name, sizeof(name), command);
116         pos = strchr(name, ' ');
117         if (pos)
118                 pos[0] = '\0';
119         for (i = 0; i < ELEMENTSOF(builtins); i++)
120                 if (strcmp(builtins[i]->name, name) == 0)
121                         return i;
122         return UDEV_BUILTIN_MAX;
123 }
124
125 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
126 {
127         char arg[UTIL_PATH_SIZE];
128         int argc;
129         char *argv[128];
130
131         optind = 0;
132         util_strscpy(arg, sizeof(arg), command);
133         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
134         return builtins[cmd]->cmd(dev, argc, argv, test);
135 }
136
137 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val)
138 {
139         struct udev_list_entry *entry;
140
141         entry = udev_device_add_property(dev, key, val);
142         /* store in db, skip private keys */
143         if (key[0] != '.')
144                 udev_list_entry_set_num(entry, true);
145
146         if (test)
147                 printf("%s=%s\n", key, val);
148         return 0;
149 }