1 /* diskctl - control disk device driver parameters - by D.C. van Moolenbroek */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 static void __dead
usage(void)10 usage(void)
11 {
12 fprintf(stderr,
13 "usage: %s <device> <command> [args]\n"
14 "\n"
15 "supported commands:\n"
16 " getwcache return write cache status\n"
17 " setwcache [on|off] set write cache status\n"
18 " flush flush write cache\n",
19 getprogname());
20
21 exit(EXIT_FAILURE);
22 }
23
24 static int
open_dev(const char * dev,int flags)25 open_dev(const char * dev, int flags)
26 {
27 int fd;
28
29 fd = open(dev, flags);
30
31 if (fd < 0) {
32 perror("open");
33
34 exit(EXIT_FAILURE);
35 }
36
37 return fd;
38 }
39
40 int
main(int argc,char ** argv)41 main(int argc, char ** argv)
42 {
43 int fd, val;
44
45 setprogname(argv[0]);
46
47 if (argc < 3) usage();
48
49 if (!strcasecmp(argv[2], "getwcache")) {
50 if (argc != 3) usage();
51
52 fd = open_dev(argv[1], O_RDONLY);
53
54 if (ioctl(fd, DIOCGETWC, &val) != 0) {
55 perror("ioctl");
56
57 return EXIT_FAILURE;
58 }
59
60 close(fd);
61
62 printf("write cache is %s\n", val ? "on" : "off");
63
64 } else if (!strcasecmp(argv[2], "setwcache")) {
65 if (argc != 4) usage();
66
67 if (!strcasecmp(argv[3], "on"))
68 val = 1;
69 else if (!strcasecmp(argv[3], "off"))
70 val = 0;
71 else
72 usage();
73
74 fd = open_dev(argv[1], O_WRONLY);
75
76 if (ioctl(fd, DIOCSETWC, &val) != 0) {
77 perror("ioctl");
78
79 return EXIT_FAILURE;
80 }
81
82 close(fd);
83
84 printf("write cache %sabled\n", val ? "en" : "dis");
85
86 } else if (!strcasecmp(argv[2], "flush")) {
87 if (argc != 3) usage();
88
89 fd = open_dev(argv[1], O_WRONLY);
90
91 if (ioctl(fd, DIOCFLUSH, NULL) != 0) {
92 perror("ioctl");
93
94 return EXIT_FAILURE;
95 }
96
97 close(fd);
98
99 printf("write cache flushed\n");
100
101 } else
102 usage();
103
104 return EXIT_SUCCESS;
105 }
106