]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/detect-virt/detect-virt.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / detect-virt / detect-virt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25
26 #include "util.h"
27 #include "virt.h"
28
29 static bool arg_quiet = false;
30 static enum {
31 ANY_VIRTUALIZATION,
32 ONLY_VM,
33 ONLY_CONTAINER,
34 ONLY_CHROOT,
35 ONLY_PRIVATE_USERS,
36 } arg_mode = ANY_VIRTUALIZATION;
37
38 static void help(void) {
39 printf("%s [OPTIONS...]\n\n"
40 "Detect execution in a virtualized environment.\n\n"
41 " -h --help Show this help\n"
42 " --version Show package version\n"
43 " -c --container Only detect whether we are run in a container\n"
44 " -v --vm Only detect whether we are run in a VM\n"
45 " -r --chroot Detect whether we are run in a chroot() environment\n"
46 " --private-users Only detect whether we are running in a user namespace\n"
47 " -q --quiet Don't output anything, just set return value\n"
48 , program_invocation_short_name);
49 }
50
51 static int parse_argv(int argc, char *argv[]) {
52
53 enum {
54 ARG_VERSION = 0x100,
55 ARG_PRIVATE_USERS,
56 };
57
58 static const struct option options[] = {
59 { "help", no_argument, NULL, 'h' },
60 { "version", no_argument, NULL, ARG_VERSION },
61 { "container", no_argument, NULL, 'c' },
62 { "vm", no_argument, NULL, 'v' },
63 { "chroot", no_argument, NULL, 'r' },
64 { "private-users", no_argument, NULL, ARG_PRIVATE_USERS },
65 { "quiet", no_argument, NULL, 'q' },
66 {}
67 };
68
69 int c;
70
71 assert(argc >= 0);
72 assert(argv);
73
74 while ((c = getopt_long(argc, argv, "hqcvr", options, NULL)) >= 0)
75
76 switch (c) {
77
78 case 'h':
79 help();
80 return 0;
81
82 case ARG_VERSION:
83 return version();
84
85 case 'q':
86 arg_quiet = true;
87 break;
88
89 case 'c':
90 arg_mode = ONLY_CONTAINER;
91 break;
92
93 case ARG_PRIVATE_USERS:
94 arg_mode = ONLY_PRIVATE_USERS;
95 break;
96
97 case 'v':
98 arg_mode = ONLY_VM;
99 break;
100
101 case 'r':
102 arg_mode = ONLY_CHROOT;
103 break;
104
105 case '?':
106 return -EINVAL;
107
108 default:
109 assert_not_reached("Unhandled option");
110 }
111
112 if (optind < argc) {
113 log_error("%s takes no arguments.", program_invocation_short_name);
114 return -EINVAL;
115 }
116
117 return 1;
118 }
119
120 int main(int argc, char *argv[]) {
121 int r;
122
123 /* This is mostly intended to be used for scripts which want
124 * to detect whether we are being run in a virtualized
125 * environment or not */
126
127 log_parse_environment();
128 log_open();
129
130 r = parse_argv(argc, argv);
131 if (r <= 0)
132 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
133
134 switch (arg_mode) {
135
136 case ONLY_VM:
137 r = detect_vm();
138 if (r < 0) {
139 log_error_errno(r, "Failed to check for VM: %m");
140 return EXIT_FAILURE;
141 }
142
143 break;
144
145 case ONLY_CONTAINER:
146 r = detect_container();
147 if (r < 0) {
148 log_error_errno(r, "Failed to check for container: %m");
149 return EXIT_FAILURE;
150 }
151
152 break;
153
154 case ONLY_CHROOT:
155 r = running_in_chroot();
156 if (r < 0) {
157 log_error_errno(r, "Failed to check for chroot() environment: %m");
158 return EXIT_FAILURE;
159 }
160
161 return r ? EXIT_SUCCESS : EXIT_FAILURE;
162
163 case ONLY_PRIVATE_USERS:
164 r = running_in_userns();
165 if (r < 0) {
166 log_error_errno(r, "Failed to check for user namespace: %m");
167 return EXIT_FAILURE;
168 }
169
170 return r ? EXIT_SUCCESS : EXIT_FAILURE;
171
172 case ANY_VIRTUALIZATION:
173 default:
174 r = detect_virtualization();
175 if (r < 0) {
176 log_error_errno(r, "Failed to check for virtualization: %m");
177 return EXIT_FAILURE;
178 }
179
180 break;
181 }
182
183 if (!arg_quiet)
184 puts(virtualization_to_string(r));
185
186 return r != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
187 }