]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hwdb/hwdb.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / hwdb / hwdb.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "sd-hwdb.h"
6
7 #include "alloc-util.h"
8 #include "hwdb-util.h"
9 #include "main-func.h"
10 #include "pretty-print.h"
11 #include "selinux-util.h"
12 #include "terminal-util.h"
13 #include "util.h"
14 #include "verbs.h"
15
16 static const char *arg_hwdb_bin_dir = NULL;
17 static const char *arg_root = NULL;
18 static bool arg_strict = false;
19
20 static int verb_query(int argc, char *argv[], void *userdata) {
21 return hwdb_query(argv[1]);
22 }
23
24 static int verb_update(int argc, char *argv[], void *userdata) {
25 return hwdb_update(arg_root, arg_hwdb_bin_dir, arg_strict, false);
26 }
27
28 static 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
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"
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"
46 " -r --root=PATH Alternative root path in the filesystem\n\n"
47 "\nSee the %s for details.\n"
48 , program_invocation_short_name
49 , ansi_highlight()
50 , ansi_normal()
51 , link
52 );
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, "ust:r: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("Unknown option");
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_parse_environment();
122 log_open();
123
124 r = parse_argv(argc, argv);
125 if (r <= 0)
126 return r;
127
128 r = mac_selinux_init();
129 if (r < 0)
130 return r;
131
132 return hwdb_main(argc, argv);
133 }
134
135 DEFINE_MAIN_FUNCTION(run);