]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
dca59f62 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
11c3a366 TA |
21 | #include <errno.h> |
22 | #include <getopt.h> | |
23 | #include <stdbool.h> | |
24 | #include <stddef.h> | |
25 | ||
26 | #include "log.h" | |
27 | #include "macro.h" | |
07630cea | 28 | #include "string-util.h" |
dca59f62 | 29 | #include "verbs.h" |
a16f96cd | 30 | #include "virt.h" |
dca59f62 LP |
31 | |
32 | int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) { | |
33 | const Verb *verb; | |
34 | const char *name; | |
35 | unsigned i; | |
36 | int left; | |
37 | ||
38 | assert(verbs); | |
39 | assert(verbs[0].dispatch); | |
40 | assert(argc >= 0); | |
41 | assert(argv); | |
42 | assert(argc >= optind); | |
43 | ||
44 | left = argc - optind; | |
45 | name = argv[optind]; | |
46 | ||
47 | for (i = 0;; i++) { | |
48 | bool found; | |
49 | ||
50 | /* At the end of the list? */ | |
51 | if (!verbs[i].dispatch) { | |
52 | if (name) | |
53 | log_error("Unknown operation %s.", name); | |
54 | else | |
55 | log_error("Requires operation parameter."); | |
56 | return -EINVAL; | |
57 | } | |
58 | ||
43343ee7 | 59 | if (name) |
dca59f62 | 60 | found = streq(name, verbs[i].verb); |
43343ee7 LP |
61 | else |
62 | found = !!(verbs[i].flags & VERB_DEFAULT); | |
dca59f62 LP |
63 | |
64 | if (found) { | |
65 | verb = &verbs[i]; | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
43343ee7 LP |
70 | assert(verb); |
71 | ||
72 | if (!name) | |
73 | left = 1; | |
74 | ||
dca59f62 LP |
75 | if (verb->min_args != VERB_ANY && |
76 | (unsigned) left < verb->min_args) { | |
77 | log_error("Too few arguments."); | |
78 | return -EINVAL; | |
79 | } | |
80 | ||
81 | if (verb->max_args != VERB_ANY && | |
82 | (unsigned) left > verb->max_args) { | |
540d8581 | 83 | log_error("Too many arguments."); |
dca59f62 LP |
84 | return -EINVAL; |
85 | } | |
86 | ||
a16f96cd LP |
87 | if ((verb->flags & VERB_NOCHROOT) && running_in_chroot() > 0) { |
88 | log_info("Running in chroot, ignoring request."); | |
89 | return 0; | |
90 | } | |
91 | ||
43343ee7 LP |
92 | if (name) |
93 | return verb->dispatch(left, argv + optind, userdata); | |
94 | else { | |
95 | char* fake[2] = { | |
96 | (char*) verb->verb, | |
97 | NULL | |
98 | }; | |
99 | ||
100 | return verb->dispatch(1, fake, userdata); | |
101 | } | |
dca59f62 | 102 | } |