~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to tests/kernel/file-concurrent/test.c

  • Committer: Tyler Hicks
  • Date: 2017-06-08 23:06:20 UTC
  • mfrom: (885.1.2 ecryptfs)
  • Revision ID: tyhicks@canonical.com-20170608230620-vcosxagt2cwab9eo
ecryptfs-migrate-home: Pass --nopwcheck to ecryptfs-setup-private (LP: #1630477)

[tyhicks: Remove extra spaces in usage output and add changelog entry]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: Colin King <colin.king@canonical.com>
 
3
 *
 
4
 * Copyright (C) 2012 Canonical, Ltd.
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
19
 */
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <stdbool.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/select.h>
 
29
#include <sys/wait.h>
 
30
#include <fcntl.h>
 
31
#include <unistd.h>
 
32
#include <signal.h>
 
33
 
 
34
#define TEST_PASSED     (0)
 
35
#define TEST_FAILED     (1)
 
36
#define TEST_ERROR      (2)
 
37
 
 
38
#define MAX_FILES       (16)    /* number of files to create per iteration */
 
39
#define THREADS_PER_CPU (8)     /* number of child processes per CPU */
 
40
 
 
41
#define TIMEOUT         (30)    /* system hang timeout in seconds */
 
42
 
 
43
#define TEST_DURATION   (60)    /* duration of test (seconds) */
 
44
#define MIN_DURATION    (1)     /* minimum test duration (seconds) */
 
45
 
 
46
#define CREAT           (0)
 
47
#define TRUNCATE        (1)
 
48
#define UNLINK          (2)
 
49
#define UNKNOWN         (3)
 
50
 
 
51
#define DIE_SIGINT      (1)     /* Kill test threads because of SIGINT */
 
52
#define DIE_COMPLETE    (2)     /* Kill test threads because end of test duration */
 
53
 
 
54
static volatile int die = 0;
 
55
 
 
56
static char *options[] = {
 
57
        "creat()",
 
58
        "unlink()",
 
59
        "truncate()",
 
60
        "<unknown>"
 
61
};
 
62
 
 
63
/*
 
64
 *  Create many threads that try and create create/truncate/unlink aces
 
65
 */
 
66
 
 
67
/*
 
68
 *  Run creat/tuncate/unlink as a child and detect any timeouts.  This
 
69
 *  is a little heavy handed, but allows us to detect kernel
 
70
 *  hangs on the syscalls if we lock up.
 
71
 *
 
72
 */
 
73
int hang_check(int option, const char *filename)
 
74
{
 
75
        pid_t pid;
 
76
        struct timeval tv;
 
77
        int ret;
 
78
        int status;
 
79
        int pipefd[2];
 
80
        int fd;
 
81
        fd_set readfds;
 
82
 
 
83
        tv.tv_sec = TIMEOUT;
 
84
        tv.tv_usec = 0;
 
85
 
 
86
        if (pipe(pipefd) < 0) {
 
87
                fprintf(stderr, "pipe error\n");
 
88
                return TEST_ERROR;
 
89
        }
 
90
 
 
91
        pid = fork();
 
92
        switch (pid) {
 
93
        case -1:
 
94
                fprintf(stderr, "failed to fork child\n");
 
95
                return TEST_ERROR;
 
96
 
 
97
        case 0:
 
98
                /* Child */
 
99
                close(pipefd[0]);
 
100
 
 
101
                switch (option) {
 
102
                case CREAT:
 
103
                        fd = creat(filename, 0700);
 
104
                        if (fd >= 0)
 
105
                                close(fd);
 
106
                        break;
 
107
 
 
108
                case TRUNCATE:
 
109
                        /*
 
110
                         * We want to try and force a lock up -
 
111
                         * we don't care about the return since
 
112
                         * the file may not exit, but we assign
 
113
                         * ret to keep gcc happy
 
114
                         */
 
115
                        ret = truncate(filename, 64 * 1024);
 
116
                        ret = truncate(filename, 128 * 1024);
 
117
                        ret = truncate(filename, 64 * 1024);
 
118
                        ret = truncate(filename, 0);
 
119
                        ret = truncate(filename, 64 * 1024);
 
120
                        break;
 
121
 
 
122
                case UNLINK:
 
123
                        unlink(filename);
 
124
                        break;
 
125
 
 
126
                default:
 
127
                        break;
 
128
                }
 
129
                if (write(pipefd[1], "EXIT", 4) < 0)
 
130
                        fprintf(stderr, "pipe write failed\n");
 
131
 
 
132
                close(pipefd[1]);
 
133
                _exit(0);
 
134
                break;
 
135
 
 
136
        default:
 
137
                /* Parent */
 
138
                close(pipefd[1]);
 
139
 
 
140
                FD_ZERO(&readfds);
 
141
                FD_SET(pipefd[0], &readfds);
 
142
 
 
143
                /* parent, sleep until we get a message from the child */
 
144
                ret = select(pipefd[0]+1, &readfds, NULL, NULL, &tv);
 
145
                close(pipefd[0]);
 
146
 
 
147
                switch (ret) {
 
148
                case 0:
 
149
                        /* Timed out on select, no signal from child! */
 
150
                        fprintf(stderr, "Timed out after %d seconds doing %s - possible eCryptfs hang\n",
 
151
                                TIMEOUT, options[option > TRUNCATE ? UNKNOWN : option]);
 
152
                        /* Vainly attempt to kill child */
 
153
                        kill(pid, SIGINT);
 
154
                        return TEST_FAILED;
 
155
                case -1:
 
156
                        if (errno != EINTR) {
 
157
                                fprintf(stderr, "Unexpected return from select(): %d %s\n", errno, strerror(errno));
 
158
                                waitpid(pid, &status, 0);
 
159
                                return TEST_ERROR;
 
160
                        }
 
161
                        else {
 
162
                                /*
 
163
                                 * We got sent a signal from controlling process to
 
164
                                 * tell us to stop, so return TEST_PASSED since we have
 
165
                                 * not detected any failures from our child
 
166
                                 */
 
167
                                waitpid(pid, &status, 0);
 
168
                                return TEST_PASSED;
 
169
                        }
 
170
                default:
 
171
                        /* Child completed the required operation and wrote down the pipe, lets reap */
 
172
                        waitpid(pid, &status, 0);
 
173
                        return TEST_PASSED;
 
174
                }
 
175
        }
 
176
}
 
177
 
 
178
int test_files(const char *path, const int max_files)
 
179
{
 
180
        int i;
 
181
        char *filename;
 
182
        size_t len = strlen(path) + 32;
 
183
        int ret = TEST_PASSED;
 
184
 
 
185
        if ((filename = malloc(len)) == NULL) {
 
186
                fprintf(stderr, "failed to malloc filename\n");
 
187
                return TEST_ERROR;
 
188
        }
 
189
 
 
190
        for (;;) {
 
191
                for (i = 0; i < max_files; i++) {
 
192
                        snprintf(filename, len, "%s/%d", path, i);
 
193
                        if ((ret = hang_check(CREAT, filename)) != TEST_PASSED) {
 
194
                                free(filename);
 
195
                                return ret;
 
196
                        }
 
197
                        if (die)
 
198
                                goto cleanup;
 
199
                }
 
200
 
 
201
                for (i = 0; i < max_files; i++) {
 
202
                        snprintf(filename, len, "%s/%d", path, i);
 
203
                        if ((ret = hang_check(TRUNCATE, filename)) != TEST_PASSED) {
 
204
                                free(filename);
 
205
                                return ret;
 
206
                        }
 
207
                        if (die)
 
208
                                goto cleanup;
 
209
                }
 
210
 
 
211
cleanup:
 
212
                for (i = 0; i < max_files; i++) {
 
213
                        snprintf(filename, len, "%s/%d", path, i);
 
214
                        if ((ret = hang_check(UNLINK, filename)) != TEST_PASSED) {
 
215
                                free(filename);
 
216
                                return ret;
 
217
                        }
 
218
                }
 
219
 
 
220
                if (die)
 
221
                        break;
 
222
        }
 
223
 
 
224
        free(filename);
 
225
 
 
226
        if (die & DIE_SIGINT)
 
227
                ret = TEST_ERROR;       /* Got aborted */
 
228
 
 
229
        return ret;
 
230
}
 
231
 
 
232
void sigint_handler(int dummy)
 
233
{
 
234
        die = DIE_SIGINT;
 
235
}
 
236
 
 
237
void sigusr1_handler(int dummy)
 
238
{
 
239
        die = DIE_COMPLETE;
 
240
}
 
241
 
 
242
int test_exercise(const char *path, const int max_files, const int duration)
 
243
{
 
244
        int i;
 
245
        long threads = sysconf(_SC_NPROCESSORS_CONF) * THREADS_PER_CPU;
 
246
        pid_t *pids;
 
247
        int ret = TEST_PASSED;
 
248
 
 
249
        if ((pids = calloc(threads, sizeof(pid_t))) == NULL) {
 
250
                fprintf(stderr, "failed to calloc pids\n");
 
251
                return TEST_ERROR;
 
252
        }
 
253
 
 
254
        /* Go forth and multiply.. */
 
255
        for (i = 0; i < threads; i++) {
 
256
                pid_t pid;
 
257
 
 
258
                pid = fork();
 
259
                switch (pid) {
 
260
                case -1:
 
261
                        fprintf(stderr, "failed to fork child %d of %ld\n", i+1, threads);
 
262
                        break;
 
263
                case 0:
 
264
                        exit(test_files(path, max_files));
 
265
                default:
 
266
                        pids[i] = pid;
 
267
                        break;
 
268
                }
 
269
        }
 
270
 
 
271
        sleep(duration);
 
272
 
 
273
        for (i = 0; i < threads; i++)
 
274
                kill(pids[i], SIGUSR1);
 
275
 
 
276
        for (i = 0; i < threads; i++) {
 
277
                int status;
 
278
                waitpid(pids[i], &status, 0);
 
279
 
 
280
                if (WEXITSTATUS(status) != TEST_PASSED)
 
281
                        ret = WEXITSTATUS(status);
 
282
        }
 
283
 
 
284
        free(pids);
 
285
 
 
286
        return ret;
 
287
}
 
288
 
 
289
void show_usage(char *name)
 
290
{
 
291
        fprintf(stderr, "Syntax: %s [-d duration] pathname\n", name);
 
292
        fprintf(stderr, "\t-d duration of test (in seconds)\n");
 
293
        exit(TEST_ERROR);
 
294
}
 
295
 
 
296
int main(int argc, char **argv)
 
297
{
 
298
        int opt;
 
299
        int duration = TEST_DURATION;
 
300
 
 
301
        while ((opt = getopt(argc, argv, "d:")) != -1) {
 
302
                switch (opt) {
 
303
                case 'd':
 
304
                        duration = atoi(optarg);
 
305
                        break;
 
306
                default:
 
307
                        show_usage(argv[0]);
 
308
                        break;
 
309
                }
 
310
        }
 
311
 
 
312
        if (optind >= argc)
 
313
                show_usage(argv[0]);
 
314
 
 
315
        if (duration < MIN_DURATION) {
 
316
                fprintf(stderr,
 
317
                        "Test duration must be %d or more seconds long.\n",
 
318
                        MIN_DURATION);
 
319
                exit(TEST_ERROR);
 
320
        }
 
321
 
 
322
        if (access(argv[optind], R_OK | W_OK) < 0) {
 
323
                fprintf(stderr, "Cannot access %s\n", argv[1]);
 
324
                exit(TEST_ERROR);
 
325
        }
 
326
 
 
327
        signal(SIGINT, sigint_handler);
 
328
        signal(SIGUSR1, sigusr1_handler);
 
329
 
 
330
        exit(test_exercise(argv[optind], MAX_FILES, duration));
 
331
}