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