Whamcloud - gitweb
c260bf6ba3c050118a08e2cee0c96a5b5d1cb743
[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 "../misc/nls-enable.h"
33
34 #ifdef __STDC__
35 #define NOARGS void
36 #else
37 #define NOARGS
38 #define const
39 #endif
40
41 /* For Linux/i386, define BLKFLSBUF */
42 #if (!defined(BLKFLSBUF) && defined(__i386__))
43 #define BLKFLSBUF  0x1261       /* flush buffer cache */
44 #endif
45
46 const char *progname;
47
48 static void usage(NOARGS)
49 {
50         fprintf(stderr, _("Usage: %s disk\n"), progname);
51         exit(1);
52 }       
53         
54 int main(int argc, char **argv)
55 {
56         int     fd;
57         
58         progname = argv[0];
59         if (argc != 2)
60                 usage();
61
62         fd = open(argv[1], O_RDONLY, 0);
63         if (fd < 0) {
64                 perror("open");
65                 exit(1);
66         }
67         /*
68          * Note: to reread the partition table, use the ioctl
69          * BLKRRPART instead of BLKFSLBUF.
70          */
71 #ifdef BLKFLSBUF
72         if (ioctl(fd, BLKFLSBUF, 0) < 0) {
73                 perror("ioctl BLKFLSBUF");
74                 exit(1);
75         }
76         return 0;
77 #else
78         fprintf(stderr,
79                 _("BLKFLSBUF ioctl not supported!  Can't flush buffers.\n"));
80         return 1;
81 #endif
82 }