@@ -17,6 +17,8 @@ MODULE_VERSION("0.1");
17
17
static dev_t dev = -1 ;
18
18
static struct cdev cdev ;
19
19
static struct class * class ;
20
+ static void * user_data = NULL ;
21
+ static size_t user_data_size = 0 ;
20
22
21
23
struct workqueue_struct * workqueue ;
22
24
@@ -35,23 +37,45 @@ static int num_compare(const void *a, const void *b)
35
37
return (* (int * ) a - * (int * ) b );
36
38
}
37
39
40
+ static ssize_t sort_write (struct file * file ,
41
+ const char __user * buf ,
42
+ size_t size ,
43
+ loff_t * offset )
44
+ {
45
+ unsigned long len ;
46
+ void * tmp ;
47
+
48
+ tmp = kmalloc (size , GFP_KERNEL );
49
+ if (!tmp )
50
+ return 0 ;
51
+
52
+ len = copy_from_user (tmp , buf , size );
53
+ if (len != 0 ) {
54
+ kfree (tmp );
55
+ return 0 ;
56
+ }
57
+
58
+ kfree (user_data );
59
+ user_data = tmp ;
60
+ user_data_size = size ;
61
+
62
+ return size ;
63
+ }
64
+
38
65
static ssize_t sort_read (struct file * file ,
39
66
char * buf ,
40
67
size_t size ,
41
68
loff_t * offset )
42
69
{
43
70
unsigned long len ;
44
71
size_t es ;
72
+ void * sort_buffer ;
45
73
46
- void * sort_buffer = kmalloc (size , GFP_KERNEL );
47
- if (!sort_buffer )
74
+ if (!user_data || size != user_data_size )
48
75
return 0 ;
49
76
50
- /* FIXME: Requiring users to manually input data into a buffer for read
51
- * operations is not ideal, even if it is only for testing purposes.
52
- */
53
- len = copy_from_user (sort_buffer , buf , size );
54
- if (len != 0 )
77
+ sort_buffer = kmemdup (user_data , size , GFP_KERNEL );
78
+ if (!sort_buffer )
55
79
return 0 ;
56
80
57
81
/* TODO: While currently designed to handle integer arrays, there is an
@@ -62,21 +86,14 @@ static ssize_t sort_read(struct file *file,
62
86
sort_main (sort_buffer , size / es , es , num_compare );
63
87
64
88
len = copy_to_user (buf , sort_buffer , size );
89
+ kfree (sort_buffer );
90
+
65
91
if (len != 0 )
66
92
return 0 ;
67
93
68
- kfree (sort_buffer );
69
94
return size ;
70
95
}
71
96
72
- static ssize_t sort_write (struct file * file ,
73
- const char * buf ,
74
- size_t size ,
75
- loff_t * offset )
76
- {
77
- return 0 ;
78
- }
79
-
80
97
static const struct file_operations fops = {
81
98
.read = sort_read ,
82
99
.write = sort_write ,
0 commit comments