-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcommands.c
245 lines (199 loc) · 5.66 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Copyright 2008 Google Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "commands.h"
#include "platform.h"
#ifdef ARCH_X86
#include <sys/io.h>
#endif /* #ifdef ARCH_X86 */
/* Provide shared parameters the describe the size of the operation for
* subcommand implementations. */
#define MAKE_SIZE_PARAM(size_) \
const struct size_param size ##size_ = { \
.size = SIZE ##size_, \
}
MAKE_SIZE_PARAM(8);
MAKE_SIZE_PARAM(16);
MAKE_SIZE_PARAM(32);
MAKE_SIZE_PARAM(64);
static struct cmd_group *group_head;
static const struct cmd_info *
locate_command(const char *cmd)
{
int i;
const struct cmd_group *group;
for (group = group_head; group; group = group->next) {
for (i=0; i < group->num_commands; i++) {
if (!strcmp(cmd, group->commands[i].name))
return &group->commands[i];
}
}
/* Command not found. */
return NULL;
}
static int
check_prereqs(int argc, const char *argv[], const struct prereq_params *params)
{
/* If there are no prerequisites the check has succeeded. */
if (params == NULL) {
return 0;
}
if (argc < params->min_args || argc > params->max_args) {
fprintf(stderr, "usage: %s %s\n", argv[0], params->usage);
return -1;
}
#ifdef ARCH_X86
/* If iopl_needed is non-zero attempt to change to iopl. */
if (params->iopl_needed != 0 && iopl(params->iopl_needed) < 0) {
fprintf(stderr, "can't set io privilege level\n");
return -1;
}
#endif /* #ifdef ARCH_X86 */
return 0;
}
static int
_run_command(int argc, const char *argv[], const struct cmd_info *cmd_info)
{
if (check_prereqs(argc, argv, cmd_info->params) < 0) {
return -1;
}
return cmd_info->entry(argc, argv, cmd_info);
}
int
run_command(int argc, const char *argv[])
{
const struct cmd_info *cmd_info;
const char *cmd_name;
/* First check if the 1st parameter is a command that exists.
* i.e. iotools io_read8 0x70 */
if (argc > 1) {
cmd_info = locate_command(argv[1]);
/* If command is found, execute it directly. */
if (cmd_info != NULL) {
return _run_command(--argc, ++argv, cmd_info);
}
}
/* Assume iotools was executed through a symlink, but strip off the any
* leading path to obtain the desired subcommand. */
cmd_name = strrchr(argv[0], '/');
cmd_name = (cmd_name == NULL) ? argv[0] : cmd_name+1;
cmd_info = locate_command(cmd_name);
if (cmd_info != NULL) {
return _run_command(argc, argv, cmd_info);
}
/* Subcommand was not found. Perform the fallback operation. */
return iotools_fallback(argc, argv);
}
static int
locate_path_of_binary(char **path_to_bin, char **bin_name)
{
char *lbin_name, *lpath_to_bin;
static char bin_fullpath[FILENAME_MAX];
/* Find the location of the currently executing binary. */
if (readlink("/proc/self/exe", bin_fullpath, FILENAME_MAX) == -1) {
fprintf(stderr, "Unable to locate currently running binary.\n");
return -1;
}
lbin_name = strrchr(bin_fullpath, '/');
*lbin_name++ = '\0';
lpath_to_bin = &bin_fullpath[0];
*bin_name = lbin_name;
*path_to_bin = lpath_to_bin;
return 0;
}
static const char *
build_symlink_name(const char *path_to_bin, const struct cmd_info *cmd)
{
static char link_name[FILENAME_MAX];
snprintf(link_name, FILENAME_MAX, "%s/%s", path_to_bin, cmd->name);
return link_name;
}
int
make_command_links(void)
{
char *bin_name, *path_to_bin;
int i;
const struct cmd_group *group;
if (locate_path_of_binary(&path_to_bin, &bin_name) < 0) {
return -1;
}
for (group = group_head; group; group = group->next) {
for (i=0; i < group->num_commands; i++) {
const char *link_name;
link_name = build_symlink_name(path_to_bin,
&group->commands[i]);
fprintf(stdout, "Creating link: %s -> %s\n",
link_name, bin_name);
if (symlink(bin_name, link_name) != 0) {
fprintf(stderr,
"Unable to create link: %s -> %s\n",
link_name, bin_name);
}
}
}
return 0;
}
int
clean_command_links(void)
{
char *bin_name, *path_to_bin;
int i;
const struct cmd_group *group;
if (locate_path_of_binary(&path_to_bin, &bin_name) < 0) {
return -1;
}
for (group = group_head; group; group = group->next) {
for (i=0; i < group->num_commands; i++) {
int r;
const char *link_name;
link_name = build_symlink_name(path_to_bin,
&group->commands[i]);
r = unlink(link_name);
if (r < 0 && errno != ENOENT) {
fprintf(stderr,
"Unable to remove link: %s: %s\n",
link_name, strerror(errno));
}
}
}
return 0;
}
int
list_commands(void)
{
int i;
const struct cmd_group *group;
for (group = group_head; group; group = group->next) {
fprintf(stdout, "%s%s%s\n", group->name,
group->description ? ": " : "",
group->description ? : "");
for (i=0; i < group->num_commands; i++) {
fprintf(stdout, " %s\n", group->commands[i].name);
}
}
return 0;
}
int
register_command_group(struct cmd_group *group)
{
/* insert group at the beginning of the list. */
group->next = group_head;
group_head = group;
return 0;
}