Files
libvirt/tests/virscsitest.c
T
Laine Stump f83c0a5e6e consistently use glib g_getenv() instead of libc getenv()
We've been using glib g_setenv() since commit
2c33532423 in December 2019 (switching
away from the gnulib version of setenv()). Most (but not all) of the
calls to get environment variables have remained using libc's getenv()
though, even though there is a g_getenv() wrapper in glib to match the
g_setenv() wrapper.

While getenv() doesn't have the thread safety problems of setenv(),
it's still recommended that users of g_setenv() also use g_getenv()
(for consistency, and because the glib functions handle UTF-8 properly
while libc getenv() may or may not depending on the setting of LANG in
the environment).

This patch changes all calls to getenv() to use g_getenv() instead,
with the exceptions of:

1) the call to getenv() in virt-login-shell.c (because
virt-login-shell runs setuid root, and we don't want glib or any other
gigantic library anywhere near a setuid program). In a few cases a
char * needs to be made const, and the return from getenv() needs to
be g_strdup()ed if it must stick around for any amount of time (since
the buffer returned from g_getenv() might be recycled/re-used if there
is another call to g_getenv()/g_setenv()).

2) the call to getenv() in libvirt_nss_log.c because it is compiled
into a loadable module that will be loaded into a process after the
process's normal startup, and so any initialization that might be
required for a glib function to operate properly may not be called.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2026-03-09 01:58:08 -04:00

221 lines
5.3 KiB
C

/*
* Copyright (C) 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 <unistd.h>
#include "virscsi.h"
#include "testutils.h"
#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#define VIR_SCSI_DATA "/virscsidata"
VIR_LOG_INIT("tests.scsitest");
static char *virscsi_prefix;
static int
test1(const void *data G_GNUC_UNUSED)
{
g_autofree char *name = NULL;
if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
"scsi_host1", 0, 0, 0)))
return -1;
if (STRNEQ(name, "sdh"))
return -1;
return 0;
}
/*
* Two test devices are used, one has address "0:0:0:0", the
* other has address "1:0:0:0", see "virscsidata/" for more
* details.
*/
static int
test2(const void *data G_GNUC_UNUSED)
{
g_autoptr(virSCSIDeviceList) list = NULL;
virSCSIDevice *dev = NULL;
virSCSIDevice *dev1 = NULL;
bool free_dev = true;
bool free_dev1 = true;
virSCSIDevice *tmp = NULL;
g_autofree char *sgname = NULL;
int ret = -1;
sgname = virSCSIDeviceGetSgName(virscsi_prefix,
"scsi_host1", 0, 0, 0);
if (!sgname || STRNEQ(sgname, "sg8"))
goto cleanup;
if (!(dev = virSCSIDeviceNew(virscsi_prefix, "scsi_host1",
0, 0, 0, false, true)))
goto cleanup;
if (STRNEQ_NULLABLE(virSCSIDeviceGetName(dev), "1:0:0:0") ||
virSCSIDeviceGetAdapter(dev) != 1 ||
virSCSIDeviceGetBus(dev) != 0 ||
virSCSIDeviceGetTarget(dev) != 0 ||
virSCSIDeviceGetUnit(dev) != 0 ||
virSCSIDeviceGetReadonly(dev) ||
!virSCSIDeviceGetShareable(dev))
goto cleanup;
if (!virSCSIDeviceIsAvailable(dev))
goto cleanup;
virSCSIDeviceSetUsedBy(dev, "QEMU", "fc18");
if (virSCSIDeviceIsAvailable(dev))
goto cleanup;
virSCSIDeviceSetUsedBy(dev, "QEMU", "fc20");
if (virSCSIDeviceIsAvailable(dev))
goto cleanup;
if (!(list = virSCSIDeviceListNew()))
goto cleanup;
if (virSCSIDeviceListAdd(list, dev) < 0)
goto cleanup;
/* virSCSIDeviceListDispose will take care of freeing
* the device.
*/
free_dev = false;
if (!virSCSIDeviceListFind(list, dev))
goto cleanup;
virSCSIDeviceListDel(list, dev, "QEMU", "fc20");
if (!virSCSIDeviceListFind(list, dev))
goto cleanup;
if (virSCSIDeviceIsAvailable(dev))
goto cleanup;
if (virSCSIDeviceListCount(list) != 1)
goto cleanup;
if (!(dev1 = virSCSIDeviceNew(virscsi_prefix, "scsi_host0",
0, 0, 0, true, false)))
goto cleanup;
if (virSCSIDeviceListAdd(list, dev1) < 0)
goto cleanup;
/* virSCSIDeviceListDispose will take care of freeing
* the device.
*/
free_dev1 = false;
if (virSCSIDeviceListCount(list) != 2)
goto cleanup;
if (!(tmp = virSCSIDeviceListSteal(list, dev1)))
goto cleanup;
virSCSIDeviceFree(tmp);
if (virSCSIDeviceListCount(list) != 1)
goto cleanup;
ret = 0;
cleanup:
if (free_dev)
virSCSIDeviceFree(dev);
if (free_dev1)
virSCSIDeviceFree(dev1);
return ret;
}
static int
create_symlink(const char *tmpdir, const char *src_name, const char *dst_name)
{
g_autofree char *src_path = NULL;
g_autofree char *dst_path = NULL;
src_path = g_strdup_printf("%s/%s", virscsi_prefix, src_name);
dst_path = g_strdup_printf("%s/%s", tmpdir, dst_name);
if (symlink(src_path, dst_path) < 0) {
VIR_WARN("Failed to create symlink '%s' to '%s'", src_path, dst_path);
return -1;
}
return 0;
}
static int
mymain(void)
{
int ret = 0;
char *tmpdir = NULL;
char template[] = "/tmp/libvirt_XXXXXX";
virscsi_prefix = g_strdup_printf("%s" VIR_SCSI_DATA, abs_srcdir);
tmpdir = g_mkdtemp(template);
if (tmpdir == NULL) {
VIR_WARN("Failed to create temporary directory");
ret = -1;
goto cleanup;
}
#define CREATE_SYMLINK(src_name, dst_name) \
do { \
if (create_symlink(tmpdir, src_name, dst_name) < 0) { \
ret = -1; \
goto cleanup; \
} \
} while (0)
CREATE_SYMLINK("0-0-0-0", "0:0:0:0");
CREATE_SYMLINK("1-0-0-0", "1:0:0:0");
CREATE_SYMLINK("sg0", "sg0");
CREATE_SYMLINK("sg8", "sg8");
VIR_FREE(virscsi_prefix);
virscsi_prefix = g_strdup(tmpdir);
if (virTestRun("test1", test1, NULL) < 0)
ret = -1;
if (virTestRun("test2", test2, NULL) < 0)
ret = -1;
cleanup:
if (g_getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
virFileDeleteTree(tmpdir);
VIR_FREE(virscsi_prefix);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)