chiark / gitweb /
udev: update file headers
[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         [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
34         [UDEV_BUILTIN_BTRFS] = &udev_builtin_btrfs,
35         [UDEV_BUILTIN_FIRMWARE] = &udev_builtin_firmware,
36         [UDEV_BUILTIN_HWDB] = &udev_builtin_hwdb,
37         [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
38         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
39         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
40         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
41 #ifdef HAVE_ACL
42         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
43 #endif
44 };
45
46 void udev_builtin_init(struct udev *udev)
47 {
48         unsigned int i;
49
50         if (initialized)
51                 return;
52
53         for (i = 0; i < ELEMENTSOF(builtins); i++)
54                 if (builtins[i]->init)
55                         builtins[i]->init(udev);
56
57         initialized = true;
58 }
59
60 void udev_builtin_exit(struct udev *udev)
61 {
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 {
76         unsigned int i;
77
78         for (i = 0; i < ELEMENTSOF(builtins); i++)
79                 if (builtins[i]->validate && builtins[i]->validate(udev))
80                         return true;
81         return false;
82 }
83
84 void udev_builtin_list(struct udev *udev)
85 {
86         unsigned int i;
87
88         for (i = 0; i < ELEMENTSOF(builtins); i++)
89                 fprintf(stderr, "  %-12s %s\n", builtins[i]->name, builtins[i]->help);
90 }
91
92 const char *udev_builtin_name(enum udev_builtin_cmd cmd)
93 {
94         return builtins[cmd]->name;
95 }
96
97 bool udev_builtin_run_once(enum udev_builtin_cmd cmd)
98 {
99         return builtins[cmd]->run_once;
100 }
101
102 enum udev_builtin_cmd udev_builtin_lookup(const char *command)
103 {
104         char name[UTIL_PATH_SIZE];
105         enum udev_builtin_cmd i;
106         char *pos;
107
108         util_strscpy(name, sizeof(name), command);
109         pos = strchr(name, ' ');
110         if (pos)
111                 pos[0] = '\0';
112         for (i = 0; i < ELEMENTSOF(builtins); i++)
113                 if (strcmp(builtins[i]->name, name) == 0)
114                         return i;
115         return UDEV_BUILTIN_MAX;
116 }
117
118 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
119 {
120         char arg[UTIL_PATH_SIZE];
121         int argc;
122         char *argv[128];
123
124         /* we need '0' here to reset the internal state */
125         optind = 0;
126         util_strscpy(arg, sizeof(arg), command);
127         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
128         return builtins[cmd]->cmd(dev, argc, argv, test);
129 }
130
131 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val)
132 {
133         struct udev_list_entry *entry;
134
135         entry = udev_device_add_property(dev, key, val);
136         /* store in db, skip private keys */
137         if (key[0] != '.')
138                 udev_list_entry_set_num(entry, true);
139
140         if (test)
141                 printf("%s=%s\n", key, val);
142         return 0;
143 }