Skip to content

Commit 3c8e2d9

Browse files
authored
Merge pull request #8 from EricccTaiwan/read_and_write
Add functional implementation for sort_write and sort_read
2 parents 30c8def + 243a09e commit 3c8e2d9

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

sort_mod.c

+33-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ MODULE_VERSION("0.1");
1717
static dev_t dev = -1;
1818
static struct cdev cdev;
1919
static struct class *class;
20+
static void *user_data = NULL;
21+
static size_t user_data_size = 0;
2022

2123
struct workqueue_struct *workqueue;
2224

@@ -35,23 +37,45 @@ static int num_compare(const void *a, const void *b)
3537
return (*(int *) a - *(int *) b);
3638
}
3739

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+
3865
static ssize_t sort_read(struct file *file,
3966
char *buf,
4067
size_t size,
4168
loff_t *offset)
4269
{
4370
unsigned long len;
4471
size_t es;
72+
void *sort_buffer;
4573

46-
void *sort_buffer = kmalloc(size, GFP_KERNEL);
47-
if (!sort_buffer)
74+
if (!user_data || size != user_data_size)
4875
return 0;
4976

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)
5579
return 0;
5680

5781
/* TODO: While currently designed to handle integer arrays, there is an
@@ -62,21 +86,14 @@ static ssize_t sort_read(struct file *file,
6286
sort_main(sort_buffer, size / es, es, num_compare);
6387

6488
len = copy_to_user(buf, sort_buffer, size);
89+
kfree(sort_buffer);
90+
6591
if (len != 0)
6692
return 0;
6793

68-
kfree(sort_buffer);
6994
return size;
7095
}
7196

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-
8097
static const struct file_operations fops = {
8198
.read = sort_read,
8299
.write = sort_write,

user.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ int main()
2828
for (size_t i = 0; i < n_elements; i++)
2929
inbuf[i] = rand() % n_elements;
3030

31+
ssize_t w_sz = write(fd, inbuf, size);
32+
if (w_sz != size) {
33+
perror("Failed to write character device");
34+
ret = EXIT_FAILURE;
35+
goto error_rw;
36+
}
37+
3138
ssize_t r_sz = read(fd, inbuf, size);
3239
if (r_sz != size) {
3340
perror("Failed to read character device");
3441
ret = EXIT_FAILURE;
35-
goto error_read;
42+
goto error_rw;
3643
}
3744

3845
bool pass = true;
@@ -46,7 +53,7 @@ int main()
4653

4754
printf("Sorting %s!\n", pass ? "succeeded" : "failed");
4855

49-
error_read:
56+
error_rw:
5057
free(inbuf);
5158
error_alloc:
5259
close(fd);

0 commit comments

Comments
 (0)