{"id":264960,"date":"2023-02-22T14:21:29","date_gmt":"2023-02-22T06:21:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/dxwjsjwjwjwj.html"},"modified":"2023-02-22T14:21:29","modified_gmt":"2023-02-22T06:21:29","slug":"dxwjsjwjwjwj","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/dxwjsjwjwjwj.html","title":{"rendered":"\u8bfb\u5199\u6587\u4ef6\u6570\u636e\u6587\u4ef6\u6587\u4ef6\u6587\u4ef6"},"content":{"rendered":"

Linux<\/a>\u5185\u6838\u8bfb\u5199\u6587\u4ef6<\/p>\n

\u987b\u8981\u8c03\u8bd5\u7684\u9a71\u52a8\u7a0b\u4e2d\u8bfb\u5199\u6587\u4ef6\u6570\u636e\uff0c\u4f8b\u5982\u8bf4\u5f53\u9a71\u52a8\u987b\u8981\u8bb0\u5f55\u7684\u65e5\u5fd7\u6bd4\u8f83\u591a\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06printk()\u51fd\u6570\u590d\u5370\u7684\u4fe1\u606f\u90fd\u8bb2\u5230\u6587\u4ef6\u505a\u540e\u7eed\u5256\u6790\u3002\u5728kernel\u4e2d\u64cd\u4f5c\u6587\u4ef6\u6ca1\u6709\u6807\u51c6\u5e93\u53ef\u7528\uff0c\u987b\u8981\u501f\u52a9kernel\u7684\u4e00\u4e9b\u51fd\u6570\uff0c\u8fd9\u79cd\u51fd\u6570\u4e3b\u8981\u6709\uff1afilp_open()filp_close(),kernel_read()\uff0ckernel_write()\u8fd9\u79cd\u51fd\u6570\u5728linux\/fs.h\u548casm\/uaccess.h\u5934\u6587\u4ef6\u4e2d\u7533\u660e\u3002\u4e0b\u8fb9\u4ecb\u7ecd\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n

\u4e00\u3001\u6253\u5f00\u6587\u4ef6<\/p>\n

filp_open()\u5728kernel\u4e2d\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\uff0c\u5176\u539f\u5f62\u5982\u4e0b\uff1a<\/p>\n

strcutfile*filp_open(constchar*filename,intopen_mode,intmode);<\/p>\n

\u8be5\u51fd\u6570\u8fd4\u56destrcutfile*\u7ed3\u6784\u8868\u9488\uff0c\u4f9b\u540e\u7ee7\u51fd\u6570\u64cd\u4f5c\u4f7f\u7528linux\u5199\u5165txt\u6587\u4ef6<\/strong>\uff0c\u8be5\u8fd4\u56de\u503c\u7528IS\uff3fERR\uff08\uff09\u6765\u68c0\u9a8c\u5176\u6709\u6548\u6027\u3002<\/p>\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

filename\uff1a\u8868\u660e\u8981\u6253\u5f00\u6216\u521b\u5efa\u6587\u4ef6\u7684\u540d\u79f0\uff08\u5305\u62ec\u8def\u5f84\u90e8\u4efd\uff09\u3002\u5728\u5185\u6838\u4e2d\u6253\u5f00\u7684\u6587\u4ef6\u65f6\u987b\u8981\u6ce8\u610f\u6253\u5f00\u7684\u65f6\u673a\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0\u987b\u8981\u6253\u5f00\u6587\u4ef6\u7684\u9a71\u52a8\u5f88\u65e9\u5c31\u52a0\u8f7d\u5e76\u6253\u5f00\u6587\u4ef6linux\u5199\u5165txt\u6587\u4ef6<\/strong>\uff0c\u4f46\u987b\u8981\u6253\u5f00\u7684\u6587\u4ef6\u6240\u5728\u8bbe\u5907\u8fd8\u4e0d\u6709\u6302\u8f7d\u5230\u6587\u4ef6\u7cfb\u7edf<\/a>\u4e2d\uff0c\u800c\u9020\u6210\u6253\u5f00\u5931\u8d25\u3002<\/p>\n

open_mode\uff1a\u6587\u4ef6\u7684\u6253\u5f00\u65b9\u6cd5\uff0c\u5176\u53d6\u503c\u4e0e\u6807\u51c6\u5e93\u4e2d\u7684open\u76f8\u5e94\u53c2\u6570\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u53d6O_CREAT,O_RDWR,O_RDONLY\u7b49\u3002<\/p>\n

mode\uff1a\u521b\u5efa\u6587\u4ef6\u65f6\u4f7f\u7528\uff0c\u8bbe\u7f6e\u521b\u5efa\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\u7ea2\u65d7linux\u7cfb\u7edf\u4e0b\u8f7d\uff0c\u5176\u5b83\u60c5\u51b5\u53ef\u4ee5\u5306\u7565\u8bbe\u4e3a0<\/p>\n

\u4e8c\u3001\u8bfb\u5199\u6587\u4ef6<\/p>\n

kernel\u4e2d\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528kernel_read()\u548ckernel_write\uff0c\u5747\u4e3a\u5185\u6838\u5bfc\u5165\u51fd\u6570\u5185\u6838\u51fd\u6570\u539f\u578b\u4e3a\uff1a<\/p>\n

\n

ssize_t<\/span> kernel_read<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span> void<\/span> *<\/span>buf,<\/span> size_t<\/span> count,<\/span> loff_t<\/span> *<\/span>pos)<\/span>\n{<\/span>\n\tssize_t<\/span> ret;<\/span>\n\tret =<\/span> rw_verify_area<\/span>(<\/span>READ,<\/span> file,<\/span> pos,<\/span> count)<\/span>;<\/span>\n

<\/p>\n\tif<\/span> (<\/span>ret)<\/span>\n\t\treturn<\/span> ret;<\/span>\n\treturn<\/span> __kernel_read<\/span>(<\/span>file,<\/span> buf,<\/span> count,<\/span> pos)<\/span>;<\/span>\n}<\/span>\nEXPORT_SYMBOL<\/span>(<\/span>kernel_read)<\/span>;<\/span>\nssize_t<\/span> kernel_write<\/span>(<\/span>struct<\/span> file<\/span> *<\/span>file,<\/span> const<\/span> void<\/span> *<\/span>buf,<\/span> size_t<\/span> count,<\/span>\n\t\t\t loff_t<\/span> *<\/span>pos)<\/span>\n{<\/span>\n\tssize_t<\/span> ret;<\/span>\n\tret =<\/span> rw_verify_area<\/span>(<\/span>WRITE,<\/span> file,<\/span> pos,<\/span> count)<\/span>;<\/span>\n\tif<\/span> (<\/span>ret)<\/span>\n\t\treturn<\/span> ret;<\/span>\n\tfile_start_write<\/span>(<\/span>file)<\/span>;<\/span>\n\tret =<\/span> __kernel_write<\/span>(<\/span>file,<\/span> buf,<\/span> count,<\/span> pos)<\/span>;<\/span>\n

<\/p>\n\tfile_end_write<\/span>(<\/span>file)<\/span>;<\/span>\n\treturn<\/span> ret;<\/span>\n}<\/span>\nEXPORT_SYMBOL<\/span>(<\/span>kernel_write)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u4e0a\u9762\u7684\u53c2\u6570\u548cring3\u8bfb\u5199\u662f\u4e00\u6837\u7684\uff0c\u9886\u5230filp\u53e5\u67c4\u4ee5\u540e\u5c31\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u4e86\u3002<\/p>\n

\u4e09\u3001\u5173\u95ed\u6587\u4ef6<\/p>\n

intfilp_close(structfile*filp,fl_owner_tid);<\/p>\n

\u8be5\u51fd\u6570\u7684\u4f7f\u7528\u5f88\u7b80\u5355\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u901a\u5e38\u4f20\u9012NULL\u503c\uff0c\u4e5f\u6709\u7528current->files\u4f5c\u4e3a\u5f62\u53c2\u7684\u3002<\/p>\n

\u4f7f\u7528\u4ee5\u4e0a\u51fd\u6570\u7684\u5176\u5b83\u6ce8\u610f\u70b9\uff1a<\/p>\n

\u867d\u7136LinuxKernel\u7ec4\u6210\u5458\u4e0d\u8d5e\u540c\u5728kernel\u4e2d\u72ec\u7acb\u7684\u8bfb\u5199\u6587\u4ef6\uff08\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u7b56\u7565\u548c\u5b89\u5168\u95ee\u9898\uff09\uff0c\u5bf9\u5185\u6838\u987b\u8981\u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u6700\u597d\u7531\u5e94\u7528\u5c42\u914d\u5408\u5b8c\u6210\u3002\u5728\u53ef\u52a0\u8f7d\u7684kernelmodule\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8bfb\u5199\u6587\u4ef6\u53ef\u80fd\u4f7f\u6a21\u5757\u52a0\u8f7d\u5931\u8d25\uff0c\u7f18\u7531\u662f\u5185\u6838\u53ef\u80fd\u6ca1\u6709EXPORT\u4f60\u6240\u987b\u8981\u7684\u6240\u6709\u8fd9\u79cd\u51fd\u6570\u3002<\/p>\n

3.\u5256\u6790\u4ee5\u4e0a\u4e2a\u522b\u51fd\u6570\u7684\u53c2\u6570\u53ef\u4ee5\u770b\u51falinux \u53d1\u90ae\u4ef6\uff0c\u8fd9\u79cd\u51fd\u6570\u7684\u6b63\u786e\u8fd0\u884c\u987b\u8981\u4f9d\u8d56\u4e8e\u8fdb\u7a0b\u73af\u5883\uff0c\u4e3a\u6b64\uff0c\u6709\u4e9b\u51fd\u6570\u4e0d\u80fd\u5728\u4e2d\u65ad\u7684handle\u6216Kernel\u4e2d\u4e0d\u5c5e\u4e8e\u4efb\u53ef\u8fdb\u7a0b\u7684\u4ee3\u7801\u4e2d\u6267\u884c\uff0c\u5426\u5219\u53ef\u80fd\u51fa\u73b0\u5d29\u6e83\uff0c\u8981\u9632\u6b62\u6b64\u7c7b\u60c5\u51b5\u53d1\u751f\uff0c\u53ef\u4ee5\u5728kernel\u4e2d\u521b\u5efa\u5185\u6838\u7ebf\u7a0b\uff0c\u5c06\u8fd9\u79cd\u51fd\u6570\u653e\u5230\u7ebf\u7a0b\u73af\u5883\u4e0b\u6267\u884c(\u521b\u5efa\u5185\u6838\u7ebf\u7a0b\u7684\u65b9\u6cd5\u8bf7\u53c2\u6570kernel_thread()\u51fd\u6570\uff09\u3002\u56db\u3001\u9a71\u52a8\u4ee3\u7801<\/p>\n

\n

#<\/span>include<\/span> <\/a><\/span><\/span>\n#<\/span>define<\/span> MY_FILE<\/span> \"\/tmp\/log.txt\"<\/span><\/span>\nchar<\/span> buf[<\/span>256<\/span>]<\/span>;<\/span>\n

linux\u5199\u5165txt\u6587\u4ef6_linux \u5199\u5165\u6587\u4ef6_linux\u65e0\u6cd5\u6253\u5f00\u5e76\u5199\u5165\u6587\u4ef6<\/p>\nloff_t<\/span> pos =<\/span> 0<\/span>;<\/span>\nstruct<\/span> file<\/span> *<\/span>filep =<\/span> NULL<\/span>;<\/span>\nstatic<\/span> int<\/span> __init init<\/span>(<\/span>void<\/span>)<\/span>\n{<\/span>\n printk<\/span>(<\/span>\"Hello, I'm the module that intends to write messages to file.n\"<\/span>)<\/span>;<\/span>\n if<\/span>(<\/span>filep ==<\/span> NULL<\/span>)<\/span> {<\/span>\n filep =<\/span> filp_open<\/span>(<\/span>MY_FILE,<\/span> O_RDWR |<\/span> O_APPEND |<\/span> O_CREAT,<\/span> 0644<\/span>)<\/span>;<\/span>\n }<\/span>\n if<\/span> (<\/span>IS_ERR<\/span>(<\/span>filep)<\/span>)<\/span> {<\/span>\n printk<\/span>(<\/span>\"Open file %s errorn\"<\/span>,<\/span> MY_FILE)<\/span>;<\/span>\n return<\/span> -<\/span>1<\/span>;<\/span>\n }<\/span>\n sprintf<\/span>(<\/span>buf,<\/span>\"%sn\"<\/span>,<\/span> \"This is test message!\"<\/span>)<\/span>;<\/span>\n

<\/p>\n memset<\/span>(<\/span>buf,<\/span> 0<\/span>,<\/span> sizeof<\/span>(<\/span>buf)<\/span>)<\/span>;<\/span>\n kernel_write<\/span>(<\/span>filep,<\/span> buf,<\/span> strlen<\/span>(<\/span>buf)<\/span>,<\/span> &<\/span>pos)<\/span>;<\/span>\n memset<\/span>(<\/span>buf,<\/span> 0<\/span>,<\/span> sizeof<\/span>(<\/span>buf)<\/span>)<\/span>;<\/span>\n kernel_read<\/span>(<\/span>filep,<\/span> buf,<\/span> sizeof<\/span>(<\/span>buf)<\/span>,<\/span> &<\/span>pos)<\/span>;<\/span>\n printk<\/span>(<\/span>\"Read buf -> %sn\"<\/span>,<\/span> buf)<\/span>;<\/span>\n return<\/span> 0<\/span>;<\/span>\n}<\/span>\nstatic<\/span> void<\/span> __exit fini<\/span>(<\/span>void<\/span>)<\/span>\n{<\/span>\n printk<\/span>(<\/span>\"Kernel read\/write exitn\"<\/span>)<\/span>;<\/span>\n if<\/span>(<\/span>filep !=<\/span> NULL<\/span>)<\/span> {<\/span>\n filp_close<\/span>(<\/span>filep,<\/span> NULL<\/span>)<\/span>;<\/span>\n

<\/p>\n }<\/span>\n}<\/span>\nmodule_init<\/span>(<\/span>init)<\/span>;<\/span>\nmodule_exit<\/span>(<\/span>fini)<\/span>;<\/span>\nMODULE_LICENSE<\/span>(<\/span>\"GPL\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n<\/p>\n

\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n

1\u3001\u5f53\u9a71\u52a8\u987b\u8981\u6301\u7eed\u8bfb\u5199\u6587\u4ef6\u7684\u8bdd\uff0c\u987b\u8981\u8003\u8651\u5e76\u53d1\u7684\u60c5\u51b5\uff0c\u987b\u8981\u7ed9buf\u7684\u8bfb\u5199\u52a0\u9501\u3002<\/p>\n

\u67e5\u770blog\u65e5\u5fd7\uff1a<\/p>\n

\n

curtis@curtis-Aspire-E5-471G:~\/write_code\/kernel_write$ dmesg<\/span>\n[<\/span>145731.662192<\/span>]<\/span> Hello, I'm the module that intends to write<\/span> messages to file.\n[<\/span>145731.662206<\/span>]<\/span> pos -><\/span> 0<\/span>\n[<\/span>145731.662210<\/span>]<\/span> Read buf -><\/span> This is test<\/span> message!<\/span>\ncurtis@curtis-Aspire-E5-471G:~\/write_code\/kernel_write$ cat<\/span> \/tmp\/log.txt\nThis is test<\/span> message!<\/span>\n<\/code><\/pre>\n<\/p>\n

\u53c2\u8003\u94fe\u63a5\uff1aLinux\u5185\u6838\u4e2d\u8bfb\u5199\u6587\u4ef6\u6570\u636e\u7684\u65b9\u5f0f_wxie\u7684Linux\u4eba\u751f-CSDN\u535a\u5ba2<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5185\u6838\u8bfb\u5199\u6587\u4ef6\u4e8c\u3001\u8bfb\u5199\u6587\u4ef6kernel\u4e2d\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528kernel_read()\u548ckernel_write\uff0c\u5747\u4e3a\u5185\u6838\u5bfc\u51fa\u51fd\u6570\u5185\u6838\u51fd\u6570\u539f\u578b\u4e3a\uff1a\u91cc\u9762\u7684\u53c2\u6570\u548cring3\u8bfb\u5199\u662f\u4e00\u6837\u7684\uff0c\u62ff\u5230filp\u53e5\u67c4\u4e4b\u540e\u5c31\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u4e86\u3002module\u4e2d\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u8bfb\u5199\u6587\u4ef6\u53ef\u80fd\u4f7f\u6a21\u5757\u52a0\u8f7d\u5931\u8d25\uff0c\u539f\u56e0\u662f\u5185\u6838\u53ef\u80fd\u6ca1\u6709EXPORT\u4f60\u6240\u9700\u8981\u7684\u6240\u6709\u8fd9\u4e9b\u51fd\u6570\u3002<\/p>\n","protected":false},"author":1,"featured_media":264961,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-264960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/264960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=264960"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/264960\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/264961"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=264960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=264960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=264960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}