Linux3.10核心之後proc檔案系統的使用
最近在通過無線掃描周邊的手機mac的事情,拿到AP周邊的mac後需要送到應用層,之前接觸的都是RTOS的系統,資料互動比較簡單,Linux下應用層無法直接和驅動交換資料,需要通過kernel提供的一些通道,因為只是嚮應用層送資料,和應用層的互動比較簡單,所以嘗試採用proc檔案系統的方式。
開始找到一些列子,編寫好程式以後編譯移植提示creat_proc_entry介面找不到,感覺很奇怪,直接去翻看對應的標頭檔案linux/proc_fs.h,發現果然沒有,原來3.10核心的改動非常之大,很多介面被替換或者去掉了,其中就有creat_proc_entry這個介面。3.10之後推薦使用的是proc_create介面。下面是一個小例子:
#define PROCREG_DIR "myownproc"
struct proc_dir_entry *procRegDir;
static const struct file_operations maclist_proc_fops = {
.owner = THIS_MODULE,
.open = maclist_proc_open,
.write = maclist_proc_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
#define MAX_MACLIST_LENGTH 1024
static struct proc_dir_entry *entry_wl_beacon_mac;
static char *maclistbuffer;
static int mac_index;
static int mac_next = 0;
static char fifo_from_user = 0;
UCHAR GLOBAL_AddrLocalNum = 0;
UCHAR GLOBAL_AddrLocal[MAX_MCAST_LIST_SIZE][6];
UCHAR GLOBAL_AddrLocalNum1 = 0;
UCHAR GLOBAL_AddrLocal1[MAX_MCAST_LIST_SIZE][6];
static int maclist_proc_show(struct seq_file *m, void *v)
{
if(maclistbuffer[0] == 's')
{
maclistbuffer[0] = '0';
int index=0;
for(index=0;index<GLOBAL_AddrLocalNum;index++)
{
seq_printf(m,"%02x:%02x:%02x:%02x:%02x:%02x\n", GLOBAL_AddrLocal[index][0],GLOBAL_AddrLocal[index][1],GLOBAL_AddrLocal[index][2],GLOBAL_AddrLocal[index][3],GLOBAL_AddrLocal[index][4],GLOBAL_AddrLocal[index][5]);
}
GLOBAL_AddrLocalNum = 0;
}
else if(maclistbuffer[0] == 'a')
{
maclistbuffer[0] = '0';
int index=0;
for(index=0;index<GLOBAL_AddrLocalNum1;index++)
{
seq_printf(m,"%02x:%02x:%02x:%02x:%02x:%02x\n", GLOBAL_AddrLocal1[index][0],GLOBAL_AddrLocal1[index][1],GLOBAL_AddrLocal1[index][2],GLOBAL_AddrLocal1[index][3],GLOBAL_AddrLocal1[index][4],GLOBAL_AddrLocal1[index][5]);
}
GLOBAL_AddrLocalNum1 = 0;
}
else
{
seq_printf(m,"sta number is %d, proc!\n", GLOBAL_AddrLocalNum);
seq_printf(m,"ap number is %d, proc!\n", GLOBAL_AddrLocalNum1);
}
return 0;
}
static int maclist_proc_open(struct inode *inode, struct file *file)
{
return single_open(file,maclist_proc_show,inode->i_private);
}
static ssize_t maclist_proc_write(struct file *file, const char *buffer, size_t len, loff_t *off)
{
int user_len = 0;
if (len > MAX_MACLIST_LENGTH)
{
user_len = MAX_MACLIST_LENGTH;
}
else
{
user_len = len;
}
if(copy_from_user(maclistbuffer, buffer, user_len))
{
return -EFAULT;
}
return user_len;
}
int wl_proc_init(void)
{
if (procRegDir == NULL)
procRegDir = proc_mkdir(PROCREG_DIR, NULL);
#if 1
maclistbuffer = (char *)vmalloc(MAX_MACLIST_LENGTH);
if(!maclistbuffer)
{
return -ENOMEM;
}
else
{
memset(maclistbuffer,0,MAX_MACLIST_LENGTH);
entry_wl_beacon_mac = proc_create("beaconmaclist", 0x0644, procRegDir, &maclist_proc_fops);
if(entry_wl_beacon_mac)
{
mac_index = 0;
mac_next = 0;
}
else
{
vfree(maclistbuffer);
}
}
#endif
return 0;
}
int wl_proc_exit(void)
{
remove_proc_entry("beaconmaclist", entry_wl_beacon_mac);
vfree(maclistbuffer);
return 0;
}