Skip to content
This repository
Browse code

Merge pull request #61 from lastguru1/upstream

Tool to set whitelist/blacklist ioctl
  • Loading branch information...
commit d741759b447d6ce96c014e1c937197dd35735afd 2 parents ef37f60 + 041416a
Mohan Srinivasan authored April 03, 2012
7  src/utils/Makefile
... ...
@@ -1,6 +1,6 @@
1 1
 COMMIT_REV ?= $(shell git describe  --always --abbrev=12)
2 2
 CFLAGS += -I.. -I. -DCOMMIT_REV="\"$(COMMIT_REV)\"" -g
3  
-PROGRAMS += flashcache_create flashcache_destroy flashcache_load
  3
+PROGRAMS += flashcache_create flashcache_destroy flashcache_load flashcache_setioctl
4 4
 INSTALL_DIR = $(DESTDIR)/sbin/
5 5
 
6 6
 .PHONY:all
@@ -21,6 +21,11 @@ flashcache_load: flashcache_load.o
21 21
 
22 22
 -include flashcache_load.d
23 23
 
  24
+flashcache_setioctl: flashcache_setioctl.o
  25
+	$(CC) $^ -o $@
  26
+
  27
+-include flashcache_setioctl.d
  28
+
24 29
 %.o: %.c
25 30
 	$(CC) -c $(CFLAGS) $*.c -o $*.o
26 31
 	@$(CC) -MM $(CFLAGS) -MF $*.d -MT $*.o $*.c
134  src/utils/flashcache_setioctl.c
... ...
@@ -0,0 +1,134 @@
  1
+/*
  2
+ * Copyright (c) 2012, Dmitry Golubev
  3
+ * All rights reserved.
  4
+ * 
  5
+ * Redistribution and use in source and binary forms, with or without
  6
+ * modification, are permitted provided that the following conditions are met:
  7
+ * 
  8
+ * Redistributions of source code must retain the above copyright notice,
  9
+ * this list of conditions and the following disclaimer.
  10
+ * Redistributions in binary form must reproduce the above copyright notice,
  11
+ *  this list of conditions and the following disclaimer in the documentation
  12
+ * and/or other materials provided with the distribution.
  13
+ * Neither the name Facebook nor the names of its contributors may be used to
  14
+ * endorse or promote products derived from this software without specific
  15
+ * prior written permission.
  16
+ * 
  17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27
+*/
  28
+#include <fcntl.h>
  29
+#include <sys/ioctl.h>
  30
+#include <linux/fs.h>
  31
+#include <stdio.h>
  32
+#include <inttypes.h>
  33
+#include <sys/types.h>
  34
+#include <unistd.h>
  35
+#include <stdlib.h>
  36
+#include <linux/types.h>
  37
+#include <flashcache_ioctl.h>
  38
+
  39
+void usage(char *pname)
  40
+{
  41
+	fprintf(stderr, "Usage: %s (-c | -a | -r) (-b pid |-w pid) ssd_devname \n", pname);
  42
+	exit(1);
  43
+}
  44
+
  45
+main(int argc, char **argv)
  46
+{
  47
+	int cache_fd, c, result;
  48
+	char action = ' ', list = ' ', *cachedev, *pname = argv[0];
  49
+	intmax_t pidmax;
  50
+	char *tmp;
  51
+	pid_t pid;
  52
+
  53
+	while ((c = getopt(argc, argv, "carb:w:")) != -1) {
  54
+		switch (c) {
  55
+			case 'c':
  56
+				action = 'c';
  57
+				break;
  58
+			case 'a':
  59
+				action = 'a';
  60
+				break;
  61
+			case 'r':
  62
+				action = 'r';
  63
+				break;
  64
+			case 'b':
  65
+				list = 'b';
  66
+				pidmax = strtoimax(optarg, &tmp, 10);
  67
+				if(tmp == optarg || *tmp != '\0' || pidmax != (pid_t)pidmax) {
  68
+					fprintf(stderr, "Bad PID!\n");
  69
+					exit(1);
  70
+				}
  71
+				else {
  72
+					pid = (pid_t)pidmax;
  73
+				}
  74
+				break;
  75
+			case 'w':
  76
+				list = 'w';
  77
+				pidmax = strtoimax(optarg, &tmp, 10);
  78
+				if(tmp == optarg || *tmp != '\0' || pidmax != (pid_t)pidmax) {
  79
+					fprintf(stderr, "Bad PID!\n");
  80
+					exit(1);
  81
+				}
  82
+				else {
  83
+					pid = (pid_t)pidmax;
  84
+				}
  85
+				break;
  86
+			case '?':
  87
+				usage(pname);
  88
+		}
  89
+	}
  90
+	if (action == ' ')
  91
+		usage(pname);
  92
+	if (list == ' ')
  93
+		usage(pname);
  94
+	if (optind == argc) 
  95
+		usage(pname);
  96
+	cachedev = argv[optind++];
  97
+	cache_fd = open(cachedev, O_RDONLY);
  98
+	if (cache_fd < 0) {
  99
+		fprintf(stderr, "Failed to open %s\n", cachedev);
  100
+		exit(1);
  101
+	}
  102
+	if (list == 'w') {
  103
+		switch (action) {
  104
+			case 'a':
  105
+				result=ioctl(cache_fd, FLASHCACHEADDWHITELIST, &pid);
  106
+				break;
  107
+			case 'r':
  108
+				result=ioctl(cache_fd, FLASHCACHEDELWHITELIST, &pid);
  109
+				break;
  110
+			case 'c':
  111
+				result=ioctl(cache_fd, FLASHCACHEDELALLWHITELIST, &pid);
  112
+				break;
  113
+		}
  114
+	}
  115
+	else {
  116
+		switch (action) {
  117
+			case 'a':
  118
+				result=ioctl(cache_fd, FLASHCACHEADDBLACKLIST, &pid);
  119
+				break;
  120
+			case 'r':
  121
+				result=ioctl(cache_fd, FLASHCACHEDELBLACKLIST, &pid);
  122
+				break;
  123
+			case 'c':
  124
+				result=ioctl(cache_fd, FLASHCACHEDELALLBLACKLIST, &pid);
  125
+				break;
  126
+		}
  127
+	}
  128
+	close(cache_fd);
  129
+	if (result < 0) {
  130
+		fprintf(stderr, "ioctl failed on %s\n", cachedev);
  131
+		exit(1);
  132
+	}
  133
+}
  134
+

0 notes on commit d741759

Please sign in to comment.
Something went wrong with that request. Please try again.