{"id":2535,"date":"2018-04-16T11:13:10","date_gmt":"2018-04-16T03:13:10","guid":{"rendered":"https:\/\/boweihe.me\/?p=2535"},"modified":"2018-04-16T11:13:10","modified_gmt":"2018-04-16T03:13:10","slug":"no-core-will-be-dumped-when-calling-exit-in-a-signal-handler","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2535","title":{"rendered":"No core will be dumped when calling exit() in a signal handler"},"content":{"rendered":"<p>The customized abnormal signal handler (e.g. SIGSEGV handler) will function abnormally if we call exit() inside the handler: the program will exit &#8220;as normal&#8221; however it did had some severe memory issue.<br \/>\nSee code snippet below, try to run with \/ without &#8220;X&#8221; argument.<\/p>\n<pre class=\"theme:vs2012-black lang:c++ decode:true \">\/* exit example *\/\n#include &lt;stdio.h&gt;      \/* printf, fopen *\/\n#include &lt;stdlib.h&gt;     \/* exit, EXIT_FAILURE *\/\n#include &lt;signal.h&gt;     \/* signal *\/\n#include &lt;string.h&gt;\nbool disable_exit = false;\nvoid InitiateAbnormalShutdown()\n{\n    printf(\"ABNORMAL SHUTDOWN!!!\\n\");\n    if (!disable_exit)\n        exit(EXIT_FAILURE);\n}\nvoid AbnormalSignalHandler(int signo)\n{\n    signal(signo, SIG_DFL);\n    InitiateAbnormalShutdown();\n    raise(signo);\n}\nbool give_me_an_error()\n{\n    int* test = new int;\n    *test = 500;\n    delete test;\n    test = NULL;\n    *test = 900;\n    if (*test &gt; 500)\n    {\n        return false;\n    }\n    return true;\n}\nint main(int argc, char *argv[])\n{\n    if (argc &gt; 1)\n    {\n        printf(\"arg == %s\\n\", argv[1]);\n        if (strcmp(argv[1], \"X\") == 0)\n        {\n            printf(\"disable exit on abnormal handler...\\n\");\n            disable_exit = true;\n        }\n    }\n    signal(SIGSEGV, AbnormalSignalHandler);\n    give_me_an_error();\n    return 0;\n}<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:sh decode:true \">bowei.he:\/sandbox\/bowei.he\/temp$ .\/sigf\nABNORMAL SHUTDOWN!!!\nbowei.he:\/sandbox\/bowei.he\/temp$ .\/sigf X\narg == X\ndisable exit on abnormal handler...\nABNORMAL SHUTDOWN!!!\nSegmentation fault (core dumped)\n<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The customized abnormal signal handler (e.g. SIGSEGV handler) will function abnormally if we call exit() inside the handler: the program will exit &#8220;as normal&#8221; however it did had some severe memory issue. See code snippet below, try to run with \/ without &#8220;X&#8221; argument. \/* exit example *\/ #include &lt;stdio.h&gt; \/* printf, fopen *\/ #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-2535","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2535"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2535\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}