SHARE
TWEET

Untitled

a guest Mar 19th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. logger "Hello"
  2.      
  3. dmesg
  4.      
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7.  
  8. int init_module(void)
  9. {
  10.     printk(KERN_INFO "Hello worldn");
  11.     return 0;
  12. }
  13.  
  14. void cleanup_module(void)
  15. {
  16.     printk(KERN_INFO "Goodbye worldn");
  17.  
  18. }
  19.      
  20. obj-m += hello.o
  21.  
  22. all:
  23.     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
  24.      
  25. $ make
  26. $ sudo insmod hello.ko
  27. $ dmesg | tail -n1
  28.  [7089996.746366] Hello world
  29.      
  30. fixnum:~# echo Some message > /dev/kmsg
  31.  fixnum:~# dmesg | tail -n1
  32.  [28078118.692242] Some message
  33.      
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/init.h>
  37. #include <linux/proc_fs.h>
  38. #include <asm/uaccess.h>
  39.  
  40. static int pk_write(struct file *file, const char *buffer, unsigned long count, void *data)
  41. {
  42.         char string[256];
  43.         count = count < 255 ? count : 255;
  44.  
  45.         if(copy_from_user(string, buffer, count))
  46.                 return -EFAULT;
  47.  
  48.         string[count] = '';        
  49.         printk(string);
  50.         return count;
  51. }
  52.  
  53.  
  54. static int __init printk_init(void)
  55. {
  56.         struct proc_dir_entry *pk_file;
  57.  
  58.         pk_file = create_proc_entry("printk", 0222, NULL);
  59.         if(pk_file == NULL)
  60.                 return -ENOMEM;
  61.  
  62.         pk_file->write_proc = pk_write;
  63.         pk_file->owner = THIS_MODULE;
  64.  
  65.         return 0;
  66. }
  67.  
  68. static void __exit printk_cleanup(void)
  69. {
  70.         remove_proc_entry("printk", NULL);
  71. }
  72.  
  73. module_init(printk_init);
  74. module_exit(printk_cleanup);
  75. MODULE_LICENSE("GPL");
  76.      
  77. echo "Hello" > /proc/printk
  78.      
  79. #include <linux/module.h>
  80. #include <linux/kernel.h>
  81. #include <linux/proc_fs.h>
  82. #include <asm/uaccess.h>
  83.  
  84. static ssize_t write_proc(struct file *filep, const char *buffer, size_t count, loff_t *offsetp)
  85. {
  86.     char string[256];
  87.     count = count < 255 ? count : 255;
  88.  
  89.     if(copy_from_user(string, buffer, count) != 0) {
  90.         return -EFAULT;
  91.     }
  92.  
  93.     string[count] = '';
  94.     printk(string);
  95.     return count;
  96. }
  97.  
  98. static const struct file_operations proc_fops = {
  99.     .owner = THIS_MODULE,
  100.     .write = write_proc,
  101. };
  102.  
  103. static int proc_init(void) {
  104.     struct proc_dir_entry *proc_file;
  105.     proc_file = proc_create("printk_user", 0, NULL, &proc_fops);
  106.  
  107.     if(proc_file == NULL) {
  108.         return -ENOMEM;
  109.     }
  110.  
  111.     return 0;
  112. }
  113.  
  114. static void proc_cleanup(void) {
  115.     remove_proc_entry("printk_user", NULL);
  116. }
  117.  
  118. MODULE_LICENSE("GPL");
  119. module_init(proc_init);
  120. module_exit(proc_cleanup);
  121.      
  122. TARGET = printk_user
  123. obj-m := $(TARGET).o
  124.  
  125. KERNEL_VERSION=$(shell uname -r)
  126. KDIR = /lib/modules/$(KERNEL_VERSION)/build
  127. PWD = $(shell pwd)
  128.  
  129. printk:
  130.     $(MAKE) -C $(KDIR) M=$(PWD) modules
  131.  
  132. clean:
  133.     $(MAKE) -C $(KDIR) M=$(PWD) clean
  134.      
  135. void dmesg( const char *tag, const char *msg, const int len )
  136. {
  137.     const int TAG_LEN=3;
  138.     char buffer[128]={0};
  139.     memcpy( &buffer[0], tag, TAG_LEN );
  140.     memcpy( &buffer[TAG_LEN], msg, len );
  141.     int fd_kmsg = open( "/dev/kmsg", O_WRONLY );
  142.     write( fd_kmsg, &buffer, TAG_LEN+len );
  143.     close( fd_kmsg );
  144. }
  145. void dmesgWarn(  const char *msg, const int len ){ dmesg( "<4>", msg, len ); }
  146. void dmesgInfo(  const char *msg, const int len ){ dmesg( "<6>", msg, len ); }
  147. void dmesgDebug( const char *msg, const int len ){ dmesg( "<7>", msg, len ); }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top