virtestmock.c 7.6 KB
Newer Older
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
/*
 * Copyright (C) 2016 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/>.
 *
 * Author: Michal Privoznik <mprivozn@redhat.com>
 */

#include <config.h>

#include "virmock.h"
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
27 28
#include <execinfo.h>
#include <sys/file.h>
29
#include <sys/stat.h>
30 31 32

#include "internal.h"
#include "configmake.h"
33 34 35
#include "virstring.h"
#include "viralloc.h"
#include "virfile.h"
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* stat can be a macro as follows:
 *
 *   #define stat stat64
 *
 * This wouldn't fly with our mock. Make sure that the macro (and
 * all its friends) are undefined. We don't want anybody mangling
 * our code. */
#undef stat
#undef stat64
#undef __xstat
#undef __xstat64
#undef lstat
#undef lstat64
#undef __lxstat
#undef __lxstat64

53 54 55 56
static int (*real_open)(const char *path, int flags, ...);
static FILE *(*real_fopen)(const char *path, const char *mode);
static int (*real_access)(const char *path, int mode);
static int (*real_stat)(const char *path, struct stat *sb);
57
static int (*real_stat64)(const char *path, void *sb);
58
static int (*real___xstat)(int ver, const char *path, struct stat *sb);
59
static int (*real___xstat64)(int ver, const char *path, void *sb);
60
static int (*real_lstat)(const char *path, struct stat *sb);
61
static int (*real_lstat64)(const char *path, void *sb);
62
static int (*real___lxstat)(int ver, const char *path, struct stat *sb);
63
static int (*real___lxstat64)(int ver, const char *path, void *sb);
64

65 66 67 68 69
static const char *progname;
const char *output;

#define VIR_FILE_ACCESS_DEFAULT abs_builddir "/test_file_access.txt"

70 71 72 73 74 75 76 77 78
static void init_syms(void)
{
    if (real_open)
        return;

    VIR_MOCK_REAL_INIT(open);
    VIR_MOCK_REAL_INIT(fopen);
    VIR_MOCK_REAL_INIT(access);
    VIR_MOCK_REAL_INIT_ALT(stat, __xstat);
79
    VIR_MOCK_REAL_INIT_ALT(stat64, __xstat64);
80
    VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat);
81
    VIR_MOCK_REAL_INIT_ALT(lstat64, __lxstat64);
82 83 84
}

static void
85
printFile(const char *file)
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
    FILE *fp;
    const char *testname = getenv("VIR_TEST_MOCK_TESTNAME");

    if (!progname) {
        progname = getenv("VIR_TEST_MOCK_PROGNAME");

        if (!progname)
            return;

        output = getenv("VIR_TEST_FILE_ACCESS_OUTPUT");
        if (!output)
            output = VIR_FILE_ACCESS_DEFAULT;
    }

    if (!(fp = real_fopen(output, "a"))) {
        fprintf(stderr, "Unable to open %s: %s\n", output, strerror(errno));
        abort();
    }

    if (flock(fileno(fp), LOCK_EX) < 0) {
        fprintf(stderr, "Unable to lock %s: %s\n", output, strerror(errno));
        fclose(fp);
        abort();
    }

    /* Now append the following line into the output file:
     * $file: $progname $testname */

    fprintf(fp, "%s: %s", file, progname);
    if (testname)
        fprintf(fp, ": %s", testname);

    fputc('\n', fp);

    flock(fileno(fp), LOCK_UN);
    fclose(fp);
}

static void
checkPath(const char *path)
{
    char *fullPath = NULL;
    char *relPath = NULL;
    char *crippledPath = NULL;

    if (path[0] != '/' &&
        virAsprintfQuiet(&relPath, "./%s", path) < 0)
        goto error;

    /* Le sigh. Both canonicalize_file_name() and realpath()
     * expect @path to exist otherwise they return an error. So
     * if we are called over an non-existent file, this could
     * return an error. In that case do our best and hope we will
     * catch possible error. */
    if ((fullPath = canonicalize_file_name(relPath ? relPath : path))) {
        path = fullPath;
    } else {
        /* Yeah, our worst nightmares just became true. Path does
         * not exist. Cut off the last component and retry. */
        if (VIR_STRDUP_QUIET(crippledPath, relPath ? relPath : path) < 0)
            goto error;

        virFileRemoveLastComponent(crippledPath);

        if ((fullPath = canonicalize_file_name(crippledPath)))
            path = fullPath;
    }


    if (!STRPREFIX(path, abs_topsrcdir) &&
        !STRPREFIX(path, abs_topbuilddir)) {
        printFile(path);
    }

    VIR_FREE(crippledPath);
    VIR_FREE(relPath);
    VIR_FREE(fullPath);

    return;
 error:
    fprintf(stderr, "Out of memory\n");
    abort();
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
}


int open(const char *path, int flags, ...)
{
    int ret;

    init_syms();

    checkPath(path);

    if (flags & O_CREAT) {
        va_list ap;
        mode_t mode;
        va_start(ap, flags);
        mode = va_arg(ap, mode_t);
        va_end(ap);
        ret = real_open(path, flags, mode);
    } else {
        ret = real_open(path, flags);
    }
    return ret;
}

FILE *fopen(const char *path, const char *mode)
{
    init_syms();

    checkPath(path);

    return real_fopen(path, mode);
}


int access(const char *path, int mode)
{
    init_syms();

    checkPath(path);

    return real_access(path, mode);
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
/* Okay, the following ifdef rain forest may look messy at a
 * first glance. But here's the thing: during run time linking of
 * a binary, stat() may not be acutally needing symbol stat. It
 * might as well not had been stat() in the first place (see the
 * reasoning at the beginning of this file). However, if we would
 * expose stat symbol here, we will poison the well and trick
 * dynamic linker into thinking we are some old binary that still
 * uses the symbol. So whenever code from upper layers calls
 * stat(), the control would get here, but real_stat can actually
 * be a NULL pointer because newer glibc have __xstat instead.
 * Worse, it can have __xstat64 instead __xstat.
 *
 * Anyway, these ifdefs are there to implement the following
 * preference function:
 *
 * stat < stat64 < __xstat < __xstat64
 *
 * It's the same story with lstat.
 * Also, I feel sorry for you that you had to read this.
 */
#if defined(HAVE_STAT) && !defined(HAVE___XSTAT)
233 234 235 236 237 238
int stat(const char *path, struct stat *sb)
{
    init_syms();

    checkPath(path);

239
    return real_stat(path, sb);
240
}
241 242 243 244 245 246
#endif

#if defined(HAVE_STAT64) && !defined(HAVE___XSTAT64)
int stat64(const char *path, struct stat64 *sb)
{
    init_syms();
247

248 249 250 251 252 253 254
    checkPath(path);

    return real_stat64(path, sb);
}
#endif

#if defined(HAVE___XSTAT) && !defined(HAVE___XSTAT64)
255 256 257 258 259 260 261
int
__xstat(int ver, const char *path, struct stat *sb)
{
    init_syms();

    checkPath(path);

262
    return real___xstat(ver, path, sb);
263
}
264 265 266 267 268 269 270 271 272 273 274 275 276
#endif

#if defined(HAVE___XSTAT64)
int
__xstat64(int ver, const char *path, struct stat64 *sb)
{
    init_syms();

    checkPath(path);

    return real___xstat64(ver, path, sb);
}
#endif
277

278
#if defined(HAVE_LSTAT) && !defined(HAVE___LXSTAT)
279 280 281 282 283 284 285
int
lstat(const char *path, struct stat *sb)
{
    init_syms();

    checkPath(path);

286
    return real_lstat(path, sb);
287
}
288
#endif
289

290 291 292 293 294 295 296 297 298 299 300 301 302
#if defined(HAVE_LSTAT64) && !defined(HAVE___LXSTAT64)
int
lstat64(const char *path, struct stat64 *sb)
{
    init_syms();

    checkPath(path);

    return real_lstat64(path, sb);
}
#endif

#if defined(HAVE___LXSTAT) && !defined(HAVE___LXSTAT64)
303 304 305 306 307 308 309
int
__lxstat(int ver, const char *path, struct stat *sb)
{
    init_syms();

    checkPath(path);

310
    return real___lxstat(ver, path, sb);
311
}
312 313 314 315 316 317 318 319 320 321 322 323 324
#endif

#if defined(HAVE___LXSTAT64)
int
__lxstat64(int ver, const char *path, struct stat64 *sb)
{
    init_syms();

    checkPath(path);

    return real___lxstat64(ver, path, sb);
}
#endif