{"id":4406,"date":"2014-03-19T10:57:05","date_gmt":"2014-03-19T02:57:05","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4406"},"modified":"2014-03-20T17:47:11","modified_gmt":"2014-03-20T09:47:11","slug":"linux%e5%86%85%e6%a0%b8%e4%b8%ad%e9%80%9a%e8%bf%87%e6%96%87%e4%bb%b6%e6%8f%8f%e8%bf%b0%e7%ac%a6%e8%8e%b7%e5%8f%96%e7%bb%9d%e5%af%b9%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4406","title":{"rendered":"Linux\u5185\u6838\u4e2d\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84"},"content":{"rendered":"<h3>\u80cc\u666f<\/h3>\n<p>\u5728Linux\u5185\u6838\u4e2d\uff0c\u5df2\u77e5\u4e00\u4e2a\u8fdb\u7a0b\u7684pid\u548c\u5176\u6253\u5f00\u6587\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26fd\uff0c\u5982\u4f55\u83b7\u53d6\u8be5\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f\u57fa\u672c\u601d\u8def\u662f\u5148\u83b7\u53d6\u8be5\u6587\u4ef6\u5728\u5185\u6838\u4e2d\u7684file\u7ed3\u6784\u4f53\uff0c\u518d\u901a\u8fc7d_path()\u83b7\u53d6\u5230\u6574\u4e2a\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<h3>\u65b9\u6cd5\u4e00<\/h3>\n<p>\u5982\u679c\u7406\u89e3\u4e86\u8fdb\u7a0b\u548c\u6587\u4ef6\u7cfb\u7edf\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u91c7\u7528\u3002\u57fa\u672c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>1.\u901a\u8fc7\u8fdb\u7a0bpid\u83b7\u53d6\u8fdb\u7a0b\u63cf\u8ff0\u7b26task_struct\uff1b<\/p>\n<p>2.\u901a\u8fc7task_struct\u83b7\u53d6\u8be5\u8fdb\u7a0b\u6253\u5f00\u6587\u4ef6\u7ed3\u6784files_struct\uff0c\u4ece\u800c\u83b7\u53d6\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\uff1b<\/p>\n<p>3.\u4ee5fd\u4e3a\u7d22\u5f15\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u4e2d\u83b7\u53d6\u5bf9\u5e94\u6587\u4ef6\u7684\u7ed3\u6784\u4f53file\uff1b<\/p>\n<p>4.\u901a\u8fc7file\u83b7\u53d6\u5bf9\u5e94path\u7ed3\u6784\uff0c\u8be5\u7ed3\u6784\u5c01\u88c5\u5f53\u524d\u6587\u4ef6\u5bf9\u5e94\u7684dentry\u548c\u6302\u8f7d\u70b9\uff1b<\/p>\n<p>5.\u901a\u8fc7\u5185\u6838\u51fd\u6570d_path()\u83b7\u53d6\u8be5\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1b<\/p>\n<p>\u901a\u8fc7\u8fdb\u7a0bpid\u83b7\u53d6\u8fdb\u7a0b\u63cf\u8ff0\u7b26demo\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct task_struct *get_proc(pid_t pid)\r\n{\r\n\tstruct pid *pid_struct = NULL;\r\n\tstruct task_struct *mytask = NULL;\r\n\r\n\tpid_struct = find_get_pid(pid);\r\n\tif (!pid_struct)\r\n\t\treturn NULL;\r\n\tmytask = pid_task(pid_struct, PIDTYPE_PID);\r\n\treturn mytask;\r\n}\r\n<\/pre>\n<p>\u901a\u8fc7fd\u4ee5\u53cad_path()\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84demo\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint get_path(struct task_struct *mytask, int fd)\r\n{\r\n        struct file *myfile = NULL;\r\n        struct files_struct *files = NULL;\r\n        char path&#x5B;100] = {'&#92;&#48;'};\r\n        char *ppath = path;\r\n\r\n        files = mytask-&gt;files;\r\n        if (!files) {\r\n                printk(&quot;files is null..\\n&quot;);\r\n                return -1;\r\n        }\r\n        myfile = files-&gt;fdt-&gt;fd&#x5B;fd];\r\n        if (!myfile) {\r\n                printk(&quot;myfile is null..\\n&quot;);\r\n                return -1;\r\n        }\r\n        ppath = d_path(&amp;(myfile-&gt;f_path), ppath, 100);\r\n\r\n        printk(&quot;path:%s\\n&quot;, ppath);\r\n        return 0;\r\n}\r\n<\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u4ecefd\u5230file\u7ed3\u6784\u7684\u83b7\u53d6\u5747\u901a\u8fc7\u5404\u4e2a\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u7684\u6307\u5411\u5173\u7cfb\u83b7\u53d6\u3002<\/p>\n<h3>\u65b9\u6cd5\u4e8c<\/h3>\n<p>\u4e0e\u65b9\u6cd5\u4e00\u7684\u601d\u8def\u76f8\u540c\uff0c\u4f46\u662f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5185\u6838\u63d0\u4f9b\u7684\u51fd\u6570fget()\u8fdb\u884cfd\u5230file\u7684\u83b7\u53d6\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u6bd4\u8f83\u7b80\u5355\uff0c\u7a0b\u5e8f\u66f4\u52a0\u5b89\u5168\uff0c\u4e0d\u8fc7\u5c31\u662f\u5c11\u4e86\u5bf9\u6570\u636e\u7ed3\u6784\u5173\u7cfb\u7684\u601d\u8003\u8fc7\u7a0b\u3002\u5176\u5b9e\u4e5f\u53ef\u4ee5\u5c06fget()\u51fd\u6570\u7684\u5b9e\u73b0\u8fc7\u7a0b\u4f5c\u4e3a\u53c2\u8003\uff0c\u6b23\u8d4f\u5185\u6838\u4e2d\u4ee3\u7801\u5b9e\u73b0\u7684\u4e25\u8c28\u6027\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u5728Linux\u5185\u6838\u4e2d\uff0c\u5df2\u77e5\u4e00\u4e2a\u8fdb\u7a0b\u7684pid\u548c\u5176\u6253\u5f00\u6587\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26fd\uff0c\u5982\u4f55\u83b7\u53d6\u8be5\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff1f\u57fa\u672c\u601d\u8def [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[278],"tags":[230,404,382],"class_list":["post-4406","post","type-post","status-publish","format-standard","hentry","category-linux","tag-230","tag-404","tag-382"],"views":17138,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4406"}],"version-history":[{"count":7,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4406\/revisions"}],"predecessor-version":[{"id":4413,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4406\/revisions\/4413"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4406"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}