]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hwdb/hwdb.c
label: Rename to label-util.h
[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"
d6b4d1c7 8#include "build.h"
65eb4378 9#include "hwdb-util.h"
5e332028 10#include "main-func.h"
294bf0c3 11#include "pretty-print.h"
ea683512 12#include "selinux-util.h"
353b2baa 13#include "terminal-util.h"
3f6fd1ba 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) {
beff73f9 21 return hwdb_query(argv[1], arg_root);
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"
aecc04f1 46 " -r --root=PATH Alternative root path in the filesystem\n"
bc556335
DDM
47 "\nSee the %s for details.\n",
48 program_invocation_short_name,
49 ansi_highlight(),
50 ansi_normal(),
51 link);
37ec0fdd
LP
52
53 return 0;
65eb4378
TG
54}
55
56static int parse_argv(int argc, char *argv[]) {
57 enum {
424d110a
LP
58 ARG_VERSION = 0x100,
59 ARG_USR,
65eb4378
TG
60 };
61
62 static const struct option options[] = {
424d110a
LP
63 { "help", no_argument, NULL, 'h' },
64 { "version", no_argument, NULL, ARG_VERSION },
65 { "usr", no_argument, NULL, ARG_USR },
aacbcab6 66 { "strict", no_argument, NULL, 's' },
424d110a 67 { "root", required_argument, NULL, 'r' },
65eb4378
TG
68 {}
69 };
70
71 int c;
72
73 assert(argc >= 0);
74 assert(argv);
75
5674b74c 76 while ((c = getopt_long(argc, argv, "sr:h", options, NULL)) >= 0)
79893116 77 switch (c) {
424d110a
LP
78
79 case 'h':
37ec0fdd 80 return help();
424d110a
LP
81
82 case ARG_VERSION:
3f6fd1ba 83 return version();
424d110a 84
65eb4378
TG
85 case ARG_USR:
86 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
87 break;
424d110a 88
aacbcab6
NB
89 case 's':
90 arg_strict = true;
91 break;
92
65eb4378
TG
93 case 'r':
94 arg_root = optarg;
95 break;
424d110a 96
65eb4378
TG
97 case '?':
98 return -EINVAL;
424d110a 99
65eb4378 100 default:
04499a70 101 assert_not_reached();
65eb4378 102 }
65eb4378
TG
103
104 return 1;
105}
106
107static int hwdb_main(int argc, char *argv[]) {
15c3626e 108 static const Verb verbs[] = {
6a34639e
YW
109 { "update", 1, 1, 0, verb_update },
110 { "query", 2, 2, 0, verb_query },
caa8dab2 111 {},
65eb4378
TG
112 };
113
caa8dab2 114 return dispatch_verb(argc, argv, verbs, NULL);
65eb4378
TG
115}
116
06213aae 117static int run(int argc, char *argv[]) {
65eb4378
TG
118 int r;
119
120 log_parse_environment();
121 log_open();
122
123 r = parse_argv(argc, argv);
124 if (r <= 0)
06213aae 125 return r;
65eb4378 126
a9ba0e32
CG
127 r = mac_selinux_init();
128 if (r < 0)
129 return r;
ea683512 130
06213aae 131 return hwdb_main(argc, argv);
65eb4378 132}
06213aae
ZJS
133
134DEFINE_MAIN_FUNCTION(run);