libvirt/src/locking/sanlock_helper.c
Cole Robinson e7db227810 util: Add virGettextInitialize, convert the code
Take setlocale/gettext error handling pattern from tools/virsh-*
and use it for all standalone binaries via a new shared
virGettextInitialize routine. The virsh* pattern differed slightly
from other callers. All users now consistently:

* Ignore setlocale errors. virsh has done this forever, presumably for
  good reason. This has been partially responsible for some bug reports:

  https://bugzilla.redhat.com/show_bug.cgi?id=1312688
  https://bugzilla.redhat.com/show_bug.cgi?id=1026514
  https://bugzilla.redhat.com/show_bug.cgi?id=1016158

* Report the failed function name
* Report strerror
2016-04-14 13:22:40 -04:00

111 lines
2.4 KiB
C

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "internal.h"
#include "virconf.h"
#include "viralloc.h"
#include "domain_conf.h"
#include "virgettext.h"
static int
getArgs(int argc,
char **argv,
const char **uri,
const char **uuid,
virDomainLockFailureAction *action)
{
int act;
if (argc != 4) {
fprintf(stderr, _("%s uri uuid action\n"), argv[0]);
return -1;
}
*uri = argv[1];
*uuid = argv[2];
act = virDomainLockFailureTypeFromString(argv[3]);
if (act < 0) {
fprintf(stderr, _("invalid failure action: '%s'\n"), argv[3]);
return -1;
}
*action = act;
return 0;
}
static int
authCallback(virConnectCredentialPtr cred ATTRIBUTE_UNUSED,
unsigned int ncred ATTRIBUTE_UNUSED,
void *cbdata ATTRIBUTE_UNUSED)
{
return -1;
}
int
main(int argc, char **argv)
{
const char *uri;
const char *uuid;
virDomainLockFailureAction action;
char *xml = NULL;
virConnectPtr conn = NULL;
virDomainPtr dom = NULL;
int ret = EXIT_FAILURE;
int authTypes[] = {
VIR_CRED_AUTHNAME,
VIR_CRED_ECHOPROMPT,
VIR_CRED_PASSPHRASE,
VIR_CRED_NOECHOPROMPT,
};
virConnectAuth auth = {
.credtype = authTypes,
.ncredtype = ARRAY_CARDINALITY(authTypes),
.cb = authCallback,
};
if (virGettextInitialize() < 0)
exit(EXIT_FAILURE);
if (getArgs(argc, argv, &uri, &uuid, &action) < 0)
goto cleanup;
if (!(conn = virConnectOpenAuth(uri, &auth, 0)) ||
!(dom = virDomainLookupByUUIDString(conn, uuid)))
goto cleanup;
switch (action) {
case VIR_DOMAIN_LOCK_FAILURE_POWEROFF:
if (virDomainDestroy(dom) == 0 ||
virDomainIsActive(dom) == 0)
ret = EXIT_SUCCESS;
break;
case VIR_DOMAIN_LOCK_FAILURE_PAUSE:
if (virDomainSuspend(dom) == 0)
ret = EXIT_SUCCESS;
break;
case VIR_DOMAIN_LOCK_FAILURE_DEFAULT:
case VIR_DOMAIN_LOCK_FAILURE_RESTART:
case VIR_DOMAIN_LOCK_FAILURE_IGNORE:
case VIR_DOMAIN_LOCK_FAILURE_LAST:
fprintf(stderr, _("unsupported failure action: '%s'\n"),
virDomainLockFailureTypeToString(action));
break;
}
cleanup:
virObjectUnref(dom);
if (conn)
virConnectClose(conn);
VIR_FREE(xml);
return ret;
}