转载:一个简单的linux驱动示例

时间:2022-12-29 23:38:02

        本文参考百度文库“linux驱动开发入门”点击打开链接和转载博文:点击打开链接

一、基本知识        

        Linux设备驱动分为:字符设备、块设备和网络设备。原理图如下:

转载:一个简单的linux驱动示例



二、示例

示例主要转载自博客园的博客,见上。只是我采用的的Linux内核版本比那篇博文的新,有小许改动,粘贴代码如下:

内核版本:

ry@ubuntu:/$ uname -a
Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

文件:hello_mod.c

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  hello.c 
  5.  * 
  6.  *    Description:  hello_mod 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  01/28/2011 05:07:55 PM 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  Tishion (shion), tishion@163.com 
  14.  *        Company:  LIM 
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18.   
  19. #include <linux/module.h>  
  20. #include <linux/init.h>  
  21. #include <linux/kernel.h>  
  22. #include <linux/fs.h>  
  23. #include <linux/uaccess.h>  
  24. #include <linux/semaphore.h>  
  25. #include <linux/cdev.h>  
  26. #include <linux/device.h>  
  27. #include <linux/ioctl.h>  
  28. #include <linux/slab.h>  
  29. #include <linux/errno.h>  
  30. #include <linux/string.h>  
  31. #include “hello_mod_ioctl.h”  
  32.   
  33. #define MAJOR_NUM 250  
  34. #define MINOR_NUM 0  
  35. #define IN_BUF_LEN 256  
  36. #define OUT_BUF_LEN 512  
  37.   
  38. MODULE_AUTHOR(”Tishion”);  
  39. MODULE_DESCRIPTION(”Hello_mod driver by tishion”);  
  40.   
  41. static struct class * hello_class;  
  42. static struct cdev hello_cdev;  
  43. static dev_t devnum = 0;  
  44. static char * modname = “hello_mod”;  
  45. static char * devicename = “hello”;  
  46. static char * classname = “hello_class”;  
  47.   
  48. static int open_count = 0;  
  49. static struct semaphore sem;  
  50. static DEFINE_SPINLOCK(spin);  
  51. static char * inbuffer = NULL;  
  52. static char * outbuffer = NULL;  
  53. static lang_t langtype;  
  54.   
  55. static int hello_mod_open(struct inode *, struct file *);  
  56. static int hello_mod_release(struct inode *, struct file *);  
  57. static ssize_t hello_mod_read(struct file *, char *, size_t, loff_t *);  
  58. static ssize_t hello_mod_write(struct file *, const char *, size_t, loff_t *);  
  59. static long hello_mod_ioctl(struct file *, unsigned int, unsigned long);  
  60.   
  61. struct file_operations hello_mod_fops =   
  62. {  
  63.     .owner = THIS_MODULE,  
  64.     .open = hello_mod_open,  
  65.     .read = hello_mod_read,  
  66.     .write = hello_mod_write,  
  67.     .unlocked_ioctl = hello_mod_ioctl,  
  68.     .release = hello_mod_release,  
  69. };  
  70.   
  71. static int hello_mod_open(struct inode *inode, struct file *pfile)  
  72. {  
  73.     printk(”+hello_mod_open()!/n”);  
  74.     spin_lock(&spin);  
  75.     if(open_count)  
  76.     {  
  77.         spin_unlock(&spin);  
  78.         return -EBUSY;  
  79.     }  
  80.     open_count++;  
  81.     spin_unlock(&spin);  
  82.     printk(”-hello_mod_open()!/n”);  
  83.     return 0;  
  84. }  
  85. static int hello_mod_release(struct inode *inode, struct file *pfile)  
  86. {  
  87.     printk(”+hello_mod_release()!/n”);  
  88.     open_count–;  
  89.     printk(”-hello_mod_release()!/n”);  
  90.     return 0;  
  91. }  
  92. static ssize_t hello_mod_read(struct file *pfile, char *user_buf, size_t len, loff_t *off)  
  93. {  
  94.     printk(”+hello_mod_read()!/n”);  
  95.   
  96.     if(down_interruptible(&sem))  
  97.     {  
  98.         return -ERESTARTSYS;   
  99.     }  
  100.     memset(outbuffer, 0, OUT_BUF_LEN);  
  101.     printk(”    +switch()/n”);  
  102.     switch(langtype)  
  103.     {  
  104.         case english:  
  105.             printk(”        >in case: english/n”);  
  106.             sprintf(outbuffer, ”Hello! %s.”, inbuffer);  
  107.             break;  
  108.         case chinese:  
  109.             printk(”        >in case: chinese/n”);  
  110.             sprintf(outbuffer, ”你好! %s.”, inbuffer);  
  111.             break;  
  112.         case pinyin:  
  113.             printk(”        >in case: pinyin/n”);  
  114.             sprintf(outbuffer, ”ni hao! %s.”, inbuffer);  
  115.             break;  
  116.         default:  
  117.             printk(”        >in case: default/n”);  
  118.             break;  
  119.     }  
  120.     printk(”    -switch()/n”);  
  121.     if(copy_to_user(user_buf, outbuffer, len))  
  122.     {  
  123.         up(&sem);  
  124.         return -EFAULT;  
  125.     }  
  126.     up(&sem);  
  127.     printk(”-hello_mod_read()!/n”);  
  128.     return 0;  
  129. }  
  130. static ssize_t hello_mod_write(struct file *pfile, const char *user_buf, size_t len, loff_t *off)  
  131. {  
  132.     printk(”+hello_mod_write()!/n”);  
  133.     if(down_interruptible(&sem))  
  134.     {  
  135.         return -ERESTARTSYS;  
  136.     }  
  137.     if(len > IN_BUF_LEN)  
  138.     {  
  139.         printk(”Out of input buffer/n”);  
  140.         return -ERESTARTSYS;  
  141.     }  
  142.     if(copy_from_user(inbuffer, user_buf, len))  
  143.     {  
  144.         up(&sem);  
  145.         return -EFAULT;  
  146.     }  
  147.     up(&sem);      
  148.     printk(”-hello_mod_write()!/n”);  
  149.     return 0;  
  150. }  
  151. static long hello_mod_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)  
  152. {  
  153.     int err = 0;  
  154.     printk(”+hello_mod_ioctl()!/n”);  
  155.     printk(”    +switch()/n”);  
  156.     switch(cmd)  
  157.     {  
  158.         case HELLO_IOCTL_RESETLANG:  
  159.             printk(”        >in case: HELLO_IOCTL_RESETLANG/n”);  
  160.             langtype = english;  
  161.             break;  
  162.         case HELLO_IOCTL_GETLANG:  
  163.             printk(”        >in case: HELLO_IOCTL_GETLANG/n”);  
  164.             err = copy_to_user((int *)arg, &langtype, sizeof(int));  
  165.             break;  
  166.         case HELLO_IOCTL_SETLANG:  
  167.             printk(”        >in case: HELLO_IOCTL_SETLANG/n”);  
  168.             err = copy_from_user(&langtype,(int *)arg, sizeof(int));  
  169.             break;  
  170.         default:  
  171.             printk(”        >in case: default/n”);  
  172.             err = ENOTSUPP;  
  173.             break;  
  174.     }  
  175.     printk(”    -switch()/n”);  
  176.     printk(”-hello_mod_ioctl()!/n”);  
  177.     return err;  
  178. }  
  179. static int __init hello_mod_init(void)  
  180. {  
  181.     int result;  
  182.     printk(”+hello_mod_init()!/n”);  
  183.     devnum = MKDEV(MAJOR_NUM, MINOR_NUM);  
  184.     result = register_chrdev_region(devnum, 1, modname);  
  185.   
  186.     if(result < 0)  
  187.     {  
  188.         printk(”hello_mod : can’t get major number!/n”);  
  189.         return result;  
  190.     }      
  191.   
  192.     cdev_init(&hello_cdev, &hello_mod_fops);  
  193.     hello_cdev.owner = THIS_MODULE;  
  194.     hello_cdev.ops = &hello_mod_fops;  
  195.     result = cdev_add(&hello_cdev, devnum, 1);  
  196.     if(result)  
  197.         printk(”Failed at cdev_add()”);  
  198.     hello_class = class_create(THIS_MODULE, classname);  
  199.     if(IS_ERR(hello_class))  
  200.     {  
  201.         printk(”Failed at class_create().Please exec [mknod] before operate the device/n”);  
  202.     }  
  203.     else  
  204.     {  
  205.         device_create(hello_class, NULL, devnum,NULL, devicename);  
  206.     }  
  207.   
  208.     open_count = 0;  
  209.     langtype = english;  
  210.     inbuffer = (char *)kmalloc(IN_BUF_LEN, GFP_KERNEL);  
  211.     outbuffer = (char *)kmalloc(OUT_BUF_LEN, GFP_KERNEL);  
  212.     sema_init(&sem, 1);  
  213.     printk(”-hello_mod_init()!/n”);  
  214.     return 0;  
  215. }  
  216.   
  217. static void __exit hello_mod_exit(void)  
  218. {  
  219.     printk(”+hello_mod_exit!/n”);  
  220.     kfree(inbuffer);  
  221.     kfree(outbuffer);  
  222.     cdev_del(&hello_cdev);  
  223.     device_destroy(hello_class, devnum);  
  224.     class_destroy(hello_class);  
  225.     unregister_chrdev_region(devnum, 1);  
  226.     printk(”-hello_mod_exit!/n”);  
  227.     return ;  
  228. }  
  229.   
  230. module_init(hello_mod_init);  
  231. module_exit(hello_mod_exit);  
  232. MODULE_LICENSE(”GPL”);  
/*
 * =====================================================================================
 *
 *       Filename:  hello.c
 *
 *    Description:  hello_mod
 *
 *        Version:  1.0
 *        Created:  01/28/2011 05:07:55 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Tishion (shion), tishion@163.com
 *        Company:  LIM
 *
 * =====================================================================================
 */





include <linux/module.h>

include <linux/init.h>

include <linux/kernel.h>

include <linux/fs.h>

include <linux/uaccess.h>

include <linux/semaphore.h>

include <linux/cdev.h>

include <linux/device.h>

include <linux/ioctl.h>

include <linux/slab.h>

include <linux/errno.h>

include <linux/string.h>

include "hello_mod_ioctl.h"

define MAJOR_NUM 250

define MINOR_NUM 0

define IN_BUF_LEN 256

define OUT_BUF_LEN 512

MODULE_AUTHOR("Tishion");
MODULE_DESCRIPTION("Hello_mod driver by tishion");

static struct class * hello_class;
static struct cdev hello_cdev;
static dev_t devnum = 0;
static char * modname = "hello_mod";
static char * devicename = "hello";
static char * classname = "hello_class";

static int open_count = 0;
static struct semaphore sem;
static DEFINE_SPINLOCK(spin);
static char * inbuffer = NULL;
static char * outbuffer = NULL;
static lang_t langtype;

static int hello_mod_open(struct inode , struct file );
static int hello_mod_release(struct inode , struct file );
static ssize_t hello_mod_read(struct file , char , size_t, loff_t *);
static ssize_t hello_mod_write(struct file , const char , size_t, loff_t *);
static long hello_mod_ioctl(struct file *, unsigned int, unsigned long);

struct file_operations hello_mod_fops =
{
.owner = THIS_MODULE,
.open = hello_mod_open,
.read = hello_mod_read,
.write = hello_mod_write,
.unlocked_ioctl = hello_mod_ioctl,
.release = hello_mod_release,
};

static int hello_mod_open(struct inode *inode, struct file *pfile)
{
printk("+hello_mod_open()!/n");
spin_lock(&spin);
if(open_count)
{
spin_unlock(&spin);
return -EBUSY;
}
open_count++;
spin_unlock(&spin);
printk("-hello_mod_open()!/n");
return 0;
}
static int hello_mod_release(struct inode *inode, struct file *pfile)
{
printk("+hello_mod_release()!/n");
open_count--;
printk("-hello_mod_release()!/n");
return 0;
}
static ssize_t hello_mod_read(struct file *pfile, char *user_buf, size_t len, loff_t *off)
{
printk("+hello_mod_read()!/n");

if(down_interruptible(&amp;sem))
{
    return -ERESTARTSYS; 
}
memset(outbuffer, 0, OUT_BUF_LEN);
printk("    +switch()/n");
switch(langtype)
{
    case english:
        printk("        &gt;in case: english/n");
        sprintf(outbuffer, "Hello! %s.", inbuffer);
        break;
    case chinese:
        printk("        &gt;in case: chinese/n");
        sprintf(outbuffer, "你好! %s.", inbuffer);
        break;
    case pinyin:
        printk("        &gt;in case: pinyin/n");
        sprintf(outbuffer, "ni hao! %s.", inbuffer);
        break;
    default:
        printk("        &gt;in case: default/n");
        break;
}
printk("    -switch()/n");
if(copy_to_user(user_buf, outbuffer, len))
{
    up(&amp;sem);
    return -EFAULT;
}
up(&amp;sem);
printk("-hello_mod_read()!/n");
return 0;

}
static ssize_t hello_mod_write(struct file *pfile, const char *user_buf, size_t len, loff_t *off)
{
printk(“+hello_mod_write()!/n”);
if(down_interruptible(&sem))
{
return -ERESTARTSYS;
}
if(len > IN_BUF_LEN)
{
printk(“Out of input buffer/n”);
return -ERESTARTSYS;
}
if(copy_from_user(inbuffer, user_buf, len))
{
up(&sem);
return -EFAULT;
}
up(&sem);
printk(“-hello_mod_write()!/n”);
return 0;
}
static long hello_mod_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
int err = 0;
printk(“+hello_mod_ioctl()!/n”);
printk(” +switch()/n”);
switch(cmd)
{
case HELLO_IOCTL_RESETLANG:
printk(” >in case: HELLO_IOCTL_RESETLANG/n”);
langtype = english;
break;
case HELLO_IOCTL_GETLANG:
printk(” >in case: HELLO_IOCTL_GETLANG/n”);
err = copy_to_user((int *)arg, &langtype, sizeof(int));
break;
case HELLO_IOCTL_SETLANG:
printk(” >in case: HELLO_IOCTL_SETLANG/n”);
err = copy_from_user(&langtype,(int *)arg, sizeof(int));
break;
default:
printk(” >in case: default/n”);
err = ENOTSUPP;
break;
}
printk(” -switch()/n”);
printk(“-hello_mod_ioctl()!/n”);
return err;
}
static int __init hello_mod_init(void)
{
int result;
printk(“+hello_mod_init()!/n”);
devnum = MKDEV(MAJOR_NUM, MINOR_NUM);
result = register_chrdev_region(devnum, 1, modname);

if(result &lt; 0)
{
    printk("hello_mod : can't get major number!/n");
    return result;
}    

cdev_init(&amp;hello_cdev, &amp;hello_mod_fops);
hello_cdev.owner = THIS_MODULE;
hello_cdev.ops = &amp;hello_mod_fops;
result = cdev_add(&amp;hello_cdev, devnum, 1);
if(result)
    printk("Failed at cdev_add()");
hello_class = class_create(THIS_MODULE, classname);
if(IS_ERR(hello_class))
{
    printk("Failed at class_create().Please exec [mknod] before operate the device/n");
}
else
{
    device_create(hello_class, NULL, devnum,NULL, devicename);
}

open_count = 0;
langtype = english;
inbuffer = (char *)kmalloc(IN_BUF_LEN, GFP_KERNEL);
outbuffer = (char *)kmalloc(OUT_BUF_LEN, GFP_KERNEL);
sema_init(&amp;sem, 1);
printk("-hello_mod_init()!/n");
return 0;

}

static void __exit hello_mod_exit(void)
{
printk(“+hello_mod_exit!/n”);
kfree(inbuffer);
kfree(outbuffer);
cdev_del(&hello_cdev);
device_destroy(hello_class, devnum);
class_destroy(hello_class);
unregister_chrdev_region(devnum, 1);
printk(“-hello_mod_exit!/n”);
return ;
}

module_init(hello_mod_init);
module_exit(hello_mod_exit);
MODULE_LICENSE(“GPL”);

头文件:hello_mod_ioctl.h

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  hello_mod_ioctl.h 
  5.  * 
  6.  *    Description:  define the cmd supported by hello_mod 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  06/19/2011 10:24:20 PM 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  Tishion (shion), tishion@163.com 
  14.  *        Company:  LIM 
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18.   
  19. #ifndef __HELLO_MOD_IOCTL_H__  
  20. #define __HELLO_MOD_IOCTL_H__  
  21.   
  22. #define HELLO_MAGIC    12  
  23. #define HELLO_IOCTL_RESETLANG    _IO(HELLO_MAGIC,0)        //set langtype = english  
  24. #define HELLO_IOCTL_GETLANG        _IOR(HELLO_MAGIC,1,int)    //get langtype  
  25. #define HELLO_IOCTL_SETLANG        _IOW(HELLO_MAGIC,2,int)    //set langtype  
  26.   
  27. typedef enum _lang_t  
  28. {  
  29.     english, chinese, pinyin  
  30. }lang_t;  
  31.   
  32. #endif  
/*
 * =====================================================================================
 *
 *       Filename:  hello_mod_ioctl.h
 *
 *    Description:  define the cmd supported by hello_mod
 *
 *        Version:  1.0
 *        Created:  06/19/2011 10:24:20 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Tishion (shion), tishion@163.com
 *        Company:  LIM
 *
 * =====================================================================================
 */





#ifndef __HELLO_MOD_IOCTL_H__ #define __HELLO_MOD_IOCTL_H__ #define HELLO_MAGIC 12 #define HELLO_IOCTL_RESETLANG _IO(HELLO_MAGIC,0) //set langtype = english #define HELLO_IOCTL_GETLANG _IOR(HELLO_MAGIC,1,int) //get langtype #define HELLO_IOCTL_SETLANG _IOW(HELLO_MAGIC,2,int) //set langtype typedef enum _lang_t { english, chinese, pinyin }lang_t; #endif
Makefile文件:(注意:Makefile的’M’要大写)

  1. #**********************************************  
  2. # Makefile linux 2.6 Module   
  3. # This makefile is written for Ubuntu 10.10  
  4. # It may not perfomance without erros on the  
  5. # other version or distributions.  
  6. #**********************************************  
  7. #    BY:tishion  
  8. #    Mail:tishion@163.com  
  9. #    2011/06/19  
  10. #**********************************************  
  11. obj-m += hello_mod.o  
  12. CURRENT_PATH :=  (shell&nbsp;pwd)&nbsp;&nbsp;</span></li><li class="alt"><span>LINUX_KERNEL&nbsp;:=&nbsp;
  13. LINUX_KERNEL := (shell uname -r)  
  14. LINUX_KERNEL_PATH := /usr/src/linux-headers- (LINUX_KERNEL)&nbsp;&nbsp;</span></li><li class="alt"><span>all:&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;make&nbsp;-C&nbsp;
  15. all:  
  16.     make -C (LINUX_KERNEL_PATH) M= (CURRENT_PATH)&nbsp;modules&nbsp;&nbsp;</span></li><li class="alt"><span>clean:&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;make&nbsp;-C&nbsp;
  17. clean:  
  18.     make -C (LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean  
  19. install:  
  20.     insmod hello_mod.ko  
  21. unistall:  
  22.     rmmod hello_mod  
#**********************************************




# Makefile linux 2.6 Module # This makefile is written for Ubuntu 10.10 # It may not perfomance without erros on the # other version or distributions. #********************************************** # BY:tishion # Mail:tishion@163.com # 2011/06/19 #********************************************** obj-m += hello_mod.o CURRENT_PATH := ( s h e l l p w d ) L I N U X K E R N E L := (shell uname -r) LINUX_KERNEL_PATH := /usr/src/linux-headers- ( L I N U X K E R N E L ) a l l : m a k e C (LINUX_KERNEL_PATH) M= ( C U R R E N T P A T H ) m o d u l e s c l e a n : m a k e C (LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean install: insmod hello_mod.ko unistall: rmmod hello_mod
以上三个文件即为驱动代码

另外,再给一个应用层测试代码,并共用驱动的头文件

hello_mod_test.c文件:

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  hell_mod_test.c 
  5.  * 
  6.  *    Description:  hell_mod test app 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  06/20/2011 01:44:11 AM 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  Tishion (shion), tishion@163.com 
  14.  *        Company:  LIM 
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18.   
  19. #include <stdio.h>  
  20. #include <sys/types.h>  
  21. #include <sys/stat.h>  
  22. #include <fcntl.h>  
  23. #include <unistd.h>  
  24. #include <linux/ioctl.h>  
  25. #include <string.h>  
  26. #include <errno.h>  
  27. #include ”../hello_mod_ioctl.h”  
  28.   
  29. int main()  
  30. {  
  31.     char outbuf[512];  
  32.     char * myname = “tishion”;  
  33.     lang_t langtype = english;  
  34.     int fd = open(“/dev/hello”, O_RDWR, S_IRUSR|S_IWUSR);  
  35.     if(fd != -1)  
  36.     {  
  37.         write(fd, myname, strlen(myname)+1);  
  38.         langtype = chinese;  
  39.         ioctl(fd, HELLO_IOCTL_SETLANG, &langtype);  
  40.         read(fd, outbuf, 512);  
  41.         printf(”langtype=chinese:%s/n”, outbuf);  
  42.         memset(outbuf, 0, 512);  
  43.         langtype = pinyin;  
  44.         ioctl(fd, HELLO_IOCTL_SETLANG, &langtype);  
  45.         read(fd, outbuf, 512);      
  46.         printf(”langtype=pinyin:%s/n”, outbuf);  
  47.         memset(outbuf, 0, 512);  
  48.         ioctl(fd, HELLO_IOCTL_RESETLANG, &langtype);      
  49.         read(fd, outbuf, 512);  
  50.         printf(”langtype=english:%s/n”, outbuf);  
  51.     }  
  52.     else  
  53.     {  
  54.         perror(”Failed at open():”);  
  55.     }  
  56.     return 0;  
  57. }  
/*
 * =====================================================================================
 *
 *       Filename:  hell_mod_test.c
 *
 *    Description:  hell_mod test app
 *
 *        Version:  1.0
 *        Created:  06/20/2011 01:44:11 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Tishion (shion), tishion@163.com
 *        Company:  LIM
 *
 * =====================================================================================
 */





#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <linux/ioctl.h> #include <string.h> #include <errno.h>

include “../hello_mod_ioctl.h”

int main()
{
char outbuf[512];
char * myname = “tishion”;
lang_t langtype = english;
int fd = open(“/dev/hello”, O_RDWR, S_IRUSR|S_IWUSR);
if(fd != -1)
{
write(fd, myname, strlen(myname)+1);
langtype = chinese;
ioctl(fd, HELLO_IOCTL_SETLANG, &langtype);
read(fd, outbuf, 512);
printf(“langtype=chinese:%s/n”, outbuf);
memset(outbuf, 0, 512);
langtype = pinyin;
ioctl(fd, HELLO_IOCTL_SETLANG, &langtype);
read(fd, outbuf, 512);
printf(“langtype=pinyin:%s/n”, outbuf);
memset(outbuf, 0, 512);
ioctl(fd, HELLO_IOCTL_RESETLANG, &langtype);
read(fd, outbuf, 512);
printf(“langtype=english:%s/n”, outbuf);
}
else
{
perror(“Failed at open():”);
}
return 0;
}
最后,安装原博客编译和查看结果。

需要注意的是,需要到“/var/log/”目录下着“syslog”文件。


        </div>
            </div>

转载:https://blog.csdn.net/sagittarius_warrior/article/details/51067518