{"id":2700,"date":"2016-05-25T22:59:44","date_gmt":"2016-05-25T14:59:44","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2700"},"modified":"2016-05-25T22:59:44","modified_gmt":"2016-05-25T14:59:44","slug":"linux%e8%bf%9b%e7%a8%8b%e6%8e%a7%e5%88%b6%e4%b9%8bexec%e5%87%bd%e6%95%b0%e6%97%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2700.html","title":{"rendered":"Linux\u8fdb\u7a0b\u63a7\u5236\u4e4bexec\u51fd\u6570\u65cf"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u5de5\u4f5c\u3001\u5b66\u4e60\u9700\u8981\u3002<\/p>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>\u5728\u8fdb\u7a0b\u7684\u521b\u5efa\u4e0aUnix\u91c7\u7528\u4e86\u4e00\u4e2a\u72ec\u7279\u7684\u65b9\u6cd5\uff0c\u5b83\u5c06\u8fdb\u7a0b\u521b\u5efa\u4e0e\u52a0\u8f7d\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u6620\u8c61\u5206\u79bb\u3002\u8fd9\u6837\u7684\u597d\u5904\u662f\u6709\u66f4\u591a\u7684\u4f59\u5730\u5bf9\u4e24\u79cd\u64cd\u4f5c\u8fdb\u884c\u7ba1\u7406\u3002\u5f53\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u4e4b\u540e\uff0c\u901a\u5e38\u5c06\u5b50\u8fdb\u7a0b\u66ff\u6362\u6210\u65b0\u7684\u8fdb\u7a0b\u6620\u8c61\uff0c\u8fd9\u53ef\u4ee5\u7528exec\u7cfb\u5217\u7684\u51fd\u6570\u6765\u8fdb\u884c\u3002\u5f53\u7136\uff0cexec\u7cfb\u5217\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u5f53\u524d\u8fdb\u7a0b\u66ff\u6362\u6389\u3002\u4f8b\u5982\uff1a\u5728shell\u547d\u4ee4\u884c\u6267\u884cps\u547d\u4ee4\uff0c\u5b9e\u9645\u4e0a\u662fshell\u8fdb\u7a0b\u8c03\u7528fork\u590d\u5236\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\uff0c\u518d\u5229\u7528exec\u7cfb\u7edf\u8c03\u7528\u5c06\u65b0\u4ea7\u751f\u7684\u5b50\u8fdb\u7a0b\u5b8c\u5168\u66ff\u6362\u6210ps\u8fdb\u7a0b\u3002<\/p>\n<p><strong>\u56e0\u4e3aexec\u7cfb\u5217\u51fd\u6570\u5e76\u4e0d\u521b\u5efa\u4efb\u4f55\u65b0\u8fdb\u7a0b\uff0c\u6240\u4ee5\u524d\u540e\u7684\u8fdb\u7a0bID\u6ca1\u6709\u53d1\u751f\u4efb\u4f55\u6539\u53d8\uff0c<span style=\"color: #0000ff;\">exec\u6240\u505a\u7684\u5c31\u662f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u6b63\u6587\u6bb5\u3001\u6570\u636e\u3001\u5806\u548c\u6808\u6bb5<\/span>\u3002<\/strong> exec\u7cfb\u5217\u51fd\u6570\u5305\u62ec\uff1a<\/p>\n<pre class=\"lang:default decode:true \">int execl(const char* pathname, const char* arg0, ...);     \/\/ end with NULL\nint execv(const char* pathname, char* const argv[]);        \/\/ end with NULL\nint execle(const char* pathname, const char* arg0, ...);    \/\/ end with NULL and char* const envp[]\nint execve(const char* pathname, char* const argv[], char* const envp);  \/\/ exec\u7cfb\u5217\u51fd\u6570\u4e2d\u552f\u4e00\u7684\u7cfb\u7edf\u8c03\u7528(\u5176\u5b83\u7684\u53ea\u662f\u5e93\u51fd\u6570\uff0c\u6700\u7ec8\u90fd\u8981\u8c03\u7528execve)\nint execlp(const char* filename, const char* arg0, ...);    \/\/ end with NULL\nint execvp(const char* filename, char* const argv[]);       \/\/ end with NULL\n\n\/\/ \u8fd96\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1a\u82e5\u51fa\u9519\u5219\u8fd4\u56de-1\uff0c\u82e5\u6210\u529f\u5219\u4e0d\u8fd4\u56de\u503c\n\/\/ l -&gt; list \u300cl\u548cv\u4e92\u65a5\u300d\n\/\/ v -&gt; vector \u300cl\u548cv\u4e92\u65a5\u300d\n\/\/ p -&gt; path\n\/\/ e -&gt; env<\/pre>\n<p>\u8fd96\u4e2aexec\u51fd\u6570\u7684\u53c2\u6570\u5f88\u96be\u8bb0\u5fc6\u3002\u51fd\u6570\u540d\u4e2d\u7684\u5b57\u7b26\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e9b\u5e2e\u52a9\uff1a<span style=\"color: #0000ff;\"><strong>\u5b57\u6bcdp\u8868\u793a\u8be5\u51fd\u6570\u53d6filename\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4e14\u7528PATH\u73af\u5883\u53d8\u91cf\u5bfb\u627e\u53ef\u6267\u884c\u6587\u4ef6<\/strong><\/span>\uff1b<span style=\"color: #ff0000;\"><strong>\u5b57\u6bcdl\u8868\u793a\u8be5\u51fd\u6570\u53d6\u4e00\u4e2a\u53c2\u6570\u8868\uff0c\u5b83\u4e0e\u5b57\u6bcdv\u4e92\u65a5\uff1b\u5b57\u6bcdv\u8868\u793a\u8be5\u51fd\u6570\u53d6\u4e00\u4e2aargv[]\u77e2\u91cf<\/strong><\/span>\uff1b\u6700\u540e\uff0c<span style=\"color: #0000ff;\"><strong>\u5b57\u6bcde\u8868\u793a\u8be5\u51fd\u6570\u53d6envp[]\u6570\u7ec4\uff0c\u800c\u4e0d\u4f7f\u7528\u5f53\u524d\u73af\u5883<\/strong><\/span>\u3002<strong>\u4e0a\u97626\u4e2a\u51fd\u6570\u4e2d\u53ea\u6709execve\u662f\u5185\u6838\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u5176\u5b83\u76845\u4e2a\u53ea\u662f\u5e93\u51fd\u6570\uff0c\u6700\u7ec8\u90fd\u8981\u8c03\u7528\u8be5\u7cfb\u7edf\u8c03\u7528<\/strong>\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2016\/05\/UNIX_exec.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2701\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2016\/05\/UNIX_exec.png\" alt=\"UNIX_exec\" width=\"938\" height=\"250\" \/><\/a><\/p>\n<p>\u4ee5e\u7ed3\u5c3e\u7684\u4e24\u4e2a\u51fd\u6570\uff08execle\u548cexecve\uff09\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u6307\u5411\u73af\u5883\u5b57\u7b26\u4e32\u6307\u9488\u6570\u7ec4\u7684\u6307\u9488\u3002\u5176\u4ed6\u56db\u4e2a\u51fd\u6570\u5219\u4f7f\u7528\u8c03\u7528\u8fdb\u7a0b\u4e2d\u7684environ\u53d8\u91cf\u4e3a\u65b0\u7a0b\u5e8f\u590d\u5236\u73b0\u6709\u7684\u73af\u5883\u3002<\/p>\n<p>exec \u5982\u679c\u4f20\u5165\u7684\u662f filename\uff0c\u90a3\u4e48\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u5305\u542b \/\uff0c\u90a3\u4e48\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u8def\u5f84\u540d pathname\uff1b\u5426\u5219\u5728 PATH \u73af\u5883\u53d8\u91cf\u91cc\u67e5\u627e\u5230\u7b2c\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6<\/li>\n<li>\u5982\u679c\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u662f\u94fe\u63a5\u5668\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u8ba4\u4e3a\u662f\u4e00\u4e2a shell \u6587\u4ef6\uff0c\u4f7f\u7528 \/bin\/sh \u6267\u884c<\/li>\n<\/ul>\n<p>\u6267\u884c exec \u51fd\u6570\uff0c\u4e0b\u9762\u5c5e\u6027\u662f\u4e0d\u53d1\u751f\u53d8\u5316\u7684\uff1a<\/p>\n<ul>\n<li>\u8fdb\u7a0b ID \u548c\u7236\u8fdb\u7a0b ID<\/li>\n<li>\u5b9e\u9645\u7528\u6237 ID \u548c\u5b9e\u9645\u7ec4 ID<\/li>\n<li>\u9644\u52a0\u7ec4 ID<\/li>\n<li>\u4f1a\u8bdd ID<\/li>\n<li>\u63a7\u5236\u7ec8\u7aef<\/li>\n<li>\u95f9\u949f\u4f59\u7559\u65f6\u95f4<\/li>\n<li>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/li>\n<li>\u6839\u76ee\u5f55<\/li>\n<li>umask<\/li>\n<li>\u6587\u4ef6\u9501<\/li>\n<li>\u8fdb\u7a0b\u4fe1\u53f7\u5c4f\u853d<\/li>\n<li>\u672a\u5904\u7406\u4fe1\u53f7<\/li>\n<li>\u8d44\u6e90\u9650\u5236<\/li>\n<li>\u8fdb\u7a0b\u65f6\u95f4<\/li>\n<\/ul>\n<p>\u800c\u4e0b\u9762\u5c5e\u6027\u662f\u53d1\u751f\u53d8\u5316\u7684\uff1a<\/p>\n<ul>\n<li>\u6587\u4ef6\u63cf\u8ff0\u7b26\u5982\u679c\u5b58\u5728 close-on-exec \u6807\u8bb0\uff0c\u90a3\u4e48\u4f1a\u5173\u95ed<\/li>\n<li>\u53ef\u6267\u884c\u7a0b\u5e8f\u5b58\u5728\u8bbe\u7f6e\u7528\u6237 ID \u548c\u7ec4 ID \u4f4d\uff0c\u90a3\u4e48\u6709\u6548\u7528\u6237 ID \u548c\u7ec4 ID \u4f1a\u53d1\u751f\u53d8\u5316<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a class=\"external-link\" href=\"http:\/\/www.cnblogs.com\/mickole\/p\/3187409.html\" target=\"_blank\" rel=\"nofollow\">linux\u7cfb\u7edf\u7f16\u7a0b\u4e4b\u8fdb\u7a0b\uff08\u4e94\uff09\uff1aexec\u7cfb\u5217\u51fd\u6570\uff08execl,execlp,execle,execv,execvp)\u4f7f\u7528<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/blog.csdn.net\/y_23k_bug\/article\/details\/9885185\" target=\"_blank\" rel=\"nofollow\">Linux c\u8fdb\u7a0b\u7ba1\u7406\u2014\u521b\u5efa\u8fdb\u7a0b system\u3001execl\u3001execlp\u3001fork<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/linux.die.net\/man\/3\/execl\" target=\"_blank\" rel=\"nofollow\">http:\/\/linux.die.net\/man\/3\/execl<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a class=\"external-link\" href=\"http:\/\/linux.die.net\/man\/3\/execve\" target=\"_blank\" rel=\"nofollow\">http:\/\/linux.die.net\/man\/3\/execve<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/linux.die.net\/man\/2\/execve\" target=\"_blank\" rel=\"nofollow\">http:\/\/linux.die.net\/man\/2\/execve<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/wuleying.github.io\/books\/apue\/s8\/8.5.html\" target=\"_blank\" rel=\"nofollow\">http:\/\/wuleying.github.io\/books\/apue\/s8\/8.5.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/dirtysalt.info\/apue.html\" target=\"_blank\" rel=\"nofollow\">http:\/\/dirtysalt.info\/apue.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/www.cnblogs.com\/nufangrensheng\/p\/3510821.html\" target=\"_blank\" rel=\"nofollow\">\u8fdb\u7a0b\u63a7\u5236\u4e4bexec\u51fd\u6570<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/kernel\/syscall\/part3\/\" target=\"_blank\" rel=\"nofollow\">\u7cfb\u7edf\u8c03\u7528\u8ddf\u6211\u5b66(3)<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/yq.aliyun.com\/articles\/41467\" target=\"_blank\" rel=\"nofollow\">\u300aAPUE\u300b\u8bfb\u4e66\u7b14\u8bb0\u2014\u7b2c\u516b\u7ae0\u8fdb\u7a0b\u63a7\u5236<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/segmentfault.com\/a\/1190000004443038#articleHeader4\" target=\"_blank\" rel=\"nofollow\">[\u5355\u5237APUE\u7cfb\u5217]\u7b2c\u516b\u7ae0\u2014\u2014\u8fdb\u7a0b\u63a7\u5236[2]<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/name5566.com\/3201.html\" target=\"_blank\" rel=\"nofollow\">APUE \u2014 Process Control<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/blog.csdn.net\/willib\/article\/details\/15809361\" target=\"_blank\" rel=\"nofollow\">exec\u51fd\u6570\u65cf\u7684\u4f5c\u7528\u4e0e\u8bb2\u89e3<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5de5\u4f5c\u3001\u5b66\u4e60\u9700\u8981\u3002 \u53c2\u8003\u89e3\u7b54\uff1a \u5728\u8fdb\u7a0b\u7684\u521b\u5efa\u4e0aUnix\u91c7\u7528\u4e86\u4e00\u4e2a\u72ec\u7279\u7684\u65b9\u6cd5\uff0c\u5b83\u5c06\u8fdb\u7a0b\u521b\u5efa [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[610,611,612,613,30],"class_list":["post-2700","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-apue","tag-execl","tag-execv","tag-execve","tag-linux"],"views":12659,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2700"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2700\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}