chiark / gitweb /
d2aec1b200b7d41fed9c428c50064f8bfd346e7c
[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 <stdio.h>
21 #include <string.h>
22 #include <getopt.h>
23
24 #include "udev.h"
25
26 static bool initialized;
27
28 static const struct udev_builtin *builtins[] = {
29 #ifdef HAVE_BLKID
30         [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
31 #endif
32         [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs,
33         [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb,
34         [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
35         [UDEV_BUILTIN_KEYBOARD] = &udev_builtin_keyboard,
36 #ifdef HAVE_KMOD
37         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
38 #endif
39         [UDEV_BUILTIN_NET_ID] = &udev_builtin_net_id,
40         [UDEV_BUILTIN_NET_LINK] = &udev_builtin_net_setup_link,
41         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
42         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
43 #ifdef HAVE_ACL
44         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
45 #endif
46 };
47
48 void udev_builtin_init(struct udev *udev) {
49         unsigned int i;
50
51         if (initialized)
52                 return;
53
54         for (i = 0; i < ELEMENTSOF(builtins); i++)
55                 if (builtins[i]->init)
56                         builtins[i]->init(udev);
57
58         initialized = true;
59 }
60
61 void udev_builtin_exit(struct udev *udev) {
62         unsigned int i;
63
64         if (!initialized)
65                 return;
66
67         for (i = 0; i < ELEMENTSOF(builtins); i++)
68                 if (builtins[i]->exit)
69                         builtins[i]->exit(udev);
70
71         initialized = false;
72 }
73
74 bool udev_builtin_validate(struct udev *udev) {
75         unsigned int i;
76
77         for (i = 0; i < ELEMENTSOF(builtins); i++)
78                 if (builtins[i]->validate && builtins[i]->validate(udev))
79                         return true;
80         return false;
81 }
82
83 void udev_builtin_list(struct udev *udev) {
84         unsigned int i;
85
86         for (i = 0; i < ELEMENTSOF(builtins); i++)
87                 fprintf(stderr, "  %-14s  %s\n", builtins[i]->name, builtins[i]->help);
88 }
89
90 const char *udev_builtin_name(enum udev_builtin_cmd cmd) {
91         return builtins[cmd]->name;
92 }
93
94 bool udev_builtin_run_once(enum udev_builtin_cmd cmd) {
95         return builtins[cmd]->run_once;
96 }
97
98 enum udev_builtin_cmd udev_builtin_lookup(const char *command) {
99         char name[UTIL_PATH_SIZE];
100         enum udev_builtin_cmd i;
101         char *pos;
102
103         strscpy(name, sizeof(name), command);
104         pos = strchr(name, ' ');
105         if (pos)
106                 pos[0] = '\0';
107         for (i = 0; i < ELEMENTSOF(builtins); i++)
108                 if (streq(builtins[i]->name, name))
109                         return i;
110         return UDEV_BUILTIN_MAX;
111 }
112
113 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test) {
114         char arg[UTIL_PATH_SIZE];
115         int argc;
116         char *argv[128];
117
118         /* we need '0' here to reset the internal state */
119         optind = 0;
120         strscpy(arg, sizeof(arg), command);
121         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
122         return builtins[cmd]->cmd(dev, argc, argv, test);
123 }
124
125 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val) {
126         struct udev_list_entry *entry;
127
128         entry = udev_device_add_property(dev, key, val);
129         /* store in db, skip private keys */
130         if (key[0] != '.')
131                 udev_list_entry_set_num(entry, true);
132
133         if (test)
134                 printf("%s=%s\n", key, val);
135         return 0;
136 }