debuggers.hg

view tools/libxl/xl.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents d4a30bee5701
children
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
4 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 */
17 #include "libxl_osdeps.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <time.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <inttypes.h>
30 #include "libxl.h"
31 #include "libxl_utils.h"
32 #include "libxlutil.h"
33 #include "xl.h"
35 xentoollog_logger_stdiostream *logger;
36 int autoballoon = 1;
37 char *lockfile;
39 static xentoollog_level minmsglevel = XTL_PROGRESS;
41 static void parse_global_config(const char *configfile,
42 const char *configfile_data,
43 int configfile_len)
44 {
45 long l;
46 XLU_Config *config;
47 int e;
48 const char *buf;
50 config = xlu_cfg_init(stderr, configfile);
51 if (!config) {
52 fprintf(stderr, "Failed to allocate for configuration\n");
53 exit(1);
54 }
56 e = xlu_cfg_readdata(config, configfile_data, configfile_len);
57 if (e) {
58 fprintf(stderr, "Failed to parse config file: %s\n", strerror(e));
59 exit(1);
60 }
62 if (!xlu_cfg_get_long (config, "autoballoon", &l))
63 autoballoon = l;
65 if (!xlu_cfg_get_string (config, "lockfile", &buf))
66 lockfile = strdup(buf);
67 else {
68 e = asprintf(&lockfile, "%s/xl", (char *)libxl_lock_dir_path());
69 if (e < 0) {
70 fprintf(stderr, "asprintf memory allocation failed\n");
71 exit(1);
72 }
73 }
75 xlu_cfg_destroy(config);
76 }
78 int main(int argc, char **argv)
79 {
80 int opt = 0;
81 char *cmd = 0;
82 struct cmd_spec *cspec;
83 int ret;
84 char *config_file;
85 void *config_data = 0;
86 int config_len = 0;
88 while ((opt = getopt(argc, argv, "+v")) >= 0) {
89 switch (opt) {
90 case 'v':
91 if (minmsglevel > 0) minmsglevel--;
92 break;
93 default:
94 fprintf(stderr, "unknown global option\n");
95 exit(2);
96 }
97 }
99 cmd = argv[optind];
101 if (!cmd) {
102 help(NULL);
103 exit(1);
104 }
105 opterr = 0;
107 logger = xtl_createlogger_stdiostream(stderr, minmsglevel, 0);
108 if (!logger) exit(1);
110 if (libxl_ctx_init(&ctx, LIBXL_VERSION, (xentoollog_logger*)logger)) {
111 fprintf(stderr, "cannot init xl context\n");
112 exit(1);
113 }
115 /* Read global config file options */
116 ret = asprintf(&config_file, "%s/xl.conf", libxl_xen_config_dir_path());
117 if (ret < 0) {
118 fprintf(stderr, "memory allocation failed ret=%d, errno=%d\n", ret, errno);
119 exit(1);
120 }
122 ret = libxl_read_file_contents(&ctx, config_file,
123 &config_data, &config_len);
124 if (ret)
125 fprintf(stderr, "Failed to read config file: %s: %s\n",
126 config_file, strerror(errno));
127 parse_global_config(config_file, config_data, config_len);
128 free(config_file);
130 /* Reset options for per-command use of getopt. */
131 argv += optind;
132 argc -= optind;
133 optind = 1;
135 cspec = cmdtable_lookup(cmd);
136 if (cspec)
137 ret = cspec->cmd_impl(argc, argv);
138 else if (!strcmp(cmd, "help")) {
139 help(argv[1]);
140 ret = 0;
141 } else {
142 fprintf(stderr, "command not implemented\n");
143 ret = 1;
144 }
146 libxl_ctx_free(&ctx);
147 xtl_logger_destroy((xentoollog_logger*)logger);
149 return ret;
150 }