aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-04-19 12:34:48 +0200
committerlonkaars <loek@pipeframe.xyz>2024-04-19 12:34:48 +0200
commitbacaaf0b7595730904613e86d40400f72682904d (patch)
treee0394a93cd8eeed53962fce04e03916ccdfda579
parent4335a620a364b70f06421b1698706a9dc3324a46 (diff)
finish tests
-rw-r--r--1/fopdrv.c6
-rw-r--r--1/main.c6
-rw-r--r--1/test/main.c38
-rwxr-xr-x1/test/test.sh10
4 files changed, 55 insertions, 5 deletions
diff --git a/1/fopdrv.c b/1/fopdrv.c
index 31c76cd..2c74fea 100644
--- a/1/fopdrv.c
+++ b/1/fopdrv.c
@@ -2,16 +2,18 @@
// driver/char/mem.c read_null (/dev/null)
ssize_t fop_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
+ printk("%s(<file>, <buf>, %lu, <ppos>)\n", __PRETTY_FUNCTION__, count);
return 0;
}
// driver/char/mem.c write_null
ssize_t fop_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) {
+ printk("%s(<file>, <buf>, %lu, <ppos>)\n", __PRETTY_FUNCTION__, count);
return count;
}
int fop_open(struct inode * inode, struct file * file) {
- printk("%s\n", __PRETTY_FUNCTION__);
+ printk("%s(<inode>, <file>)\n", __PRETTY_FUNCTION__);
return 0;
// 0 seems to be a safe return value as it's used in driver/char/mem.c. The
// manual page for open(2) says that the system call returns a nonnegative
@@ -20,7 +22,7 @@ int fop_open(struct inode * inode, struct file * file) {
}
int fop_release(struct inode * inode, struct file * file) {
- printk("%s\n", __PRETTY_FUNCTION__);
+ printk("%s(<inode>, <file>)\n", __PRETTY_FUNCTION__);
return 0;
// same as above, but found in driver/char/lp.c
}
diff --git a/1/main.c b/1/main.c
index 101f675..d441f19 100644
--- a/1/main.c
+++ b/1/main.c
@@ -53,7 +53,7 @@ static int mod_init(void) {
goto free_device;
}
- printk("%s %d:%d\n", __PRETTY_FUNCTION__, MAJOR(node), MINOR(node));
+ printk("%s() -> 0 (%d:%d)\n", __PRETTY_FUNCTION__, MAJOR(node), MINOR(node));
return 0;
free_device:
@@ -63,7 +63,7 @@ free_cdev:
free_class:
class_destroy(class);
return_err:
- printk("%s: %d\n", __PRETTY_FUNCTION__, err);
+ printk("%s() -> %d\n", __PRETTY_FUNCTION__, err);
return err;
}
@@ -71,7 +71,7 @@ static void mod_exit(void) {
device_destroy(class, node);
cdev_del(cdev);
class_destroy(class);
- printk("%s\n", __PRETTY_FUNCTION__);
+ printk("%s()\n", __PRETTY_FUNCTION__);
}
module_init(mod_init);
diff --git a/1/test/main.c b/1/test/main.c
index 7329e16..b87e56c 100644
--- a/1/test/main.c
+++ b/1/test/main.c
@@ -1,5 +1,8 @@
#include <stdio.h>
#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
int main(int argc, char** argv) {
if (geteuid() != 0) {
@@ -13,6 +16,41 @@ int main(int argc, char** argv) {
return 1;
}
+ char* dev = argv[1];
+ const char* input = "test";
+ int fd;
+ ssize_t bytes;
+
+ fd = open(dev, O_RDWR);
+ if (-1 == fd) {
+ fprintf(stderr, "open() failed with code %d\n", errno);
+ return 1;
+ }
+
+ bytes = write(fd, input, strlen(input));
+ if (-1 == bytes) {
+ fprintf(stderr, "write() failed with code %d\n", errno);
+ return 1;
+ }
+ fprintf(stderr, "input \"%s\" to %s (%ld bytes succesful)\n", input, dev, bytes);
+
+ char buf[80];
+ bytes = read(fd, buf, 80);
+ if (-1 == bytes) {
+ fprintf(stderr, "read() failed with code %d\n", errno);
+ return 1;
+ }
+ fprintf(stderr, "output from %s: (%ld bytes)\n", dev, bytes);
+ if (bytes > 0) {
+ if (bytes < 80) buf[bytes] = '\0'; // null terminate string for printf
+ printf("%s\n", buf);
+ }
+
+ if (-1 == close(fd)) {
+ fprintf(stderr, "close() failed with code %d\n", errno);
+ return 1;
+ }
+
return 0;
}
diff --git a/1/test/test.sh b/1/test/test.sh
index 18263e6..7dba005 100755
--- a/1/test/test.sh
+++ b/1/test/test.sh
@@ -10,5 +10,15 @@ if [ $# -eq 0 ] ; then
exit 1
fi
+DEV="$1"
+INPUT="test"
+
+echo "input \"$INPUT\" to $DEV" >&2
+echo "$INPUT" > "$DEV"
+echo "(EC $?)" >&2
+
+echo "output from $DEV:" >&2
+cat "$DEV"
+echo "(EC $?)" >&2
exit 0