proc文件的简单读写
生活随笔
收集整理的這篇文章主要介紹了
proc文件的简单读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在開發模塊功能時,需要用到一些調試或者傳值給模塊的方法,最簡單的就是構造一個proc文件,然后配置傳值或查看 。
如下提供了一個簡單模板,需要時可以直接復制過去使用。
/* Init a test proc file 'btn_test'*/ #include <linux/proc_fs.h>static char nvram_query_str[256]={0}; static ssize_t btn_test_read(struct file *file,char __user *buf,size_t len, loff_t *pos) {int ret=0;ret = sprintf(buf, "%s", nvram_query_str);return simple_read_from_buffer(buf, len, pos, buf, strlen(buf));}static ssize_t btn_test_write(struct file *file, const char *buff, size_t len, loff_t *offset) {memset(nvram_query_str, '\0', sizeof(nvram_query_str));if ((len > sizeof(nvram_query_str)-1) || (copy_from_user(nvram_query_str, buff, len) != 0))return -EFAULT;if('1' == nvram_query_str[0]){printk("[%s][%d] Do Restore action.\n", __FUNCTION__, __LINE__);btnHook_RestoreToDefault(0, NULL);}return len; }const struct file_operations btn_test_fops = {.owner = THIS_MODULE,.read = btn_test_read,.write = btn_test_write,};static int init_proc_btn_test(void) {proc_create("btn_test", S_IRUGO, NULL, &btn_test_fops);printk("[%s][%d] Success.s\n", __FUNCTION__, __LINE__);return 0; }//file: fs/libfs.c /*** simple_read_from_buffer - copy data from the buffer to user space* @to: the user space buffer to read to* @count: the maximum number of bytes to read* @ppos: the current position in the buffer* @from: the buffer to read from* @available: the size of the buffer** The simple_read_from_buffer() function reads up to @count bytes from the* buffer @from at offset @ppos into the user space address starting at @to.** On success, the number of bytes read is returned and the offset @ppos is* advanced by this number, or negative value is returned on error.**/ ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,const void *from, size_t available) {loff_t pos = *ppos;size_t ret;if (pos < 0)return -EINVAL;if (pos >= available || !count)return 0;if (count > available - pos)count = available - pos;ret = copy_to_user(to, from + pos, count);if (ret == count)return -EFAULT;count -= ret;*ppos = pos + count;return count; } EXPORT_SYMBOL(simple_read_from_buffer); /*** simple_write_to_buffer - copy data from user space to the buffer* @to: the buffer to write to* @available: the size of the buffer* @ppos: the current position in the buffer* @from: the user space buffer to read from* @count: the maximum number of bytes to read** The simple_write_to_buffer() function reads up to @count bytes from the user* space address starting at @from into the buffer @to at offset @ppos.** On success, the number of bytes written is returned and the offset @ppos is* advanced by this number, or negative value is returned on error.**/ ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,const void __user *from, size_t count) {loff_t pos = *ppos;size_t res;if (pos < 0)return -EINVAL;if (pos >= available || !count)return 0;if (count > available - pos)count = available - pos;res = copy_from_user(to + pos, from, count);if (res == count)return -EFAULT;count -= res;*ppos = pos + count;return count; } EXPORT_SYMBOL(simple_write_to_buffer);?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的proc文件的简单读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DHCP状态机
- 下一篇: Linux数据报文接收发送总结1