1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/*********************************************************
* Copyright (C) 2008 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/**
* @file vmtoolsConfig.c
*
* Convenience functions for loading tools configuration files, and
* automatically migrating from old-style tools configuration files.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include "vmtools.h"
#include "conf.h"
#include "file.h"
#include "guestApp.h"
#include "str.h"
#include "util.h"
/** Data types supported for translation. */
typedef enum {
CFG_BOOLEAN,
CFG_INTEGER,
CFG_STRING,
CFG_CALLBACK
} ConfigType;
/** Holds information about how to upgrade an old config entry. */
typedef struct ConfigEntry {
const gchar *key;
const gchar *destGroup;
const gchar *destKey;
const ConfigType type;
const gpointer data;
} ConfigEntry;
typedef void (*CfgCallback)(GKeyFile *cfg, const ConfigEntry *, const char *);
/**
* Upgrade the logging configuration.
*
* @param[in] cfg Config dictionary.
* @param[in] entry The ConfigEntry instance.
* @param[in] value Config value.
*/
static void
VMToolsConfigUpgradeLog(GKeyFile *cfg,
const ConfigEntry *entry,
const char *value)
{
/*
* The old configuration used the same file path for both guestd
* and vmware-user. Instead of baking in the library whether we're
* running one or the other, separate the two configurations, so that
* the user can choose a different location for one or another.
*
* Also append the PID to the vmusr log path, since the old code did
* that automatically.
*/
gchar *userlog;
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_GUEST_SERVICE ".handler", "file");
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_GUEST_SERVICE ".level", "debug");
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_GUEST_SERVICE ".data", value);
userlog = g_strdup_printf("%s.user.${PID}", value);
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_USER_SERVICE ".handler", "file");
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_USER_SERVICE ".level", "debug");
g_key_file_set_string(cfg, entry->destGroup,
VMTOOLS_USER_SERVICE ".data", userlog);
/*
* Keep the log.file entry since vmware-user is still using the old-style
* config. This can go away once it's ported over.
*/
g_key_file_set_string(cfg, entry->destGroup, CONFNAME_LOGFILE, value);
g_free(userlog);
}
/**
* Translates a VMware dictionary containing tools configuration data
* to a GKeyFile dictionary. Only known tools options are translated.
*
* @param[in] old The old configuration data.
* @param[in] dst The destination.
*/
static void
VMToolsConfigUpgrade(GuestApp_Dict *old,
GKeyFile *dst)
{
const ConfigEntry entries[] = {
/* Logging options. */
{ CONFNAME_LOGFILE, "logging", NULL, CFG_CALLBACK, VMToolsConfigUpgradeLog },
{ CONFNAME_LOG, "logging", "log", CFG_BOOLEAN, NULL },
/* Power op scripts. */
{ CONFNAME_POWEROFFSCRIPT, "powerops", CONFNAME_POWEROFFSCRIPT, CFG_STRING, NULL },
{ CONFNAME_POWERONSCRIPT, "powerops", CONFNAME_POWERONSCRIPT, CFG_STRING, NULL },
{ CONFNAME_RESUMESCRIPT, "powerops", CONFNAME_RESUMESCRIPT, CFG_STRING, NULL },
{ CONFNAME_SUSPENDSCRIPT, "powerops", CONFNAME_SUSPENDSCRIPT, CFG_STRING, NULL },
/* guestd options. */
{ CONFNAME_MAX_WIPERSIZE, "vmsvc", CONFNAME_MAX_WIPERSIZE, CFG_INTEGER, NULL },
{ CONFNAME_DISABLEQUERYDISKINFO, "guestinfo", CONFNAME_DISABLEQUERYDISKINFO, CFG_BOOLEAN, NULL },
{ CONFNAME_DISABLETOOLSVERSION, "vmsvc", CONFNAME_DISABLETOOLSVERSION, CFG_BOOLEAN, NULL },
#if defined(_WIN32)
/* Tray options. */
{ CONFNAME_SHOW_WIRELESS_ICON, "vmtray", CONFNAME_SHOW_WIRELESS_ICON, CFG_BOOLEAN, NULL },
#endif
/* Null terminator. */
{ NULL, }
};
const ConfigEntry *entry;
for (entry = entries; entry->key != NULL; entry++) {
const char *value = GuestApp_GetDictEntry(old, entry->key);
const char *dfltValue = GuestApp_GetDictEntryDefault(old, entry->key);
if (value == NULL || (dfltValue != NULL && strcmp(value, dfltValue) == 0)) {
continue;
}
switch (entry->type) {
case CFG_BOOLEAN:
{
gboolean val = GuestApp_GetDictEntryBool(old, entry->key);
g_key_file_set_boolean(dst, entry->destGroup, entry->destKey, val);
break;
}
case CFG_INTEGER:
{
gint val;
if (GuestApp_GetDictEntryInt(old, entry->key, &val)) {
g_key_file_set_integer(dst, entry->destGroup, entry->destKey, val);
}
break;
}
case CFG_STRING:
g_key_file_set_string(dst, entry->destGroup, entry->destKey, value);
break;
case CFG_CALLBACK:
ASSERT(entry->data);
((CfgCallback)entry->data)(dst, entry, value);
break;
default:
g_assert_not_reached();
}
}
}
/**
* Returns the path to the default tools config file.
*
* @return String with the default config path (should be freed by caller).
*/
gchar *
VMTools_GetToolsConfFile(void)
{
char *confPath = GuestApp_GetConfPath();
gchar *confFilePath;
/*
* XXX: GuestApp_GetConfPath() is racy. If two different people are calling
* the function and the conf directory doesn't exist, there's the risk that
* one of them will fail to create the directory and return NULL. So if we
* get NULL back, retry the call (at which point the directory will
* hopefully exist), and assert if it's NULL (which now should only happen
* if we fail to allocate memory).
*/
if (confPath == NULL) {
confPath = GuestApp_GetConfPath();
g_assert(confPath != NULL);
}
confFilePath = g_strdup_printf("%s%c%s", confPath, DIRSEPC, CONF_FILE);
free(confPath);
return confFilePath;
}
/**
* Loads the configuration file at the given path. If an old configuration
* file is detected, the caller can request for it to be automatically upgraded
* to the new configuration format (the old configuration file is saved with a
* ".old" extension).
*
* @param[in] path Path to the configuration file.
* @param[in] flags Flags for opening the file.
* @param[in] autoUpgrade Whether to try to upgrade old tools configuration.
*
* @return A configuration dictionary, or NULL on error.
*/
GKeyFile *
VMTools_LoadConfig(const gchar *path,
GKeyFileFlags flags,
gboolean autoUpgrade)
{
gchar *backup = NULL;
gchar *localPath;
GuestApp_Dict *old = NULL;
GError *err = NULL;
GKeyFile *cfg;
cfg = g_key_file_new();
localPath = VMTOOLS_GET_FILENAME_LOCAL(path, &err);
if (err != NULL) {
g_warning("Error converting to local encoding: %s\n", err->message);
goto exit;
}
if (!File_IsFile(path) || File_GetSizeByPath(path) == 0) {
goto exit;
}
g_key_file_load_from_file(cfg, localPath, flags, &err);
if (err == NULL) {
goto exit;
}
if (err->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) {
g_warning("Cannot load config file: %s", err->message);
goto error;
}
/* Failed to load the config file; try to upgrade if requested. */
if (!autoUpgrade) {
goto error;
}
old = Conf_Load();
if (old == NULL) {
g_warning("Error loading old tools config data, bailing out.\n");
goto error;
}
VMToolsConfigUpgrade(old, cfg);
backup = g_strdup_printf("%s.old", path);
if (!File_IsFile(backup)) {
if (!File_Rename(path, backup)) {
g_warning("Error creating backup of old config file.\n");
goto error;
}
} else {
g_warning("Backup config exists, skipping backup.\n");
}
g_clear_error(&err);
if (!VMTools_WriteConfig(path, cfg, NULL)) {
goto error;
}
goto exit;
error:
g_key_file_free(cfg);
cfg = NULL;
exit:
g_clear_error(&err);
if (old != NULL) {
GuestApp_FreeDict(old);
}
g_free(backup);
VMTOOLS_RELEASE_FILENAME_LOCAL(localPath);
return cfg;
}
/**
* Reloads the configuration file at the given path if it has changed since the
* given timestamp. No translation (such as in VMTools_LoadConfig()) will be
* performed.
*
* @param[in] path Path to the config file.
* @param[in] flags Flags to use when opening the file.
* @param[in,out] config GKeyFile object; when reloading the file, the old
* config object will be destroyed.
* @param[in,out] mtime Last known modification time of the config file.
* When the function succeeds, will contain the new
* modification time read from the file.
*
* @return Whether the file was reloaded.
*/
gboolean
VMTools_ReloadConfig(const gchar *path,
GKeyFileFlags flags,
GKeyFile **config,
time_t *mtime)
{
struct stat confStat;
gboolean ret = FALSE;
GKeyFile *newConfig = NULL;
g_assert(config != NULL);
g_assert(mtime != NULL);
if (g_stat(path, &confStat) == -1) {
g_warning("Failed to stat conf file: %s\n", strerror(errno));
goto exit;
}
if (*mtime == 0 || confStat.st_mtime > *mtime) {
GError *err = NULL;
gchar *localPath;
localPath = VMTOOLS_GET_FILENAME_LOCAL(path, &err);
if (err != NULL) {
g_warning("Error converting to local encoding: %s\n", err->message);
goto exit;
}
newConfig = g_key_file_new();
g_key_file_load_from_file(newConfig, localPath, flags, &err);
if (err != NULL) {
g_warning("Error loading conf file: %s\n", err->message);
g_clear_error(&err);
g_key_file_free(newConfig);
newConfig = NULL;
} else {
ret = TRUE;
}
VMTOOLS_RELEASE_FILENAME_LOCAL(localPath);
}
if (newConfig != NULL) {
if (*config != NULL) {
g_key_file_free(*config);
}
*config = newConfig;
*mtime = confStat.st_mtime;
}
exit:
return ret;
}
/**
* Saves the given config data to the given path.
*
* @param[in] path Where to save the data.
* @param[in] config Config data.
* @param[out] err Where to store error information (may be NULL).
*
* @return Whether saving was successful.
*/
gboolean
VMTools_WriteConfig(const gchar *path,
GKeyFile *config,
GError **err)
{
gboolean ret = FALSE;
gchar *data = NULL;
gchar *localPath = NULL;
FILE *out = NULL;
GError *lerr = NULL;
g_assert(path != NULL);
g_assert(config != NULL);
localPath = VMTOOLS_GET_FILENAME_LOCAL(path, &lerr);
if (lerr != NULL) {
g_warning("Error converting to local encoding: %s\n", lerr->message);
goto exit;
}
data = g_key_file_to_data(config, NULL, &lerr);
if (lerr != NULL) {
g_warning("Error serializing conf data: %s\n", lerr->message);
goto exit;
}
out = g_fopen(localPath, "w");
if (out == NULL) {
const char *errstr = strerror(errno);
g_warning("Error opening conf file for writing: %s\n", errstr);
g_set_error(&lerr, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", errstr);
goto exit;
}
if (g_fprintf(out, "%s", data) < 0) {
const char *errstr = strerror(errno);
g_warning("Error writing conf file: %s\n", errstr);
g_set_error(&lerr, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s", errstr);
goto exit;
}
ret = TRUE;
exit:
if (out != NULL) {
fclose(out);
}
if (err != NULL && lerr != NULL) {
*err = lerr;
} else {
g_clear_error(&lerr);
}
g_free(data);
VMTOOLS_RELEASE_FILENAME_LOCAL(localPath);
return ret;
}
|