• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check that fault injection works properly.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 
33 #include <assert.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 #include <sys/wait.h>
44 
45 static int exp_fd;
46 static int got_fd;
47 static int out_fd;
48 
49 #define DEFAULT_ERRNO ENOSYS
50 
51 static const char *errstr;
52 static int is_raw, err, first, step, iter, try;
53 
54 static void
invoke(int fail)55 invoke(int fail)
56 {
57 	static char buf[sizeof(int) * 3 + 3];
58 	const struct iovec io = {
59 		.iov_base = buf,
60 		.iov_len = sprintf(buf, "%d.", ++try)
61 	};
62 	int rc;
63 
64 	if (!fail) {
65 		rc = write(exp_fd, io.iov_base, io.iov_len);
66 		if (rc != (int) io.iov_len)
67 			perror_msg_and_fail("write");
68 	}
69 
70 	errno = 0;
71 	rc = writev(got_fd, &io, 1);
72 
73 	if (fail) {
74 		if (!(rc == -1 && errno == err))
75 			perror_msg_and_fail("expected errno %d"
76 					    ", got rc == %d, errno == %d",
77 					    err, rc, errno);
78 
79 		if (is_raw)
80 			tprintf("writev(%#x, %p, 0x1)"
81 				" = -1 %s (%m) (INJECTED)\n",
82 				got_fd, &io, errstr);
83 		else
84 			tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)"
85 				" = -1 %s (%m) (INJECTED)\n",
86 				got_fd, buf, (int) io.iov_len, errstr);
87 	} else {
88 		if (rc != (int) io.iov_len)
89 			perror_msg_and_fail("expected %d"
90 					    ", got rc == %d, errno == %d",
91 					    (int) io.iov_len, rc, errno);
92 
93 		if (is_raw)
94 			tprintf("writev(%#x, %p, 0x1) = %#x\n",
95 				got_fd, &io, rc);
96 		else
97 			tprintf("writev(%d, [{iov_base=\"%s\", iov_len=%d}], 1)"
98 				" = %d\n",
99 				got_fd, buf, (int) io.iov_len,
100 				(int) io.iov_len);
101 	}
102 }
103 
104 static int
open_file(const char * prefix,int proc)105 open_file(const char *prefix, int proc)
106 {
107 	static const int open_flags = O_WRONLY | O_TRUNC | O_CREAT;
108 	static char path[PATH_MAX + 1];
109 
110 	snprintf(path, sizeof(path), "%s.%d", prefix, proc);
111 
112 	int fd = open(path, open_flags, 0600);
113 	if (fd < 0)
114 		perror_msg_and_fail("open: %s", path);
115 
116 	return fd;
117 }
118 
119 int
main(int argc,char * argv[])120 main(int argc, char *argv[])
121 {
122 	assert(argc == 11);
123 
124 	is_raw = !strcmp("raw", argv[1]);
125 
126 	errstr = argv[2];
127 	err = atoi(errstr);
128 	assert(err >= 0);
129 
130 	if (!err) {
131 		if (!*errstr)
132 			err = DEFAULT_ERRNO;
133 		else if (!strcasecmp(errstr, "EINVAL"))
134 			err = EINVAL;
135 		else
136 			err = ENOSYS;
137 	}
138 
139 	errno = err;
140 	errstr = errno2name();
141 
142 	first = atoi(argv[3]);
143 	step = atoi(argv[4]);
144 	iter = atoi(argv[5]);
145 	int num_procs = atoi(argv[6]);
146 	char *exp_prefix = argv[7];
147 	char *got_prefix = argv[8];
148 	char *out_prefix = argv[9];
149 	char *pid_prefix = argv[10];
150 
151 	assert(first > 0);
152 	assert(step >= 0);
153 	assert(num_procs > 0);
154 
155 	int proc;
156 	for (proc = 0; proc < num_procs; ++proc) {
157 		int ret = fork();
158 
159 		if (ret < 0)
160 			perror_msg_and_fail("fork");
161 
162 		if (ret > 0) {
163 			int pidfd = open_file(pid_prefix, proc);
164 
165 			char pidstr[sizeof(ret) * 3];
166 			int len = snprintf(pidstr, sizeof(pidstr), "%d", ret);
167 			assert(len > 0 && len < (int) sizeof(pidstr));
168 			assert(write(pidfd, pidstr, len) == len);
169 
170 			close(pidfd);
171 
172 			continue;
173 		}
174 
175 		tprintf("%s", "");
176 
177 		exp_fd = open_file(exp_prefix, proc);
178 		got_fd = open_file(got_prefix, proc);
179 		out_fd = open_file(out_prefix, proc);
180 
181 		/* This magic forces tprintf to write where we want it. */
182 		dup2(out_fd, 3);
183 
184 		int i;
185 		for (i = 1; i <= iter; ++i) {
186 			int fail = 0;
187 			if (first > 0) {
188 				--first;
189 				if (first == 0) {
190 					fail = 1;
191 					first = step;
192 				}
193 			}
194 			invoke(fail);
195 		}
196 
197 		tprintf("%s\n", "+++ exited with 0 +++");
198 		return 0;
199 	}
200 
201 	for (proc = 0; proc < num_procs; ++proc) {
202 		int status;
203 		int ret = wait(&status);
204 		if (ret <= 0)
205 			perror_msg_and_fail("wait %d", proc);
206 		if (status)
207 			error_msg_and_fail("wait: pid=%d status=%d",
208 					   ret, status);
209 	}
210 
211 	return 0;
212 }
213