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