#include #include /* For the tty declarations */ #include #include "../include/utils.h" #include "../include/message.h" /** * create_message() * * @buff: pointer to buffer containing string. * @len: length to read from buffer * @user: flag to check if user land buffer * * Populates a message struct with a given string */ message *create_message(const char *buff, size_t len, int user) { message *m; int ret; m = kmalloc(sizeof(message), GFP_KERNEL); if (!m) { printk("%s - [ERROR]: Failed to initialize message\n", MODNAME); return NULL; } if (!buff) { kfree(m); return NULL; } m->data = kmalloc(len, GFP_KERNEL); if (user) { // buff comes from user space ret = strncpy_from_user(m->data, buff, len); } else { // buff comes from kernel space if (strncpy(m->data, buff, len) == NULL) { printk("%s - [ERROR]: Could not copy string for new message\n", MODNAME); return 0; } } m->length = len; return m; } /** * create_message() * * @m: pointer to a message struct. * * Frees the memory associated with the message */ void destroy_message(message *m) { if (!m) { return; } if (!m->data) { kfree(m); return; } kfree(m->data); kfree(m); return; }