Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more comments #2

Merged
merged 5 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion kudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ int main(int argc, char **argv){

FILE *fptr;

// Open a file in writing mode
// Open the file in writing mode
fptr = fopen("/proc/kudo_interface", "w");

// Make sure the file opened
if (fptr == NULL){
fprintf(stderr, "Insufficient Permissions.\n");
return 1;
}

// Write all arguments to the file
for (int i = 1; i < argc; i++){
fprintf(fptr, "%s", argv[i]);
fprintf(fptr, " ");
Expand Down
10 changes: 10 additions & 0 deletions kudo_mod.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
// Includes
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>

#include <linux/proc_fs.h>
#include <linux/vmalloc.h>

// Predeclares
ssize_t kudo_read(struct file *file, char __user *user, size_t size, loff_t *off);
ssize_t kudo_write(struct file *file, const char __user *user, size_t size, loff_t *off);

// The Global Proc File Reference
static struct proc_dir_entry *kudo_proc = NULL;

// On Read
ssize_t kudo_read(struct file *file, char __user *user, size_t size, loff_t *off)
{
return 0;
}

// On Write
ssize_t kudo_write(struct file *file, const char __user *user, size_t size, loff_t *off)
{
char *user_buffer;
short use_multipage = 0;
// Whether to use k or vmalloc
if (size > PAGE_SIZE) {
use_multipage = 1;
user_buffer = vmalloc(size);
}else{
user_buffer = kmalloc(size, GFP_KERNEL);
}

// Memory allocation fails
if (user_buffer == NULL){
printk(KERN_ERR "Failed to allocate %lu bytes of memory.", size);
return size;
Expand Down Expand Up @@ -63,6 +70,7 @@ static const struct proc_ops kudo_proc_fops =
.proc_write = kudo_write,
};

// On Load
static int __init kudo_init(void)
{
printk(KERN_INFO "Kudo Module Loaded\n");
Expand All @@ -75,12 +83,14 @@ static int __init kudo_init(void)
return 0;
}

// On Unload
static void __exit kudo_exit(void)
{
printk(KERN_INFO "Kudo Module Unloaded\n");
proc_remove(kudo_proc);
}

// Metadata
module_init(kudo_init);
module_exit(kudo_exit);

Expand Down