File: _failure_injection.c

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (184 lines) | stat: -rw-r--r-- 3,865 bytes parent folder | download | duplicates (7)
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2016 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Jan Pokorny <jpokorny@redhat.com>
 *
 * This file is part of libqb.
 *
 * libqb 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.
 *
 * libqb 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 libqb.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "_failure_injection.h"
#include "config.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <dlfcn.h>  /* dlsym */
#include <errno.h>
#include <fcntl.h>  /* O_CREAT, ... */
#include <stdarg.h>
#include <stdio.h>  /* perror */
#include <sys/types.h>  /* mode_t, off_t */


/*** unlink (failure injection) ***/

int _fi_unlink_inject_failure = 0;
typedef int (func_unlink)(const char *);
func_unlink unlink;

static int
fake_unlink(const char *pathname)
{
	errno = EACCES;
	return -1;
}

int
unlink(const char *pathname)
{
	static func_unlink *real_unlink;

	if (_fi_unlink_inject_failure) {
		return fake_unlink(pathname);
	} else if (!real_unlink) {
		real_unlink = (func_unlink *)
			      dlsym(RTLD_NEXT, "unlink");
		if (!real_unlink) {
			perror("dlsym(RTLD_NEXT, \"unlink\"");
			real_unlink = fake_unlink;
		}
	}
	return real_unlink(pathname);
}


/*** unlinkat (failure injection) ***/

#if defined(HAVE_UNLINKAT)
typedef int (func_unlinkat)(int, const char *, int);
func_unlinkat unlinkat;

static int
fake_unlinkat(int dirfd, const char *pathname, int flags)
{
	errno = EACCES;
	return -1;
}

int
unlinkat(int dirfd, const char *pathname, int flags)
{
	static func_unlinkat *real_unlinkat;

	if (_fi_unlink_inject_failure) {
		return fake_unlinkat(dirfd, pathname, flags);
	} else if (!real_unlinkat) {
		real_unlinkat = (func_unlinkat *)
				 dlsym(RTLD_NEXT, "unlinkat");
		if (!real_unlinkat) {
			perror("dlsym(RTLD_NEXT, \"unlinkat\"");
			real_unlinkat = fake_unlinkat;
		}
	}
	return real_unlinkat(dirfd, pathname, flags);
}
#endif


/*** truncate (trigger detection) ***/

int _fi_truncate_called = 0;
typedef int (func_truncate)(const char *, off_t);
func_truncate truncate;

static int
fake_truncate(const char *path, off_t length)
{
	errno = EIO;
	return -1;
}

int
truncate(const char *path, off_t length)
{
	static func_truncate *real_truncate;

	if (!real_truncate) {
		real_truncate = (func_truncate *)
				 dlsym(RTLD_NEXT, "truncate");
		if (!real_truncate) {
			perror("dlsym(RTLD_NEXT, \"truncate\"");
			real_truncate = fake_truncate;
		}
	}
	_fi_truncate_called++;
	return real_truncate(path, length);
}


/*** openat (trigger detection) ***/

int _fi_openat_called = 0;
#if defined(HAVE_UNLINKAT)
typedef int (func_openat)(int, const char *, int, ...);
func_openat openat;

static int
fake_openat(int fd, const char *path, int oflag, ...)
{
	errno = EBADF;
	return -1;
}

#ifndef O_TMPFILE
#  define O_TMPFILE 0
#endif

int
openat(int fd, const char *path, int oflag, ...)
{
	static func_openat *real_openat;
	int use_mode = 0;
	mode_t mode;
	va_list ap;

	if (!real_openat) {
		real_openat = (func_openat *)
				 dlsym(RTLD_NEXT, "openat");
		if (!real_openat) {
			perror("dlsym(RTLD_NEXT, \"openat\"");
			real_openat = fake_openat;
		}
	}
	_fi_openat_called++;

	va_start(ap, oflag);
	if (oflag & (O_CREAT | O_TMPFILE)) {
		mode = va_arg(ap, mode_t);
		use_mode = 1;
	}
	va_end(ap);

	if (use_mode) {
		return real_openat(fd, path, oflag, mode);
	} else {
		return real_openat(fd, path, oflag);
	}
}
#endif