]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hwdb/hwdb.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / hwdb / hwdb.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
65eb4378 2
65eb4378 3#include <getopt.h>
6a34639e
YW
4
5#include "sd-hwdb.h"
65eb4378 6
b5efdb8a 7#include "alloc-util.h"
65eb4378 8#include "hwdb-util.h"
5e332028 9#include "main-func.h"
294bf0c3 10#include "pretty-print.h"
ea683512 11#include "selinux-util.h"
353b2baa 12#include "terminal-util.h"
3f6fd1ba
LP
13#include "util.h"
14#include "verbs.h"
65eb4378 15
6a34639e
YW
16static const char *arg_hwdb_bin_dir = NULL;
17static const char *arg_root = NULL;
18static bool arg_strict = false;
65eb4378 19
6a34639e 20static int verb_query(int argc, char *argv[], void *userdata) {
d6609f82 21 return hwdb_query(argv[1]);
65eb4378
TG
22}
23
6a34639e
YW
24static int verb_update(int argc, char *argv[], void *userdata) {
25 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
65eb4378
TG
26}
27
37ec0fdd
LP
28static int help(void) {
29 _cleanup_free_ char *link = NULL;
30 int r;
31
32 r = terminal_urlify_man("systemd-hwdb", "8", &link);
33 if (r < 0)
34 return log_oom();
35
353b2baa
LP
36 printf("%s [OPTIONS...] COMMAND ...\n\n"
37 "%sUpdate or query the hardware database.%s\n"
38 "\nCommands:\n"
39 " update Update the hwdb database\n"
40 " query MODALIAS Query database and print result\n"
41 "\nOptions:\n"
7ccf7603
ZJS
42 " -h --help Show this help\n"
43 " --version Show package version\n"
44 " -s --strict When updating, return non-zero exit value on any parsing error\n"
45 " --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
46 " -r --root=PATH Alternative root path in the filesystem\n\n"
37ec0fdd
LP
47 "\nSee the %s for details.\n"
48 , program_invocation_short_name
353b2baa
LP
49 , ansi_highlight()
50 , ansi_normal()
37ec0fdd
LP
51 , link
52 );
53
54 return 0;
65eb4378
TG
55}
56
57static int parse_argv(int argc, char *argv[]) {
58 enum {
424d110a
LP
59 ARG_VERSION = 0x100,
60 ARG_USR,
65eb4378
TG
61 };
62
63 static const struct option options[] = {
424d110a
LP
64 { "help", no_argument, NULL, 'h' },
65 { "version", no_argument, NULL, ARG_VERSION },
66 { "usr", no_argument, NULL, ARG_USR },
aacbcab6 67 { "strict", no_argument, NULL, 's' },
424d110a 68 { "root", required_argument, NULL, 'r' },
65eb4378
TG
69 {}
70 };
71
72 int c;
73
74 assert(argc >= 0);
75 assert(argv);
76
6a34639e 77 while ((c = getopt_long(argc, argv, "ust:r:h", options, NULL)) >= 0)
65eb4378 78 switch(c) {
424d110a
LP
79
80 case 'h':
37ec0fdd 81 return help();
424d110a
LP
82
83 case ARG_VERSION:
3f6fd1ba 84 return version();
424d110a 85
65eb4378
TG
86 case ARG_USR:
87 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
88 break;
424d110a 89
aacbcab6
NB
90 case 's':
91 arg_strict = true;
92 break;
93
65eb4378
TG
94 case 'r':
95 arg_root = optarg;
96 break;
424d110a 97
65eb4378
TG
98 case '?':
99 return -EINVAL;
424d110a 100
65eb4378
TG
101 default:
102 assert_not_reached("Unknown option");
103 }
65eb4378
TG
104
105 return 1;
106}
107
108static int hwdb_main(int argc, char *argv[]) {
15c3626e 109 static const Verb verbs[] = {
6a34639e
YW
110 { "update", 1, 1, 0, verb_update },
111 { "query", 2, 2, 0, verb_query },
caa8dab2 112 {},
65eb4378
TG
113 };
114
caa8dab2 115 return dispatch_verb(argc, argv, verbs, NULL);
65eb4378
TG
116}
117
06213aae 118static int run(int argc, char *argv[]) {
65eb4378
TG
119 int r;
120
121 log_parse_environment();
122 log_open();
123
124 r = parse_argv(argc, argv);
125 if (r <= 0)
06213aae 126 return r;
65eb4378 127
a9ba0e32
CG
128 r = mac_selinux_init();
129 if (r < 0)
130 return r;
ea683512 131
06213aae 132 return hwdb_main(argc, argv);
65eb4378 133}
06213aae
ZJS
134
135DEFINE_MAIN_FUNCTION(run);