]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hwdb/hwdb.c
Move missing_xyz.h for glibc headers to src/basic/include/ (#37960)
[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 4
b5efdb8a 5#include "alloc-util.h"
d6b4d1c7 6#include "build.h"
65eb4378 7#include "hwdb-util.h"
8857aa74 8#include "label-util.h"
93a1f792 9#include "log.h"
5e332028 10#include "main-func.h"
294bf0c3 11#include "pretty-print.h"
3f6fd1ba 12#include "verbs.h"
65eb4378 13
6a34639e
YW
14static const char *arg_hwdb_bin_dir = NULL;
15static const char *arg_root = NULL;
16static bool arg_strict = false;
65eb4378 17
6a34639e 18static int verb_query(int argc, char *argv[], void *userdata) {
beff73f9 19 return hwdb_query(argv[1], arg_root);
65eb4378
TG
20}
21
6a34639e 22static int verb_update(int argc, char *argv[], void *userdata) {
3d11b46b
DDM
23 if (hwdb_bypass())
24 return 0;
25
6a34639e 26 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
65eb4378
TG
27}
28
37ec0fdd
LP
29static int help(void) {
30 _cleanup_free_ char *link = NULL;
31 int r;
32
33 r = terminal_urlify_man("systemd-hwdb", "8", &link);
34 if (r < 0)
35 return log_oom();
36
353b2baa
LP
37 printf("%s [OPTIONS...] COMMAND ...\n\n"
38 "%sUpdate or query the hardware database.%s\n"
39 "\nCommands:\n"
40 " update Update the hwdb database\n"
41 " query MODALIAS Query database and print result\n"
42 "\nOptions:\n"
7ccf7603
ZJS
43 " -h --help Show this help\n"
44 " --version Show package version\n"
45 " -s --strict When updating, return non-zero exit value on any parsing error\n"
46 " --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
aecc04f1 47 " -r --root=PATH Alternative root path in the filesystem\n"
bc556335
DDM
48 "\nSee the %s for details.\n",
49 program_invocation_short_name,
50 ansi_highlight(),
51 ansi_normal(),
52 link);
37ec0fdd
LP
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
5674b74c 77 while ((c = getopt_long(argc, argv, "sr:h", options, NULL)) >= 0)
79893116 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 101 default:
04499a70 102 assert_not_reached();
65eb4378 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
aa976d87 121 log_setup();
65eb4378
TG
122
123 r = parse_argv(argc, argv);
124 if (r <= 0)
06213aae 125 return r;
65eb4378 126
a452c807 127 r = mac_init();
a9ba0e32
CG
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);