]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hwdb/hwdb.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[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 24static int verb_update(int argc, char *argv[], void *userdata) {
3d11b46b
DDM
25 if (hwdb_bypass())
26 return 0;
27
6a34639e 28 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
65eb4378
TG
29}
30
37ec0fdd
LP
31static int help(void) {
32 _cleanup_free_ char *link = NULL;
33 int r;
34
35 r = terminal_urlify_man("systemd-hwdb", "8", &link);
36 if (r < 0)
37 return log_oom();
38
353b2baa
LP
39 printf("%s [OPTIONS...] COMMAND ...\n\n"
40 "%sUpdate or query the hardware database.%s\n"
41 "\nCommands:\n"
42 " update Update the hwdb database\n"
43 " query MODALIAS Query database and print result\n"
44 "\nOptions:\n"
7ccf7603
ZJS
45 " -h --help Show this help\n"
46 " --version Show package version\n"
47 " -s --strict When updating, return non-zero exit value on any parsing error\n"
48 " --usr Generate in " UDEVLIBEXECDIR " instead of /etc/udev\n"
aecc04f1 49 " -r --root=PATH Alternative root path in the filesystem\n"
bc556335
DDM
50 "\nSee the %s for details.\n",
51 program_invocation_short_name,
52 ansi_highlight(),
53 ansi_normal(),
54 link);
37ec0fdd
LP
55
56 return 0;
65eb4378
TG
57}
58
59static int parse_argv(int argc, char *argv[]) {
60 enum {
424d110a
LP
61 ARG_VERSION = 0x100,
62 ARG_USR,
65eb4378
TG
63 };
64
65 static const struct option options[] = {
424d110a
LP
66 { "help", no_argument, NULL, 'h' },
67 { "version", no_argument, NULL, ARG_VERSION },
68 { "usr", no_argument, NULL, ARG_USR },
aacbcab6 69 { "strict", no_argument, NULL, 's' },
424d110a 70 { "root", required_argument, NULL, 'r' },
65eb4378
TG
71 {}
72 };
73
74 int c;
75
76 assert(argc >= 0);
77 assert(argv);
78
5674b74c 79 while ((c = getopt_long(argc, argv, "sr:h", options, NULL)) >= 0)
79893116 80 switch (c) {
424d110a
LP
81
82 case 'h':
37ec0fdd 83 return help();
424d110a
LP
84
85 case ARG_VERSION:
3f6fd1ba 86 return version();
424d110a 87
65eb4378
TG
88 case ARG_USR:
89 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
90 break;
424d110a 91
aacbcab6
NB
92 case 's':
93 arg_strict = true;
94 break;
95
65eb4378
TG
96 case 'r':
97 arg_root = optarg;
98 break;
424d110a 99
65eb4378
TG
100 case '?':
101 return -EINVAL;
424d110a 102
65eb4378 103 default:
04499a70 104 assert_not_reached();
65eb4378 105 }
65eb4378
TG
106
107 return 1;
108}
109
110static int hwdb_main(int argc, char *argv[]) {
15c3626e 111 static const Verb verbs[] = {
6a34639e
YW
112 { "update", 1, 1, 0, verb_update },
113 { "query", 2, 2, 0, verb_query },
caa8dab2 114 {},
65eb4378
TG
115 };
116
caa8dab2 117 return dispatch_verb(argc, argv, verbs, NULL);
65eb4378
TG
118}
119
06213aae 120static int run(int argc, char *argv[]) {
65eb4378
TG
121 int r;
122
123 log_parse_environment();
124 log_open();
125
126 r = parse_argv(argc, argv);
127 if (r <= 0)
06213aae 128 return r;
65eb4378 129
a452c807 130 r = mac_init();
a9ba0e32
CG
131 if (r < 0)
132 return r;
ea683512 133
06213aae 134 return hwdb_main(argc, argv);
65eb4378 135}
06213aae
ZJS
136
137DEFINE_MAIN_FUNCTION(run);