summaryrefslogtreecommitdiff
path: root/minix/usr.sbin/diskctl/diskctl.c
blob: 849d5b95038452f16c9e077a21980b6ef7b89791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* diskctl - control disk device driver parameters - by D.C. van Moolenbroek */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

static void __dead
usage(void)
{
	fprintf(stderr,
	    "usage: %s <device> <command> [args]\n"
	    "\n"
	    "supported commands:\n"
	    "  getwcache           return write cache status\n"
	    "  setwcache [on|off]  set write cache status\n"
	    "  flush               flush write cache\n",
	    getprogname());

	exit(EXIT_FAILURE);
}

static int
open_dev(const char * dev, int flags)
{
	int fd;

	fd = open(dev, flags);

	if (fd < 0) {
		perror("open");

		exit(EXIT_FAILURE);
	}

	return fd;
}

int
main(int argc, char ** argv)
{
	int fd, val;

	setprogname(argv[0]);

	if (argc < 3) usage();

	if (!strcasecmp(argv[2], "getwcache")) {
		if (argc != 3) usage();

		fd = open_dev(argv[1], O_RDONLY);

		if (ioctl(fd, DIOCGETWC, &val) != 0) {
			perror("ioctl");

			return EXIT_FAILURE;
		}

		close(fd);

		printf("write cache is %s\n", val ? "on" : "off");

	} else if (!strcasecmp(argv[2], "setwcache")) {
		if (argc != 4) usage();

		if (!strcasecmp(argv[3], "on"))
			val = 1;
		else if (!strcasecmp(argv[3], "off"))
			val = 0;
		else
			usage();

		fd = open_dev(argv[1], O_WRONLY);

		if (ioctl(fd, DIOCSETWC, &val) != 0) {
			perror("ioctl");

			return EXIT_FAILURE;
		}

		close(fd);

		printf("write cache %sabled\n", val ? "en" : "dis");

	} else if (!strcasecmp(argv[2], "flush")) {
		if (argc != 3) usage();

		fd = open_dev(argv[1], O_WRONLY);

		if (ioctl(fd, DIOCFLUSH, NULL) != 0) {
			perror("ioctl");

			return EXIT_FAILURE;
		}

		close(fd);

		printf("write cache flushed\n");

	} else
		usage();

	return EXIT_SUCCESS;
}