{"id":3561,"date":"2017-08-19T17:53:26","date_gmt":"2017-08-19T09:53:26","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3561"},"modified":"2017-08-19T17:53:26","modified_gmt":"2017-08-19T09:53:26","slug":"linux%e4%b8%8bc%e8%af%ad%e8%a8%80%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e4%b8%80%e4%ba%9b%e7%bb%8f%e9%aa%8c%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3561.html","title":{"rendered":"Linux\u4e0bC\u8bed\u8a00\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e9b\u7ecf\u9a8c\u5c0f\u7ed3"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u603b\u7ed3\u3001\u63d0\u9ad8\u9700\u8981<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>1\u3001<span style=\"color: #ff0000;\"><strong>\u5728\u4f7f\u7528\u4e00\u4e2a\u8fd4\u56de\u7c7b\u578b\u4e3a<code>\u6307\u9488<\/code>\u7684\u51fd\u6570\u65f6\uff0c\u4e00\u5b9a\u8981\u660e\u786e\u77e5\u9053\u662f\u5426\u9700\u8981\u5728\u4f7f\u7528\u540efree<\/strong><\/span>\uff0c\u56e0\u4e3a\u4e0dfree\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\uff0c\u4f46\u4e0d\u9700\u8981free\u5374\u53c8\u4e3b\u52a8free\u7684\u8bdd\u4f1a\u9020\u6210\u7a0b\u5e8f\u5f02\u5e38\u4e14\u4e0d\u5bb9\u6613\u68c0\u6d4b\uff1b\u6bd4\u5982get_current_dir_name()\u51fd\u6570\u8fd4\u56de\u7684\u6307\u9488\u5c31\u9700\u8981\u4e3b\u52a8free()\uff0c\u5728<a href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/get_current_dir_name.3.html#DESCRIPTION\">\u5b83\u7684\u624b\u518c<\/a>\u4e2d\u63d0\u5230\u5b83\u662fmalloc()\u540e\u8fd4\u56de\u7684\uff0c\u9700\u8981\u8c03\u7528\u8005\u5bf9\u975eNULL\u8fd4\u56de\u8c03\u7528free()\uff0c\u4f46\u662fttyname()\u8fd4\u56de\u7684\u6307\u9488\u5c31\u4e0d\u5e94\u8be5\u624b\u52a8free()\uff0c\u56e0\u4e3attyname()\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u9759\u6001\u5206\u914d\u7684\u5b57\u7b26\u4e32\uff1b<\/p>\n<p>2\u3001<span style=\"color: #ff0000;\"><strong>\u5c3d\u91cf\u901a\u8fc7\u4f20\u53c2\u7684\u65b9\u5f0f\u83b7\u53d6\u5904\u7406\u7ed3\u679c\uff0c\u907f\u514d\u901a\u8fc7\u51fd\u6570\u5185\u90e8malloc\u540e\u8fd4\u56de\u6307\u9488\u7684\u65b9\u5f0f<\/strong><\/span>\uff0c\u4e00\u6765\u662f\u907f\u514d\u8c03\u7528\u8005\u5fd8\u8bb0free\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u9732\uff0c\u6216\u8005\u4e3b\u52a8free\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5f02\u5e38\uff0c\u4e8c\u6765\u662f\u8fd9\u6837\u7f16\u5199\u7684\u51fd\u6570\u662f\u53ef\u91cd\u5165\u4e14\u7ebf\u7a0b\u5b89\u5168\u7684\uff1b\u6bd4\u5982 ttyname() \u548c ttyname_r() \u4e2d\u5c31\u5e94\u8be5\u4f18\u5148\u4f7f\u7528ttyname_r()\uff1b<\/p>\n<p>3\u3001<strong><span style=\"color: #ff0000;\">\u5728<code>\u5173\u952e\u8def\u5f84<\/code>\u4e0a\u4f7f\u7528\u7684\u51fd\u6570\u6700\u597d\u8981\u662f\u5f02\u6b65\u7684\uff0c\u907f\u514d\u6210\u4e3a\u74f6\u9888\u4ece\u800c\u5f15\u53d1\u6545\u969c<\/span><\/strong>\uff1b\u6bd4\u5982\uff1asyslog()\u51fd\u6570\u5176\u5b9e\u662f\u540c\u6b65\u800c\u975e\u5f02\u6b65\u7684\uff0c\u5177\u4f53\u4fe1\u606f\u53ef\u53c2\u89c1\u300c<a href=\"https:\/\/ixyzero.com\/blog\/archives\/3388.html\">rsyslog\u4f7f\u7528\u4e4b\u5751<\/a>\u300d\uff1b<\/p>\n<p>4\u3001<span style=\"color: #ff0000;\"><strong>\u4e0d\u8981\u76f8\u4fe1\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\uff0c\u51fd\u6570\u4e2d\u7684\u7b2c\u4e00\u6b65\u64cd\u4f5c\u5c31\u5e94\u8be5\u662f\u53c2\u6570\u7684\u5408\u7406\/\u5408\u6cd5\u6027\u6821\u9a8c\uff0c\u4e0d\u901a\u8fc7\u7684\u8bdd\u76f4\u63a5return\uff0c\u65e2\u9ad8\u6548\u53c8\u53ef\u907f\u514d\u9519\u8bef<\/strong><\/span>\uff1b\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre class=\"lang:default decode:true\">int get_cmdline_from_pid (int pid, char cmdline[PATH_MAX])\r\n{\r\n    char file[PATH_MAX];\r\n    snprintf(file, PATH_MAX, \"\/proc\/%d\/cmdline\", pid);\r\n    int i, r = 0, fd = open(file, O_RDONLY);\r\n    memset(cmdline, 0, PATH_MAX);\r\n    if (fd &gt; 0) {\r\n        r = read(fd, cmdline, PATH_MAX);\r\n        close(fd);\r\n\r\n        for (i = 0; r &gt; 0 &amp;&amp; i &lt; r-1; i++) {\r\n            if (cmdline[i] == 0)\r\n                cmdline[i] = ' ';\r\n        }\r\n        return 0;\r\n    }\r\n    return -1;\r\n}<\/pre>\n<p>\u5b58\u5728\u51e0\u4e2a\u95ee\u9898\uff1a1.\u73b0\u6709\u7684\u4e24\u4e2a\u53c2\u6570\u5e94\u8be5\u663e\u5f0f\u6539\u4e3a\u4e09\u4e2a\u53c2\u6570\uff08\u56e0\u4e3aC\u8bed\u8a00\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u662f\u6ca1\u6709\u6570\u7ec4\u8fd9\u4e00\u7c7b\u578b\u7684\uff0c\u5b83\u5bf9\u4e8e\u51fd\u6570\u6765\u8bf4\u5c31\u662f\u4e00\u6307\u9488\uff0c\u6307\u9488\u6307\u5411\u7684\u539f\u59cb\u6570\u7ec4\u5927\u5c0f\u9700\u8981\u7528\u53e6\u4e00\u4e2a\u53c2\u6570\u6765\u663e\u5f0f\u8bf4\u660e\uff09\uff1b2.\u51fd\u6570\u4f53\u4e2d\u7b2c\u4e00\u6b65\u5e94\u8be5\u505a\u53c2\u6570\u7684\u5408\u7406\u6027\u6821\u9a8c\uff0c\u907f\u514d\u540e\u9762\u51fa\u73b0\u5bf9NULL\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u5bfc\u81f4segmentation fault\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff1b3.\u5bf9\u4e8e\u7cfb\u7edf\u8c03\u7528read()\u7684\u8fd4\u56de\u503c\u6ca1\u6709\u505a\u5224\u65ad\u4ee5\u53ca\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u8fd9\u6837\u867d\u7136\u5bf9cmdline\u7684\u5185\u5bb9\u6ca1\u4ec0\u4e48\u5f71\u54cd\uff0c\u4f46\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5374\u4f1a\u662f0\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u8c03\u7528\u8005\u7684\u8bef\u89e3\u5e76\u5f15\u53d1\u540e\u7eed\u9519\u8bef\u3002\u4fee\u6539\u540e\u7684\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">int get_cmdline_from_pid (int pid, char cmdline[], int cmdline_size)\r\n{\r\n    if (pid &lt;= 0 || cmdline == NULL || cmdline_size &lt;= 0) {\r\n        return -1;\r\n    }\r\n\r\n    char file[PATH_MAX];\r\n    snprintf(file, PATH_MAX, \"\/proc\/%d\/cmdline\", pid);\r\n    int i, r = 0, fd = open(file, O_RDONLY);\r\n    if (fd &gt; 0) {\r\n        memset(cmdline, 0, cmdline_size);\r\n        r = read(fd, cmdline, cmdline_size);\r\n        if (r &lt;= 0) {\r\n            close(fd);\r\n            return -1;\r\n        }\r\n\r\n        for (i = 0; r &gt; 0 &amp;&amp; i &lt; r-1; i++) {\r\n            if (cmdline[i] == 0)\r\n                cmdline[i] = ' ';\r\n        }\r\n        close(fd);\r\n        return 0;\r\n    }\r\n    return -1;\r\n}<\/pre>\n<p>\u589e\u52a0\u4e86\u4e00\u4e9b\u68c0\u67e5\u3001\u5224\u65ad\u4e4b\u540e\uff0c\u6574\u4f53\u903b\u8f91\u8981\u6e05\u6670\u4e86\u4e0d\u5c11\uff0c\u4e5f\u53ef\u907f\u514d\u4e0d\u5c11\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n<p>\u8865\u5145\u4e00\u4e2amemset()\u5bf9NULL\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\u5bfc\u81f4\u300c\u6bb5\u9519\u8bef\u300d\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;unistd.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char const *argv[])\r\n{\r\n    \/\/ char *cwd = get_current_dir_name();\r\n    \/\/ printf(\"current_dir_name = %s\\n\", cwd);\r\n    \/\/ if (cwd) {\r\n    \/\/     free(cwd);    \/\/ we should free\r\n    \/\/ }\r\n\r\n    char *ttyname_b = ttyname (0);\r\n    printf(\"ttyname = %s\\n\", ttyname_b);\r\n    if (ttyname_b) {\r\n        free(ttyname_b);    \/\/ should not free\r\n    }\r\n\r\n    char arr[100] = \"this is an char array\";\r\n    printf(\"arr = %s\\n\", arr);\r\n    memset(arr, 0, sizeof(arr));\r\n    printf(\"arr = %s\\n\", arr);\r\n\r\n    char *str = NULL;\r\n    printf(\"str = %s\\n\", str);\r\n    memset(str, 0, sizeof(str));    \/\/ segmentation fault\r\n    printf(\"str = %s\\n\", str);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/ttyname.3.html#RETURN_VALUE\">http:\/\/man7.org\/linux\/man-pages\/man3\/ttyname.3.html#RETURN_VALUE<\/a><\/li>\n<li><a href=\"http:\/\/man7.org\/tlpi\/code\/online\/dist\/tty\/ttyname.c.html\">http:\/\/man7.org\/tlpi\/code\/online\/dist\/tty\/ttyname.c.html<\/a><\/li>\n<li><a href=\"http:\/\/www.codeforge.com\/read\/217200\/apue.2e-_-termios-_-ttyname.c__html\">http:\/\/www.codeforge.com\/read\/217200\/apue.2e-_-termios-_-ttyname.c__html<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/syslog.3.html\">http:\/\/man7.org\/linux\/man-pages\/man3\/syslog.3.html<\/a><\/li>\n<li><a href=\"https:\/\/unix.stackexchange.com\/questions\/205883\/understand-logging-in-linux\">https:\/\/unix.stackexchange.com\/questions\/205883\/understand-logging-in-linux<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/208098\/can-syslog-performance-be-improved\">https:\/\/stackoverflow.com\/questions\/208098\/can-syslog-performance-be-improved<\/a><\/li>\n<li><a href=\"http:\/\/www.rsyslog.com\/doc\/v8-stable\/tutorials\/reliable_forwarding.html#forwarding-to-more-than-one-server\">http:\/\/www.rsyslog.com\/doc\/v8-stable\/tutorials\/reliable_forwarding.html#forwarding-to-more-than-one-server<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u603b\u7ed3\u3001\u63d0\u9ad8\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a 1\u3001\u5728\u4f7f\u7528\u4e00\u4e2a\u8fd4\u56de\u7c7b\u578b\u4e3a\u6307\u9488\u7684\u51fd\u6570\u65f6\uff0c\u4e00\u5b9a\u8981\u660e\u786e\u77e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[100,925,922],"class_list":["post-3561","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-c","tag-925","tag-922"],"views":7347,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3561","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3561"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3561\/revisions"}],"predecessor-version":[{"id":3565,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3561\/revisions\/3565"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}