chiark / gitweb /
9804c50faecc8cff4abdc4920c24232639cabbf2
[elogind.git] / udev / udev-builtin.c
1 /*
2  * Copyright (C) 2007-2009 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 const struct udev_builtin *builtins[] = {
29         [UDEV_BUILTIN_BLKID] = &udev_builtin_blkid,
30         [UDEV_BUILTIN_FIRMWARE] = &udev_builtin_firmware,
31         [UDEV_BUILTIN_INPUT_ID] = &udev_builtin_input_id,
32         [UDEV_BUILTIN_KMOD] = &udev_builtin_kmod,
33         [UDEV_BUILTIN_PATH_ID] = &udev_builtin_path_id,
34         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
35 };
36
37 int udev_builtin_init(struct udev *udev)
38 {
39         unsigned int i;
40         int err;
41
42         for (i = 0; i < ARRAY_SIZE(builtins); i++) {
43                 if (builtins[i]->init) {
44                         err = builtins[i]->init(udev);
45                         if (err < 0)
46                                 break;
47                 }
48         }
49         return err;
50 }
51
52 void udev_builtin_exit(struct udev *udev)
53 {
54         unsigned int i;
55
56         for (i = 0; i < ARRAY_SIZE(builtins); i++)
57                 if (builtins[i]->exit)
58                         builtins[i]->exit(udev);
59 }
60
61 bool udev_builtin_validate(struct udev *udev)
62 {
63         unsigned int i;
64         bool change = false;
65
66         for (i = 0; i < ARRAY_SIZE(builtins); i++)
67                 if (builtins[i]->validate)
68                         if (builtins[i]->validate(udev))
69                                 change = true;
70         return change;
71 }
72
73 void udev_builtin_list(struct udev *udev)
74 {
75         unsigned int i;
76
77         for (i = 0; i < ARRAY_SIZE(builtins); i++)
78                 fprintf(stderr, "  %-12s %s\n", builtins[i]->name, builtins[i]->help);
79 }
80
81 const char *udev_builtin_name(enum udev_builtin_cmd cmd)
82 {
83         return builtins[cmd]->name;
84 }
85
86 bool udev_builtin_run_once(enum udev_builtin_cmd cmd)
87 {
88         return builtins[cmd]->run_once;
89 }
90
91 enum udev_builtin_cmd udev_builtin_lookup(const char *command)
92 {
93         char name[UTIL_PATH_SIZE];
94         enum udev_builtin_cmd i;
95         char *pos;
96
97         util_strscpy(name, sizeof(name), command);
98         pos = strchr(name, ' ');
99         if (pos)
100                 pos[0] = '\0';
101         for (i = 0; i < ARRAY_SIZE(builtins); i++)
102                 if (strcmp(builtins[i]->name, name) == 0)
103                         return i;
104         return UDEV_BUILTIN_MAX;
105 }
106
107 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
108 {
109         char arg[UTIL_PATH_SIZE];
110         int argc;
111         char *argv[128];
112
113         optind = 0;
114         util_strscpy(arg, sizeof(arg), command);
115         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
116         return builtins[cmd]->cmd(dev, argc, argv, test);
117 }
118
119 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val)
120 {
121         struct udev_list_entry *entry;
122
123         entry = udev_device_add_property(dev, key, val);
124         /* store in db, skip private keys */
125         if (key[0] != '.')
126                 udev_list_entry_set_num(entry, true);
127
128         info(udev_device_get_udev(dev), "%s=%s\n", key, val);
129         if (test)
130                 printf("%s=%s\n", key, val);
131         return 0;
132 }