chiark / gitweb /
udev: fix a few issues detected by the llvm static analyzer
[elogind.git] / src / 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_PCI_DB] = &udev_builtin_pci_db,
35         [UDEV_BUILTIN_USB_DB] = &udev_builtin_usb_db,
36         [UDEV_BUILTIN_USB_ID] = &udev_builtin_usb_id,
37 #ifdef HAVE_ACL
38         [UDEV_BUILTIN_UACCESS] = &udev_builtin_uaccess,
39 #endif
40 };
41
42 int udev_builtin_init(struct udev *udev)
43 {
44         unsigned int i;
45         int err;
46
47         for (i = 0; i < ARRAY_SIZE(builtins); i++) {
48                 if (builtins[i]->init) {
49                         err = builtins[i]->init(udev);
50                         if (err < 0)
51                                 break;
52                 }
53         }
54         return err;
55 }
56
57 void udev_builtin_exit(struct udev *udev)
58 {
59         unsigned int i;
60
61         for (i = 0; i < ARRAY_SIZE(builtins); i++)
62                 if (builtins[i]->exit)
63                         builtins[i]->exit(udev);
64 }
65
66 bool udev_builtin_validate(struct udev *udev)
67 {
68         unsigned int i;
69         bool change = false;
70
71         for (i = 0; i < ARRAY_SIZE(builtins); i++)
72                 if (builtins[i]->validate)
73                         if (builtins[i]->validate(udev))
74                                 change = true;
75         return change;
76 }
77
78 void udev_builtin_list(struct udev *udev)
79 {
80         unsigned int i;
81
82         for (i = 0; i < ARRAY_SIZE(builtins); i++)
83                 fprintf(stderr, "  %-12s %s\n", builtins[i]->name, builtins[i]->help);
84 }
85
86 const char *udev_builtin_name(enum udev_builtin_cmd cmd)
87 {
88         return builtins[cmd]->name;
89 }
90
91 bool udev_builtin_run_once(enum udev_builtin_cmd cmd)
92 {
93         return builtins[cmd]->run_once;
94 }
95
96 enum udev_builtin_cmd udev_builtin_lookup(const char *command)
97 {
98         char name[UTIL_PATH_SIZE];
99         enum udev_builtin_cmd i;
100         char *pos;
101
102         util_strscpy(name, sizeof(name), command);
103         pos = strchr(name, ' ');
104         if (pos)
105                 pos[0] = '\0';
106         for (i = 0; i < ARRAY_SIZE(builtins); i++)
107                 if (strcmp(builtins[i]->name, name) == 0)
108                         return i;
109         return UDEV_BUILTIN_MAX;
110 }
111
112 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
113 {
114         char arg[UTIL_PATH_SIZE];
115         int argc;
116         char *argv[128];
117
118         optind = 0;
119         util_strscpy(arg, sizeof(arg), command);
120         udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
121         return builtins[cmd]->cmd(dev, argc, argv, test);
122 }
123
124 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val)
125 {
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         log_debug("%s=%s\n", key, val);
134         if (test)
135                 printf("%s=%s\n", key, val);
136         return 0;
137 }