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