Whamcloud - gitweb
Try to use secure_getenv() in preference to __secure_getenv()
[tools/e2fsprogs.git] / lib / ext2fs / test_io.c
1 /*
2  * test_io.c --- This is the Test I/O interface.
3  *
4  * Copyright (C) 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #if HAVE_SECURE_GETENV
14 #define _GNU_SOURCE
15 #endif
16 #if HAVE_SECURE_GETENV
17 #define _GNU_SOURCE
18 #endif
19 #include <stdio.h>
20 #include <string.h>
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #include <fcntl.h>
25 #include <time.h>
26 #if HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #if HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #else
35 #define PR_GET_DUMPABLE 3
36 #endif
37 #if (!defined(HAVE_PRCTL) && defined(linux))
38 #include <sys/syscall.h>
39 #endif
40
41 #include "ext2_fs.h"
42 #include "ext2fs.h"
43
44 /*
45  * For checking structure magic numbers...
46  */
47
48 #define EXT2_CHECK_MAGIC(struct, code) \
49           if ((struct)->magic != (code)) return (code)
50
51 struct test_private_data {
52         int     magic;
53         io_channel real;
54         int flags;
55         FILE *outfile;
56         unsigned long block;
57         int read_abort_count, write_abort_count;
58         void (*read_blk)(unsigned long block, int count, errcode_t err);
59         void (*write_blk)(unsigned long block, int count, errcode_t err);
60         void (*set_blksize)(int blksize, errcode_t err);
61         void (*write_byte)(unsigned long block, int count, errcode_t err);
62         void (*read_blk64)(unsigned long long block, int count, errcode_t err);
63         void (*write_blk64)(unsigned long long block, int count, errcode_t err);
64 };
65
66 static errcode_t test_open(const char *name, int flags, io_channel *channel);
67 static errcode_t test_close(io_channel channel);
68 static errcode_t test_set_blksize(io_channel channel, int blksize);
69 static errcode_t test_read_blk(io_channel channel, unsigned long block,
70                                int count, void *data);
71 static errcode_t test_write_blk(io_channel channel, unsigned long block,
72                                 int count, const void *data);
73 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
74                                int count, void *data);
75 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
76                                 int count, const void *data);
77 static errcode_t test_flush(io_channel channel);
78 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
79                                  int count, const void *buf);
80 static errcode_t test_set_option(io_channel channel, const char *option,
81                                  const char *arg);
82 static errcode_t test_get_stats(io_channel channel, io_stats *stats);
83 static errcode_t test_discard(io_channel channel, unsigned long long block,
84                               unsigned long long count);
85
86 static struct struct_io_manager struct_test_manager = {
87         EXT2_ET_MAGIC_IO_MANAGER,
88         "Test I/O Manager",
89         test_open,
90         test_close,
91         test_set_blksize,
92         test_read_blk,
93         test_write_blk,
94         test_flush,
95         test_write_byte,
96         test_set_option,
97         test_get_stats,
98         test_read_blk64,
99         test_write_blk64,
100         test_discard,
101 };
102
103 io_manager test_io_manager = &struct_test_manager;
104
105 /*
106  * These global variable can be set by the test program as
107  * necessary *before* calling test_open
108  */
109 io_manager test_io_backing_manager = 0;
110 void (*test_io_cb_read_blk)
111         (unsigned long block, int count, errcode_t err) = 0;
112 void (*test_io_cb_write_blk)
113         (unsigned long block, int count, errcode_t err) = 0;
114 void (*test_io_cb_read_blk64)
115         (unsigned long long block, int count, errcode_t err) = 0;
116 void (*test_io_cb_write_blk64)
117         (unsigned long long block, int count, errcode_t err) = 0;
118 void (*test_io_cb_set_blksize)
119         (int blksize, errcode_t err) = 0;
120 void (*test_io_cb_write_byte)
121         (unsigned long block, int count, errcode_t err) = 0;
122
123 /*
124  * Test flags
125  */
126 #define TEST_FLAG_READ                  0x01
127 #define TEST_FLAG_WRITE                 0x02
128 #define TEST_FLAG_SET_BLKSIZE           0x04
129 #define TEST_FLAG_FLUSH                 0x08
130 #define TEST_FLAG_DUMP                  0x10
131 #define TEST_FLAG_SET_OPTION            0x20
132 #define TEST_FLAG_DISCARD               0x40
133
134 static void test_dump_block(io_channel channel,
135                             struct test_private_data *data,
136                             unsigned long block, const void *buf)
137 {
138         const unsigned char *cp;
139         FILE *f = data->outfile;
140         int     i;
141         unsigned long   cksum = 0;
142
143         for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
144                 cksum += *cp;
145         }
146         fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
147         for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
148                 if ((i % 16) == 0)
149                         fprintf(f, "%04x: ", i);
150                 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
151         }
152 }
153
154 static void test_abort(io_channel channel, unsigned long block)
155 {
156         struct test_private_data *data;
157         FILE *f;
158
159         data = (struct test_private_data *) channel->private_data;
160         f = data->outfile;
161         test_flush(channel);
162
163         fprintf(f, "Aborting due to I/O to block %lu\n", block);
164         fflush(f);
165         abort();
166 }
167
168 static char *safe_getenv(const char *arg)
169 {
170         if ((getuid() != geteuid()) || (getgid() != getegid()))
171                 return NULL;
172 #if HAVE_PRCTL
173         if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
174                 return NULL;
175 #else
176 #if (defined(linux) && defined(SYS_prctl))
177         if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
178                 return NULL;
179 #endif
180 #endif
181
182 #if defined(HAVE_SECURE_GETENV)
183         return secure_getenv(arg);
184 #elif defined(HAVE___SECURE_GETENV)
185         return __secure_getenv(arg);
186 #else
187         return getenv(arg);
188 #endif
189 }
190
191 static errcode_t test_open(const char *name, int flags, io_channel *channel)
192 {
193         io_channel      io = NULL;
194         struct test_private_data *data = NULL;
195         errcode_t       retval;
196         char            *value;
197
198         if (name == 0)
199                 return EXT2_ET_BAD_DEVICE_NAME;
200         retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
201         if (retval)
202                 goto cleanup;
203         memset(io, 0, sizeof(struct struct_io_channel));
204         io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
205         retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
206         if (retval)
207                 goto cleanup;
208         io->manager = test_io_manager;
209         retval = ext2fs_get_mem(strlen(name)+1, &io->name);
210         if (retval)
211                 goto cleanup;
212
213         strcpy(io->name, name);
214         io->private_data = data;
215         io->block_size = 1024;
216         io->read_error = 0;
217         io->write_error = 0;
218         io->refcount = 1;
219
220         memset(data, 0, sizeof(struct test_private_data));
221         data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
222         if (test_io_backing_manager) {
223                 retval = test_io_backing_manager->open(name, flags,
224                                                        &data->real);
225                 if (retval)
226                         goto cleanup;
227         } else
228                 data->real = 0;
229         data->read_blk =        test_io_cb_read_blk;
230         data->write_blk =       test_io_cb_write_blk;
231         data->set_blksize =     test_io_cb_set_blksize;
232         data->write_byte =      test_io_cb_write_byte;
233         data->read_blk64 =      test_io_cb_read_blk64;
234         data->write_blk64 =     test_io_cb_write_blk64;
235
236         data->outfile = NULL;
237         if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
238                 data->outfile = fopen(value, "w");
239         if (!data->outfile)
240                 data->outfile = stderr;
241
242         data->flags = 0;
243         if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
244                 data->flags = strtoul(value, NULL, 0);
245
246         data->block = 0;
247         if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
248                 data->block = strtoul(value, NULL, 0);
249
250         data->read_abort_count = 0;
251         if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
252                 data->read_abort_count = strtoul(value, NULL, 0);
253
254         data->write_abort_count = 0;
255         if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
256                 data->write_abort_count = strtoul(value, NULL, 0);
257
258         if (data->real)
259                 io->align = data->real->align;
260
261         *channel = io;
262         return 0;
263
264 cleanup:
265         if (io)
266                 ext2fs_free_mem(&io);
267         if (data)
268                 ext2fs_free_mem(&data);
269         return retval;
270 }
271
272 static errcode_t test_close(io_channel channel)
273 {
274         struct test_private_data *data;
275         errcode_t       retval = 0;
276
277         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
278         data = (struct test_private_data *) channel->private_data;
279         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
280
281         if (--channel->refcount > 0)
282                 return 0;
283
284         if (data->real)
285                 retval = io_channel_close(data->real);
286
287         if (data->outfile && data->outfile != stderr)
288                 fclose(data->outfile);
289
290         ext2fs_free_mem(&channel->private_data);
291         if (channel->name)
292                 ext2fs_free_mem(&channel->name);
293         ext2fs_free_mem(&channel);
294         return retval;
295 }
296
297 static errcode_t test_set_blksize(io_channel channel, int blksize)
298 {
299         struct test_private_data *data;
300         errcode_t       retval = 0;
301
302         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
303         data = (struct test_private_data *) channel->private_data;
304         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
305
306         if (data->real) {
307                 retval = io_channel_set_blksize(data->real, blksize);
308                 channel->align = data->real->align;
309         }
310         if (data->set_blksize)
311                 data->set_blksize(blksize, retval);
312         if (data->flags & TEST_FLAG_SET_BLKSIZE)
313                 fprintf(data->outfile,
314                         "Test_io: set_blksize(%d) returned %s\n",
315                         blksize, retval ? error_message(retval) : "OK");
316         channel->block_size = blksize;
317         return retval;
318 }
319
320
321 static errcode_t test_read_blk(io_channel channel, unsigned long block,
322                                int count, void *buf)
323 {
324         struct test_private_data *data;
325         errcode_t       retval = 0;
326
327         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
328         data = (struct test_private_data *) channel->private_data;
329         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
330
331         if (data->real)
332                 retval = io_channel_read_blk(data->real, block, count, buf);
333         if (data->read_blk)
334                 data->read_blk(block, count, retval);
335         if (data->flags & TEST_FLAG_READ)
336                 fprintf(data->outfile,
337                         "Test_io: read_blk(%lu, %d) returned %s\n",
338                         block, count, retval ? error_message(retval) : "OK");
339         if (data->block && data->block == block) {
340                 if (data->flags & TEST_FLAG_DUMP)
341                         test_dump_block(channel, data, block, buf);
342                 if (--data->read_abort_count == 0)
343                         test_abort(channel, block);
344         }
345         return retval;
346 }
347
348 static errcode_t test_write_blk(io_channel channel, unsigned long block,
349                                int count, const void *buf)
350 {
351         struct test_private_data *data;
352         errcode_t       retval = 0;
353
354         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
355         data = (struct test_private_data *) channel->private_data;
356         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
357
358         if (data->real)
359                 retval = io_channel_write_blk(data->real, block, count, buf);
360         if (data->write_blk)
361                 data->write_blk(block, count, retval);
362         if (data->flags & TEST_FLAG_WRITE)
363                 fprintf(data->outfile,
364                         "Test_io: write_blk(%lu, %d) returned %s\n",
365                         block, count, retval ? error_message(retval) : "OK");
366         if (data->block && data->block == block) {
367                 if (data->flags & TEST_FLAG_DUMP)
368                         test_dump_block(channel, data, block, buf);
369                 if (--data->write_abort_count == 0)
370                         test_abort(channel, block);
371         }
372         return retval;
373 }
374
375 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
376                                int count, void *buf)
377 {
378         struct test_private_data *data;
379         errcode_t       retval = 0;
380
381         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
382         data = (struct test_private_data *) channel->private_data;
383         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
384
385         if (data->real)
386                 retval = io_channel_read_blk64(data->real, block, count, buf);
387         if (data->read_blk64)
388                 data->read_blk64(block, count, retval);
389         if (data->flags & TEST_FLAG_READ)
390                 fprintf(data->outfile,
391                         "Test_io: read_blk64(%llu, %d) returned %s\n",
392                         block, count, retval ? error_message(retval) : "OK");
393         if (data->block && data->block == block) {
394                 if (data->flags & TEST_FLAG_DUMP)
395                         test_dump_block(channel, data, block, buf);
396                 if (--data->read_abort_count == 0)
397                         test_abort(channel, block);
398         }
399         return retval;
400 }
401
402 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
403                                int count, const void *buf)
404 {
405         struct test_private_data *data;
406         errcode_t       retval = 0;
407
408         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
409         data = (struct test_private_data *) channel->private_data;
410         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
411
412         if (data->real)
413                 retval = io_channel_write_blk64(data->real, block, count, buf);
414         if (data->write_blk64)
415                 data->write_blk64(block, count, retval);
416         if (data->flags & TEST_FLAG_WRITE)
417                 fprintf(data->outfile,
418                         "Test_io: write_blk64(%llu, %d) returned %s\n",
419                         block, count, retval ? error_message(retval) : "OK");
420         if (data->block && data->block == block) {
421                 if (data->flags & TEST_FLAG_DUMP)
422                         test_dump_block(channel, data, block, buf);
423                 if (--data->write_abort_count == 0)
424                         test_abort(channel, block);
425         }
426         return retval;
427 }
428
429 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
430                                int count, const void *buf)
431 {
432         struct test_private_data *data;
433         errcode_t       retval = 0;
434
435         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
436         data = (struct test_private_data *) channel->private_data;
437         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
438
439         if (data->real && data->real->manager->write_byte)
440                 retval = io_channel_write_byte(data->real, offset, count, buf);
441         if (data->write_byte)
442                 data->write_byte(offset, count, retval);
443         if (data->flags & TEST_FLAG_WRITE)
444                 fprintf(data->outfile,
445                         "Test_io: write_byte(%lu, %d) returned %s\n",
446                         offset, count, retval ? error_message(retval) : "OK");
447         return retval;
448 }
449
450 /*
451  * Flush data buffers to disk.
452  */
453 static errcode_t test_flush(io_channel channel)
454 {
455         struct test_private_data *data;
456         errcode_t       retval = 0;
457
458         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
459         data = (struct test_private_data *) channel->private_data;
460         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
461
462         if (data->real)
463                 retval = io_channel_flush(data->real);
464
465         if (data->flags & TEST_FLAG_FLUSH)
466                 fprintf(data->outfile, "Test_io: flush() returned %s\n",
467                         retval ? error_message(retval) : "OK");
468
469         return retval;
470 }
471
472 static errcode_t test_set_option(io_channel channel, const char *option,
473                                  const char *arg)
474 {
475         struct test_private_data *data;
476         errcode_t       retval = 0;
477
478         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
479         data = (struct test_private_data *) channel->private_data;
480         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
481
482
483         if (data->flags & TEST_FLAG_SET_OPTION)
484                 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
485                         option, arg);
486         if (data->real && data->real->manager->set_option) {
487                 retval = (data->real->manager->set_option)(data->real,
488                                                            option, arg);
489                 if (data->flags & TEST_FLAG_SET_OPTION)
490                         fprintf(data->outfile, "returned %s\n",
491                                 retval ? error_message(retval) : "OK");
492         } else {
493                 if (data->flags & TEST_FLAG_SET_OPTION)
494                         fprintf(data->outfile, "not implemented\n");
495         }
496         return retval;
497 }
498
499 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
500 {
501         struct test_private_data *data;
502         errcode_t       retval = 0;
503
504         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
505         data = (struct test_private_data *) channel->private_data;
506         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
507
508         if (data->real && data->real->manager->get_stats) {
509                 retval = (data->real->manager->get_stats)(data->real, stats);
510         }
511         return retval;
512 }
513
514 static errcode_t test_discard(io_channel channel, unsigned long long block,
515                               unsigned long long count)
516 {
517         struct test_private_data *data;
518         errcode_t       retval = 0;
519
520         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
521         data = (struct test_private_data *) channel->private_data;
522         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
523
524         if (data->real)
525                 retval = io_channel_discard(data->real, block, count);
526         if (data->flags & TEST_FLAG_DISCARD)
527                 fprintf(data->outfile,
528                         "Test_io: discard(%llu, %llu) returned %s\n",
529                         block, count, retval ? error_message(retval) : "OK");
530         return retval;
531 }