forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnssmock.c
132 lines (108 loc) · 2.86 KB
/
nssmock.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
/*
* 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/>.
*/
#include <config.h>
#ifdef WITH_NSS
# include "virmock.h"
# include <sys/types.h>
# include <dirent.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include "configmake.h"
# include "virstring.h"
# include "viralloc.h"
static int (*real_open)(const char *path, int flags, ...);
static DIR * (*real_opendir)(const char *name);
static int (*real_access)(const char *path, int mode);
# define LEASEDIR LOCALSTATEDIR "/lib/libvirt/dnsmasq/"
/*
* Functions to load the symbols and init the environment
*/
static void
init_syms(void)
{
if (real_open)
return;
VIR_MOCK_REAL_INIT(open);
VIR_MOCK_REAL_INIT(opendir);
VIR_MOCK_REAL_INIT(access);
}
static int
getrealpath(char **newpath,
const char *path)
{
if (STRPREFIX(path, LEASEDIR)) {
*newpath = g_strdup_printf("%s/nssdata/%s",
abs_srcdir,
path + strlen(LEASEDIR));
} else {
*newpath = g_strdup(path);
}
return 0;
}
int
open(const char *path, int flags, ...)
{
int ret;
char *newpath = NULL;
init_syms();
if (STRPREFIX(path, LEASEDIR) &&
getrealpath(&newpath, path) < 0)
return -1;
if (flags & O_CREAT) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = (mode_t) va_arg(ap, int);
va_end(ap);
ret = real_open(newpath ? newpath : path, flags, mode);
} else {
ret = real_open(newpath ? newpath : path, flags);
}
free(newpath);
return ret;
}
DIR *
opendir(const char *path)
{
DIR *ret;
char *newpath = NULL;
init_syms();
if (STRPREFIX(path, LEASEDIR) &&
getrealpath(&newpath, path) < 0)
return NULL;
ret = real_opendir(newpath ? newpath : path);
free(newpath);
return ret;
}
int
access(const char *path, int mode)
{
int ret;
char *newpath = NULL;
init_syms();
if (STRPREFIX(path, LEASEDIR) &&
getrealpath(&newpath, path) < 0)
return -1;
ret = real_access(newpath ? newpath : path, mode);
free(newpath);
return ret;
}
#else
/* Nothing to override if NSS plugin is not enabled */
#endif