Guest User

Untitled

a guest
Oct 9th, 2013
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Makefile:
  2. obj-m += utsname.o
  3.  
  4. MODULE_DIR := /lib/modules/$(shell uname -r)
  5.  
  6. all:
  7.     make -C $(MODULE_DIR)/build M=$(PWD) modules
  8.  
  9. clean:
  10.     make -C $(MODULE_DIR)/build M=$(PWD) clean
  11.  
  12. install: utsname.ko
  13.     install -c -D -m 644 utsname.ko $(MODULE_DIR)/misc
  14.     depmod -a
  15.  
  16. uninstall:
  17.     rm -f $(MODULE_DIR)/misc/utsname.ko
  18.     depmod -a
  19. */
  20.  
  21. /* utsname.c */
  22.  
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/utsname.h>
  26. #include <linux/init.h>
  27.  
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("logan");
  30. MODULE_DESCRIPTION("A utsname setting module");
  31.  
  32. static int verbose;
  33.  
  34. #define _UTSNAME_LEN __NEW_UTS_LEN
  35.  
  36. static char old_sysname[_UTSNAME_LEN + 1], *sysname;
  37. static char old_release[_UTSNAME_LEN + 1], *release;
  38. static char old_version[_UTSNAME_LEN + 1], *version;
  39.  
  40. #define utsname_copy(dst, src) \
  41.   (strncpy(dst, src, _UTSNAME_LEN))
  42.  
  43. #define utsname_default(str) \
  44.   (!strcmp(str, "default") || !strcmp(str, "reset") || !strcmp(str, "none"))
  45.  
  46. static void utsname_strip_newline(char *str)
  47. {
  48.   char *cp = strrchr(str, '\n');
  49.  
  50.   if (cp && !cp[1]) {
  51.     *cp = 0;
  52.   }
  53. }
  54.  
  55. static int utsname_set_sysname(const char *val, struct kernel_param *kp)
  56. {
  57.   char str[_UTSNAME_LEN + 1];
  58.   utsname_copy(str, val);
  59.   utsname_strip_newline(str);
  60.  
  61.   if (!sysname) {
  62.     sysname = init_uts_ns.name.sysname;
  63.     utsname_copy(old_sysname, sysname);
  64.   }
  65.  
  66.   if (utsname_default(str)) {
  67.     utsname_copy(str, old_sysname);
  68.   }
  69.  
  70.   if (*str && strcmp(sysname, str)) {
  71.     utsname_copy(sysname, str);
  72.  
  73.     if (verbose) {
  74.       printk(KERN_INFO "utsname.sysname changed to '%s'\n", sysname);
  75.     }
  76.   }
  77.  
  78.   return 0;
  79. }
  80.  
  81. static int utsname_get_sysname(char *buffer, struct kernel_param *kp)
  82. {
  83.   return sprintf(buffer, sysname);
  84. }
  85.  
  86. static int utsname_set_release(const char *val, struct kernel_param *kp)
  87. {
  88.   char str[_UTSNAME_LEN + 1];
  89.   utsname_copy(str, val);
  90.   utsname_strip_newline(str);
  91.  
  92.   if (!release) {
  93.     release = init_uts_ns.name.release;
  94.     utsname_copy(old_release, release);
  95.   }
  96.  
  97.   if (utsname_default(str)) {
  98.     utsname_copy(str, old_release);
  99.   }
  100.  
  101.   if (*str && strcmp(release, str)) {
  102.     utsname_copy(release, str);
  103.  
  104.     if (verbose) {
  105.       printk(KERN_INFO "utsname.release changed to '%s'\n", release);
  106.     }
  107.   }
  108.  
  109.   return 0;
  110. }
  111.  
  112. static int utsname_get_release(char *buffer, struct kernel_param *kp)
  113. {
  114.   return sprintf(buffer, release);
  115. }
  116.  
  117. static int utsname_set_version(const char *val, struct kernel_param *kp)
  118. {
  119.   char str[_UTSNAME_LEN + 1];
  120.   utsname_copy(str, val);
  121.   utsname_strip_newline(str);
  122.  
  123.   if (!version) {
  124.     version = init_uts_ns.name.version;
  125.     utsname_copy(old_version, version);
  126.   }
  127.  
  128.   if (utsname_default(str)) {
  129.     utsname_copy(str, old_version);
  130.   }
  131.  
  132.   if (*str && strcmp(version, str)) {
  133.     utsname_copy(version, str);
  134.  
  135.     if (verbose) {
  136.       printk(KERN_INFO "utsname.version changed to '%s'\n", version);
  137.     }
  138.   }
  139.  
  140.   return 0;
  141. }
  142.  
  143. static int utsname_get_version(char *buffer, struct kernel_param *kp)
  144. {
  145.   return sprintf(buffer, version);
  146. }
  147.  
  148. static int __init utsname_init(void)
  149. {
  150.   if (!sysname) {
  151.     sysname = init_uts_ns.name.sysname;
  152.     utsname_copy(old_sysname, sysname);
  153.   }
  154.  
  155.   if (!release) {
  156.     release = init_uts_ns.name.release;
  157.     utsname_copy(old_release, release);
  158.   }
  159.  
  160.   if (!version) {
  161.     version = init_uts_ns.name.version;
  162.     utsname_copy(old_version, version);
  163.   }
  164.  
  165.   if (verbose) {
  166.     printk(KERN_INFO "utsname loaded\n");
  167.   }
  168.  
  169.   return 0;
  170. }
  171.  
  172. static void __exit utsname_cleanup(void)
  173. {
  174.   sysname = init_uts_ns.name.sysname;
  175.   release = init_uts_ns.name.release;
  176.   version = init_uts_ns.name.version;
  177.  
  178.   if (strcmp(old_sysname, sysname)) {
  179.     utsname_copy(sysname, old_sysname);
  180.  
  181.     if (verbose) {
  182.       printk(KERN_INFO "utsname.sysname reset to '%s'\n", sysname);
  183.     }
  184.   }
  185.  
  186.   if (strcmp(old_release, release)) {
  187.     utsname_copy(release, old_release);
  188.  
  189.     if (verbose) {
  190.       printk(KERN_INFO "utsname.release reset to '%s'\n", release);
  191.     }
  192.   }
  193.  
  194.   if (strcmp(old_version, version)) {
  195.     utsname_copy(version, old_version);
  196.  
  197.     if (verbose) {
  198.       printk(KERN_INFO "utsname.version reset to '%s'\n", version);
  199.     }
  200.   }
  201.  
  202.   if (verbose) {
  203.     printk(KERN_INFO "utsname unloaded\n");
  204.   }
  205. }
  206.  
  207. MODULE_PARM_DESC(verbose, "Verbose mode");
  208. module_param(verbose, int, S_IRUGO|S_IWUSR);
  209.  
  210. MODULE_PARM_DESC(sysname, "Set utsname.sysname");
  211. module_param_call(sysname,
  212.                   utsname_set_sysname,
  213.                   utsname_get_sysname,
  214.                   NULL,
  215.                   S_IRUGO|S_IWUSR);
  216.  
  217. MODULE_PARM_DESC(release, "Set utsname.release");
  218. module_param_call(release,
  219.                   utsname_set_release,
  220.                   utsname_get_release,
  221.                   NULL,
  222.                   S_IRUGO|S_IWUSR);
  223.  
  224. MODULE_PARM_DESC(version, "Set utsname.version");
  225. module_param_call(version,
  226.                   utsname_set_version,
  227.                   utsname_get_version,
  228.                   NULL,
  229.                   S_IRUGO|S_IWUSR);
  230.  
  231. module_init(utsname_init);
  232. module_exit(utsname_cleanup);
RAW Paste Data