backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / nssmock.c
blob230044275ada8b944a59be8197bee7502eb92453
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #ifdef NSS
22 # include "virmock.h"
23 # include <sys/types.h>
24 # include <dirent.h>
25 # include <sys/stat.h>
26 # include <fcntl.h>
27 # include <unistd.h>
29 # include "configmake.h"
30 # include "virstring.h"
31 # include "viralloc.h"
33 static int (*real_open)(const char *path, int flags, ...);
34 static DIR * (*real_opendir)(const char *name);
35 static int (*real_access)(const char *path, int mode);
37 # define LEASEDIR LOCALSTATEDIR "/lib/libvirt/dnsmasq/"
40 * Functions to load the symbols and init the environment
42 static void
43 init_syms(void)
45 if (real_open)
46 return;
48 VIR_MOCK_REAL_INIT(open);
49 VIR_MOCK_REAL_INIT(opendir);
50 VIR_MOCK_REAL_INIT(access);
53 static int
54 getrealpath(char **newpath,
55 const char *path)
57 if (STRPREFIX(path, LEASEDIR)) {
58 if (virAsprintfQuiet(newpath, "%s/nssdata/%s",
59 abs_srcdir,
60 path + strlen(LEASEDIR)) < 0) {
61 errno = ENOMEM;
62 return -1;
64 } else {
65 if (VIR_STRDUP_QUIET(*newpath, path) < 0)
66 return -1;
69 return 0;
72 int
73 open(const char *path, int flags, ...)
75 int ret;
76 char *newpath = NULL;
78 init_syms();
80 if (STRPREFIX(path, LEASEDIR) &&
81 getrealpath(&newpath, path) < 0)
82 return -1;
84 if (flags & O_CREAT) {
85 va_list ap;
86 mode_t mode;
87 va_start(ap, flags);
88 mode = (mode_t) va_arg(ap, int);
89 va_end(ap);
90 ret = real_open(newpath ? newpath : path, flags, mode);
91 } else {
92 ret = real_open(newpath ? newpath : path, flags);
95 free(newpath);
96 return ret;
99 DIR *
100 opendir(const char *path)
102 DIR *ret;
103 char *newpath = NULL;
105 init_syms();
107 if (STRPREFIX(path, LEASEDIR) &&
108 getrealpath(&newpath, path) < 0)
109 return NULL;
111 ret = real_opendir(newpath ? newpath : path);
113 free(newpath);
114 return ret;
118 access(const char *path, int mode)
120 int ret;
121 char *newpath = NULL;
123 init_syms();
125 if (STRPREFIX(path, LEASEDIR) &&
126 getrealpath(&newpath, path) < 0)
127 return -1;
129 ret = real_access(newpath ? newpath : path, mode);
131 free(newpath);
132 return ret;
134 #else
135 /* Nothing to override if NSS plugin is not enabled */
136 #endif