chiark / gitweb /
importd: add new bus calls for importing local tar and raw images
[elogind.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 <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25 #include <getopt.h>
26
27 #include "util.h"
28 #include "virt.h"
29 #include "build.h"
30
31 static bool arg_quiet = false;
32 static enum {
33         ANY_VIRTUALIZATION,
34         ONLY_VM,
35         ONLY_CONTAINER
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                "  -q --quiet            Don't output anything, just set return value\n"
46                , program_invocation_short_name);
47 }
48
49 static int parse_argv(int argc, char *argv[]) {
50
51         enum {
52                 ARG_VERSION = 0x100
53         };
54
55         static const struct option options[] = {
56                 { "help",      no_argument,       NULL, 'h'           },
57                 { "version",   no_argument,       NULL, ARG_VERSION   },
58                 { "container", no_argument,       NULL, 'c'           },
59                 { "vm",        optional_argument, NULL, 'v'           },
60                 { "quiet",     no_argument,       NULL, 'q'           },
61                 {}
62         };
63
64         int c;
65
66         assert(argc >= 0);
67         assert(argv);
68
69         while ((c = getopt_long(argc, argv, "hqcv", options, NULL)) >= 0)
70
71                 switch (c) {
72
73                 case 'h':
74                         help();
75                         return 0;
76
77                 case ARG_VERSION:
78                         puts(PACKAGE_STRING);
79                         puts(SYSTEMD_FEATURES);
80                         return 0;
81
82                 case 'q':
83                         arg_quiet = true;
84                         break;
85
86                 case 'c':
87                         arg_mode = ONLY_CONTAINER;
88                         break;
89
90                 case 'v':
91                         arg_mode = ONLY_VM;
92                         break;
93
94                 case '?':
95                         return -EINVAL;
96
97                 default:
98                         assert_not_reached("Unhandled option");
99                 }
100
101         if (optind < argc) {
102                 log_error("%s takes no arguments.",
103                           program_invocation_short_name);
104                 return -EINVAL;
105         }
106
107         return 1;
108 }
109
110 int main(int argc, char *argv[]) {
111         const char *id = NULL;
112         int retval = EXIT_SUCCESS;
113         int r;
114
115         /* This is mostly intended to be used for scripts which want
116          * to detect whether we are being run in a virtualized
117          * environment or not */
118
119         log_parse_environment();
120         log_open();
121
122         r = parse_argv(argc, argv);
123         if (r <= 0)
124                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
125
126         switch (arg_mode) {
127
128         case ANY_VIRTUALIZATION: {
129                 int v;
130
131                 v = detect_virtualization(&id);
132                 if (v < 0) {
133                         log_error_errno(v, "Failed to check for virtualization: %m");
134                         return EXIT_FAILURE;
135                 }
136
137                 retval = v != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
138                 break;
139         }
140
141         case ONLY_CONTAINER:
142                 r = detect_container(&id);
143                 if (r < 0) {
144                         log_error_errno(r, "Failed to check for container: %m");
145                         return EXIT_FAILURE;
146                 }
147
148                 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
149                 break;
150
151         case ONLY_VM:
152                 r = detect_vm(&id);
153                 if (r < 0) {
154                         log_error_errno(r, "Failed to check for vm: %m");
155                         return EXIT_FAILURE;
156                 }
157
158                 retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
159                 break;
160         }
161
162         if (!arg_quiet)
163                 puts(id ? id : "none");
164
165         return retval;
166 }