2012-08-02 19:06:50 +00:00
|
|
|
/*
|
2014-03-07 13:38:51 +00:00
|
|
|
* lock_daemon_config.c: virtlockd config file handling
|
2012-08-02 19:06:50 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2012 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library 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; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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 GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "lock_daemon_config.h"
|
2012-12-12 16:35:35 +00:00
|
|
|
#include "virconf.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2015-03-16 14:02:41 +00:00
|
|
|
#include "rpc/virnetdaemon.h"
|
2012-08-02 19:06:50 +00:00
|
|
|
#include "configmake.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "virutil.h"
|
2012-08-02 19:06:50 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CONF
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("locking.lock_daemon_config");
|
|
|
|
|
2012-08-02 19:06:50 +00:00
|
|
|
|
|
|
|
/* A helper function used by each of the following macros. */
|
|
|
|
static int
|
|
|
|
checkType(virConfValuePtr p, const char *filename,
|
|
|
|
const char *key, virConfType required_type)
|
|
|
|
{
|
|
|
|
if (p->type != required_type) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("remoteReadConfigFile: %s: %s: invalid type:"
|
|
|
|
" got %s; expected %s"), filename, key,
|
2014-12-09 13:53:28 +00:00
|
|
|
virConfTypeToString(p->type),
|
|
|
|
virConfTypeToString(required_type));
|
2012-08-02 19:06:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there is no config data for the key, #var_name, then do nothing.
|
2013-05-03 12:43:12 +00:00
|
|
|
If there is valid data of type VIR_CONF_STRING, and VIR_STRDUP succeeds,
|
|
|
|
store the result in var_name. Otherwise, (i.e. invalid type, or VIR_STRDUP
|
2012-08-02 19:06:50 +00:00
|
|
|
failure), give a diagnostic and "goto" the cleanup-and-fail label. */
|
|
|
|
#define GET_CONF_STR(conf, filename, var_name) \
|
|
|
|
do { \
|
|
|
|
virConfValuePtr p = virConfGetValue(conf, #var_name); \
|
|
|
|
if (p) { \
|
|
|
|
if (checkType(p, filename, #var_name, VIR_CONF_STRING) < 0) \
|
|
|
|
goto error; \
|
|
|
|
VIR_FREE(data->var_name); \
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(data->var_name, p->str) < 0) \
|
2012-08-02 19:06:50 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2014-12-09 15:22:09 +00:00
|
|
|
/* Like GET_CONF_STR, but for signed integer values. */
|
2012-08-02 19:06:50 +00:00
|
|
|
#define GET_CONF_INT(conf, filename, var_name) \
|
|
|
|
do { \
|
|
|
|
virConfValuePtr p = virConfGetValue(conf, #var_name); \
|
|
|
|
if (p) { \
|
2014-12-09 15:22:09 +00:00
|
|
|
if (p->type != VIR_CONF_ULONG && \
|
|
|
|
checkType(p, filename, #var_name, VIR_CONF_LONG) < 0) \
|
|
|
|
goto error; \
|
|
|
|
data->var_name = p->l; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Like GET_CONF_STR, but for unsigned integer values. */
|
|
|
|
#define GET_CONF_UINT(conf, filename, var_name) \
|
|
|
|
do { \
|
|
|
|
virConfValuePtr p = virConfGetValue(conf, #var_name); \
|
|
|
|
if (p) { \
|
|
|
|
if (checkType(p, filename, #var_name, VIR_CONF_ULONG) < 0) \
|
2012-08-02 19:06:50 +00:00
|
|
|
goto error; \
|
|
|
|
data->var_name = p->l; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
int
|
|
|
|
virLockDaemonConfigFilePath(bool privileged, char **configfile)
|
|
|
|
{
|
|
|
|
if (privileged) {
|
2013-05-03 12:43:12 +00:00
|
|
|
if (VIR_STRDUP(*configfile, SYSCONFDIR "/libvirt/virtlockd.conf") < 0)
|
|
|
|
goto error;
|
2012-08-02 19:06:50 +00:00
|
|
|
} else {
|
|
|
|
char *configdir = NULL;
|
|
|
|
|
|
|
|
if (!(configdir = virGetUserConfigDirectory()))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virAsprintf(configfile, "%s/virtlockd.conf", configdir) < 0) {
|
|
|
|
VIR_FREE(configdir);
|
2013-07-04 10:11:02 +00:00
|
|
|
goto error;
|
2012-08-02 19:06:50 +00:00
|
|
|
}
|
|
|
|
VIR_FREE(configdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2012-08-02 19:06:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virLockDaemonConfigPtr
|
|
|
|
virLockDaemonConfigNew(bool privileged ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virLockDaemonConfigPtr data;
|
|
|
|
|
2013-07-04 10:11:02 +00:00
|
|
|
if (VIR_ALLOC(data) < 0)
|
2012-08-02 19:06:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-08-19 11:38:23 +00:00
|
|
|
data->max_clients = 1024;
|
2012-08-02 19:06:50 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
virLockDaemonConfigFree(virLockDaemonConfigPtr data)
|
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_FREE(data->log_filters);
|
|
|
|
VIR_FREE(data->log_outputs);
|
|
|
|
|
|
|
|
VIR_FREE(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLockDaemonConfigLoadOptions(virLockDaemonConfigPtr data,
|
|
|
|
const char *filename,
|
|
|
|
virConfPtr conf)
|
|
|
|
{
|
2014-12-09 15:22:09 +00:00
|
|
|
GET_CONF_UINT(conf, filename, log_level);
|
2012-08-02 19:06:50 +00:00
|
|
|
GET_CONF_STR(conf, filename, log_filters);
|
|
|
|
GET_CONF_STR(conf, filename, log_outputs);
|
2014-12-09 15:22:09 +00:00
|
|
|
GET_CONF_UINT(conf, filename, max_clients);
|
2012-08-02 19:06:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:51:47 +00:00
|
|
|
error:
|
2012-08-02 19:06:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read the config file if it exists.
|
|
|
|
* Only used in the remote case, hence the name.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virLockDaemonConfigLoadFile(virLockDaemonConfigPtr data,
|
|
|
|
const char *filename,
|
|
|
|
bool allow_missing)
|
|
|
|
{
|
|
|
|
virConfPtr conf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (allow_missing &&
|
|
|
|
access(filename, R_OK) == -1 &&
|
|
|
|
errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
conf = virConfReadFile(filename, 0);
|
|
|
|
if (!conf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = virLockDaemonConfigLoadOptions(data, filename, conf);
|
|
|
|
virConfFree(conf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virLockDaemonConfigLoadData(virLockDaemonConfigPtr data,
|
|
|
|
const char *filename,
|
|
|
|
const char *filedata)
|
|
|
|
{
|
|
|
|
virConfPtr conf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
conf = virConfReadMem(filedata, strlen(filedata), 0);
|
|
|
|
if (!conf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = virLockDaemonConfigLoadOptions(data, filename, conf);
|
|
|
|
virConfFree(conf);
|
|
|
|
return ret;
|
|
|
|
}
|