Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include "internal.h" // contains struct proc_dir_entry
- #include <asm/uaccess.h> // contains copy_from_user
- #include <linux/utsname.h>
- static struct proc_dir_entry *hostname_entry = NULL;
- #define MAX_BUFFER_SIZE 100
- static ssize_t hostname_proc_write(struct file *file, const char __user *user_data, size_t len, loff_t *offset)
- {
- int buffer_size = 0;
- int i;
- char buffer[MAX_BUFFER_SIZE];
- printk(KERN_INFO "hostname_proc_write, len=%ld\n", len);
- buffer_size = len > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : len;
- i = 0;
- buffer[0] = '\0';
- while ((i < buffer_size) && (user_data[i] > ' ')) {
- buffer[i] = user_data[i];
- i++;
- }
- buffer[i] = '\0';
- buffer_size = i+1;
- printk(KERN_INFO "wrote %d bytes\n", buffer_size);
- printk(KERN_INFO "hostname=%s\n", buffer);
- if (buffer_size <= __NEW_UTS_LEN)
- for (i = 0; i < buffer_size; i++)
- utsname()->nodename[i] = buffer[i];
- return len;
- }
- static int hostname_proc_show(struct seq_file *m, void *v)
- {
- seq_printf(m, "system hostname is currently: %s\n", utsname()->nodename);
- seq_printf(m, "write new name to this file to change hostname\n\n");
- return 0;
- }
- static int hostname_proc_open(struct inode *inode, struct file *file)
- {
- return single_open(file, hostname_proc_show, NULL);
- }
- static int hostname_proc_permission(struct inode *inode, int op)
- {
- // allow access to everybody, for read (4) and write(2),
- // but not execute (1)
- return ((op == 4) || (op == 2));
- }
- static const struct inode_operations hostname_proc_iops = {
- .permission = hostname_proc_permission,
- };
- static const struct file_operations hostname_proc_fops = {
- .open = hostname_proc_open,
- .read = seq_read,
- .write = hostname_proc_write,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static int __init hostname_proc_init(void)
- {
- printk(KERN_INFO "hostname loading\n");
- hostname_entry = proc_create("hostname", 0666, NULL, &hostname_proc_fops);
- if (hostname_entry == NULL)
- printk(KERN_INFO "hostname could not create /proc entry\n");
- else {
- hostname_entry->proc_iops = &hostname_proc_iops;
- printk(KERN_INFO "hostname /proc entry created\n");
- }
- return 0;
- }
- static void __exit hostname_proc_exit(void)
- {
- printk(KERN_INFO "hostname unloading\n");
- if (hostname_entry == NULL)
- printk(KERN_INFO "hostname /proc entry does not exist, not removing\n");
- else {
- proc_remove(hostname_entry);
- printk(KERN_INFO "hostname /proc entry removed\n");
- }
- }
- module_init(hostname_iproc_nit);
- module_exit(hostname_proc_exit);
- MODULE_AUTHOR("Alan Ward");
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("hostname module for Full Circle Magazine");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement