Whamcloud - gitweb
Add SIGINT and SIGTERM handling to fsck and e2fsck. For e2fsck,
[tools/e2fsprogs.git] / e2fsck / util.c
1 /*
2  * util.c --- miscellaneous utilities
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #ifdef HAVE_CONIO_H
18 #undef HAVE_TERMIOS_H
19 #include <conio.h>
20 #define read_a_char()   getch()
21 #else
22 #ifdef HAVE_TERMIOS_H
23 #include <termios.h>
24 #endif
25 #include <stdio.h>
26 #define read_a_char()   getchar()
27 #endif
28
29 #ifdef HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
32
33 #include "e2fsck.h"
34
35 #include <sys/time.h>
36 #include <sys/resource.h>
37
38 void fatal_error(e2fsck_t ctx, const char *msg)
39 {
40         if (msg) 
41                 fprintf (stderr, "e2fsck: %s\n", msg);
42         if (ctx->fs && ctx->fs->io) {
43                 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
44                         io_channel_flush(ctx->fs->io);
45                 else
46                         fprintf(stderr, "e2fsck: io manager magic bad!\n");
47         }
48         ctx->flags |= E2F_FLAG_ABORT;
49         if (ctx->flags & E2F_FLAG_SETJMP_OK)
50                 longjmp(ctx->abort_loc, 1);
51         exit(FSCK_ERROR);
52 }
53
54 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
55                              const char *description)
56 {
57         void *ret;
58         char buf[256];
59
60 #ifdef DEBUG_ALLOCATE_MEMORY
61         printf("Allocating %d bytes for %s...\n", size, description);
62 #endif
63         ret = malloc(size);
64         if (!ret) {
65                 sprintf(buf, "Can't allocate %s\n", description);
66                 fatal_error(ctx, buf);
67         }
68         memset(ret, 0, size);
69         return ret;
70 }
71
72 int ask_yn(const char * string, int def)
73 {
74         int             c;
75         const char      *defstr;
76         const char      *short_yes = _("yY");
77         const char      *short_no = _("nN");
78
79 #ifdef HAVE_TERMIOS_H
80         struct termios  termios, tmp;
81
82         tcgetattr (0, &termios);
83         tmp = termios;
84         tmp.c_lflag &= ~(ICANON | ECHO);
85         tmp.c_cc[VMIN] = 1;
86         tmp.c_cc[VTIME] = 0;
87         tcsetattr (0, TCSANOW, &tmp);
88 #endif
89
90         if (def == 1)
91                 defstr = _("<y>");
92         else if (def == 0)
93                 defstr = _("<n>");
94         else
95                 defstr = _(" (y/n)");
96         printf("%s%s? ", string, defstr);
97         while (1) {
98                 fflush (stdout);
99                 if ((c = read_a_char()) == EOF)
100                         break;
101                 if (strchr(short_yes, (char) c)) {
102                         def = 1;
103                         break;
104                 }
105                 else if (strchr(short_no, (char) c)) {
106                         def = 0;
107                         break;
108                 }
109                 else if ((c == ' ' || c == '\n') && (def != -1))
110                         break;
111         }
112         if (def)
113                 printf ("yes\n\n");
114         else
115                 printf ("no\n\n");
116 #ifdef HAVE_TERMIOS_H
117         tcsetattr (0, TCSANOW, &termios);
118 #endif
119         return def;
120 }
121
122 int ask (e2fsck_t ctx, const char * string, int def)
123 {
124         if (ctx->options & E2F_OPT_NO) {
125                 printf (_("%s? no\n\n"), string);
126                 return 0;
127         }
128         if (ctx->options & E2F_OPT_YES) {
129                 printf (_("%s? yes\n\n"), string);
130                 return 1;
131         }
132         if (ctx->options & E2F_OPT_PREEN) {
133                 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
134                 return def;
135         }
136         return ask_yn(string, def);
137 }
138
139 void e2fsck_read_bitmaps(e2fsck_t ctx)
140 {
141         ext2_filsys fs = ctx->fs;
142         errcode_t       retval;
143
144         if (ctx->invalid_bitmaps) {
145                 com_err(ctx->program_name, 0,
146                     _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
147                         ctx->device_name);
148                 fatal_error(ctx, 0);
149         }
150
151         ehandler_operation(_("reading inode and block bitmaps"));
152         retval = ext2fs_read_bitmaps(fs);
153         ehandler_operation(0);
154         if (retval) {
155                 com_err(ctx->program_name, retval,
156                         _("while retrying to read bitmaps for %s"),
157                         ctx->device_name);
158                 fatal_error(ctx, 0);
159         }
160 }
161
162 void e2fsck_write_bitmaps(e2fsck_t ctx)
163 {
164         ext2_filsys fs = ctx->fs;
165         errcode_t       retval;
166
167         if (ext2fs_test_bb_dirty(fs)) {
168                 ehandler_operation(_("writing block bitmaps"));
169                 retval = ext2fs_write_block_bitmap(fs);
170                 ehandler_operation(0);
171                 if (retval) {
172                         com_err(ctx->program_name, retval,
173                             _("while retrying to write block bitmaps for %s"),
174                                 ctx->device_name);
175                         fatal_error(ctx, 0);
176                 }
177         }
178
179         if (ext2fs_test_ib_dirty(fs)) {
180                 ehandler_operation(_("writing inode bitmaps"));
181                 retval = ext2fs_write_inode_bitmap(fs);
182                 ehandler_operation(0);
183                 if (retval) {
184                         com_err(ctx->program_name, retval,
185                             _("while retrying to write inode bitmaps for %s"),
186                                 ctx->device_name);
187                         fatal_error(ctx, 0);
188                 }
189         }
190 }
191
192 void preenhalt(e2fsck_t ctx)
193 {
194         ext2_filsys fs = ctx->fs;
195
196         if (!(ctx->options & E2F_OPT_PREEN))
197                 return;
198         fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
199                 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
200                ctx->device_name);
201         if (fs != NULL) {
202                 fs->super->s_state |= EXT2_ERROR_FS;
203                 ext2fs_mark_super_dirty(fs);
204                 ext2fs_close(fs);
205         }
206         exit(FSCK_UNCORRECTED);
207 }
208
209 #ifdef RESOURCE_TRACK
210 void init_resource_track(struct resource_track *track)
211 {
212 #ifdef HAVE_GETRUSAGE
213         struct rusage r;
214 #endif
215         
216         track->brk_start = sbrk(0);
217         gettimeofday(&track->time_start, 0);
218 #ifdef HAVE_GETRUSAGE
219 #ifdef sun
220         memset(&r, 0, sizeof(struct rusage));
221 #endif
222         getrusage(RUSAGE_SELF, &r);
223         track->user_start = r.ru_utime;
224         track->system_start = r.ru_stime;
225 #else
226         track->user_start.tv_sec = track->user_start.tv_usec = 0;
227         track->system_start.tv_sec = track->system_start.tv_usec = 0;
228 #endif
229 }
230
231 #ifdef __GNUC__
232 #define _INLINE_ __inline__
233 #else
234 #define _INLINE_
235 #endif
236
237 static _INLINE_ float timeval_subtract(struct timeval *tv1,
238                                        struct timeval *tv2)
239 {
240         return ((tv1->tv_sec - tv2->tv_sec) +
241                 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
242 }
243
244 void print_resource_track(const char *desc, struct resource_track *track)
245 {
246 #ifdef HAVE_GETRUSAGE
247         struct rusage r;
248 #endif
249 #ifdef HAVE_MALLINFO
250         struct mallinfo malloc_info;
251 #endif
252         struct timeval time_end;
253
254         gettimeofday(&time_end, 0);
255
256         if (desc)
257                 printf("%s: ", desc);
258
259 #ifdef HAVE_MALLINFO
260 #define kbytes(x)       (((x) + 1023) / 1024)
261         
262         malloc_info = mallinfo();
263         printf(_("Memory used: %dk/%dk (%dk/%dk), "),
264                kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
265                kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
266 #else
267         printf(_("Memory used: %d, "),
268                (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
269 #endif  
270 #ifdef HAVE_GETRUSAGE
271         getrusage(RUSAGE_SELF, &r);
272
273         printf(_("time: %5.2f/%5.2f/%5.2f\n"),
274                timeval_subtract(&time_end, &track->time_start),
275                timeval_subtract(&r.ru_utime, &track->user_start),
276                timeval_subtract(&r.ru_stime, &track->system_start));
277 #else
278         printf(_("elapsed time: %6.3f\n"),
279                timeval_subtract(&time_end, &track->time_start));
280 #endif
281 }
282 #endif /* RESOURCE_TRACK */
283
284 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
285                               struct ext2_inode * inode, const char *proc)
286 {
287         int retval;
288
289         retval = ext2fs_read_inode(ctx->fs, ino, inode);
290         if (retval) {
291                 com_err("ext2fs_read_inode", retval,
292                         _("while reading inode %ld in %s"), ino, proc);
293                 fatal_error(ctx, 0);
294         }
295 }
296
297 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
298                                struct ext2_inode * inode, const char *proc)
299 {
300         int retval;
301
302         retval = ext2fs_write_inode(ctx->fs, ino, inode);
303         if (retval) {
304                 com_err("ext2fs_write_inode", retval,
305                         _("while writing inode %ld in %s"), ino, proc);
306                 fatal_error(ctx, 0);
307         }
308 }
309
310 #ifdef MTRACE
311 void mtrace_print(char *mesg)
312 {
313         FILE    *malloc_get_mallstream();
314         FILE    *f = malloc_get_mallstream();
315
316         if (f)
317                 fprintf(f, "============= %s\n", mesg);
318 }
319 #endif
320
321 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
322                    io_manager manager)
323 {
324         struct ext2_super_block *sb;
325         io_channel              io = NULL;
326         void                    *buf = NULL;
327         int                     blocksize;
328         blk_t                   superblock, ret_sb = 8193;
329         
330         if (fs && fs->super) {
331                 ret_sb = (fs->super->s_blocks_per_group +
332                           fs->super->s_first_data_block);
333                 if (ctx) {
334                         ctx->superblock = ret_sb;
335                         ctx->blocksize = fs->blocksize;
336                 }
337                 return ret_sb;
338         }
339                 
340         if (ctx) {
341                 if (ctx->blocksize) {
342                         ret_sb = ctx->blocksize * 8;
343                         if (ctx->blocksize == 1024)
344                                 ret_sb++;
345                         ctx->superblock = ret_sb;
346                         return ret_sb;
347                 }
348                 ctx->superblock = ret_sb;
349                 ctx->blocksize = 1024;
350         }
351
352         if (!name || !manager)
353                 goto cleanup;
354
355         if (manager->open(name, 0, &io) != 0)
356                 goto cleanup;
357
358         if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
359                 goto cleanup;
360         sb = (struct ext2_super_block *) buf;
361
362         for (blocksize = EXT2_MIN_BLOCK_SIZE;
363              blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
364                 superblock = blocksize*8;
365                 if (blocksize == 1024)
366                         superblock++;
367                 io_channel_set_blksize(io, blocksize);
368                 if (io_channel_read_blk(io, superblock,
369                                         -SUPERBLOCK_SIZE, buf))
370                         continue;
371 #ifdef EXT2FS_ENABLE_SWAPFS
372                 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
373                         ext2fs_swap_super(sb);
374 #endif
375                 if (sb->s_magic == EXT2_SUPER_MAGIC) {
376                         ret_sb = ctx->superblock = superblock;
377                         ctx->blocksize = blocksize;
378                         break;
379                 }
380         }
381
382 cleanup:
383         if (io)
384                 io_channel_close(io);
385         if (buf)
386                 ext2fs_free_mem(&buf);
387         return (ret_sb);
388 }
389
390 /*
391  * Given a mode, return the ext2 file type
392  */
393 int ext2_file_type(unsigned int mode)
394 {
395         if (LINUX_S_ISREG(mode))
396                 return EXT2_FT_REG_FILE;
397
398         if (LINUX_S_ISDIR(mode))
399                 return EXT2_FT_DIR;
400         
401         if (LINUX_S_ISCHR(mode))
402                 return EXT2_FT_CHRDEV;
403         
404         if (LINUX_S_ISBLK(mode))
405                 return EXT2_FT_BLKDEV;
406         
407         if (LINUX_S_ISLNK(mode))
408                 return EXT2_FT_SYMLINK;
409
410         if (LINUX_S_ISFIFO(mode))
411                 return EXT2_FT_FIFO;
412         
413         if (LINUX_S_ISSOCK(mode))
414                 return EXT2_FT_SOCK;
415         
416         return 0;
417 }