Advertisement
Guest User

Kernel Pt.6 - FCM#93

a guest
Jan 16th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/seq_file.h>
  7. #include "internal.h"       // contains struct proc_dir_entry
  8. #include <asm/uaccess.h>    // contains copy_from_user
  9. #include <linux/utsname.h>
  10.  
  11. static struct proc_dir_entry *hostname_entry = NULL;
  12. #define MAX_BUFFER_SIZE 100
  13.  
  14. static ssize_t hostname_proc_write(struct file *file, const char __user *user_data, size_t len, loff_t *offset)
  15. {
  16.     int buffer_size = 0;
  17.     int i;
  18.     char buffer[MAX_BUFFER_SIZE];
  19.     printk(KERN_INFO "hostname_proc_write, len=%ld\n", len);
  20.     buffer_size = len > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : len;
  21.     i = 0;
  22.     buffer[0] = '\0';
  23.     while ((i < buffer_size) && (user_data[i] > ' ')) {
  24.         buffer[i] = user_data[i];
  25.         i++;
  26.     }
  27.     buffer[i] = '\0';
  28.     buffer_size = i+1;
  29.    
  30.     printk(KERN_INFO "wrote %d bytes\n", buffer_size);
  31.     printk(KERN_INFO "hostname=%s\n", buffer);
  32.    
  33.     if (buffer_size <= __NEW_UTS_LEN)
  34.         for (i = 0; i < buffer_size; i++)
  35.             utsname()->nodename[i] = buffer[i];
  36.    
  37.     return len;
  38. }
  39.  
  40. static int hostname_proc_show(struct seq_file *m, void *v)
  41. {
  42.     seq_printf(m, "system hostname is currently: %s\n", utsname()->nodename);
  43.     seq_printf(m, "write new name to this file to change hostname\n\n");
  44.     return 0;
  45. }
  46.  
  47. static int hostname_proc_open(struct inode *inode, struct file *file)
  48. {
  49.     return single_open(file, hostname_proc_show, NULL);
  50. }
  51.  
  52. static int hostname_proc_permission(struct inode *inode, int op)
  53. {
  54.     // allow access to everybody, for read (4) and write(2),
  55.     // but not execute (1)
  56.     return ((op == 4) || (op == 2));
  57. }
  58.  
  59. static const struct inode_operations hostname_proc_iops = {
  60.     .permission = hostname_proc_permission,
  61. };
  62.  
  63. static const struct file_operations hostname_proc_fops = {
  64.     .open       = hostname_proc_open,
  65.     .read       = seq_read,
  66.     .write      = hostname_proc_write,
  67.     .llseek     = seq_lseek,
  68.     .release    = single_release,
  69. };
  70.  
  71. static int __init hostname_proc_init(void)
  72. {  
  73.     printk(KERN_INFO "hostname loading\n");
  74.     hostname_entry = proc_create("hostname", 0666, NULL, &hostname_proc_fops);
  75.    
  76.     if (hostname_entry == NULL)
  77.         printk(KERN_INFO "hostname could not create /proc entry\n");
  78.     else {
  79.         hostname_entry->proc_iops = &hostname_proc_iops;
  80.         printk(KERN_INFO "hostname /proc entry created\n");
  81.     }
  82.    
  83.     return 0;
  84. }
  85.  
  86. static void __exit hostname_proc_exit(void)
  87. {
  88.     printk(KERN_INFO "hostname unloading\n");
  89.    
  90.     if (hostname_entry == NULL)
  91.         printk(KERN_INFO "hostname /proc entry does not exist, not removing\n");
  92.     else {
  93.         proc_remove(hostname_entry);
  94.         printk(KERN_INFO "hostname /proc entry removed\n");
  95.     }
  96. }
  97.  
  98. module_init(hostname_iproc_nit);
  99. module_exit(hostname_proc_exit);
  100.  
  101. MODULE_AUTHOR("Alan Ward");
  102. MODULE_LICENSE("GPL v2");
  103. MODULE_DESCRIPTION("hostname module for Full Circle Magazine");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement