Skip to content

Commit

Permalink
Adding to all printk the module name and removing all obscuring '...'…
Browse files Browse the repository at this point in the history
… from messages
  • Loading branch information
tpiekarski committed Mar 30, 2020
1 parent 23df329 commit c184877
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

test:
$(info Running all available tests...)
$(info Running all available tests)
@$(MAKE) test-module name=lkm_device
@$(MAKE) test-module name=lkm_parameters
@$(MAKE) test-module name=lkm_proc
Expand Down Expand Up @@ -67,7 +67,7 @@ test-device:
@sudo rmmod $(module_filename)

test-module:
$(info >> Testing module '$(name)' by loading and displaying Kernel Message Ring Buffer...)
$(info >> Testing module '$(name)' by loading and displaying Kernel Message Ring Buffer)
$(info >> Root permissions are needed for clearing buffer with dmesg and loading/unloading with insmod/rmmod)

@test ${name} || (echo "!! Please provide a valid module name to test, like 'make test name=lkm_sandbox'."; exit 1)
Expand Down
32 changes: 17 additions & 15 deletions lkm_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ static ssize_t device_read(struct file *flip, char *buffer, size_t len,
loff_t *offset)
{
int bytes_read = 0;
printk(KERN_INFO "Starting to read from sandbox device.\n");
printk(KERN_INFO "lkm_device: Starting to read from sandbox device.\n");

if (*message_ptr == 0)
message_ptr = message_buffer;

while (len && *message_ptr) {
printk(KERN_INFO "Reading from device...\n");
printk(KERN_INFO "lkm_device: Reading from device.\n");
if (put_user(*(message_ptr++), buffer++) == -EFAULT) {
printk(KERN_ALERT
"Failed copying message from kernel to user space.\n");
"lkm_device: Failed copying message from kernel to user space.\n");

break;
}
Expand All @@ -95,19 +95,20 @@ static ssize_t device_read(struct file *flip, char *buffer, size_t len,
static ssize_t device_write(struct file *flip, const char *buffer, size_t len,
loff_t *offset)
{
printk(KERN_ALERT "Writing to sandbox device is not supported.\n");
printk(KERN_ALERT
"lkm_device: Writing to sandbox device is not supported.\n");

return -EINVAL;
}

static int device_open(struct inode *inode, struct file *file)
{
if (device_open_count > 0) {
printk(KERN_INFO "Sandbox device already open.\n");
printk(KERN_INFO "lkm_device: Sandbox device already open.\n");
return -EBUSY;
}

printk(KERN_INFO "Opening sandbox device.\n");
printk(KERN_INFO "lkm_device: Opening sandbox device.\n");
device_open_count++;
try_module_get(THIS_MODULE);

Expand All @@ -116,7 +117,7 @@ static int device_open(struct inode *inode, struct file *file)

static int device_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "Closing sandbox device.\n");
printk(KERN_INFO "lkm_device: Closing sandbox device.\n");
device_open_count--;
module_put(THIS_MODULE);

Expand All @@ -132,7 +133,7 @@ static int proc_show(struct seq_file *seq, void *v)

static int __init lkm_device_init(void)
{
printk(KERN_INFO "Initialize Sandbox Device Module...\n");
printk(KERN_INFO "lkm_device: Initialize Sandbox Device Module.\n");

if (device_init_sub() < 0) {
return -1;
Expand All @@ -147,30 +148,30 @@ static int __init lkm_device_init(void)

static void __exit lkm_device_exit(void)
{
printk(KERN_INFO "Exiting Sandbox Device Module.\n");
printk(KERN_INFO "lkm_device: Exiting Sandbox Device Module.\n");
unregister_chrdev(major_num, DEVICE_NAME);
remove_proc_entry(PROC_FILE_NAME, PROC_PARENT);
}

static int device_init_sub(void)
{
printk(KERN_INFO
"Registering character device to print test message.\n");
"lkm_device: Registering character device to print test message.\n");

strncpy(message_buffer, MESSAGE, MESSAGE_BUFFER_LENGTH);
message_ptr = message_buffer;

if (param_major_num != 0) {
printk(KERN_INFO
"Failed allocating %d as major for sandbox device.\n",
"lkm_device: Failed allocating %d as major for sandbox device.\n",
param_major_num);
}

major_num = register_chrdev(param_major_num, DEVICE_NAME, &device_fops);

if (major_num < 0) {
printk(KERN_ALERT
"Failed to register sandbox device with major %d.\n",
"lkm_device: Failed to register sandbox device with major %d.\n",
major_num);

return -1;
Expand All @@ -181,18 +182,19 @@ static int device_init_sub(void)

static int proc_init_sub(void)
{
printk(KERN_INFO "Registered sandbox device with major number %d.\n",
printk(KERN_INFO
"lkm_device: Registered sandbox device with major number %d.\n",
major_num);

printk(KERN_INFO
"Creating /proc file %s for storing major number %d.\n",
"lkm_device: Creating /proc file %s for storing major number %d.\n",
PROC_FILE_NAME, major_num);
proc_major_entry = proc_create_single(PROC_FILE_NAME, PROC_PERMISSION,
PROC_PARENT, proc_show);

if (proc_major_entry == NULL) {
printk(KERN_ALERT
"Failed to create /proc entry '%s' for device major.\n",
"lkm_device: Failed to create /proc entry '%s' for device major.\n",
PROC_FILE_NAME);

return -1;
Expand Down
9 changes: 5 additions & 4 deletions lkm_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,26 @@ module_param(message, charp, DEFAULT_PERMISSIONS);

static int __init lkm_parameters_init(void)
{
printk(KERN_INFO "Initializing Sandbox Parameters Module...\n");
printk(KERN_INFO
"lkm_parameters: Initializing Sandbox Parameters Module.\n");

if (number == 42 && strcmp(message, "That's a parameter.") == 0) {
printk(KERN_INFO
"Module was loaded with default parameters.\n");
"lkm_parameters: Module was loaded with default parameters.\n");

return 0;
}

printk(KERN_INFO
"Module was loaded with number = %d and message = %s\n",
"lkm_parameters: Module was loaded with number = %d and message = %s.\n",
number, message);

return 0;
}

static void __exit lkm_parameters_exit(void)
{
// nothing todo here...
// nothing todo here
}

module_init(lkm_parameters_init);
Expand Down
9 changes: 6 additions & 3 deletions lkm_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ struct proc_dir_entry *lkm_proc_entry;

static int __init lkm_proc_init(void)
{
printk(KERN_INFO "Initializing module for accessing /proc/%s...\n",
printk(KERN_INFO
"lkm_proc: Initializing module for accessing /proc/%s.\n",
LKM_PROC_FILE_NAME);
lkm_proc_entry =
proc_create_single(LKM_PROC_FILE_NAME, LKM_PROC_PERMISSION,
LKM_PROC_PARENT, lkm_proc_show);

if (lkm_proc_entry == NULL) {
printk(KERN_ALERT "Failed to create entry '%s' in /proc.",
printk(KERN_ALERT
"lkm_proc: Failed to create entry '%s' in /proc.\n",
LKM_PROC_FILE_NAME);
}

Expand All @@ -59,7 +61,8 @@ static int __init lkm_proc_init(void)

static void __exit lkm_proc_exit(void)
{
printk(KERN_INFO "Removing /proc/%s...\n", LKM_PROC_FILE_NAME);
printk(KERN_INFO "lkm_proc: Removing /proc/%s.\n",
LKM_PROC_FILE_NAME);

remove_proc_entry(LKM_PROC_FILE_NAME, LKM_PROC_PARENT);
}
Expand Down
5 changes: 3 additions & 2 deletions lkm_sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ MODULE_VERSION("0.1");

static int __init lkm_sandbox_init(void)
{
printk(KERN_INFO "Initializing and entering the sandbox...\n");
printk(KERN_INFO
"lkm_sandbox: Initializing and entering the sandbox.\n");

return 0;
}

static void __exit lkm_sandbox_exit(void)
{
printk(KERN_INFO "Exiting the sandbox...\n");
printk(KERN_INFO "lkm_sandbox: Exiting the sandbox.\n");
}

module_init(lkm_sandbox_init);
Expand Down

0 comments on commit c184877

Please sign in to comment.