Whamcloud - gitweb
Add SIGINT and SIGTERM handling to fsck and e2fsck. For e2fsck,
[tools/e2fsprogs.git] / e2fsck / flushb.c
1 /*
2  * flushb.c --- This routine flushes the disk buffers for a disk
3  *
4  * Copyright 1997, 2000, by Theodore Ts'o.
5  * 
6  * This program may be used under the provisions of the GNU Public
7  * License, *EXCEPT* that a binary copy of the executable may not be
8  * packaged as a part of binary package which is distributed as part
9  * of a Linux distribution.  (Yes, this violates the Debian Free
10  * Software Guidelines of restricting its field of use.  That's the
11  * point.  I don't want this program being distributed in Debian,
12  * because I don't care to support it, and the maintainer, Yann
13  * Dirson, doesn't seem to pay attention to my wishes on this matter.
14  * So I'm deliberately adding this clause so it violates the Debian
15  * Free Software Guidelines to force him to take it out.  (What part
16  * of THIS IS FOR MY OWN USE don't you understand?  And no, I'm going
17  * to write a man page for it either.  And don't file a bug about it
18  * or bug me about it.)  If this doesn't work, I'll have to remove it
19  * from the upstream source distribution at the next release.  End of
20  * Rant.  :-)
21  * 
22  * (BTW, use of flushb on some older 2.2 kernels on a heavily loaded
23  * system will corrupt filesystems.)
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mount.h>
33 #include "../misc/nls-enable.h"
34
35 /* For Linux, define BLKFLSBUF if necessary */
36 #if (!defined(BLKFLSBUF) && defined(__linux__))
37 #define BLKFLSBUF       _IO(0x12,97)    /* flush buffer cache */
38 #endif
39
40 const char *progname;
41
42 static void usage(void)
43 {
44         fprintf(stderr, _("Usage: %s disk\n"), progname);
45         exit(1);
46 }       
47         
48 int main(int argc, char **argv)
49 {
50         int     fd;
51         
52         progname = argv[0];
53         if (argc != 2)
54                 usage();
55
56         fd = open(argv[1], O_RDONLY, 0);
57         if (fd < 0) {
58                 perror("open");
59                 exit(1);
60         }
61         /*
62          * Note: to reread the partition table, use the ioctl
63          * BLKRRPART instead of BLKFSLBUF.
64          */
65 #ifdef BLKFLSBUF
66         if (ioctl(fd, BLKFLSBUF, 0) < 0) {
67                 perror("ioctl BLKFLSBUF");
68                 exit(1);
69         }
70         return 0;
71 #else
72         fprintf(stderr,
73                 _("BLKFLSBUF ioctl not supported!  Can't flush buffers.\n"));
74         return 1;
75 #endif
76 }