<commit_before>/*
 * lock_driver_sanlock.c: A lock driver for Sanlock
 *
 * Copyright (C) 2010-2014 Red Hat, Inc.
 *
 * 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/>.
 *
 */

#include <config.h>

#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sanlock.h>
#include <sanlock_resource.h>
#include <sanlock_admin.h>

#include "lock_driver.h"
#include "virlog.h"
#include "virerror.h"
#include "viralloc.h"
#include "vircrypto.h"
#include "virfile.h"
#include "virconf.h"
#include "virstring.h"
#include "virutil.h"

#include "configmake.h"

#define VIR_FROM_THIS VIR_FROM_LOCKING

VIR_LOG_INIT("locking.lock_driver_sanlock");

#define VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE "__LIBVIRT__DISKS__"
#define VIR_LOCK_MANAGER_SANLOCK_KILLPATH LIBEXECDIR "/libvirt_sanlock_helper"

/*
 * temporary fix for the case where the sanlock devel package is
 * too old to provide that define, and probably the functionality too
 */
#ifndef SANLK_RES_SHARED
# define SANLK_RES_SHARED    0x4
#endif

typedef struct _virLockManagerSanlockDriver virLockManagerSanlockDriver;
typedef virLockManagerSanlockDriver *virLockManagerSanlockDriverPtr;

typedef struct _virLockManagerSanlockPrivate virLockManagerSanlockPrivate;
typedef virLockManagerSanlockPrivate *virLockManagerSanlockPrivatePtr;

struct _virLockManagerSanlockDriver {
    bool requireLeaseForDisks;
    unsigned int hostID;
    bool autoDiskLease;
    char *autoDiskLeasePath;
    unsigned int io_timeout;

    /* under which permissions does sanlock run */
    uid_t user;
    gid_t group;
};

static virLockManagerSanlockDriverPtr sanlockDriver;

struct _virLockManagerSanlockPrivate {
    const char *vm_uri;
    char *vm_name;
    unsigned char vm_uuid[VIR_UUID_BUFLEN];
    unsigned int vm_id;
    int vm_pid;
    unsigned int flags;
    bool hasRWDisks;
    int res_count;
    struct sanlk_resource *res_args[SANLK_MAX_RESOURCES];

    /* whether the VM was registered or not */
    bool registered;
};


static bool
ATTRIBUTE_NONNULL(2)
virLockManagerSanlockError(int err,
                           char **message)
{
    if (err <= -200) {
#if HAVE_SANLOCK_STRERROR
        *message = g_strdup(sanlock_strerror(err));
#else
        *message = g_strdup_printf(_("sanlock error %d"), err);
#endif
        return true;
    } else {
        return false;
    }
}


/*
 * sanlock plugin for the libvirt virLockManager API
 */
static int
virLockManagerSanlockLoadConfig(virLockManagerSanlockDriverPtr driver,
                                const char *configFile)
{
    g_autoptr(virConf) conf = NULL;
    int ret = -1;
    char *user = NULL;
    char *group = NULL;

    if (access(configFile, R_OK) == -1) {
        if (errno != ENOENT) {
            virReportSystemError(errno,
                                 _("Unable to access config file %s"),
                                 configFile);
            return -1;
        }
        return 0;
    }

    if (!(conf = virConfReadFile(configFile, 0)))
        return -1;

    if (virConfGetValueBool(conf, "auto_disk_leases", &driver->autoDiskLease) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "disk_lease_dir", &driver->autoDiskLeasePath) < 0)
        goto cleanup;

    if (virConfGetValueUInt(conf, "host_id", &driver->hostID) < 0)
        goto cleanup;

    driver->requireLeaseForDisks = !driver->autoDiskLease;
    if (virConfGetValueBool(conf, "require_lease_for_disks", &driver->requireLeaseForDisks) < 0)
        goto cleanup;

    if (virConfGetValueUInt(conf, "io_timeout", &driver->io_timeout) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "user", &user) < 0)
        goto cleanup;
    if (user &&
        virGetUserID(user, &driver->user) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "group", &group) < 0)
        goto cleanup;
    if (group &&
        virGetGroupID(group, &driver->group) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(user);
    VIR_FREE(group);
    return ret;
}

static int
virLockManagerSanlockInitLockspace(virLockManagerSanlockDriverPtr driver,
                                   struct sanlk_lockspace *ls)
{
    int ret;

    const int max_hosts = 0; /* defaults used in sanlock_init() implementation */
    const unsigned int lockspaceFlags = 0;

    ret = sanlock_write_lockspace(ls, max_hosts, lockspaceFlags, driver->io_timeout);
    return ret;
}

/* How much ms sleep before retrying to add a lockspace? */
#define LOCKSPACE_SLEEP 100
/* How many times try adding a lockspace? */
#define LOCKSPACE_RETRIES 10

static int
virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
{
    int fd = -1;
    struct stat st;
    int rv;
    struct sanlk_lockspace ls;
    char *path = NULL;
    char *dir = NULL;
    int retries = LOCKSPACE_RETRIES;

    path = g_strdup_printf("%s/%s", driver->autoDiskLeasePath,
                           VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE);

    if (virStrcpyStatic(ls.name,
                        VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Lockspace path '%s' exceeded %d characters"),
                       VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
                       SANLK_PATH_LEN);
        goto error;
    }
    ls.host_id = 0; /* Doesn't matter for initialization */
    ls.flags = 0;
    if (virStrcpy(ls.host_id_disk.path, path, SANLK_PATH_LEN) < 0) {<commit_msg>locking: sanlock: drop unused LOCKSPACE_SLEEP<commit_after>/*
 * lock_driver_sanlock.c: A lock driver for Sanlock
 *
 * Copyright (C) 2010-2014 Red Hat, Inc.
 *
 * 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/>.
 *
 */

#include <config.h>

#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sanlock.h>
#include <sanlock_resource.h>
#include <sanlock_admin.h>

#include "lock_driver.h"
#include "virlog.h"
#include "virerror.h"
#include "viralloc.h"
#include "vircrypto.h"
#include "virfile.h"
#include "virconf.h"
#include "virstring.h"
#include "virutil.h"

#include "configmake.h"

#define VIR_FROM_THIS VIR_FROM_LOCKING

VIR_LOG_INIT("locking.lock_driver_sanlock");

#define VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE "__LIBVIRT__DISKS__"
#define VIR_LOCK_MANAGER_SANLOCK_KILLPATH LIBEXECDIR "/libvirt_sanlock_helper"

/*
 * temporary fix for the case where the sanlock devel package is
 * too old to provide that define, and probably the functionality too
 */
#ifndef SANLK_RES_SHARED
# define SANLK_RES_SHARED    0x4
#endif

typedef struct _virLockManagerSanlockDriver virLockManagerSanlockDriver;
typedef virLockManagerSanlockDriver *virLockManagerSanlockDriverPtr;

typedef struct _virLockManagerSanlockPrivate virLockManagerSanlockPrivate;
typedef virLockManagerSanlockPrivate *virLockManagerSanlockPrivatePtr;

struct _virLockManagerSanlockDriver {
    bool requireLeaseForDisks;
    unsigned int hostID;
    bool autoDiskLease;
    char *autoDiskLeasePath;
    unsigned int io_timeout;

    /* under which permissions does sanlock run */
    uid_t user;
    gid_t group;
};

static virLockManagerSanlockDriverPtr sanlockDriver;

struct _virLockManagerSanlockPrivate {
    const char *vm_uri;
    char *vm_name;
    unsigned char vm_uuid[VIR_UUID_BUFLEN];
    unsigned int vm_id;
    int vm_pid;
    unsigned int flags;
    bool hasRWDisks;
    int res_count;
    struct sanlk_resource *res_args[SANLK_MAX_RESOURCES];

    /* whether the VM was registered or not */
    bool registered;
};


static bool
ATTRIBUTE_NONNULL(2)
virLockManagerSanlockError(int err,
                           char **message)
{
    if (err <= -200) {
#if HAVE_SANLOCK_STRERROR
        *message = g_strdup(sanlock_strerror(err));
#else
        *message = g_strdup_printf(_("sanlock error %d"), err);
#endif
        return true;
    } else {
        return false;
    }
}


/*
 * sanlock plugin for the libvirt virLockManager API
 */
static int
virLockManagerSanlockLoadConfig(virLockManagerSanlockDriverPtr driver,
                                const char *configFile)
{
    g_autoptr(virConf) conf = NULL;
    int ret = -1;
    char *user = NULL;
    char *group = NULL;

    if (access(configFile, R_OK) == -1) {
        if (errno != ENOENT) {
            virReportSystemError(errno,
                                 _("Unable to access config file %s"),
                                 configFile);
            return -1;
        }
        return 0;
    }

    if (!(conf = virConfReadFile(configFile, 0)))
        return -1;

    if (virConfGetValueBool(conf, "auto_disk_leases", &driver->autoDiskLease) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "disk_lease_dir", &driver->autoDiskLeasePath) < 0)
        goto cleanup;

    if (virConfGetValueUInt(conf, "host_id", &driver->hostID) < 0)
        goto cleanup;

    driver->requireLeaseForDisks = !driver->autoDiskLease;
    if (virConfGetValueBool(conf, "require_lease_for_disks", &driver->requireLeaseForDisks) < 0)
        goto cleanup;

    if (virConfGetValueUInt(conf, "io_timeout", &driver->io_timeout) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "user", &user) < 0)
        goto cleanup;
    if (user &&
        virGetUserID(user, &driver->user) < 0)
        goto cleanup;

    if (virConfGetValueString(conf, "group", &group) < 0)
        goto cleanup;
    if (group &&
        virGetGroupID(group, &driver->group) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(user);
    VIR_FREE(group);
    return ret;
}

static int
virLockManagerSanlockInitLockspace(virLockManagerSanlockDriverPtr driver,
                                   struct sanlk_lockspace *ls)
{
    int ret;

    const int max_hosts = 0; /* defaults used in sanlock_init() implementation */
    const unsigned int lockspaceFlags = 0;

    ret = sanlock_write_lockspace(ls, max_hosts, lockspaceFlags, driver->io_timeout);
    return ret;
}

/* How many times try adding a lockspace? */
#define LOCKSPACE_RETRIES 10

static int
virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
{
    int fd = -1;
    struct stat st;
    int rv;
    struct sanlk_lockspace ls;
    char *path = NULL;
    char *dir = NULL;
    int retries = LOCKSPACE_RETRIES;

    path = g_strdup_printf("%s/%s", driver->autoDiskLeasePath,
                           VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE);

    if (virStrcpyStatic(ls.name,
                        VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Lockspace path '%s' exceeded %d characters"),
                       VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE,
                       SANLK_PATH_LEN);
        goto error;
    }
    ls.host_id = 0; /* Doesn't matter for initialization */
    ls.flags = 0;
    if (virStrcpy(ls.host_id_disk.path, path, SANLK_PATH_LEN) < 0) {
{'fraction_non_alphanumeric': 0.0899557594625594, 'fraction_numerical': 0.006963788300835654, 'mean_word_length': 3.1323628977657414, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4102911', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3914, 'n_words': 1041}