1 /*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 /**
19 * The probability of a syscall failing from 0.0 to 1.0
20 */
21 #define PROBABILITY 0.9
22
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28
29 /* for various intercepted calls */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 /* For builds on glibc */
36 #define __USE_GNU
37 #include <dlfcn.h>
38
39 #include "interrupter.h"
40
41 static int probability = PROBABILITY * RAND_MAX;
42
maybe_interrupt()43 static int maybe_interrupt() {
44 if (rand() < probability) {
45 return 1;
46 }
47 return 0;
48 }
49
50 DEFINE_INTERCEPT(read, ssize_t, int, void*, size_t);
51 DEFINE_INTERCEPT(write, ssize_t, int, const void*, size_t);
52 DEFINE_INTERCEPT(accept, int, int, struct sockaddr*, socklen_t*);
53 DEFINE_INTERCEPT(creat, int, const char*, mode_t);
54