]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hwdb/hwdb.c
6391b04133c5edc5a88a3f0f50302d7499cd1eb4
[thirdparty/systemd.git] / src / hwdb / hwdb.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "hwdb-util.h"
8 #include "label-util.h"
9 #include "log.h"
10 #include "main-func.h"
11 #include "pretty-print.h"
12 #include "verbs.h"
13
14 static const char *arg_hwdb_bin_dir = NULL;
15 static const char *arg_root = NULL;
16 static bool arg_strict = false;
17
18 static int verb_query(int argc, char *argv[], void *userdata) {
19 return hwdb_query(argv[1], arg_root);
20 }
21
22 static int verb_update(int argc, char *argv[], void *userdata) {
23 if (hwdb_bypass())
24 return 0;
25
26 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
27 }
28
29 static 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
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"
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"
47 " -r --root=PATH Alternative root path in the filesystem\n"
48 "\nSee the %s for details.\n",
49 program_invocation_short_name,
50 ansi_highlight(),
51 ansi_normal(),
52 link);
53
54 return 0;
55 }
56
57 static int parse_argv(int argc, char *argv[]) {
58 enum {
59 ARG_VERSION = 0x100,
60 ARG_USR,
61 };
62
63 static const struct option options[] = {
64 { "help", no_argument, NULL, 'h' },
65 { "version", no_argument, NULL, ARG_VERSION },
66 { "usr", no_argument, NULL, ARG_USR },
67 { "strict", no_argument, NULL, 's' },
68 { "root", required_argument, NULL, 'r' },
69 {}
70 };
71
72 int c;
73
74 assert(argc >= 0);
75 assert(argv);
76
77 while ((c = getopt_long(argc, argv, "sr:h", options, NULL)) >= 0)
78 switch (c) {
79
80 case 'h':
81 return help();
82
83 case ARG_VERSION:
84 return version();
85
86 case ARG_USR:
87 arg_hwdb_bin_dir = UDEVLIBEXECDIR;
88 break;
89
90 case 's':
91 arg_strict = true;
92 break;
93
94 case 'r':
95 arg_root = optarg;
96 break;
97
98 case '?':
99 return -EINVAL;
100
101 default:
102 assert_not_reached();
103 }
104
105 return 1;
106 }
107
108 static int hwdb_main(int argc, char *argv[]) {
109 static const Verb verbs[] = {
110 { "update", 1, 1, 0, verb_update },
111 { "query", 2, 2, 0, verb_query },
112 {},
113 };
114
115 return dispatch_verb(argc, argv, verbs, NULL);
116 }
117
118 static int run(int argc, char *argv[]) {
119 int r;
120
121 log_setup();
122
123 r = parse_argv(argc, argv);
124 if (r <= 0)
125 return r;
126
127 r = mac_init();
128 if (r < 0)
129 return r;
130
131 return hwdb_main(argc, argv);
132 }
133
134 DEFINE_MAIN_FUNCTION(run);