{"id":73358,"date":"2026-02-07T15:35:26","date_gmt":"2026-02-07T07:35:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/73358.html"},"modified":"2026-02-07T15:35:26","modified_gmt":"2026-02-07T07:35:26","slug":"linux%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%e7%b3%bb%e5%88%97-driversttysysrq","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/73358.html","title":{"rendered":"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 -- [drivers][tty]sysrq"},"content":{"rendered":"<hr \/>\n<p>title: sysrq categories:<\/p>\n<ul>\n<li>linux<\/li>\n<li>drivers<\/li>\n<li>tty tags:<\/li>\n<li>linux<\/li>\n<li>drivers<\/li>\n<li>tty abbrlink: 2db0ec3 date: 2025-10-15 13:52:27<\/li>\n<\/ul>\n<hr \/>\n<p>https:\/\/github.com\/wdfk-prog\/linux-study<\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>drivers\/tty\/sysrq.c \u5185\u6838\u9b54\u6cd5\u952e(Magic SysRq Key) \u7cfb\u7edf\u7684\u7ec8\u6781\u5e94\u6025\u4e0e\u8c03\u8bd5\u5de5\u5177<\/li>\n<li>\n<ul>\n<li>\n<ul>\n<li>\u5386\u53f2\u4e0e\u80cc\u666f<\/li>\n<li>\n<ul>\n<li>\u8fd9\u9879\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u4ec0\u4e48\u7279\u5b9a\u95ee\u9898\u800c\u8bde\u751f\u7684&#xff1f;<\/li>\n<li>\u5b83\u7684\u53d1\u5c55\u7ecf\u5386\u4e86\u54ea\u4e9b\u91cd\u8981\u7684\u91cc\u7a0b\u7891\u6216\u7248\u672c\u8fed\u4ee3&#xff1f;<\/li>\n<li>\u76ee\u524d\u8be5\u6280\u672f\u7684\u793e\u533a\u6d3b\u8dc3\u5ea6\u548c\u4e3b\u6d41\u5e94\u7528\u60c5\u51b5\u5982\u4f55&#xff1f;<\/li>\n<\/ul>\n<\/li>\n<li>\u6838\u5fc3\u539f\u7406\u4e0e\u8bbe\u8ba1<\/li>\n<li>\n<ul>\n<li>\u5b83\u7684\u6838\u5fc3\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48&#xff1f;<\/li>\n<li>\u5b83\u7684\u4e3b\u8981\u4f18\u52bf\u4f53\u73b0\u5728\u54ea\u4e9b\u65b9\u9762&#xff1f;<\/li>\n<li>\u5b83\u5b58\u5728\u54ea\u4e9b\u5df2\u77e5\u7684\u52a3\u52a3\u52bf\u3001\u5c40\u9650\u6027\u6216\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u4e0d\u9002\u7528\u6027&#xff1f;<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528\u573a\u666f<\/li>\n<li>\n<ul>\n<li>\u5728\u54ea\u4e9b\u5177\u4f53\u7684\u4e1a\u52a1\u6216\u6280\u672f\u573a\u666f\u4e0b&#xff0c;\u5b83\u662f\u9996\u9009\u89e3\u51b3\u65b9\u6848&#xff1f;<\/li>\n<li>\u662f\u5426\u6709\u4e0d\u63a8\u8350\u4f7f\u7528\u8be5\u6280\u672f\u7684\u573a\u666f&#xff1f;\u4e3a\u4ec0\u4e48&#xff1f;<\/li>\n<\/ul>\n<\/li>\n<li>\u5bf9\u6bd4\u5206\u6790<\/li>\n<li>\n<ul>\n<li>\u8bf7\u5c06\u5176 \u4e0e \u5176\u4ed6\u76f8\u4f3c\u6280\u672f \u8fdb\u884c\u8be6\u7ec6\u5bf9\u6bd4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Magic SysRq \u914d\u7f6e\u63a5\u53e3&#xff1a;init_sysrq_sysctl \u4e0e sysrq_sysctl_handler<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u8f93\u5165\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;sysrq_handler \u5b9a\u4e49\u4e0e\u6ce8\u518c<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u4e8b\u4ef6\u5904\u7406\u56de\u8c03&#xff1a;sysrq_connect, sysrq_disconnect, \u4e0e sysrq_filter<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq Keypress State Machine: sysrq_handle_keypress<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq Work Handlers: \u5bc6\u94a5\u91cd\u65b0\u6ce8\u5165\u4e0e\u7cfb\u7edf\u91cd\u7f6e<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u547d\u4ee4\u64cd\u4f5c&#xff1a;\u5168\u9762\u5b9a\u4e49\u96c6<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u547d\u4ee4\u6267\u884c\u5f15\u64ce: __handle_sysrq<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u91cd\u7f6e\u5e8f\u5217\u68c0\u6d4b\u5668&#xff1a; sysrq_detect_reset_sequence<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq \u91cd\u7f6e\u5e8f\u5217\u89e3\u6790\u5668&#xff1a; sysrq_parse_reset_sequence<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>SysRq Procfs \u89e6\u53d1\u63a5\u53e3&#xff1a;\/proc\/sysrq-trigger<\/li>\n<li>\n<ul>\n<li>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/li>\n<li>\u4ee3\u7801\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207073525-6986eb3d0c39c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <\/p>\n<h2>drivers\/tty\/sysrq.c \u5185\u6838\u9b54\u6cd5\u952e(Magic SysRq Key) \u7cfb\u7edf\u7684\u7ec8\u6781\u5e94\u6025\u4e0e\u8c03\u8bd5\u5de5\u5177<\/h2>\n<h4>\u5386\u53f2\u4e0e\u80cc\u666f<\/h4>\n<h5>\u8fd9\u9879\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u4ec0\u4e48\u7279\u5b9a\u95ee\u9898\u800c\u8bde\u751f\u7684&#xff1f;<\/h5>\n<p>Magic SysRq Key&#xff08;\u9b54\u6cd5\u7cfb\u7edf\u8bf7\u6c42\u952e&#xff09;\u662f\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e2a\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u5185\u6838\u5f00\u53d1\u8005\u9762\u4e34\u7684\u7ec8\u6781\u96be\u9898&#xff1a;\u5f53\u7cfb\u7edf\u5b8c\u5168\u51bb\u7ed3&#xff0c;\u65e0\u6cd5\u901a\u8fc7\u7f51\u7edc&#xff08;SSH&#xff09;\u6216\u672c\u5730\u7ec8\u7aef\u54cd\u5e94\u65f6&#xff0c;\u5982\u4f55\u8fdb\u884c\u8bca\u65ad\u6216\u5b89\u5168\u5730\u91cd\u542f&#xff1f;<\/p>\n<p>\u5728\u7cfb\u7edf\u4e25\u91cd\u6b7b\u9501\u6216\u8d1f\u8f7d\u6781\u9ad8\u7684\u60c5\u51b5\u4e0b&#xff0c;\u7528\u6237\u7a7a\u95f4\u7684\u7a0b\u5e8f\u3001Shell\u3001\u751a\u81f3\u5185\u6838\u7684\u8c03\u5ea6\u5668\u90fd\u53ef\u80fd\u505c\u6b62\u5de5\u4f5c\u3002\u7136\u800c&#xff0c;\u5185\u6838\u7684\u5e95\u5c42\u90e8\u5206&#xff0c;\u5982\u952e\u76d8\u4e2d\u65ad\u5904\u7406&#xff0c;\u53ef\u80fd\u4ecd\u7136\u5728\u8fd0\u884c\u3002SysRq\u673a\u5236\u5c31\u662f\u5229\u7528\u8fd9\u4e2a\u201c\u4e00\u7ebf\u751f\u673a\u201d&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u4e2a\u76f4\u63a5\u4e0e\u5185\u6838\u5e95\u5c42\u8fdb\u884c\u901a\u4fe1\u7684\u201c\u540e\u95e8\u201d\u3002\u5b83\u5141\u8bb8\u64cd\u4f5c\u5458\u901a\u8fc7\u4e00\u4e2a\u7279\u6b8a\u7684\u952e\u76d8\u7ec4\u5408\u952e&#xff0c;\u7ed5\u8fc7\u6240\u6709\u4e0a\u5c42\u8f6f\u4ef6\u6808&#xff0c;\u76f4\u63a5\u5411\u5185\u6838\u53d1\u9001\u9884\u5b9a\u4e49\u7684\u547d\u4ee4&#xff0c;\u4ece\u800c&#xff1a;<\/p>\n<ul>\n<li>\u83b7\u53d6\u8c03\u8bd5\u4fe1\u606f&#xff1a;\u5728\u7cfb\u7edf\u5d29\u6e83\u7684\u77ac\u95f4&#xff0c;\u6293\u53d6\u4efb\u52a1\u72b6\u6001\u3001\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7b49\u5feb\u7167\u3002<\/li>\n<li>\u6267\u884c\u5e94\u6025\u64cd\u4f5c&#xff1a;\u5982\u540c\u6b65\u78c1\u76d8\u3001\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u91cd\u65b0\u6302\u8f7d\u6587\u4ef6\u7cfb\u7edf\u3002<\/li>\n<li>\u5b89\u5168\u5730\u91cd\u542f\u7cfb\u7edf&#xff1a;\u5728\u522b\u65e0\u9009\u62e9\u65f6&#xff0c;\u6267\u884c\u4e00\u4e2a\u6bd4\u76f4\u63a5\u6309\u7535\u6e90\u952e&#xff08;\u786c\u91cd\u542f&#xff09;\u66f4\u5b89\u5168\u7684\u91cd\u542f\u5e8f\u5217&#xff0c;\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u6570\u636e\u635f\u574f\u7684\u98ce\u9669\u3002<\/li>\n<\/ul>\n<h5>\u5b83\u7684\u53d1\u5c55\u7ecf\u5386\u4e86\u54ea\u4e9b\u91cd\u8981\u7684\u91cc\u7a0b\u7891\u6216\u7248\u672c\u8fed\u4ee3&#xff1f;<\/h5>\n<li>\u6e90\u81eaSGI IRIX&#xff1a;\u8fd9\u4e2a\u6982\u5ff5\u5e76\u975eLinux\u539f\u521b&#xff0c;\u800c\u662f\u4eceSGI\u7684IRIX\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u501f\u9274\u800c\u6765&#xff0c;\u5e76\u5728Linux 2.1.43\u5185\u6838\u4e2d\u9996\u6b21\u88ab\u5f15\u5165\u3002<\/li>\n<li>\u547d\u4ee4\u96c6\u7684\u4e0d\u65ad\u6269\u5145&#xff1a;\u6700\u521d&#xff0c;SysRq\u53ea\u652f\u6301\u5c11\u6570\u51e0\u4e2a\u6838\u5fc3\u547d\u4ee4&#xff08;\u5982\u91cd\u542f\u3001\u540c\u6b65&#xff09;\u3002\u968f\u7740\u5185\u6838\u7684\u53d1\u5c55&#xff0c;\u5b83\u7684\u529f\u80fd\u88ab\u6781\u5927\u5730\u4e30\u5bcc\u4e86&#xff0c;\u52a0\u5165\u4e86\u5927\u91cf\u7528\u4e8e\u8c03\u8bd5\u7684\u547d\u4ee4&#xff0c;\u4f8b\u5982\u663e\u793a\u4efb\u52a1\u72b6\u6001&#xff08;t&#xff09;\u3001\u663e\u793a\u5185\u5b58\u4fe1\u606f&#xff08;m&#xff09;\u3001\u624b\u52a8\u89e6\u53d1OOM Killer&#xff08;f&#xff09;\u7b49\u3002<\/li>\n<li>\/proc\/sysrq-trigger\u63a5\u53e3\u7684\u5f15\u5165&#xff08;\u5173\u952e\u91cc\u7a0b\u7891&#xff09;&#xff1a;\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u8fdb\u6b65\u3002\u9664\u4e86\u7269\u7406\u952e\u76d8&#xff0c;\u5185\u6838\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\/proc\/sysrq-trigger\u6587\u4ef6\u63a5\u53e3\u3002\u5411\u8fd9\u4e2a\u6587\u4ef6\u5199\u5165\u4e00\u4e2a\u547d\u4ee4\u5b57\u7b26&#xff0c;\u5176\u6548\u679c\u7b49\u540c\u4e8e\u5728\u7269\u7406\u952e\u76d8\u4e0a\u6309\u4e0b\u5bf9\u5e94\u7684\u7ec4\u5408\u952e\u3002\u8fd9\u4f7f\u5f97SysRq\u7684\u529f\u80fd\u53ef\u4ee5\u88ab\u8fdc\u7a0b\u89e6\u53d1&#xff08;\u4f8b\u5982&#xff0c;\u901a\u8fc7\u5e26\u5916\u7ba1\u7406\u63a7\u5236\u53f0&#xff09;\u548c\u811a\u672c\u5316&#xff0c;\u6781\u5927\u5730\u589e\u5f3a\u4e86\u5176\u5728\u6570\u636e\u4e2d\u5fc3\u73af\u5883\u4e0b\u7684\u5b9e\u7528\u6027\u3002<\/li>\n<h5>\u76ee\u524d\u8be5\u6280\u672f\u7684\u793e\u533a\u6d3b\u8dc3\u5ea6\u548c\u4e3b\u6d41\u5e94\u7528\u60c5\u51b5\u5982\u4f55&#xff1f;<\/h5>\n<p>SysRq\u662f\u4e00\u4e2a\u975e\u5e38\u6210\u719f\u548c\u7a33\u5b9a\u7684\u5185\u6838\u7279\u6027\u3002\u5b83\u88ab\u5e7f\u6cdb\u8ba4\u4e3a\u662fLinux\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u5185\u6838\u5f00\u53d1\u8005\u7684\u5fc5\u5907\u5e94\u6025\u5de5\u5177\u3002<\/p>\n<ul>\n<li>\u670d\u52a1\u5668\u73af\u5883&#xff1a;\u5728\u751f\u4ea7\u670d\u52a1\u5668\u4e0a&#xff0c;\u5b83\u901a\u5e38\u88ab\u9ed8\u8ba4\u542f\u7528&#xff0c;\u4f5c\u4e3a\u7cfb\u7edf\u65e0\u54cd\u5e94\u65f6\u7684\u6700\u540e\u6551\u63f4\u624b\u6bb5\u3002<\/li>\n<li>\u684c\u9762\u73af\u5883&#xff1a;\u51fa\u4e8e\u5b89\u5168\u8003\u8651&#xff08;\u7269\u7406\u63a5\u89e6\u952e\u76d8\u7684\u4eba\u53ef\u4ee5\u8f7b\u6613\u5730\u4f7f\u7cfb\u7edf\u91cd\u542f&#xff09;&#xff0c;\u8bb8\u591a\u684c\u9762\u53d1\u884c\u7248\u53ef\u80fd\u9ed8\u8ba4\u9650\u5236\u6216\u7981\u7528\u4e86SysRq\u7684\u67d0\u4e9b\u529f\u80fd\u3002<\/li>\n<li>\u5185\u6838\u5f00\u53d1&#xff1a;\u5728\u8c03\u8bd5\u590d\u6742\u7684\u5185\u6838\u6b7b\u9501\u6216\u6027\u80fd\u95ee\u9898\u65f6&#xff0c;SysRq\u63d0\u4f9b\u7684\u5feb\u7167\u529f\u80fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/li>\n<\/ul>\n<h4>\u6838\u5fc3\u539f\u7406\u4e0e\u8bbe\u8ba1<\/h4>\n<h5>\u5b83\u7684\u6838\u5fc3\u5de5\u4f5c\u539f\u7406\u662f\u4ec0\u4e48&#xff1f;<\/h5>\n<p>drivers\/tty\/sysrq.c \u7684\u6838\u5fc3\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u952e\u76d8\u8f93\u5165\u8fc7\u6ee4\u5668\u548c\u547d\u4ee4\u5206\u53d1\u5668\u3002<\/p>\n<li>\u4f4e\u5c42\u952e\u76d8\u4e8b\u4ef6\u62e6\u622a&#xff1a;\u5f53\u4f60\u5728\u952e\u76d8\u4e0a\u6309\u4e0bAlt &#043; SysRq &#043; &lt;command_key&gt;\u65f6&#xff0c;\u952e\u76d8\u9a71\u52a8\u7a0b\u5e8f\u4f1a\u8bc6\u522b\u51fa\u8fd9\u4e2a\u7279\u6b8a\u7684\u7ec4\u5408\u3002sysrq.c\u4e2d\u7684\u4ee3\u7801\u4f1a\u4f5c\u4e3a\u4e00\u4e2a\u8fc7\u6ee4\u5668&#xff08;sysrq_filter&#xff09;&#xff0c;\u5728\u952e\u76d8\u4e8b\u4ef6\u88ab\u53d1\u9001\u5230\u6b63\u5e38\u7684\u7ec8\u7aef\u5904\u7406\u7a0b\u5e8f\u4e4b\u524d\u5c06\u5176\u62e6\u622a\u3002<\/li>\n<li>\u547d\u4ee4\u67e5\u627e\u4e0e\u5206\u53d1&#xff1a;sysrq.c\u5185\u90e8\u7ef4\u62a4\u7740\u4e00\u4e2a\u9759\u6001\u7684\u547d\u4ee4\u8868&#xff08;sysrq_key_table&#xff09;\u3002\u8fd9\u4e2a\u8868\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u7ec4&#xff0c;\u5c06\u547d\u4ee4\u5b57\u7b26&#xff08;\u5982&#039;b&#039;\u4ee3\u8868reboot&#xff09;\u6620\u5c04\u5230\u4e00\u4e2a\u51fd\u6570\u6307\u9488&#xff08;\u5982sysrq_handle_reboot&#xff09;\u3002<\/li>\n<li>\u76f4\u63a5\u3001\u540c\u6b65\u6267\u884c&#xff1a;\u4e00\u65e6\u627e\u5230\u5339\u914d\u7684\u547d\u4ee4&#xff0c;\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\u5c31\u4f1a\u88ab\u7acb\u5373\u3001\u76f4\u63a5\u5730\u5728\u4e2d\u65ad\u4e0a\u4e0b\u6587\u4e2d\u6216\u4e13\u7528\u7684\u5185\u6838\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\u8fd9\u662f\u5176\u201c\u9b54\u6cd5\u201d\u7684\u5173\u952e\u6240\u5728&#xff1a;\u5b83\u7ed5\u8fc7\u4e86\u5185\u6838\u7684\u8c03\u5ea6\u5668\u3001\u8fdb\u7a0b\u7ba1\u7406\u548cVFS\u7b49\u6240\u6709\u53ef\u80fd\u5df2\u7ecf\u6b7b\u9501\u6216\u65e0\u54cd\u5e94\u7684\u4e0a\u5c42\u7cfb\u7edf\u3002\u5b83\u662f\u4e00\u79cd\u5bf9\u5185\u6838\u7684\u4f4e\u7ea7\u522b\u3001\u7279\u6743\u8c03\u7528\u3002<\/li>\n<li>\/proc\u63a5\u53e3&#xff1a;\u5f53\u5411\/proc\/sysrq-trigger\u5199\u5165\u65f6&#xff0c;\u5185\u6838\u4f1a\u76f4\u63a5\u8c03\u7528\u4e0e\u952e\u76d8\u8def\u5f84\u76f8\u540c\u7684\u547d\u4ee4\u5206\u53d1\u903b\u8f91&#xff0c;\u4ece\u800c\u5b9e\u73b0\u4e86\u4e24\u79cd\u65b9\u5f0f\u7684\u529f\u80fd\u7edf\u4e00\u3002<\/li>\n<h5>\u5b83\u7684\u4e3b\u8981\u4f18\u52bf\u4f53\u73b0\u5728\u54ea\u4e9b\u65b9\u9762&#xff1f;<\/h5>\n<ul>\n<li>\u7ec8\u6781\u53ef\u9760\u6027&#xff1a;\u53ea\u8981\u5185\u6838\u7684\u4e2d\u65ad\u5904\u7406\u4ecd\u5728\u5de5\u4f5c&#xff0c;SysRq\u5c31\u6709\u53ef\u80fd\u6210\u529f\u6267\u884c&#xff0c;\u662f\u540d\u526f\u5176\u5b9e\u7684\u201c\u6700\u540e\u624b\u6bb5\u201d\u3002<\/li>\n<li>\u7ed5\u8fc7\u7cfb\u7edf\u72b6\u6001&#xff1a;\u4e0d\u4f9d\u8d56\u4e8e\u7528\u6237\u7a7a\u95f4\u3001\u8c03\u5ea6\u5668\u6216\u5927\u591a\u6570\u5185\u6838\u5b50\u7cfb\u7edf\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<li>\u4e30\u5bcc\u7684\u529f\u80fd\u96c6&#xff1a;\u63d0\u4f9b\u4e86\u4ece\u5b89\u5168\u91cd\u542f\u5230\u6df1\u5ea6\u8c03\u8bd5\u5feb\u7167\u7684\u591a\u79cd\u5f3a\u5927\u529f\u80fd\u3002<\/li>\n<li>\u8fdc\u7a0b\u53ef\u63a7&#xff1a;\u901a\u8fc7\/proc\u63a5\u53e3&#xff0c;\u5b8c\u7f8e\u9002\u914d\u4e86\u73b0\u4ee3\u6570\u636e\u4e2d\u5fc3\u7684\u8fdc\u7a0b\u7ba1\u7406\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h5>\u5b83\u5b58\u5728\u54ea\u4e9b\u5df2\u77e5\u7684\u52a3\u52a3\u52bf\u3001\u5c40\u9650\u6027\u6216\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u4e0d\u9002\u7528\u6027&#xff1f;<\/h5>\n<ul>\n<li>\u6781\u5176\u5371\u9669&#xff1a;\u8fd9\u662f\u5176\u6700\u5927\u7684\u201c\u52a3\u52bf\u201d\u3002SysRq\u547d\u4ee4\u662f\u7c97\u66b4\u4e14\u7acb\u5373\u6267\u884c\u7684&#xff0c;\u5b83\u4e0d\u4f1a\u7ed9\u5e94\u7528\u7a0b\u5e8f\u4efb\u4f55\u4f18\u96c5\u9000\u51fa\u7684\u673a\u4f1a\u3002\u4f8b\u5982&#xff0c;reboot (b) \u547d\u4ee4\u7684\u6548\u679c\u51e0\u4e4e\u7b49\u540c\u4e8e\u6309\u7269\u7406\u91cd\u7f6e\u6309\u94ae&#xff0c;\u4e0d\u4f1a\u540c\u6b65\u78c1\u76d8\u6216\u5378\u8f7d\u6587\u4ef6\u7cfb\u7edf\u3002<\/li>\n<li>\u5b89\u5168\u98ce\u9669&#xff1a;\u5982\u679c\u7cfb\u7edf\u542f\u7528\u4e86SysRq&#xff0c;\u4efb\u4f55\u80fd\u591f\u7269\u7406\u63a5\u89e6\u952e\u76d8\u7684\u4eba\u90fd\u53ef\u4ee5\u65e0\u89c6\u7cfb\u7edf\u6743\u9650&#xff0c;\u76f4\u63a5\u91cd\u542f\u6216\u5173\u95ed\u7cfb\u7edf\u3002\u540c\u6837&#xff0c;\u4efb\u4f55\u83b7\u5f97\u4e86root\u6743\u9650\u7684\u8fdb\u7a0b\u90fd\u53ef\u4ee5\u901a\u8fc7\/proc\u63a5\u53e3\u9020\u6210\u62d2\u7edd\u670d\u52a1&#xff08;DoS&#xff09;\u3002<\/li>\n<li>\u975e\u4e07\u80fd&#xff1a;\u5982\u679c\u5185\u6838\u5df2\u7ecf\u5b8c\u5168\u5d29\u6e83&#xff08;\u4f8b\u5982&#xff0c;\u53d1\u751f\u4e09\u91cd\u6545\u969c\u6216\u4e2d\u65ad\u5904\u7406\u672c\u8eab\u88ab\u7834\u574f&#xff09;&#xff0c;SysRq\u4e5f\u5c06\u65e0\u80fd\u4e3a\u529b\u3002<\/li>\n<\/ul>\n<h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<h5>\u5728\u54ea\u4e9b\u5177\u4f53\u7684\u4e1a\u52a1\u6216\u6280\u672f\u573a\u666f\u4e0b&#xff0c;\u5b83\u662f\u9996\u9009\u89e3\u51b3\u65b9\u6848&#xff1f;<\/h5>\n<p>SysRq\u662f\u5904\u7406\u7cfb\u7edf\u5b8c\u5168\u65e0\u54cd\u5e94\u573a\u666f\u7684\u552f\u4e00\u6807\u51c6\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<ul>\n<li>\u5b89\u5168\u91cd\u542f\u51bb\u7ed3\u7684\u670d\u52a1\u5668&#xff1a;\u8fd9\u662f\u6700\u7ecf\u5178\u7684\u4f7f\u7528\u573a\u666f\u3002\u5f53\u670d\u52a1\u5668SSH\u4e0d\u901a\u3001\u63a7\u5236\u53f0\u65e0\u54cd\u5e94\u65f6&#xff0c;\u7ba1\u7406\u5458\u4f1a\u4f9d\u6b21\u6309\u4e0b\u8457\u540d\u7684REISUB\u5e8f\u5217&#xff1a;\n<ul>\n<li>Raw (\u89e3\u9664\u952e\u76d8\u72ec\u5360), End (\u5411\u6240\u6709\u8fdb\u7a0b\u53d1\u9001SIGTERM), Ignore (\u5411\u6240\u6709\u8fdb\u7a0b\u53d1\u9001SIGKILL), Sync (\u540c\u6b65\u6240\u6709\u6587\u4ef6\u7cfb\u7edf), Unmount (\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u91cd\u65b0\u6302\u8f7d), Boot (\u7acb\u5373\u91cd\u542f)\u3002\u8fd9\u4e2a\u5e8f\u5217\u6700\u5927\u9650\u5ea6\u5730\u4fdd\u8bc1\u4e86\u5728\u91cd\u542f\u524d\u7684\u6570\u636e\u5b89\u5168\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8bca\u65ad\u5185\u6838\u6b7b\u9501&#xff1a;\u7cfb\u7edf\u5361\u6b7b&#xff0c;\u6000\u7591\u662f\u5185\u6838\u9501\u4e89\u7528\u3002\u53ef\u4ee5\u901a\u8fc7SysRq\u89e6\u53d1&#xff1a;\n<ul>\n<li>t: \u663e\u793a\u6240\u6709\u4efb\u52a1\u7684\u72b6\u6001\u53ca\u5176\u8c03\u7528\u6808\u3002<\/li>\n<li>w: \u663e\u793a\u6240\u6709\u5904\u4e8e\u4e0d\u53ef\u4e2d\u65ad\u7761\u7720\u72b6\u6001&#xff08;\u901a\u5e38\u662f\u7b49\u5f85I\/O\u6216\u9501&#xff09;\u7684\u4efb\u52a1\u3002<\/li>\n<li>m: \u663e\u793a\u5185\u5b58\u4f7f\u7528\u4fe1\u606f\u3002 \u5c06\u8fd9\u4e9b\u4fe1\u606f&#xff08;\u901a\u5e38\u9700\u8981\u901a\u8fc7\u4e32\u884c\u63a7\u5236\u53f0\u6216netconsole\u6355\u83b7&#xff09;\u7528\u4e8e\u4e8b\u540e\u5206\u6790\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u6a21\u62df\u7cfb\u7edf\u6545\u969c&#xff1a;\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528SysRq\u624b\u52a8\u89e6\u53d1OOM Killer (f) \u6216\u5185\u6838\u5d29\u6e83 (c)&#xff0c;\u4ee5\u6d4b\u8bd5\u5176\u5e94\u7528\u7a0b\u5e8f\u6216\u76d1\u63a7\u7cfb\u7edf\u7684\u9c81\u68d2\u6027\u3002<\/li>\n<\/ul>\n<h5>\u662f\u5426\u6709\u4e0d\u63a8\u8350\u4f7f\u7528\u8be5\u6280\u672f\u7684\u573a\u666f&#xff1f;\u4e3a\u4ec0\u4e48&#xff1f;<\/h5>\n<p>\u7edd\u5bf9\u4e0d\u5e94\u8be5\u5728\u4efb\u4f55\u53ef\u4ee5\u6b63\u5e38\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528SysRq\u6765\u91cd\u542f\u6216\u5173\u95ed\u7cfb\u7edf&#xff01;<\/p>\n<ul>\n<li>\u66ff\u4ee3shutdown\/reboot\u547d\u4ee4&#xff1a;shutdown\u3001reboot\u3001systemctl poweroff\u7b49\u547d\u4ee4\u4f1a\u6267\u884c\u4e00\u4e2a\u4f18\u96c5\u7684\u5173\u673a\u6d41\u7a0b&#xff1a;\u901a\u77e5\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684\u670d\u52a1&#xff0c;\u5141\u8bb8\u5b83\u4eec\u4fdd\u5b58\u6570\u636e\u5e76\u5e72\u51c0\u5730\u9000\u51fa&#xff1b;\u540c\u6b65\u5e76\u5378\u8f7d\u6587\u4ef6\u7cfb\u7edf&#xff1b;\u6700\u540e\u624d\u5173\u95ed\u7535\u6e90\u6216\u91cd\u542f\u3002SysRq\u5b8c\u5168\u8df3\u8fc7\u4e86\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u662f\u975e\u4f18\u96c5\u7684\u3001\u66b4\u529b\u7684\u3002<\/li>\n<\/ul>\n<h4>\u5bf9\u6bd4\u5206\u6790<\/h4>\n<h5>\u8bf7\u5c06\u5176 \u4e0e \u5176\u4ed6\u76f8\u4f3c\u6280\u672f \u8fdb\u884c\u8be6\u7ec6\u5bf9\u6bd4\u3002<\/h5>\n<table>\n<tr>\u7279\u6027Magic SysRq\u4f18\u96c5\u5173\u673a\/\u91cd\u542f (shutdown, reboot)\u770b\u95e8\u72d7\u5b9a\u65f6\u5668 (Watchdog Timer)<\/tr>\n<tbody>\n<tr>\n<td align=\"left\">\u529f\u80fd\u6982\u8ff0<\/td>\n<td align=\"left\">\u624b\u52a8\u7684\u3001\u4f4e\u7ea7\u522b\u7684\u5185\u6838\u547d\u4ee4\u63a5\u53e3&#xff0c;\u7528\u4e8e\u5e94\u6025\u548c\u8c03\u8bd5\u3002<\/td>\n<td align=\"left\">\u8f6f\u4ef6\u63a7\u5236\u7684\u3001\u9ad8\u7ea7\u522b\u7684\u3001\u6709\u5e8f\u7684\u7cfb\u7edf\u5173\u95ed\u6d41\u7a0b\u3002<\/td>\n<td align=\"left\">\u81ea\u52a8\u7684\u3001\u786c\u4ef6\u6216\u8f6f\u4ef6\u7684\u7cfb\u7edf\u590d\u4f4d\u673a\u5236\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u89e6\u53d1\u65b9\u5f0f<\/td>\n<td align=\"left\">\u4eba\u5de5\u901a\u8fc7\u952e\u76d8\u7ec4\u5408\u952e\u6216\u5199\u5165\/proc\u6587\u4ef6\u3002<\/td>\n<td align=\"left\">\u7528\u6237\u6216\u811a\u672c\u901a\u8fc7\u6267\u884c\u547d\u4ee4&#xff0c;\u7ecf\u7531systemd\u6216init\u3002<\/td>\n<td align=\"left\">\u5185\u6838\u6216\u5b88\u62a4\u8fdb\u7a0b\u672a\u80fd\u5728\u4e00\u4e2a\u8bbe\u5b9a\u7684\u65f6\u95f4\u7a97\u53e3\u5185\u201c\u5582\u72d7\u201d&#xff08;ping&#xff09;&#xff0c;\u5bfc\u81f4\u786c\u4ef6\u81ea\u52a8\u89e6\u53d1\u590d\u4f4d\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u6267\u884c\u8fc7\u7a0b<\/td>\n<td align=\"left\">\u7acb\u5373\u3001\u7c97\u66b4\u3002\u76f4\u63a5\u5728\u5185\u6838\u5e95\u5c42\u6267\u884c&#xff0c;\u7ed5\u8fc7\u5927\u90e8\u5206\u7cfb\u7edf\u670d\u52a1\u3002<\/td>\n<td align=\"left\">\u4f18\u96c5\u3001\u6709\u5e8f\u3002\u901a\u77e5\u670d\u52a1&#xff0c;\u8fd0\u884c\u5173\u673a\u811a\u672c&#xff0c;\u540c\u6b65\u78c1\u76d8&#xff0c;\u5378\u8f7d\u6587\u4ef6\u7cfb\u7edf\u3002<\/td>\n<td align=\"left\">\u6700\u7c97\u66b4\u3002\u901a\u5e38\u7b49\u540c\u4e8e\u786c\u4ef6\u590d\u4f4d&#xff0c;\u662f\u5b8c\u5168\u81ea\u52a8\u7684\u6700\u540e\u4fdd\u969c\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u4e3b\u8981\u76ee\u6807<\/td>\n<td align=\"left\">\u8bca\u65ad\u548c\u624b\u52a8\u6062\u590d\u3002<\/td>\n<td align=\"left\">\u6b63\u5e38\u7684\u3001\u8ba1\u5212\u5185\u7684\u7cfb\u7edf\u7ef4\u62a4\u3002<\/td>\n<td align=\"left\">\u81ea\u52a8\u4ece\u5b8c\u5168\u6b7b\u9501\u4e2d\u6062\u590d\u3002<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u9002\u7528\u573a\u666f<\/td>\n<td align=\"left\">\u7cfb\u7edf\u5b8c\u5168\u65e0\u54cd\u5e94&#xff0c;\u4f46\u5185\u6838\u4e2d\u65ad\u5c1a\u5b58\u3002<\/td>\n<td align=\"left\">\u6240\u6709\u6b63\u5e38\u7684\u5173\u673a\u548c\u91cd\u542f\u64cd\u4f5c\u3002<\/td>\n<td align=\"left\">\u65e0\u4eba\u503c\u5b88\u7684\u3001\u8981\u6c42\u9ad8\u53ef\u7528\u6027\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u6216\u670d\u52a1\u5668\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Magic SysRq \u914d\u7f6e\u63a5\u53e3&#xff1a;init_sysrq_sysctl \u4e0e sysrq_sysctl_handler<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86 Linux \u5185\u6838\u4e2d\u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u914d\u7f6e\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u7684 sysctl \u63a5\u53e3\u7684\u5b9e\u73b0\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;\u5728 \/proc\/sys\/kernel\/ \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a sysrq \u7684\u6587\u4ef6&#xff0c;\u5141\u8bb8\u7cfb\u7edf\u7ba1\u7406\u5458\u901a\u8fc7\u8bfb\u5199\u8be5\u6587\u4ef6\u6765\u67e5\u8be2\u548c\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u7684\u63a9\u7801&#xff08;sysrq_enabled&#xff09;&#xff0c;\u4ece\u800c\u63a7\u5236 SysRq \u529f\u80fd\u7684\u5f00\u5173\u4ee5\u53ca\u5177\u4f53\u54ea\u4e9b SysRq \u547d\u4ee4\u662f\u5141\u8bb8\u6267\u884c\u7684\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u72b6\u6001\u8f6c\u6362\u903b\u8f91&#xff0c;\u786e\u4fdd\u53ea\u6709\u5728 SysRq \u529f\u80fd\u4ece\u201c\u7981\u7528\u201d\u53d8\u4e3a\u201c\u542f\u7528\u201d&#xff08;\u6216\u53cd\u4e4b&#xff09;\u65f6&#xff0c;\u624d\u4f1a\u53bb\u6ce8\u518c\u6216\u6ce8\u9500\u5e95\u5c42\u7684\u8f93\u5165\u5904\u7406\u53e5\u67c4&#xff0c;\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u8d44\u6e90\u7ba1\u7406\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u673a\u5236\u662f\u5185\u6838 sysctl \u6846\u67b6\u4e0e SysRq \u5b50\u7cfb\u7edf\u4e4b\u95f4\u7684\u6865\u6881&#xff0c;\u5176\u8bbe\u8ba1\u4f53\u73b0\u4e86\u5b89\u5168\u914d\u7f6e\u548c\u52a8\u6001\u8d44\u6e90\u7ba1\u7406\u7684\u539f\u5219\u3002<\/p>\n<li>\n<p>\u81ea\u5b9a\u4e49 Sysctl \u5904\u7406 (sysrq_sysctl_handler):<\/p>\n<ul>\n<li>\u4e0e\u4e0a\u6587\u5206\u6790\u7684 sysctl_max_threads \u7c7b\u4f3c&#xff0c;\u6b64\u63a5\u53e3\u4e5f\u91c7\u7528\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570\u3002sysrq_sysctl_table \u7684 .data \u6210\u5458\u4e3a NULL&#xff0c;\u6240\u6709\u64cd\u4f5c\u90fd\u59d4\u6258\u7ed9 sysrq_sysctl_handler\u3002<\/li>\n<li>\u5b83\u540c\u6837\u9075\u5faa\u4e86**\u201c\u672c\u5730\u526f\u672c\u4e0e\u9a8c\u8bc1\u540e\u66f4\u65b0\u201d\u7684\u5b89\u5168\u6a21\u5f0f&#xff1a; a. \u5728\u51fd\u6570\u5f00\u59cb\u65f6&#xff0c;\u5b83\u8c03\u7528 sysrq_mask() \u83b7\u53d6\u5f53\u524d\u7684 SysRq \u63a9\u7801&#xff0c;\u5e76\u5b58\u5165\u4e00\u4e2a\u672c\u5730\u53d8\u91cf tmp**\u3002 b. \u5b83\u5c06\u4e00\u4e2a\u4e34\u65f6\u7684 ctl_table \u7ed3\u6784 t \u7684 .data \u6307\u9488\u6307\u5411\u8fd9\u4e2a\u672c\u5730\u53d8\u91cf tmp\u3002 c. \u7136\u540e&#xff0c;\u5b83\u8c03\u7528\u6807\u51c6\u7684 proc_dointvec_minmax \u51fd\u6570\u6765\u5904\u7406\u4e0e\u7528\u6237\u7a7a\u95f4\u7684\u4ea4\u4e92\u3002\u8bfb\u64cd\u4f5c\u4f1a\u8fd4\u56de tmp \u7684\u503c&#xff1b;\u5199\u64cd\u4f5c\u4f1a\u5c06\u7528\u6237\u8f93\u5165\u7684\u503c\u89e3\u6790\u5e76\u5b58\u5165 tmp\u3002 d. \u53ea\u6709\u5728\u5199\u64cd\u4f5c\u6210\u529f\u540e&#xff0c;\u5b83\u624d\u4f1a\u8c03\u7528 sysrq_toggle_support(tmp)&#xff0c;\u5c06\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u3001\u5b58\u50a8\u5728\u672c\u5730\u53d8\u91cf tmp \u4e2d\u7684\u65b0\u63a9\u7801\u5e94\u7528\u5230\u7cfb\u7edf\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u52a8\u6001\u53e5\u67c4\u7ba1\u7406 (sysrq_toggle_support):<\/p>\n<ul>\n<li>\u8fd9\u662f\u6b64\u673a\u5236\u7684\u6838\u5fc3\u903b\u8f91&#xff0c;\u8d1f\u8d23\u5c06\u63a9\u7801\u7684\u6539\u53d8\u8f6c\u5316\u4e3a\u5b9e\u9645\u7684\u884c\u4e3a\u3002<\/li>\n<li>\u72b6\u6001\u8f6c\u6362\u68c0\u6d4b: \u5b83\u9996\u5148\u901a\u8fc7 was_enabled &#061; sysrq_on() \u8bb0\u5f55\u4e0b\u5728\u5e94\u7528\u65b0\u63a9\u7801\u4e4b\u524d SysRq \u529f\u80fd\u662f\u5426\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001&#xff08;sysrq_on() \u901a\u5e38\u68c0\u67e5\u63a9\u7801\u662f\u5426\u975e\u96f6&#xff09;\u3002\u7136\u540e&#xff0c;\u5b83\u66f4\u65b0\u5168\u5c40\u7684 sysrq_enabled \u63a9\u7801\u3002\u6700\u540e&#xff0c;\u5b83\u518d\u6b21\u8c03\u7528 sysrq_on() \u6765\u83b7\u53d6\u4e4b\u540e\u7684\u72b6\u6001&#xff0c;\u5e76\u8fdb\u884c\u6bd4\u8f83 (was_enabled !&#061; sysrq_on())\u3002<\/li>\n<li>\u6309\u9700\u6ce8\u518c\/\u6ce8\u9500: a. \u53ea\u6709\u5f53\u72b6\u6001\u53d1\u751f\u4e86**\u4ece\u201c\u7981\u7528\u201d\u5230\u201c\u542f\u7528\u201d\u7684\u53d8\u5316\u65f6&#xff0c;\u5b83\u624d\u4f1a\u8c03\u7528 sysrq_register_handler()\u3002\u6b64\u51fd\u6570\u8d1f\u8d23\u5411\u8f93\u5165\u5b50\u7cfb\u7edf&#xff08;\u5982\u952e\u76d8\u9a71\u52a8&#xff09;\u6ce8\u518c\u4e00\u4e2a\u94a9\u5b50&#xff0c;\u4ee5\u62e6\u622a SysRq \u7ec4\u5408\u952e\u3002 b. \u53ea\u6709\u5f53\u72b6\u6001\u53d1\u751f\u4e86\u4ece\u201c\u542f\u7528\u201d\u5230\u201c\u7981\u7528\u201d**\u7684\u53d8\u5316\u65f6&#xff0c;\u5b83\u624d\u4f1a\u8c03\u7528 sysrq_unregister_handler() \u6765\u79fb\u9664\u8fd9\u4e2a\u94a9\u5b50\u3002<\/li>\n<li>\u4f18\u5316: \u8fd9\u4e2a\u8bbe\u8ba1\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6548\u7387\u4f18\u5316\u3002\u5982\u679c SysRq \u529f\u80fd\u672c\u8eab\u5df2\u7ecf\u662f\u542f\u7528\u7684&#xff0c;\u800c\u7528\u6237\u53ea\u662f\u4fee\u6539\u4e86\u5141\u8bb8\u7684\u547d\u4ee4\u5b50\u96c6&#xff08;\u5373\u6539\u53d8\u4e86\u63a9\u7801\u4e2d\u7684\u67d0\u4e9b\u6bd4\u7279\u4f4d&#xff0c;\u4f46\u63a9\u7801\u4ecd\u975e\u96f6&#xff09;&#xff0c;\u90a3\u4e48 was_enabled \u548c sysrq_on() \u7684\u7ed3\u679c\u5c06\u540c\u4e3a true&#xff0c;\u72b6\u6001\u6ca1\u6709\u53d1\u751f\u8f6c\u6362&#xff0c;\u4e5f\u5c31\u4e0d\u4f1a\u53d1\u751f\u4e0d\u5fc5\u8981\u7684\u53e5\u67c4\u6ce8\u9500\u548c\u91cd\u65b0\u6ce8\u518c\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_toggle_support &#8211; \u6839\u636e\u63a9\u7801\u542f\u7528\u6216\u7981\u7528 SysRq \u652f\u6301\u3002<br \/>\n * &#064;param enable_mask: \u65b0\u7684 SysRq \u529f\u80fd\u63a9\u7801\u3002<br \/>\n * &#064;return int: \u59cb\u7ec8\u8fd4\u56de0\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">sysrq_toggle_support<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> enable_mask<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8bb0\u5f55\u5728\u5e94\u7528\u65b0\u63a9\u7801\u4e4b\u524d\u7684 SysRq \u542f\u7528\u72b6\u6001\u3002<\/span><br \/>\nbool was_enabled <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">sysrq_on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5e94\u7528\u65b0\u7684\u529f\u80fd\u63a9\u7801\u3002<\/span><br \/>\nsysrq_enabled <span class=\"token operator\">&#061;<\/span> enable_mask<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6bd4\u8f83\u5e94\u7528\u524d\u540e\u7684\u72b6\u6001\u662f\u5426\u53d1\u751f\u4e86\u53d8\u5316 (\u4ece\u542f\u7528-&gt;\u7981\u7528 \u6216 \u7981\u7528-&gt;\u542f\u7528)\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>was_enabled <span class=\"token operator\">!&#061;<\/span> <span class=\"token function\">sysrq_on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">sysrq_on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u72b6\u6001\u4ece \u7981\u7528 \u53d8\u4e3a \u542f\u7528&#xff0c;\u5219\u6ce8\u518c\u5e95\u5c42\u7684 SysRq \u5904\u7406\u53e5\u67c4\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_register_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u72b6\u6001\u4ece \u542f\u7528 \u53d8\u4e3a \u7981\u7528&#xff0c;\u5219\u6ce8\u9500\u5904\u7406\u53e5\u67c4\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_unregister_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token function\">EXPORT_SYMBOL_GPL<\/span><span class=\"token punctuation\">(<\/span>sysrq_toggle_support<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_sysctl_handler &#8211; \/proc\/sys\/kernel\/sysrq \u7684\u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570\u3002<br \/>\n * &#064;param table: \u6307\u5411 ctl_table \u7ed3\u6784\u7684\u6307\u9488\u3002<br \/>\n * &#064;param write: \u6807\u5fd7\u4f4d&#xff0c;\u975e0\u8868\u793a\u5199\u64cd\u4f5c\u3002<br \/>\n * &#064;param buffer: \u6307\u5411\u7528\u6237\u7a7a\u95f4\u6570\u636e\u7684\u7f13\u51b2\u533a\u3002<br \/>\n * &#064;param lenp: \u6307\u5411\u6570\u636e\u957f\u5ea6\u7684\u6307\u9488\u3002<br \/>\n * &#064;param ppos: \u6307\u5411\u6587\u4ef6\u504f\u79fb\u91cf\u7684\u6307\u9488\u3002<br \/>\n * &#064;return int: \u6210\u529f\u8fd4\u56de0&#xff0c;\u5426\u5219\u8fd4\u56de\u9519\u8bef\u7801\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">sysrq_sysctl_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">ctl_table<\/span> <span class=\"token operator\">*<\/span>table<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> write<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>buffer<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">size_t<\/span> <span class=\"token operator\">*<\/span>lenp<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">loff_t<\/span> <span class=\"token operator\">*<\/span>ppos<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">int<\/span> tmp<span class=\"token punctuation\">,<\/span> ret<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684 ctl_table \u526f\u672c\u3002<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">ctl_table<\/span> t <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">*<\/span>table<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u7684 SysRq \u63a9\u7801\u503c&#xff0c;\u5b58\u5165\u672c\u5730\u53d8\u91cf tmp\u3002<\/span><br \/>\ntmp <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">sysrq_mask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5c06\u4e34\u65f6\u8868\u7684 .data \u6307\u9488\u6307\u5411\u672c\u5730\u53d8\u91cf tmp\u3002<\/span><br \/>\nt<span class=\"token punctuation\">.<\/span>data <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">&amp;<\/span>tmp<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u884c\u4e3a\u7c7b\u4f3c\u4e8e proc_dointvec&#xff0c;\u56e0\u4e3a t \u8868\u6ca1\u6709\u8bbe\u7f6e min \u6216 max\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8c03\u7528\u6807\u51c6\u7684\u6574\u6570\u8bfb\u5199\u51fd\u6570&#xff0c;\u4efb\u4f55\u8bfb\u5199\u90fd\u5c06\u4f5c\u7528\u4e8e\u672c\u5730\u53d8\u91cf tmp\u3002<\/span><br \/>\nret <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">proc_dointvec_minmax<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>t<span class=\"token punctuation\">,<\/span> write<span class=\"token punctuation\">,<\/span> buffer<span class=\"token punctuation\">,<\/span> lenp<span class=\"token punctuation\">,<\/span> ppos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>ret<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> ret<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5982\u679c\u662f\u4e00\u6b21\u6210\u529f\u7684\u5199\u64cd\u4f5c&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>write<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u8c03\u7528\u6838\u5fc3\u903b\u8f91\u51fd\u6570&#xff0c;\u5e94\u7528\u65b0\u7684\u63a9\u7801\u503c\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_toggle_support<\/span><span class=\"token punctuation\">(<\/span>tmp<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5b9a\u4e49 sysctl \u8868\u3002<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">ctl_table<\/span> sysrq_sysctl_table<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>procname<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;sysrq&#034;<\/span><span class=\"token punctuation\">,<\/span>           <span class=\"token comment\">\/\/ \u6587\u4ef6\u540d<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>data<span class=\"token operator\">&#061;<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span>              <span class=\"token comment\">\/\/ .data \u4e3a NULL&#xff0c;\u8868\u793a\u4f7f\u7528\u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>maxlen<span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>       <span class=\"token comment\">\/\/ \u6700\u5927\u957f\u5ea6<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>mode<span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0644<\/span><span class=\"token punctuation\">,<\/span>              <span class=\"token comment\">\/\/ \u6587\u4ef6\u6743\u9650<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>proc_handler<span class=\"token operator\">&#061;<\/span> sysrq_sysctl_handler<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u6307\u5b9a\u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief init_sysrq_sysctl &#8211; \u521d\u59cb\u5316 SysRq \u7684 sysctl \u63a5\u53e3\u3002<br \/>\n * &#064;return int: \u59cb\u7ec8\u8fd4\u56de0\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> __init <span class=\"token function\">init_sysrq_sysctl<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5728 &#034;kernel&#034; (\u5373 \/proc\/sys\/kernel\/) \u76ee\u5f55\u4e0b\u6ce8\u518c\u4e0a\u8ff0\u5b9a\u4e49\u7684 sysctl \u8868\u3002<\/span><br \/>\n<span class=\"token function\">register_sysctl_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;kernel&#034;<\/span><span class=\"token punctuation\">,<\/span> sysrq_sysctl_table<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4f7f\u7528 subsys_initcall \u5b8f&#xff0c;\u786e\u4fdd\u8be5\u521d\u59cb\u5316\u51fd\u6570\u5728\u5185\u6838\u5b50\u7cfb\u7edf\u521d\u59cb\u5316\u9636\u6bb5\u88ab\u8c03\u7528\u3002<\/span><br \/>\n<span class=\"token function\">subsys_initcall<\/span><span class=\"token punctuation\">(<\/span>init_sysrq_sysctl<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>SysRq \u8f93\u5165\u4e8b\u4ef6\u5904\u7406\u5668&#xff1a;sysrq_handler \u5b9a\u4e49\u4e0e\u6ce8\u518c<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u4e0e Linux \u5185\u6838\u8f93\u5165\u5b50\u7cfb\u7edf&#xff08;Input Subsystem&#xff09;\u8fdb\u884c\u5bf9\u63a5\u7684\u6838\u5fc3\u673a\u5236\u3002\u5176\u4e3b\u8981\u529f\u80fd\u662f&#xff1a;\u5b9a\u4e49\u4e00\u4e2a input_handler&#xff08;sysrq_handler&#xff09;&#xff0c;\u5b83\u50cf\u4e00\u4e2a\u201c\u63d2\u4ef6\u201d&#xff0c;\u4e13\u95e8\u7528\u4e8e\u76d1\u542c\u548c\u8fc7\u6ee4\u6765\u81ea\u952e\u76d8\u7b49\u8f93\u5165\u8bbe\u5907\u7684\u4e8b\u4ef6\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u5339\u914d\u89c4\u5219&#xff08;sysrq_ids&#xff09;\u6765\u81ea\u52a8\u7ed1\u5b9a\u5230\u6240\u6709\u53ef\u80fd\u4ea7\u751f SysRq \u5e8f\u5217\u7684\u8bbe\u5907&#xff08;\u5373\u6240\u6709\u5e26 Alt \u952e\u7684\u952e\u76d8&#xff09;&#xff0c;\u5e76\u901a\u8fc7 sysrq_register_handler \u548c sysrq_unregister_handler \u8fd9\u4e24\u4e2a\u51fd\u6570&#xff0c;\u5c06\u8fd9\u4e2a\u201c\u63d2\u4ef6\u201d\u52a8\u6001\u5730\u63d2\u5165\u6216\u62d4\u51fa\u8f93\u5165\u5b50\u7cfb\u7edf\u7684\u4e8b\u4ef6\u5904\u7406\u6d41\u6c34\u7ebf\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u4ee3\u7801\u662f SysRq \u529f\u80fd\u5f97\u4ee5\u5de5\u4f5c\u7684\u7269\u7406\u57fa\u7840&#xff0c;\u5b83\u5c06\u4e00\u4e2a\u62bd\u8c61\u7684\u5185\u6838\u8c03\u8bd5\u529f\u80fd&#xff0c;\u4e0e\u5177\u4f53\u7684\u786c\u4ef6\u8f93\u5165\u4e8b\u4ef6\u5173\u8054\u8d77\u6765\u3002\u5176\u5b9e\u73b0\u539f\u7406\u57fa\u4e8e\u8f93\u5165\u5b50\u7cfb\u7edf\u7684\u5206\u5c42\u548c\u56de\u8c03\u6a21\u578b\u3002<\/p>\n<li>\n<p>\u8f93\u5165\u5b50\u7cfb\u7edf\u5206\u5c42\u6a21\u578b:<\/p>\n<ul>\n<li>Linux \u7684\u8f93\u5165\u5b50\u7cfb\u7edf\u662f\u4e00\u4e2a\u5206\u5c42\u7684\u67b6\u6784\u3002\u5e95\u5c42\u662f\u8bbe\u5907\u9a71\u52a8&#xff08;\u5982\u952e\u76d8\u9a71\u52a8\u3001\u4e32\u53e3\u9a71\u52a8&#xff09;&#xff0c;\u5b83\u4eec\u5c06\u786c\u4ef6\u4fe1\u53f7\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u8f93\u5165\u4e8b\u4ef6&#xff08;input_event&#xff09;\u3002\u4e0a\u5c42\u662f\u4e8b\u4ef6\u5904\u7406\u5668&#xff08;input_handler&#xff09;&#xff0c;\u5b83\u4eec\u6d88\u8d39\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/li>\n<li>sysrq_handler \u5c31\u662f\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u5668\u3002\u5f53\u5b83\u88ab\u6ce8\u518c\u540e&#xff0c;\u8f93\u5165\u5b50\u7cfb\u7edf\u6838\u5fc3\u4f1a\u6210\u4e3a\u4e00\u4e2a\u201c\u4e2d\u95f4\u4eba\u201d&#xff0c;\u5c06\u6765\u81ea\u5339\u914d\u8bbe\u5907\u7684\u6240\u6709\u4e8b\u4ef6&#xff0c;\u90fd\u5148\u4f20\u9012\u7ed9 sysrq_handler \u7684 .filter \u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8bbe\u5907\u5339\u914d\u89c4\u5219 (sysrq_ids):<\/p>\n<ul>\n<li>sysrq_ids \u662f\u4e00\u4e2a input_device_id \u6570\u7ec4&#xff0c;\u5b83\u5b9a\u4e49\u4e86 sysrq_handler \u611f\u5174\u8da3\u7684\u8bbe\u5907\u7c7b\u578b\u3002<\/li>\n<li>\u5339\u914d\u903b\u8f91: \u5b83\u8981\u6c42\u8bbe\u5907\u5fc5\u987b\u540c\u65f6\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6&#xff1a; a. INPUT_DEVICE_ID_MATCH_EVBIT: \u8bbe\u5907\u7684\u4e8b\u4ef6\u7c7b\u578b\u4f4d\u63a9\u7801 evbit \u5fc5\u987b\u5305\u542b EV_KEY&#xff0c;\u610f\u5473\u7740\u8fd9\u662f\u4e00\u4e2a\u80fd\u4ea7\u751f\u6309\u952e\u4e8b\u4ef6\u7684\u8bbe\u5907\u3002 b. INPUT_DEVICE_ID_MATCH_KEYBIT: \u8bbe\u5907\u7684\u6309\u952e\u7801\u4f4d\u63a9\u7801 keybit \u5fc5\u987b\u5305\u542b KEY_LEFTALT&#xff0c;\u610f\u5473\u7740\u8fd9\u4e2a\u8bbe\u5907\u81f3\u5c11\u6709\u4e00\u4e2a\u5de6 Alt \u952e\u3002<\/li>\n<li>\u4e3a\u4f55\u5339\u914d Alt \u952e: \u6ce8\u91ca\u4e2d\u89e3\u91ca\u4e86\u4e00\u4e2a\u5173\u952e\u7684\u8bbe\u8ba1\u51b3\u7b56\u3002\u4e0d\u76f4\u63a5\u5339\u914d KEY_SYSRQ \u952e&#xff0c;\u662f\u56e0\u4e3a\u5f88\u591a\u952e\u76d8&#xff08;\u5c24\u5176\u662f\u4e00\u4e9b\u5d4c\u5165\u5f0f\u6216\u670d\u52a1\u5668\u8bbe\u5907&#xff09;\u6ca1\u6709\u7269\u7406\u7684 SysRq (PrintScreen) \u952e\u3002\u800c Alt \u952e\u662f\u51e0\u4e4e\u6240\u6709\u6807\u51c6\u952e\u76d8\u7684\u6807\u914d\u3002SysRq \u7ec4\u5408\u952e\u603b\u662f\u4ee5 Alt &#043; SysRq &#043; &lt;command_key&gt; \u7684\u5f62\u5f0f\u89e6\u53d1&#xff0c;\u56e0\u6b64&#xff0c;\u53ea\u8981\u80fd\u786e\u4fdd Alt \u952e\u5b58\u5728&#xff0c;\u5c31\u4e3a\u89e6\u53d1 SysRq \u63d0\u4f9b\u4e86\u524d\u63d0\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u52a8\u6001\u6ce8\u518c\u4e0e\u751f\u547d\u5468\u671f\u7ba1\u7406:<\/p>\n<ul>\n<li>sysrq_register_handler \u662f\u4e00\u4e2a\u5c01\u88c5\u51fd\u6570&#xff0c;\u7531\u4e0a\u4e00\u8282\u5206\u6790\u7684 sysrq_toggle_support \u5728 SysRq \u529f\u80fd\u88ab\u542f\u7528\u65f6\u8c03\u7528\u3002\u5b83\u8c03\u7528 input_register_handler(&amp;sysrq_handler)&#xff0c;\u5c06 sysrq_handler \u63d2\u5165\u5230\u8f93\u5165\u5b50\u7cfb\u7edf\u7684\u5168\u5c40\u5904\u7406\u5668\u5217\u8868\u4e2d\u3002\u6b64\u540e&#xff0c;\u8f93\u5165\u6838\u5fc3\u4f1a\u81ea\u52a8\u4e3a\u6240\u6709\u5df2\u5b58\u5728\u548c\u672a\u6765\u65b0\u63d2\u5165\u7684\u3001\u7b26\u5408 sysrq_ids \u89c4\u5219\u7684\u8bbe\u5907\u8c03\u7528 sysrq_handler \u7684 .connect \u56de\u8c03\u3002<\/li>\n<li>sysrq_unregister_handler \u5219\u5bf9\u79f0\u5730\u8c03\u7528 input_unregister_handler&#xff0c;\u5c06\u5904\u7406\u5668\u4ece\u7cfb\u7edf\u4e2d\u79fb\u9664&#xff0c;\u8f93\u5165\u6838\u5fc3\u4f1a\u4e3a\u6240\u6709\u5df2\u8fde\u63a5\u7684\u8bbe\u5907\u8c03\u7528\u5176 .disconnect \u56de\u8c03&#xff0c;\u4ece\u800c\u5f7b\u5e95\u65ad\u5f00 SysRq \u529f\u80fd\u4e0e\u786c\u4ef6\u8f93\u5165\u7684\u8054\u7cfb\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;var sysrq_ids<br \/>\n * &#064;brief SysRq \u8f93\u5165\u5904\u7406\u5668\u7684\u8bbe\u5907\u5339\u914d\u89c4\u5219\u8868\u3002<br \/>\n * &#064;note \u6211\u4eec\u5339\u914d KEY_LEFTALT \u800c\u4e0d\u662f KEY_SYSRQ&#xff0c;\u56e0\u4e3a\u5e76\u975e\u6240\u6709\u952e\u76d8\u90fd\u9884\u5b9a\u4e49\u4e86 SysRq \u952e&#xff0c;<br \/>\n *       \u7528\u6237\u53ef\u80fd\u7a0d\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u952e\u76d8\u6620\u5c04\u4e2d&#xff0c;\u4f46\u6211\u4eec\u671f\u671b\u6240\u6709\u8fd9\u7c7b\u952e\u76d8\u90fd\u6709\u5de6 Alt \u952e\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_device_id<\/span> sysrq_ids<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5339\u914d\u6807\u5fd7&#xff1a;\u8981\u6c42\u540c\u65f6\u5339\u914d\u4e8b\u4ef6\u7c7b\u578b\u4f4d\u63a9\u7801\u548c\u6309\u952e\u7801\u4f4d\u63a9\u7801\u3002<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>flags <span class=\"token operator\">&#061;<\/span> INPUT_DEVICE_ID_MATCH_EVBIT <span class=\"token operator\">|<\/span><br \/>\nINPUT_DEVICE_ID_MATCH_KEYBIT<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4e8b\u4ef6\u7c7b\u578b\u8981\u6c42&#xff1a;\u5fc5\u987b\u652f\u6301 EV_KEY (\u6309\u952e\u4e8b\u4ef6)\u3002<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>evbit <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token function\">BIT_WORD<\/span><span class=\"token punctuation\">(<\/span>EV_KEY<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">BIT_MASK<\/span><span class=\"token punctuation\">(<\/span>EV_KEY<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token comment\">\/\/ \u6309\u952e\u7801\u8981\u6c42&#xff1a;\u5fc5\u987b\u652f\u6301 KEY_LEFTALT (\u5de6 Alt \u952e)\u3002<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>keybit <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token function\">BIT_WORD<\/span><span class=\"token punctuation\">(<\/span>KEY_LEFTALT<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">BIT_MASK<\/span><span class=\"token punctuation\">(<\/span>KEY_LEFTALT<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u6570\u7ec4\u7ed3\u675f\u7684\u7a7a\u6761\u76ee<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;var sysrq_handler<br \/>\n * &#064;brief SysRq \u7684\u8f93\u5165\u4e8b\u4ef6\u5904\u7406\u5668\u7ed3\u6784\u4f53\u3002<br \/>\n *<br \/>\n * \u8fd9\u4e2a\u7ed3\u6784\u4f53\u5b9a\u4e49\u4e86 SysRq \u529f\u80fd\u5982\u4f55\u4f5c\u4e3a\u4e00\u4e2a\u201c\u63d2\u4ef6\u201d\u63a5\u5165\u5230 Linux \u8f93\u5165\u5b50\u7cfb\u7edf\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_handler<\/span> sysrq_handler <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>filter<span class=\"token operator\">&#061;<\/span> sysrq_filter<span class=\"token punctuation\">,<\/span>     <span class=\"token comment\">\/\/ \u4e8b\u4ef6\u8fc7\u6ee4\u51fd\u6570&#xff1a;\u5bf9\u6765\u81ea\u5339\u914d\u8bbe\u5907\u7684\u6bcf\u4e2a\u4e8b\u4ef6\u8c03\u7528<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>connect<span class=\"token operator\">&#061;<\/span> sysrq_connect<span class=\"token punctuation\">,<\/span>    <span class=\"token comment\">\/\/ \u8fde\u63a5\u51fd\u6570&#xff1a;\u5f53\u4e00\u4e2a\u5339\u914d\u7684\u8bbe\u5907\u88ab\u53d1\u73b0\u65f6\u8c03\u7528<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>disconnect<span class=\"token operator\">&#061;<\/span> sysrq_disconnect<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u65ad\u5f00\u51fd\u6570&#xff1a;\u5f53\u5339\u914d\u7684\u8bbe\u5907\u88ab\u79fb\u9664\u65f6\u8c03\u7528<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>name<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;sysrq&#034;<\/span><span class=\"token punctuation\">,<\/span>          <span class=\"token comment\">\/\/ \u5904\u7406\u5668\u7684\u540d\u79f0<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>id_table<span class=\"token operator\">&#061;<\/span> sysrq_ids<span class=\"token punctuation\">,<\/span>        <span class=\"token comment\">\/\/ \u6307\u5411\u8bbe\u5907\u5339\u914d\u89c4\u5219\u8868<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_of_get_keyreset_config<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\nu32 key<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">device_node<\/span> <span class=\"token operator\">*<\/span>np<span class=\"token punctuation\">;<\/span><\/p>\n<p>np <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">of_find_node_by_path<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/chosen\/linux,sysrq-reset-seq&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>np<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token function\">pr_debug<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;No sysrq node found&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/* Reset in case a __weak definition was present *\/<\/span><br \/>\nsysrq_reset_seq_len <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token function\">of_property_for_each_u32<\/span><span class=\"token punctuation\">(<\/span>np<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;keyset&#034;<\/span><span class=\"token punctuation\">,<\/span> key<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>key <span class=\"token operator\">&#061;&#061;<\/span> KEY_RESERVED <span class=\"token operator\">||<\/span> key <span class=\"token operator\">&gt;<\/span> KEY_MAX <span class=\"token operator\">||<\/span><br \/>\n    sysrq_reset_seq_len <span class=\"token operator\">&#061;&#061;<\/span> SYSRQ_KEY_RESET_MAX<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>sysrq_reset_seq<span class=\"token punctuation\">[<\/span>sysrq_reset_seq_len<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">short<\/span><span class=\"token punctuation\">)<\/span>key<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/* Get reset timeout if any. *\/<\/span><br \/>\n<span class=\"token function\">of_property_read_u32<\/span><span class=\"token punctuation\">(<\/span>np<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;timeout-ms&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_reset_downtime_ms<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token function\">of_node_put<\/span><span class=\"token punctuation\">(<\/span>np<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_register_handler &#8211; \u6ce8\u518c SysRq \u8f93\u5165\u5904\u7406\u5668\u3002<br \/>\n * &#064;note \u8fd9\u662f\u4e00\u4e2a\u5185\u8054\u51fd\u6570&#xff0c;\u7528\u4e8e\u5c01\u88c5\u6ce8\u518c\u903b\u8f91\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">inline<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_register_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">int<\/span> error<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4ece\u8bbe\u5907\u6811 (Open Firmware) \u83b7\u53d6 SysRq \u7684\u6309\u952e\u590d\u4f4d\u914d\u7f6e\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_of_get_keyreset_config<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5411\u8f93\u5165\u5b50\u7cfb\u7edf\u6838\u5fc3\u6ce8\u518c sysrq_handler\u3002<\/span><br \/>\nerror <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">input_register_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq_handler<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">pr_err<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Failed to register input handler, error %d&#034;<\/span><span class=\"token punctuation\">,<\/span> error<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_unregister_handler &#8211; \u6ce8\u9500 SysRq \u8f93\u5165\u5904\u7406\u5668\u3002<br \/>\n * &#064;note \u8fd9\u662f\u4e00\u4e2a\u5185\u8054\u51fd\u6570&#xff0c;\u7528\u4e8e\u5c01\u88c5\u6ce8\u9500\u903b\u8f91\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">inline<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_unregister_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece\u8f93\u5165\u5b50\u7cfb\u7edf\u6838\u5fc3\u6ce8\u9500 sysrq_handler\u3002<\/span><br \/>\n<span class=\"token function\">input_unregister_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq_handler<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq \u4e8b\u4ef6\u5904\u7406\u56de\u8c03&#xff1a;sysrq_connect, sysrq_disconnect, \u4e0e sysrq_filter<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86 sysrq_handler \u7684\u6838\u5fc3\u5b9e\u73b0&#xff0c;\u5373\u5f53\u4e00\u4e2a\u5339\u914d\u7684\u8f93\u5165\u8bbe\u5907&#xff08;\u5982\u952e\u76d8&#xff09;\u88ab\u63a5\u5165\u6216\u79fb\u9664&#xff0c;\u4ee5\u53ca\u5f53\u8be5\u8bbe\u5907\u4ea7\u751f\u8f93\u5165\u4e8b\u4ef6\u65f6&#xff0c;\u5185\u6838\u6240\u6267\u884c\u7684\u5177\u4f53\u64cd\u4f5c\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;<\/p>\n<li>sysrq_connect: \u4e3a\u6bcf\u4e00\u4e2a\u65b0\u63a5\u5165\u7684\u952e\u76d8\u8bbe\u5907&#xff0c;\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u72ec\u7acb\u7684\u72b6\u6001\u673a&#xff08;struct sysrq_state&#xff09;&#xff0c;\u5e76\u5efa\u7acb\u4e00\u4e2a input_handle \u5c06\u8bbe\u5907\u3001\u5904\u7406\u5668\u548c\u72b6\u6001\u673a\u4e09\u8005\u7262\u56fa\u5730\u7ed1\u5b9a\u5728\u4e00\u8d77\u3002<\/li>\n<li>sysrq_filter: \u4f5c\u4e3a\u4e8b\u4ef6\u5904\u7406\u7684**\u201c\u70ed\u8def\u5f84\u201d&#xff0c;\u5b83\u62e6\u622a\u6765\u81ea\u8be5\u952e\u76d8\u7684\u6bcf\u4e00\u4e2a\u8f93\u5165\u4e8b\u4ef6&#xff0c;\u5c06\u5176\u9001\u5165\u4e00\u4e2a\u72b6\u6001\u673a&#xff08;sysrq_handle_keypress&#xff09;\u8fdb\u884c\u5206\u6790&#xff0c;\u5e76\u51b3\u5b9a\u662f\u5426\u8981\u201c\u6d88\u8d39\u201d**\u8fd9\u4e2a\u4e8b\u4ef6&#xff08;\u5373\u786e\u8ba4\u4e3a SysRq \u5e8f\u5217\u7684\u4e00\u90e8\u5206\u5e76\u963b\u6b62\u5176\u88ab\u7cfb\u7edf\u5176\u4ed6\u90e8\u5206\u770b\u5230&#xff09;\u3002<\/li>\n<li>sysrq_disconnect: \u5728\u8bbe\u5907\u88ab\u62d4\u51fa\u65f6&#xff0c;\u6267\u884c\u4e00\u4e2a\u5e72\u51c0\u3001\u540c\u6b65\u7684\u62c6\u9664&#xff0c;\u786e\u4fdd\u6240\u6709\u4e0e\u8be5\u8bbe\u5907\u76f8\u5173\u7684\u8d44\u6e90&#xff08;\u72b6\u6001\u673a\u5185\u5b58\u3001\u5b9a\u65f6\u5668\u3001\u5de5\u4f5c\u961f\u5217&#xff09;\u90fd\u88ab\u5b8c\u5168\u91ca\u653e\u3002<\/li>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u673a\u5236\u662f Linux \u8f93\u5165\u5b50\u7cfb\u7edf\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u7684\u5177\u4f53\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u8bbe\u5907\u5b9e\u4f8b\u7ef4\u62a4\u4e00\u4e2a\u79c1\u6709\u72b6\u6001&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u590d\u6742\u7ec4\u5408\u952e\u5e8f\u5217\u7684\u7cbe\u786e\u8ddf\u8e2a\u3002<\/p>\n<li>\n<p>\u6bcf\u4e2a\u8bbe\u5907\u4e00\u4e2a\u72b6\u6001\u673a (sysrq_connect):<\/p>\n<ul>\n<li>sysrq_connect \u51fd\u6570\u4f53\u73b0\u4e86\u201c\u5b9e\u4f8b\u7ba1\u7406\u201d\u7684\u601d\u60f3\u3002\u5f53\u4e00\u4e2a\u7b26\u5408 sysrq_ids \u89c4\u5219\u7684\u952e\u76d8\u88ab\u63a5\u5165\u65f6&#xff0c;\u5b83\u4e0d\u4f1a\u4f7f\u7528\u4e00\u4e2a\u5168\u5c40\u7684\u72b6\u6001\u53d8\u91cf&#xff0c;\u800c\u662f kzalloc \u4e00\u4e2a sysrq_state \u7ed3\u6784\u3002\u8fd9\u81f3\u5173\u91cd\u8981&#xff0c;\u56e0\u4e3a\u5b83\u5141\u8bb8\u591a\u4e2a\u952e\u76d8\u540c\u65f6\u8fde\u63a5\u5230\u7cfb\u7edf&#xff0c;\u800c\u6bcf\u4e2a\u952e\u76d8\u7684 SysRq \u72b6\u6001&#xff08;\u4f8b\u5982&#xff0c;Alt \u952e\u662f\u5426\u88ab\u6309\u4e0b&#xff09;\u90fd\u80fd\u591f\u88ab\u72ec\u7acb\u3001\u65e0\u51b2\u7a81\u5730\u8ddf\u8e2a\u3002<\/li>\n<li>\u5b83\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5de5\u4f5c\u961f\u5217 (reinject_work) \u548c\u4e00\u4e2a\u5b9a\u65f6\u5668 (keyreset_timer)&#xff0c;\u8fd9\u4e9b\u90fd\u662f sysrq_state \u7684\u4e00\u90e8\u5206\u3002<\/li>\n<li>\u5173\u952e\u7ed1\u5b9a: \u901a\u8fc7 input_register_handle \u548c input_open_device&#xff0c;\u5b83\u5efa\u7acb\u4e86\u4e00\u4e2a\u4ece\u786c\u4ef6\u8bbe\u5907\u5230\u8fd9\u4e2a\u65b0\u521b\u5efa\u7684 sysrq_state \u7684\u4e8b\u4ef6\u6d41\u901a\u9053\u3002handle-&gt;private &#061; sysrq; \u8fd9\u4e00\u884c\u662f\u5b9e\u73b0\u72b6\u6001\u7ed1\u5b9a\u7684\u6838\u5fc3\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u8fc7\u6ee4\u4e0e\u72b6\u6001\u673a\u9a71\u52a8 (sysrq_filter):<\/p>\n<ul>\n<li>sysrq_filter \u662f\u4e00\u4e2a\u8fc7\u6ee4\u5668&#xff0c;\u800c\u4e0d\u662f\u4e00\u4e2a\u4f20\u7edf\u7684\u4e8b\u4ef6\u5904\u7406\u5668\u3002\u5b83\u7684\u8fd4\u56de\u503c (bool suppress) \u51b3\u5b9a\u4e86\u4e8b\u4ef6\u7684\u547d\u8fd0\u3002<\/li>\n<li>\u8fd4\u56de true: \u610f\u5473\u7740\u201c\u6b64\u4e8b\u4ef6\u5df2\u88ab\u6211\u5904\u7406&#xff0c;\u5e94\u5230\u6b64\u4e3a\u6b62\u201d\u3002\u8fd9\u7528\u4e8e\u201c\u5403\u6389\u201d\u7ec4\u6210 SysRq \u5e8f\u5217\u7684\u6309\u952e&#xff08;\u5982 Alt, SysRq, \u548c\u547d\u4ee4\u952e&#xff09;&#xff0c;\u9632\u6b62\u5b83\u4eec\u88ab\u53d1\u9001\u5230\u7528\u6237\u7a7a\u95f4\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u7ec8\u7aef\u3002<\/li>\n<li>\u8fd4\u56de false: \u610f\u5473\u7740\u201c\u6b64\u4e8b\u4ef6\u4e0e\u6211\u65e0\u5173&#xff0c;\u8bf7\u7ee7\u7eed\u4f20\u9012\u201d\u3002<\/li>\n<li>\u72b6\u6001\u673a\u8c03\u7528: sysrq_handle_keypress(sysrq, code, value) \u662f\u5b9e\u9645\u7684\u72b6\u6001\u673a\u903b\u8f91&#xff08;\u672a\u5728\u6b64\u4ee3\u7801\u6bb5\u4e2d\u663e\u793a&#xff09;\u3002sysrq_filter \u8d1f\u8d23\u5c06\u6309\u952e\u4e8b\u4ef6 (EV_KEY) \u5582\u7ed9\u8fd9\u4e2a\u72b6\u6001\u673a&#xff0c;\u5e76\u6839\u636e\u5176\u8f93\u51fa\u6765\u51b3\u5b9a\u662f\u5426\u8981\u6291\u5236\u4e8b\u4ef6\u3002<\/li>\n<li>\u91cd\u6ce8\u5165\u4fdd\u62a4: if (sysrq-&gt;reinjecting) \u68c0\u67e5\u662f\u4e00\u4e2a\u9632\u6b62\u65e0\u9650\u5faa\u73af\u7684\u5173\u952e\u4fdd\u62a4\u3002SysRq \u6709\u65f6\u9700\u8981\u5c06\u5b83\u201c\u5403\u6389\u201d\u7684 Alt&#043;SysRq \u952e\u91cd\u65b0\u6ce8\u5165\u56de\u8f93\u5165\u7cfb\u7edf&#xff0c;\u4ee5\u517c\u5bb9\u67d0\u4e9b\u56fe\u5f62\u73af\u5883\u3002\u8fd9\u4e2a\u68c0\u67e5\u786e\u4fdd\u4e86\u8fc7\u6ee4\u5668\u4e0d\u4f1a\u62e6\u622a\u81ea\u5df1\u91cd\u65b0\u6ce8\u5165\u7684\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u540c\u6b65\u62c6\u9664 (sysrq_disconnect):<\/p>\n<ul>\n<li>\u5f53\u952e\u76d8\u88ab\u62d4\u51fa\u65f6&#xff0c;sysrq_disconnect \u88ab\u8c03\u7528\u3002\u5b83\u5fc5\u987b\u6267\u884c\u4e00\u4e2a\u975e\u5e38\u5e72\u51c0\u7684\u9006\u5411\u64cd\u4f5c\u3002<\/li>\n<li>cancel_work_sync(&amp;sysrq-&gt;reinject_work) \u548c timer_shutdown_sync(&amp;sysrq-&gt;keyreset_timer) \u662f\u81f3\u5173\u91cd\u8981\u7684\u3002_sync \u540e\u7f00\u8868\u793a\u8fd9\u4e9b\u51fd\u6570\u4f1a\u963b\u585e\u7b49\u5f85&#xff0c;\u76f4\u5230\u4efb\u4f55\u6b63\u5728\u8fd0\u884c\u7684\u5de5\u4f5c\u961f\u5217\u6216\u5b9a\u65f6\u5668\u56de\u8c03\u51fd\u6570\u5b8c\u5168\u6267\u884c\u5b8c\u6bd5\u3002\u8fd9\u5f7b\u5e95\u675c\u7edd\u4e86\u5728 kfree(sysrq) \u4e4b\u540e&#xff0c;\u4ecd\u6709\u56de\u8c03\u51fd\u6570\u5c1d\u8bd5\u8bbf\u95ee\u5df2\u88ab\u91ca\u653e\u7684 sysrq_state \u5185\u5b58\u7684\u53ef\u80fd\u6027&#xff0c;\u662f\u9632\u6b62 use-after-free \u9519\u8bef\u7684\u5173\u952e\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_filter &#8211; SysRq \u7684\u8f93\u5165\u4e8b\u4ef6\u8fc7\u6ee4\u5668\u56de\u8c03\u3002<br \/>\n * &#064;param handle: \u6307\u5411 input_handle \u7684\u6307\u9488&#xff0c;\u4ee3\u8868\u8bbe\u5907\u4e0e\u5904\u7406\u5668\u7684\u8fde\u63a5\u3002<br \/>\n * &#064;param type:   \u8f93\u5165\u4e8b\u4ef6\u7684\u7c7b\u578b (\u5982 EV_KEY, EV_SYN)\u3002<br \/>\n * &#064;param code:   \u4e8b\u4ef6\u4ee3\u7801 (\u5982 KEY_A, KEY_B)\u3002<br \/>\n * &#064;param value:  \u4e8b\u4ef6\u503c (\u5982 1 for press, 0 for release)\u3002<br \/>\n * &#064;return bool: \u5982\u679c\u4e8b\u4ef6\u5e94\u88ab\u6291\u5236 (suppress)&#xff0c;\u5219\u8fd4\u56de true&#xff1b;\u5426\u5219\u8fd4\u56de false\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> bool <span class=\"token function\">sysrq_filter<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_handle<\/span> <span class=\"token operator\">*<\/span>handle<span class=\"token punctuation\">,<\/span><br \/>\n <span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> type<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> code<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece handle \u4e2d\u83b7\u53d6\u4e3a\u6b64\u8bbe\u5907\u5206\u914d\u7684\u79c1\u6709\u72b6\u6001\u6570\u636e\u3002<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>sysrq <span class=\"token operator\">&#061;<\/span> handle<span class=\"token operator\">-&gt;<\/span>private<span class=\"token punctuation\">;<\/span><br \/>\nbool suppress<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u5982\u679c\u6211\u4eec\u6b63\u5728\u91cd\u65b0\u6ce8\u5165 Alt&#043;SysRq \u7ec4\u5408\u952e&#xff0c;\u5219\u4e0d\u8fc7\u6ee4\u4efb\u4f55\u4e1c\u897f\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>reinjecting<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> false<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">(<\/span>type<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><\/p>\n<p><span class=\"token keyword\">case<\/span> EV_SYN<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token comment\">\/\/ \u540c\u6b65\u4e8b\u4ef6&#xff0c;\u901a\u5e38\u4e0d\u6291\u5236\u3002<\/span><br \/>\nsuppress <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">case<\/span> EV_KEY<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token comment\">\/\/ \u6309\u952e\u4e8b\u4ef6&#xff0c;\u4ea4\u7531\u72b6\u6001\u673a\u5904\u7406&#xff0c;\u5e76\u6839\u636e\u5176\u7ed3\u679c\u51b3\u5b9a\u662f\u5426\u6291\u5236\u3002<\/span><br \/>\nsuppress <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">sysrq_handle_keypress<\/span><span class=\"token punctuation\">(<\/span>sysrq<span class=\"token punctuation\">,<\/span> code<span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">default<\/span><span class=\"token operator\">:<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5176\u4ed6\u7c7b\u578b\u7684\u4e8b\u4ef6 (\u5982\u9f20\u6807\u79fb\u52a8)&#xff0c;\u5982\u679c SysRq \u5e8f\u5217\u5df2\u6fc0\u6d3b&#xff0c;\u5219\u6291\u5236\u3002<\/span><br \/>\nsuppress <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> suppress<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_connect &#8211; \u5f53\u4e00\u4e2a\u5339\u914d\u7684\u8f93\u5165\u8bbe\u5907\u8fde\u63a5\u65f6\u8c03\u7528\u7684\u56de\u8c03\u3002<br \/>\n * &#064;param handler: \u6307\u5411 sysrq_handler \u7684\u6307\u9488\u3002<br \/>\n * &#064;param dev:     \u6307\u5411\u65b0\u8fde\u63a5\u7684 input_dev \u7684\u6307\u9488\u3002<br \/>\n * &#064;param id:      \u6307\u5411\u5339\u914d\u7684 input_device_id \u7684\u6307\u9488\u3002<br \/>\n * &#064;return int: \u6210\u529f\u8fd4\u56de0&#xff0c;\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7801\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token function\">sysrq_connect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_handler<\/span> <span class=\"token operator\">*<\/span>handler<span class=\"token punctuation\">,<\/span><br \/>\n <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_dev<\/span> <span class=\"token operator\">*<\/span>dev<span class=\"token punctuation\">,<\/span><br \/>\n <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_device_id<\/span> <span class=\"token operator\">*<\/span>id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>sysrq<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span> error<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e3a\u8fd9\u4e2a\u65b0\u8bbe\u5907\u5206\u914d\u4e00\u4e2a\u79c1\u6709\u7684\u3001\u96f6\u521d\u59cb\u5316\u7684\u72b6\u6001\u7ed3\u6784\u3002<\/span><br \/>\nsysrq <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">kzalloc<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> GFP_KERNEL<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>sysrq<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span>ENOMEM<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u521d\u59cb\u5316\u7528\u4e8e\u91cd\u65b0\u6ce8\u5165\u6309\u952e\u7684\u5de5\u4f5c\u961f\u5217\u3002<\/span><br \/>\n<span class=\"token function\">INIT_WORK<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>reinject_work<span class=\"token punctuation\">,<\/span> sysrq_reinject_alt_sysrq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u586b\u5145 input_handle \u7ed3\u6784&#xff0c;\u5efa\u7acb\u8bbe\u5907\u3001\u5904\u7406\u5668\u548c\u79c1\u6709\u72b6\u6001\u4e4b\u95f4\u7684\u8fde\u63a5\u3002<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">.<\/span>dev <span class=\"token operator\">&#061;<\/span> dev<span class=\"token punctuation\">;<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">.<\/span>handler <span class=\"token operator\">&#061;<\/span> handler<span class=\"token punctuation\">;<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">.<\/span>name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;sysrq&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">.<\/span>private <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u521d\u59cb\u5316\u7528\u4e8e\u6309\u952e\u590d\u4f4d\u7684\u5b9a\u65f6\u5668\u3002<\/span><br \/>\n<span class=\"token function\">timer_setup<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">,<\/span> sysrq_do_reset<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5411\u8f93\u5165\u6838\u5fc3\u6ce8\u518c\u8fd9\u4e2a handle\u3002<\/span><br \/>\nerror <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">input_register_handle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token function\">pr_err<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Failed to register input sysrq handler, error %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\nerror<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">goto<\/span> err_free<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ &#034;\u6253\u5f00&#034;\u8bbe\u5907&#xff0c;\u8868\u793a\u6211\u4eec\u5e0c\u671b\u5f00\u59cb\u63a5\u6536\u6765\u81ea\u5b83\u7684\u4e8b\u4ef6\u3002<\/span><br \/>\nerror <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">input_open_device<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>error<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token function\">pr_err<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Failed to open input device, error %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> error<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">goto<\/span> err_unregister<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u9519\u8bef\u5904\u7406 goto \u94fe&#xff0c;\u6309\u9006\u5e8f\u6e05\u7406\u5df2\u5206\u914d\u7684\u8d44\u6e90\u3002<\/span><br \/>\n err_unregister<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token function\">input_unregister_handle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n err_free<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token function\">kfree<\/span><span class=\"token punctuation\">(<\/span>sysrq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">return<\/span> error<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_disconnect &#8211; \u5f53\u5339\u914d\u7684\u8f93\u5165\u8bbe\u5907\u65ad\u5f00\u65f6\u8c03\u7528\u7684\u56de\u8c03\u3002<br \/>\n * &#064;param handle: \u6307\u5411\u8981\u65ad\u5f00\u7684 input_handle \u7684\u6307\u9488\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_disconnect<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_handle<\/span> <span class=\"token operator\">*<\/span>handle<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>sysrq <span class=\"token operator\">&#061;<\/span> handle<span class=\"token operator\">-&gt;<\/span>private<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ &#034;\u5173\u95ed&#034;\u8bbe\u5907&#xff0c;\u505c\u6b62\u63a5\u6536\u4e8b\u4ef6\u3002<\/span><br \/>\n<span class=\"token function\">input_close_device<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u540c\u6b65\u53d6\u6d88\u4efb\u4f55\u6302\u8d77\u7684\u91cd\u65b0\u6ce8\u5165\u5de5\u4f5c\u3002<\/span><br \/>\n<span class=\"token function\">cancel_work_sync<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>reinject_work<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u540c\u6b65\u5173\u95ed\u5e76\u6e05\u7406\u5b9a\u65f6\u5668\u3002<\/span><br \/>\n<span class=\"token function\">timer_shutdown_sync<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece\u8f93\u5165\u6838\u5fc3\u6ce8\u9500\u8fd9\u4e2a handle\u3002<\/span><br \/>\n<span class=\"token function\">input_unregister_handle<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u91ca\u653e\u4e3a\u8fd9\u4e2a\u8bbe\u5907\u5206\u914d\u7684\u79c1\u6709\u72b6\u6001\u7ed3\u6784\u3002<\/span><br \/>\n<span class=\"token function\">kfree<\/span><span class=\"token punctuation\">(<\/span>sysrq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq Keypress State Machine: sysrq_handle_keypress<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u662f\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u7684\u5927\u8111\u2014\u2014\u4e00\u4e2a\u6709\u9650\u72b6\u6001\u673a (Finite State Machine, FSM)\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f\u7cbe\u786e\u5730\u8ddf\u8e2a\u952e\u76d8\u4e0a Alt, Shift, \u548c SysRq \u952e\u7684\u72b6\u6001&#xff0c;\u8bc6\u522b\u51fa\u5408\u6cd5\u7684 Alt &#043; SysRq &#043; &lt;Command&gt; \u7ec4\u5408\u952e\u5e8f\u5217&#xff0c;\u5e76\u5c06\u6700\u7ec8\u7684\u547d\u4ee4\u5b57\u7b26\u5206\u53d1\u7ed9 __handle_sysrq \u6267\u884c\u5668\u3002\u5b83\u901a\u8fc7\u201c\u6291\u5236\u201d&#xff08;suppress&#xff09;\u8f93\u5165\u4e8b\u4ef6&#xff0c;\u5b9e\u73b0\u4e86\u5bf9\u7528\u6237\u900f\u660e\u7684\u7ec4\u5408\u952e\u62e6\u622a&#xff0c;\u540c\u65f6\u8fd8\u5305\u542b\u4e86\u590d\u6742\u7684\u903b\u8f91\u6765\u5904\u7406\u6309\u952e\u7684\u91cd\u65b0\u6ce8\u5165&#xff08;re-injection&#xff09;\u548c\u7cfb\u7edf\u590d\u4f4d\u5e8f\u5217\u7684\u68c0\u6d4b\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u51fd\u6570\u662f SysRq \u673a\u5236\u7684\u6838\u5fc3\u7b97\u6cd5\u5b9e\u73b0&#xff0c;\u5b83\u901a\u8fc7\u7ba1\u7406\u4e00\u4e2a sysrq_state \u7ed3\u6784\u4f53\u6765\u9a71\u52a8\u72b6\u6001\u8f6c\u6362\u3002<\/p>\n<li>\n<p>\u6838\u5fc3\u72b6\u6001&#xff1a;sysrq-&gt;active:<\/p>\n<ul>\n<li>\u8fd9\u662f\u72b6\u6001\u673a\u7684\u6838\u5fc3\u6807\u5fd7\u4f4d\u3002false \u8868\u793a\u201c\u975e\u6fc0\u6d3b\u201d\u6216\u201c\u76d1\u542c\u201d\u72b6\u6001&#xff1b;true \u8868\u793a\u201c\u6fc0\u6d3b\u201d\u72b6\u6001\u3002<\/li>\n<li>\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001: \u5f53 SysRq \u952e\u88ab\u6309\u4e0b&#xff0c;\u5e76\u4e14\u4e00\u4e2a Alt \u952e\u5f53\u524d\u6b63\u88ab\u6309\u4f4f\u65f6&#xff0c;\u72b6\u6001\u673a\u4ece\u201c\u975e\u6fc0\u6d3b\u201d\u8f6c\u6362\u5230\u201c\u6fc0\u6d3b\u201d\u3002\u8fd9\u662f\u8bc6\u522b SysRq \u5e8f\u5217\u5f00\u59cb\u7684\u5173\u952e\u3002<\/li>\n<li>\u9000\u51fa\u6fc0\u6d3b\u72b6\u6001: \u5f53\u542f\u52a8\u4e86\u8be5\u5e8f\u5217\u7684\u90a3\u4e2a Alt \u952e\u88ab\u91ca\u653e\u65f6&#xff0c;\u72b6\u6001\u673a\u4ece\u201c\u6fc0\u6d3b\u201d\u8f6c\u6362\u56de\u201c\u975e\u6fc0\u6d3b\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u72b6\u6001\u53d8\u91cf\u7684\u7cbe\u786e\u6355\u83b7:<\/p>\n<ul>\n<li>alt, shift: \u8fd9\u4e24\u4e2a\u53d8\u91cf\u5b9e\u65f6\u8ddf\u8e2a\u5f53\u524d Alt \u548c Shift \u952e\u7684\u6309\u4e0b\u72b6\u6001\u3002<\/li>\n<li>alt_use, shift_use: \u8fd9\u4e24\u4e2a\u53d8\u91cf\u662f\u72b6\u6001\u673a\u7684\u4e00\u4e2a\u7cbe\u5999\u4e4b\u5904\u3002\u5b83\u4eec\u5728\u72b6\u6001\u673a\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001\u7684\u90a3\u4e00\u523b&#xff0c;\u201c\u5feb\u7167\u201d\u4e86\u5f53\u65f6\u7684 alt \u548c shift \u72b6\u6001\u3002\u8fd9\u610f\u5473\u7740&#xff0c;\u5373\u4f7f\u5728 SysRq \u5e8f\u5217\u6fc0\u6d3b\u540e\u7528\u6237\u6309\u4e0b\u4e86\u53e6\u4e00\u4e2a Alt \u952e\u6216\u6539\u53d8\u4e86 Shift \u952e\u7684\u72b6\u6001&#xff0c;\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u547d\u4ee4\u5904\u7406\u3002\u547d\u4ee4\u7684\u5927\u5c0f\u5199 (toupper) \u53ea\u53d6\u51b3\u4e8e\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001\u65f6\u7684 Shift \u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u6291\u5236\u4e0e\u4f20\u9012 (suppress):<\/p>\n<ul>\n<li>\u51fd\u6570\u7684\u8fd4\u56de\u503c suppress \u51e0\u4e4e\u603b\u662f\u7b49\u4e8e sysrq-&gt;active\u3002<\/li>\n<li>\u6fc0\u6d3b\u65f6: suppress \u4e3a true&#xff0c;sysrq_filter \u4f1a\u201c\u5403\u6389\u201d\u6240\u6709\u6309\u952e\u4e8b\u4ef6&#xff0c;\u9632\u6b62\u5b83\u4eec\u88ab\u53d1\u9001\u5230\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li>\u975e\u6fc0\u6d3b\u65f6: suppress \u4e3a false&#xff0c;\u6240\u6709\u6309\u952e\u4e8b\u4ef6\u90fd\u88ab\u6b63\u5e38\u4f20\u9012\u3002<\/li>\n<li>\u4f8b\u5916: \u6709\u4e00\u4e2a\u7279\u6b8a\u60c5\u51b5 if (value &#061;&#061; 0 &amp;&amp; test_and_clear_bit(code, sysrq-&gt;key_down))\u3002\u5b83\u5904\u7406\u7684\u662f&#xff1a;\u5982\u679c\u4e00\u4e2a\u952e\u5728\u8fdb\u5165 SysRq \u6a21\u5f0f\u4e4b\u524d\u5c31\u88ab\u6309\u4e0b\u4e86&#xff0c;\u90a3\u4e48\u5f53\u5b83\u5728 SysRq \u6a21\u5f0f\u6fc0\u6d3b\u671f\u95f4\u88ab\u91ca\u653e\u65f6&#xff0c;\u8fd9\u4e2a\u201c\u91ca\u653e\u201d\u4e8b\u4ef6\u5e94\u8be5\u88ab\u4f20\u9012\u4e0b\u53bb&#xff0c;\u800c\u4e0d\u662f\u6291\u5236\u3002\u8fd9\u9632\u6b62\u4e86\u6309\u952e\u72b6\u6001\u7684\u6df7\u4e71&#xff08;\u4f8b\u5982&#xff0c;\u9632\u6b62\u7cfb\u7edf\u8ba4\u4e3a\u67d0\u4e2a\u952e\u4e00\u76f4\u88ab\u6309\u4f4f&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6309\u952e\u91cd\u65b0\u6ce8\u5165 (need_reinject \u548c reinject_work):<\/p>\n<ul>\n<li>\u5f53 Alt&#043;SysRq \u88ab\u6309\u4e0b&#xff08;\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001&#xff09;\u65f6&#xff0c;need_reinject \u88ab\u8bbe\u4e3a true\u3002<\/li>\n<li>\u5982\u679c\u6b64\u65f6\u7528\u6237\u6309\u4e0b\u4e86\u4efb\u4f55\u4e00\u4e2a\u547d\u4ee4\u952e&#xff0c;need_reinject \u4f1a\u88ab\u6e05\u4e3a false\u3002<\/li>\n<li>\u5f53\u7528\u6237\u91ca\u653e Alt \u952e&#xff08;\u9000\u51fa\u6fc0\u6d3b\u72b6\u6001&#xff09;\u65f6&#xff0c;if (was_active) \u6761\u4ef6\u6ee1\u8db3&#xff0c;\u4f1a\u8c03\u5ea6 reinject_work \u5de5\u4f5c\u961f\u5217\u3002\u8fd9\u4e2a\u5de5\u4f5c\u961f\u5217\u7684\u6267\u884c\u51fd\u6570\u4f1a\u68c0\u67e5 need_reinject \u6807\u5fd7\u3002\u5982\u679c\u5b83\u4ecd\u7136\u662f true&#xff08;\u610f\u5473\u7740\u7528\u6237\u53ea\u6309\u4e86 Alt&#043;SysRq \u800c\u6ca1\u6709\u6309\u547d\u4ee4\u952e&#xff09;&#xff0c;\u5de5\u4f5c\u961f\u5217\u5c31\u4f1a\u624b\u52a8\u5730\u91cd\u65b0\u751f\u6210\u4e00\u4e2a Alt&#043;SysRq \u7684\u6309\u952e\u4e8b\u4ef6\u5e76\u6ce8\u5165\u56de\u8f93\u5165\u7cfb\u7edf\u3002<\/li>\n<li>\u76ee\u7684: \u8fd9\u786e\u4fdd\u4e86\u5982\u679c\u7528\u6237\u53ea\u662f\u60f3\u4f7f\u7528 Alt&#043;SysRq (PrintScreen) \u7684\u539f\u59cb\u529f\u80fd&#xff0c;\u8fd9\u4e2a\u6309\u952e\u7ec4\u5408\u4ecd\u7136\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002SysRq \u673a\u5236\u53ea\u5728\u6709\u547d\u4ee4\u952e\u8ddf\u968f\u7684\u60c5\u51b5\u4e0b&#xff0c;\u624d\u201c\u5077\u8d70\u201d\u8fd9\u4e2a\u7ec4\u5408\u952e\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_handle_keypress &#8211; SysRq \u7684\u6838\u5fc3\u72b6\u6001\u673a&#xff0c;\u5904\u7406\u5355\u4e2a\u6309\u952e\u4e8b\u4ef6\u3002<br \/>\n * &#064;param sysrq: \u6307\u5411\u5f53\u524d\u8bbe\u5907\u7684 sysrq_state \u7ed3\u6784\u4f53\u3002<br \/>\n * &#064;param code:  \u6309\u952e\u7801\u3002<br \/>\n * &#064;param value: \u6309\u952e\u503c (1&#061;\u6309\u4e0b, 0&#061;\u91ca\u653e, 2&#061;\u91cd\u590d)\u3002<br \/>\n * &#064;return bool: \u5982\u679c\u6b64\u4e8b\u4ef6\u5e94\u88ab\u6291\u5236&#xff0c;\u5219\u8fd4\u56de true\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> bool <span class=\"token function\">sysrq_handle_keypress<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>sysrq<span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> code<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\nbool was_active <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8bb0\u5f55\u8fdb\u5165\u51fd\u6570\u65f6\u7684\u6fc0\u6d3b\u72b6\u6001\u3002<\/span><br \/>\nbool suppress<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">(<\/span>code<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><\/p>\n<p><span class=\"token keyword\">case<\/span> KEY_LEFTALT<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">case<\/span> KEY_RIGHTALT<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>value<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ Alt \u952e\u88ab\u91ca\u653e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c SysRq \u6b63\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001&#xff0c;\u5e76\u4e14\u88ab\u91ca\u653e\u7684\u6b63\u662f\u542f\u52a8\u5e8f\u5217\u7684\u90a3\u4e2a Alt \u952e&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>active <span class=\"token operator\">&amp;&amp;<\/span> code <span class=\"token operator\">&#061;&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>alt_use<span class=\"token punctuation\">)<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>active <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ &#8230;\u5219\u9000\u51fa\u6fc0\u6d3b\u72b6\u6001\u3002<\/span><\/p>\n<p>sysrq<span class=\"token operator\">-&gt;<\/span>alt <span class=\"token operator\">&#061;<\/span> KEY_RESERVED<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6807\u8bb0 Alt \u952e\u5df2\u62ac\u8d77\u3002<\/span><\/p>\n<p><span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ Alt \u952e\u88ab\u6309\u4e0b (\u975e\u91cd\u590d)<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>alt <span class=\"token operator\">&#061;<\/span> code<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8bb0\u5f55\u54ea\u4e2a Alt \u952e\u88ab\u6309\u4e0b\u3002<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>need_reinject <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u91cd\u7f6e\u91cd\u6ce8\u5165\u6807\u5fd7\u3002<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">case<\/span> KEY_LEFTSHIFT<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">case<\/span> KEY_RIGHTSHIFT<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>value<span class=\"token punctuation\">)<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>shift <span class=\"token operator\">&#061;<\/span> KEY_RESERVED<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ Shift \u952e\u88ab\u91ca\u653e<\/span><br \/>\n<span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>shift <span class=\"token operator\">&#061;<\/span> code<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ Shift \u952e\u88ab\u6309\u4e0b<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ \u5982\u679c\u5df2\u6fc0\u6d3b&#xff0c;\u7acb\u5373\u66f4\u65b0\u6355\u83b7\u7684 shift \u72b6\u6001<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>shift_use <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>shift<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">case<\/span> KEY_SYSRQ<span class=\"token operator\">:<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c SysRq \u952e\u88ab\u6309\u4e0b&#xff0c;\u5e76\u4e14\u4e4b\u524d\u5df2\u7ecf\u6709 Alt \u952e\u88ab\u6309\u4e0b\u4e86&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">&amp;&amp;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>alt <span class=\"token operator\">!&#061;<\/span> KEY_RESERVED<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>active <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ &#8230;\u5219\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001&#xff01;<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>alt_use <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>alt<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6355\u83b7\u662f\u54ea\u4e2a Alt \u952e\u542f\u52a8\u4e86\u5e8f\u5217\u3002<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>shift_use <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>shift<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6355\u83b7\u6309\u4e0b SysRq \u65f6\u7684 Shift \u72b6\u6001\u3002<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u5982\u679c\u6ca1\u6709\u5176\u4ed6\u952e\u88ab\u6309\u4e0b&#xff0c;\u6211\u4eec\u5c06\u9700\u8981\u91cd\u65b0\u6ce8\u5165 Alt-SysRq \u952e\u51fb\u3002<br \/>\n *\/<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>need_reinject <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u5047\u88c5 SysRq \u952e\u4ece\u672a\u88ab\u6309\u4e0b\u8fc7\u3002\u8fd9\u5bf9\u4e8e\u6b63\u786e\u5904\u7406 KGDB \u662f\u5fc5\u9700\u7684&#xff0c;<br \/>\n * \u5b83\u5728\u9000\u51fa\u8c03\u8bd5\u5668\u540e\u4f1a\u5c1d\u8bd5\u91ca\u653e\u6240\u6709\u6309\u952e\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">clear_bit<\/span><span class=\"token punctuation\">(<\/span>KEY_SYSRQ<span class=\"token punctuation\">,<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">.<\/span>dev<span class=\"token operator\">-&gt;<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">default<\/span><span class=\"token operator\">:<\/span> <span class=\"token comment\">\/\/ \u5176\u4ed6\u4efb\u610f\u6309\u952e<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c SysRq \u5df2\u6fc0\u6d3b&#xff0c;\u5e76\u4e14\u8fd9\u662f\u4e00\u4e2a\u201c\u6309\u4e0b\u201d\u4e8b\u4ef6&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>active <span class=\"token operator\">&amp;&amp;<\/span> value <span class=\"token operator\">&amp;&amp;<\/span> value <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece\u8f6c\u6362\u8868\u4e2d\u67e5\u627e\u6309\u952e\u7801\u5bf9\u5e94\u7684\u547d\u4ee4\u5b57\u7b26\u3002<\/span><br \/>\n<span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">char<\/span> c <span class=\"token operator\">&#061;<\/span> sysrq_xlate<span class=\"token punctuation\">[<\/span>code<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>sysrq<span class=\"token operator\">-&gt;<\/span>need_reinject <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5df2\u6536\u5230\u547d\u4ee4\u952e&#xff0c;\u4e0d\u518d\u9700\u8981\u91cd\u6ce8\u5165\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u6839\u636e\u8fdb\u5165\u6fc0\u6d3b\u72b6\u6001\u65f6\u6355\u83b7\u7684 Shift \u72b6\u6001&#xff0c;\u51b3\u5b9a\u547d\u4ee4\u662f\u5426\u4e3a\u5927\u5199\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>shift_use <span class=\"token operator\">!&#061;<\/span> KEY_RESERVED<span class=\"token punctuation\">)<\/span><br \/>\nc <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">toupper<\/span><span class=\"token punctuation\">(<\/span>c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u6267\u884c SysRq \u547d\u4ee4&#xff01;<\/span><br \/>\n<span class=\"token function\">__handle_sysrq<\/span><span class=\"token punctuation\">(<\/span>c<span class=\"token punctuation\">,<\/span> true<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6291\u5236\u6807\u5fd7\u901a\u5e38\u4e0e\u6fc0\u6d3b\u72b6\u6001\u4e00\u81f4\u3002<\/span><br \/>\nsuppress <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5982\u679c\u5f53\u524d\u672a\u6fc0\u6d3b&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>active<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u68c0\u67e5\u590d\u4f4d\u5e8f\u5217\u81ea\u4e0a\u6b21\u4ee5\u6765\u662f\u5426\u5df2\u66f4\u6539\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>reset_seq_version <span class=\"token operator\">!&#061;<\/span> sysrq_reset_seq_version<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">sysrq_parse_reset_sequence<\/span><span class=\"token punctuation\">(<\/span>sysrq<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u5982\u679c\u6211\u4eec\u4e0d\u6291\u5236\u6309\u952e&#xff0c;\u5219\u8ddf\u8e2a\u952e\u76d8\u72b6\u6001&#xff0c;\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u91ca\u653e\u5728<br \/>\n * \u8fdb\u5165 SysRq \u6a21\u5f0f\u4e4b\u524d\u5df2\u6309\u4e0b\u7684\u952e\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">set_bit<\/span><span class=\"token punctuation\">(<\/span>code<span class=\"token punctuation\">,<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>key_down<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n<span class=\"token function\">clear_bit<\/span><span class=\"token punctuation\">(<\/span>code<span class=\"token punctuation\">,<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>key_down<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5982\u679c\u662f\u521a\u521a\u4ece\u6fc0\u6d3b\u72b6\u6001\u9000\u51fa&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>was_active<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u8c03\u5ea6\u5de5\u4f5c\u961f\u5217\u6765\u5904\u7406\u53ef\u80fd\u7684\u6309\u952e\u91cd\u6ce8\u5165\u3002<\/span><br \/>\n<span class=\"token function\">schedule_work<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>reinject_work<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/* \u68c0\u67e5\u590d\u4f4d\u5e8f\u5217 *\/<\/span><br \/>\n<span class=\"token function\">sysrq_detect_reset_sequence<\/span><span class=\"token punctuation\">(<\/span>sysrq<span class=\"token punctuation\">,<\/span> code<span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token function\">test_and_clear_bit<\/span><span class=\"token punctuation\">(<\/span>code<span class=\"token punctuation\">,<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>key_down<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u5bf9\u4e8e\u5728\u8fdb\u5165 SysRq \u6a21\u5f0f\u4e4b\u524d\u5df2\u6309\u4e0b\u7684\u952e&#xff0c;\u4f20\u9012\u5176\u201c\u91ca\u653e\u201d\u4e8b\u4ef6\u3002<br \/>\n *\/<\/span><br \/>\nsuppress <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> suppress<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq Work Handlers: \u5bc6\u94a5\u91cd\u65b0\u6ce8\u5165\u4e0e\u7cfb\u7edf\u91cd\u7f6e<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u4e2d\u4e24\u4e2a\u5f02\u6b65\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;<\/p>\n<li>sysrq_reinject_alt_sysrq: \u4f5c\u4e3a\u4e00\u4e2a\u5de5\u4f5c\u961f\u5217&#xff08;work queue&#xff09;\u5904\u7406\u51fd\u6570&#xff0c;\u5b83\u8d1f\u8d23\u5904\u7406\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\u2014\u2014\u5f53\u7528\u6237\u6309\u4e0b\u4e86 Alt&#043;SysRq \u4f46\u6ca1\u6709\u7d27\u8ddf\u4efb\u4f55\u547d\u4ee4\u952e\u65f6&#xff0c;\u6b64\u51fd\u6570\u4f1a\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u91cd\u65b0\u751f\u6210\u4e00\u4e2a Alt&#043;SysRq \u7684\u6309\u952e\u4e8b\u4ef6&#xff0c;\u5e76\u5c06\u5176\u6ce8\u5165\u56de\u8f93\u5165\u7cfb\u7edf&#xff0c;\u4ece\u800c\u4fdd\u7559\u8be5\u7ec4\u5408\u952e\u7684\u539f\u59cb\u529f\u80fd&#xff08;\u901a\u5e38\u662f PrintScreen&#xff09;\u3002<\/li>\n<li>sysrq_do_reset: \u4f5c\u4e3a\u4e00\u4e2a\u5b9a\u65f6\u5668&#xff08;timer&#xff09;\u56de\u8c03\u51fd\u6570&#xff0c;\u5b83\u662f\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u201c\u590d\u4f4d\u5e8f\u5217\u201d\u88ab\u6210\u529f\u68c0\u6d4b\u5230\u540e&#xff0c;\u7531\u5b9a\u65f6\u5668\u89e6\u53d1\u7684\u6700\u7ec8\u6267\u884c\u52a8\u4f5c&#xff0c;\u5176\u552f\u4e00\u804c\u8d23\u662f\u8c03\u7528 orderly_reboot() \u6765\u5b89\u5168\u5730\u91cd\u542f\u7cfb\u7edf\u3002<\/li>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u8fd9\u4e24\u4e2a\u51fd\u6570\u5c55\u793a\u4e86\u5185\u6838\u4e2d\u5982\u4f55\u4f7f\u7528\u5de5\u4f5c\u961f\u5217\u548c\u5b9a\u65f6\u5668&#xff0c;\u5c06\u8017\u65f6\u6216\u5ef6\u8fdf\u7684\u64cd\u4f5c\u4ece\u4e2d\u65ad\u7684\u201c\u5feb\u8def\u5f84\u201d\u4e2d\u89e3\u8026\u51fa\u6765\u3002<\/p>\n<li>\n<p>\u6309\u952e\u91cd\u65b0\u6ce8\u5165 (sysrq_reinject_alt_sysrq):<\/p>\n<ul>\n<li>\u6267\u884c\u4e0a\u4e0b\u6587: \u8fd9\u662f\u4e00\u4e2a work_struct \u7684\u5904\u7406\u51fd\u6570&#xff0c;\u610f\u5473\u7740\u5b83\u5728**\u5185\u6838\u7ebf\u7a0b&#xff08;\u8fdb\u7a0b\u4e0a\u4e0b\u6587&#xff09;**\u4e2d\u6267\u884c\u3002\u8fd9\u5141\u8bb8\u5b83\u5b89\u5168\u5730\u8c03\u7528 input_inject_event \u7b49\u53ef\u80fd\u6d89\u53ca\u590d\u6742\u9501\u6216\u5206\u914d\u5185\u5b58\u7684\u51fd\u6570\u3002<\/li>\n<li>\u89e6\u53d1\u6761\u4ef6: sysrq_handle_keypress \u72b6\u6001\u673a\u5728\u68c0\u6d4b\u5230 Alt \u952e\u88ab\u91ca\u653e&#xff08;\u5373 SysRq \u5e8f\u5217\u7ed3\u675f&#xff09;\u65f6&#xff0c;\u4f1a\u8c03\u5ea6\u8fd9\u4e2a\u5de5\u4f5c\u3002\u4f46\u662f&#xff0c;\u6b64\u51fd\u6570\u5185\u90e8\u6709\u4e00\u4e2a\u5173\u952e\u7684 if (sysrq-&gt;need_reinject) \u68c0\u67e5\u3002\u8fd9\u4e2a\u6807\u5fd7\u53ea\u6709\u5728 Alt&#043;SysRq \u88ab\u6309\u4e0b\u540e\u3001\u4e14\u6ca1\u6709\u4efb\u4f55\u547d\u4ee4\u952e\u8ddf\u968f\u7684\u60c5\u51b5\u4e0b&#xff0c;\u624d\u4f1a\u4fdd\u6301\u4e3a true\u3002<\/li>\n<li>\u6838\u5fc3\u673a\u5236 (input_inject_event): \u6b64\u51fd\u6570\u4f7f\u7528 input_inject_event \u6765\u6a21\u62df\u4e00\u4e2a\u5b8c\u6574\u7684 Alt&#043;SysRq \u6309\u952e\u7684\u6309\u4e0b\u548c\u91ca\u653e\u8fc7\u7a0b\u3002\u5b83\u7cbe\u786e\u5730\u6309\u987a\u5e8f\u6ce8\u5165\u201cAlt \u6309\u4e0b\u201d\u3001\u201cSysRq \u6309\u4e0b\u201d\u3001\u201c\u540c\u6b65\u4e8b\u4ef6\u201d\u3001\u201cSysRq \u91ca\u653e\u201d\u3001\u201cAlt \u91ca\u653e\u201d\u3001\u201c\u540c\u6b65\u4e8b\u4ef6\u201d&#xff0c;\u8fd9\u5bf9\u4e8e\u4e0a\u5c42&#xff08;\u5982\u56fe\u5f62\u754c\u9762&#xff09;\u6765\u8bf4&#xff0c;\u4e0e\u771f\u5b9e\u7684\u7269\u7406\u6309\u952e\u64cd\u4f5c\u5b8c\u5168\u65e0\u6cd5\u533a\u5206\u3002<\/li>\n<li>\u9632\u65e0\u9650\u5faa\u73af (reinjecting \u6807\u5fd7\u4e0e\u5185\u5b58\u5c4f\u969c): a. \u5728\u6ce8\u5165\u4e8b\u4ef6\u4e4b\u524d&#xff0c;\u5b83\u8bbe\u7f6e sysrq-&gt;reinjecting &#061; true;\u3002 b. \u5728 sysrq_filter \u51fd\u6570\u4e2d&#xff0c;\u7b2c\u4e00\u4e2a\u68c0\u67e5\u5c31\u662f if (sysrq-&gt;reinjecting) return false;\u3002 c. \u8fd9\u4e2a\u7ec4\u5408\u6784\u6210\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684\u201c\u65c1\u8def\u201d&#xff0c;\u5b83\u544a\u8bc9 sysrq_filter&#xff1a;\u201c\u63a5\u4e0b\u6765\u7684\u4e00\u7cfb\u5217\u4e8b\u4ef6\u662f\u6211\u81ea\u5df1\u751f\u6210\u7684&#xff0c;\u8bf7\u4e0d\u8981\u8fc7\u6ee4\u5b83\u4eec\u201d&#xff0c;\u4ece\u800c\u5b8c\u7f8e\u5730\u907f\u514d\u4e86\u8fc7\u6ee4\u5668\u6355\u83b7\u81ea\u5df1\u6ce8\u5165\u7684\u4e8b\u4ef6\u800c\u5bfc\u81f4\u7684\u65e0\u9650\u5faa\u73af\u3002 d. mb()&#xff08;\u5185\u5b58\u5c4f\u969c&#xff09;\u786e\u4fdd\u4e86\u5bf9 reinjecting \u6807\u5fd7\u7684\u4fee\u6539\u5bf9\u5176\u4ed6 CPU&#xff08;\u6216\u88ab\u7f16\u8bd1\u5668\u91cd\u6392&#xff09;\u662f\u7acb\u5373\u53ef\u89c1\u7684&#xff0c;\u4fdd\u8bc1\u4e86\u5728\u6ce8\u5165\u4e8b\u4ef6\u4e4b\u524d\u6807\u5fd7\u5df2\u8bbe\u7f6e&#xff0c;\u5728\u6ce8\u5165\u5b8c\u6210\u4e4b\u540e\u6807\u5fd7\u624d\u88ab\u6e05\u9664\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5ef6\u8fdf\u590d\u4f4d (sysrq_do_reset):<\/p>\n<ul>\n<li>\u6267\u884c\u4e0a\u4e0b\u6587: \u8fd9\u662f\u4e00\u4e2a timer_list \u7684\u5904\u7406\u51fd\u6570&#xff0c;\u5b83\u5728\u8f6f\u4e2d\u65ad&#xff08;softirq&#xff09;\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u3002<\/li>\n<li>\u89e6\u53d1\u6761\u4ef6: \u5b83\u7531 sysrq_handle_keypress \u5185\u90e8\u7684 sysrq_detect_reset_sequence \u903b\u8f91\u6765\u89e6\u53d1\u3002\u8be5\u903b\u8f91\u901a\u5e38\u4f1a\u542f\u52a8\u4e00\u4e2a\u77ed\u65f6\u95f4\u7684\u5b9a\u65f6\u5668\u3002\u5982\u679c\u7528\u6237\u5728\u5b9a\u65f6\u5668\u5230\u671f\u524d\u6309\u4e0b\u4e86\u590d\u4f4d\u5e8f\u5217\u4e2d\u7684\u4e0b\u4e00\u4e2a\u952e&#xff0c;\u5b9a\u65f6\u5668\u4f1a\u88ab\u91cd\u7f6e&#xff1b;\u5982\u679c\u7528\u6237\u5b8c\u6210\u4e86\u6574\u4e2a\u5e8f\u5217&#xff0c;\u5c31\u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u8c03\u7528\u6b64 sysrq_do_reset \u7684\u6700\u7ec8\u5b9a\u65f6\u5668\u3002<\/li>\n<li>\u6838\u5fc3\u673a\u5236 (orderly_reboot): \u6b64\u51fd\u6570\u7684\u6838\u5fc3\u662f\u8c03\u7528 orderly_reboot()\u3002\u8fd9\u662f\u4e00\u4e2a\u5185\u6838\u63d0\u4f9b\u7684\u6807\u51c6\u63a5\u53e3&#xff0c;\u5b83\u4f1a\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u5e72\u51c0\u7684\u91cd\u542f\u6d41\u7a0b&#xff08;\u540c\u6b65\u6587\u4ef6\u7cfb\u7edf\u3001\u5173\u95ed\u8bbe\u5907\u7b49&#xff09;&#xff0c;\u6bd4\u76f4\u63a5\u89e6\u53d1\u786c\u4ef6\u590d\u4f4d\u8981\u5b89\u5168\u5f97\u591a\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_reinject_alt_sysrq &#8211; \u91cd\u65b0\u6ce8\u5165 Alt&#043;SysRq \u6309\u952e\u4e8b\u4ef6\u7684\u5de5\u4f5c\u961f\u5217\u5904\u7406\u51fd\u6570\u3002<br \/>\n * &#064;param work: \u6307\u5411 work_struct \u7684\u6307\u9488\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_reinject_alt_sysrq<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">work_struct<\/span> <span class=\"token operator\">*<\/span>work<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece work_struct \u6307\u9488\u53cd\u5411\u63a8\u5bfc\u51fa\u5176\u6240\u5c5e\u7684 sysrq_state \u7ed3\u6784\u3002<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>sysrq <span class=\"token operator\">&#061;<\/span><br \/>\n<span class=\"token function\">container_of<\/span><span class=\"token punctuation\">(<\/span>work<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span><span class=\"token punctuation\">,<\/span> reinject_work<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">input_handle<\/span> <span class=\"token operator\">*<\/span>handle <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">&amp;<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>handle<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> alt_code <span class=\"token operator\">&#061;<\/span> sysrq<span class=\"token operator\">-&gt;<\/span>alt_use<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u83b7\u53d6\u542f\u52a8\u5e8f\u5217\u65f6\u4f7f\u7528\u7684 Alt \u952e\u7801\u3002<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4ec5\u5728\u9700\u8981\u91cd\u6ce8\u5165\u65f6&#xff08;\u5373\u53ea\u6309\u4e86 Alt&#043;SysRq \u800c\u6ca1\u6709\u547d\u4ee4\u952e&#xff09;\u624d\u6267\u884c\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq<span class=\"token operator\">-&gt;<\/span>need_reinject<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8bbe\u7f6e\u201c\u6b63\u5728\u91cd\u6ce8\u5165\u201d\u6807\u5fd7&#xff0c;\u9632\u6b62 sysrq_filter \u62e6\u622a\u81ea\u5df1\u751f\u6210\u7684\u4e8b\u4ef6\u3002<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>reinjecting <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5185\u5b58\u5c4f\u969c&#xff0c;\u786e\u4fdd\u5bf9 reinjecting \u7684\u5199\u5165\u5728\u6ce8\u5165\u4e8b\u4ef6\u4e4b\u524d\u5bf9\u6240\u6709\u89c2\u5bdf\u8005\u53ef\u89c1\u3002<\/span><br \/>\n<span class=\"token function\">mb<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/* \u6a21\u62df\u6309\u4e0b\u548c\u91ca\u653e Alt &#043; SysRq *\/<\/span><br \/>\n<span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_KEY<span class=\"token punctuation\">,<\/span> alt_code<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">\/\/ Alt \u6309\u4e0b<\/span><br \/>\n<span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_KEY<span class=\"token punctuation\">,<\/span> KEY_SYSRQ<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ SysRq \u6309\u4e0b<\/span><br \/>\n<span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_SYN<span class=\"token punctuation\">,<\/span> SYN_REPORT<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u540c\u6b65\u4e8b\u4ef6&#xff0c;\u63d0\u4ea4\u201c\u6309\u4e0b\u201d\u72b6\u6001<\/span><\/p>\n<p><span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_KEY<span class=\"token punctuation\">,<\/span> KEY_SYSRQ<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ SysRq \u91ca\u653e<\/span><br \/>\n<span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_KEY<span class=\"token punctuation\">,<\/span> alt_code<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">\/\/ Alt \u91ca\u653e<\/span><br \/>\n<span class=\"token function\">input_inject_event<\/span><span class=\"token punctuation\">(<\/span>handle<span class=\"token punctuation\">,<\/span> EV_SYN<span class=\"token punctuation\">,<\/span> SYN_REPORT<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u540c\u6b65\u4e8b\u4ef6&#xff0c;\u63d0\u4ea4\u201c\u91ca\u653e\u201d\u72b6\u6001<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5185\u5b58\u5c4f\u969c&#xff0c;\u786e\u4fdd\u5728\u6e05\u9664\u6807\u5fd7\u4e4b\u524d&#xff0c;\u6240\u6709\u6ce8\u5165\u4e8b\u4ef6\u7684\u64cd\u4f5c\u90fd\u5df2\u5b8c\u6210\u3002<\/span><br \/>\n<span class=\"token function\">mb<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nsysrq<span class=\"token operator\">-&gt;<\/span>reinjecting <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_do_reset &#8211; \u7531\u5b9a\u65f6\u5668\u89e6\u53d1\u7684\u3001\u6267\u884c\u7cfb\u7edf\u590d\u4f4d\u7684\u56de\u8c03\u51fd\u6570\u3002<br \/>\n * &#064;param t: \u6307\u5411\u89e6\u53d1\u6b64\u56de\u8c03\u7684 timer_list \u7684\u6307\u9488\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_do_reset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">timer_list<\/span> <span class=\"token operator\">*<\/span>t<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4ece timer_list \u6307\u9488\u5b89\u5168\u5730\u63a8\u5bfc\u51fa\u5176\u6240\u5c5e\u7684 sysrq_state \u7ed3\u6784\u3002<\/span><br \/>\n<span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>state <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">timer_container_of<\/span><span class=\"token punctuation\">(<\/span>state<span class=\"token punctuation\">,<\/span> t<span class=\"token punctuation\">,<\/span><br \/>\n       keyreset_timer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7&#xff0c;\u8868\u793a\u590d\u4f4d\u8bf7\u6c42\u5df2\u88ab\u786e\u8ba4&#xff08;\u4e3b\u8981\u7528\u4e8e\u8c03\u8bd5\u6216\u72b6\u6001\u67e5\u8be2&#xff09;\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_requested <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8c03\u7528\u5185\u6838\u7684\u6709\u5e8f\u91cd\u542f\u51fd\u6570\u3002<\/span><br \/>\n<span class=\"token function\">orderly_reboot<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq \u547d\u4ee4\u64cd\u4f5c&#xff1a;\u5168\u9762\u5b9a\u4e49\u96c6<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u662f\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u7684**\u201c\u52a8\u4f5c\u5e93\u201d\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f\u4e3a\u6bcf\u4e00\u4e2a SysRq \u547d\u4ee4\u5b57\u7b26&#xff08;\u5982 \u2018b\u2019 for reboot, \u2018c\u2019 for crash&#xff09;\u5b9a\u4e49\u4e00\u4e2a\u5bf9\u5e94\u7684\u64cd\u4f5c**\u3002\u5b83\u901a\u8fc7\u4e00\u7cfb\u5217\u9759\u6001\u5b9a\u4e49\u7684 sysrq_key_op \u7ed3\u6784\u4f53\u6765\u5b9e\u73b0&#xff0c;\u6bcf\u4e2a\u7ed3\u6784\u4f53\u90fd\u5c06\u4e00\u4e2a\u547d\u4ee4\u7684\u5904\u7406\u51fd\u6570 (handler)\u3001\u5e2e\u52a9\u4fe1\u606f (help_msg) \u548c\u6388\u6743\u63a9\u7801 (enable_mask) \u7ed1\u5b9a\u5728\u4e00\u8d77\u3002\u6700\u7ec8&#xff0c;\u8fd9\u4e9b\u64cd\u4f5c\u88ab\u7ec4\u7ec7\u5230\u4e00\u4e2a\u540d\u4e3a sysrq_key_table \u7684**\u5206\u53d1\u8868&#xff08;dispatch table&#xff09;**\u4e2d&#xff0c;\u4f9b __handle_sysrq \u51fd\u6570\u5feb\u901f\u67e5\u627e\u548c\u8c03\u7528\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u4ee3\u7801\u662f SysRq \u6846\u67b6\u4e2d\u201c\u7b56\u7565\u201d\u4e0e\u201c\u673a\u5236\u201d\u5206\u79bb\u7684\u5178\u8303\u3002__handle_sysrq \u63d0\u4f9b\u4e86\u5206\u53d1\u548c\u6388\u6743\u7684\u201c\u673a\u5236\u201d&#xff0c;\u800c\u672c\u4ee3\u7801\u7247\u6bb5\u5219\u5b9a\u4e49\u4e86\u6240\u6709\u5177\u4f53\u7684\u201c\u7b56\u7565\u201d&#xff08;\u5373\u6bcf\u4e2a\u547d\u4ee4\u5b9e\u9645\u505a\u4ec0\u4e48&#xff09;\u3002<\/p>\n<li>\n<p>struct sysrq_key_op&#xff1a;\u539f\u5b50\u64cd\u4f5c\u7684\u5c01\u88c5:<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f SysRq \u547d\u4ee4\u7684\u57fa\u672c\u5355\u5143\u3002\u5b83\u5c06\u4e00\u4e2a\u547d\u4ee4\u7684\u6240\u6709\u76f8\u5173\u4fe1\u606f\u6253\u5305\u5728\u4e00\u8d77&#xff1a;\n<ul>\n<li>.handler: \u4e00\u4e2a\u51fd\u6570\u6307\u9488&#xff0c;\u6307\u5411\u771f\u6b63\u6267\u884c\u8be5\u547d\u4ee4\u7684\u4ee3\u7801\u3002\u8fd9\u662f\u6700\u91cd\u8981\u7684\u6210\u5458\u3002<\/li>\n<li>.help_msg: \u4e00\u6bb5\u7b80\u77ed\u7684\u6587\u672c&#xff0c;\u7528\u4e8e\u5728\u7528\u6237\u8bf7\u6c42\u5e2e\u52a9\u65f6&#xff08;\u4f8b\u5982&#xff0c;\u8f93\u5165\u4e00\u4e2a\u672a\u5b9a\u4e49\u7684 SysRq \u952e&#xff09;\u663e\u793a\u3002<\/li>\n<li>.action_msg: \u4e00\u6bb5\u5728\u547d\u4ee4\u5373\u5c06\u6267\u884c\u65f6\u6253\u5370\u5230\u63a7\u5236\u53f0\u7684\u6d88\u606f&#xff0c;\u4e3a\u7528\u6237\u63d0\u4f9b\u660e\u786e\u7684\u53cd\u9988\u3002<\/li>\n<li>.enable_mask: \u4e00\u4e2a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u5c06\u6b64\u547d\u4ee4\u4e0e \/proc\/sys\/kernel\/sysrq \u4e2d\u7684\u7279\u5b9a\u63a7\u5236\u4f4d\u76f8\u5173\u8054\u3002__handle_sysrq \u4f1a\u7528\u8fd9\u4e2a\u63a9\u7801\u6765\u68c0\u67e5\u547d\u4ee4\u662f\u5426\u88ab\u6388\u6743\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5177\u4f53\u7684\u64cd\u4f5c\u5b9e\u73b0 (sysrq_handle_*):<\/p>\n<ul>\n<li>\u4ee3\u7801\u4e3a\u6bcf\u4e2a\u547d\u4ee4\u90fd\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c0f\u578b\u7684\u3001\u804c\u8d23\u5355\u4e00\u7684\u5904\u7406\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u662f SysRq \u5f3a\u5927\u529f\u80fd\u7684\u6700\u7ec8\u4f53\u73b0\u3002\u4f8b\u5982&#xff1a;\n<ul>\n<li>sysrq_handle_reboot: \u8c03\u7528 emergency_restart() \u6765\u7acb\u5373\u91cd\u542f\u7cfb\u7edf\u3002\u5b83\u8fd8\u7279\u610f\u5173\u95ed\u4e86\u9501\u4f9d\u8d56\u68c0\u67e5 (lockdep_off) \u5e76\u5f00\u542f\u4e86\u4e2d\u65ad&#xff0c;\u4ee5\u589e\u52a0\u5728\u7cfb\u7edf\u4e25\u91cd\u4e0d\u7a33\u5b9a\u65f6\u91cd\u542f\u6210\u529f\u7684\u6982\u7387\u3002<\/li>\n<li>sysrq_handle_crash: \u975e\u5e38\u76f4\u63a5&#xff0c;\u5b83\u8c03\u7528 panic() \u6765\u4e3b\u52a8\u89e6\u53d1\u5185\u6838\u5d29\u6e83&#xff08;kernel panic&#xff09;\u3002\u8fd9\u5bf9\u4e8e\u83b7\u53d6\u4e00\u4efd\u5b8c\u6574\u7684 vmcore (\u5185\u5b58\u8f6c\u50a8) \u4ee5\u8fdb\u884c\u4e8b\u540e\u8c03\u8bd5\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li>sysrq_handle_sync: \u8c03\u7528 emergency_sync()&#xff0c;\u5f3a\u5236\u5c06\u6240\u6709\u6587\u4ef6\u7cfb\u7edf\u7684\u810f\u6570\u636e\u540c\u6b65\u5230\u78c1\u76d8\u3002<\/li>\n<li>sysrq_handle_showstate: \u8c03\u7528 show_state()&#xff0c;\u6253\u5370\u51fa\u6240\u6709\u4efb\u52a1\u7684\u5f53\u524d\u72b6\u6001\u548c\u5806\u6808\u56de\u6eaf&#xff0c;\u662f\u8c03\u8bd5\u6b7b\u9501\u6216\u7cfb\u7edf\u6302\u8d77\u7684\u9996\u9009\u5de5\u5177\u3002<\/li>\n<li>sysrq_handle_term \/ kill: \u904d\u5386\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u975e\u5185\u6838\u7ebf\u7a0b\u7684\u7528\u6237\u8fdb\u7a0b&#xff0c;\u5e76\u5411\u5b83\u4eec\u53d1\u9001 SIGTERM \u6216 SIGKILL \u4fe1\u53f7\u3002<\/li>\n<li>sysrq_handle_moom: \u901a\u8fc7\u5de5\u4f5c\u961f\u5217\u5f02\u6b65\u89e6\u53d1\u4e00\u6b21\u201c\u5185\u5b58\u6ea2\u51fa\u6740\u624b\u201d&#xff08;Out-Of-Memory Killer&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5206\u53d1\u8868 (sysrq_key_table):<\/p>\n<ul>\n<li>sysrq_key_table \u662f\u4e00\u4e2a sysrq_key_op \u6307\u9488\u6570\u7ec4&#xff0c;\u5b83\u626e\u6f14\u7740\u4e00\u4e2a\u54c8\u5e0c\u8868\u6216\u5206\u53d1\u8868\u7684\u89d2\u8272\u3002\u6570\u7ec4\u7684\u7d22\u5f15\u4e0e\u547d\u4ee4\u5b57\u7b26\u7684 ASCII \u503c&#xff08;\u6216\u5176\u504f\u79fb&#xff09;\u76f8\u5bf9\u5e94\u3002<\/li>\n<li>\u5f53 __handle_sysrq \u63a5\u6536\u5230\u547d\u4ee4\u5b57\u7b26 key \u65f6&#xff0c;\u5b83\u4f1a\u901a\u8fc7\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570&#xff08;__sysrq_get_key_op&#xff0c;\u672a\u5728\u6b64\u5c55\u793a&#xff09;\u5c06 key \u6620\u5c04\u5230\u8fd9\u4e2a\u6570\u7ec4\u7684\u4e00\u4e2a\u7d22\u5f15\u4e0a&#xff0c;\u4ece\u800c\u4ee5 O(1) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5feb\u901f\u627e\u5230\u5bf9\u5e94\u7684\u64cd\u4f5c\u7ed3\u6784\u3002<\/li>\n<li>\u6570\u7ec4\u4e2d\u7684 NULL \u6761\u76ee\u8868\u793a\u8be5\u952e\u5f53\u524d\u6ca1\u6709\u5206\u914d\u4efb\u4f55 SysRq \u64cd\u4f5c&#xff0c;\u5176\u4e2d\u4e00\u4e9b\u88ab\u4fdd\u7559\u7ed9\u7b2c\u4e09\u65b9\u6a21\u5757&#xff08;\u5982\u5185\u6838\u8c03\u8bd5\u5668&#xff09;\u52a8\u6001\u6ce8\u518c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6761\u4ef6\u7f16\u8bd1\u7684\u6a21\u5757\u5316:<\/p>\n<ul>\n<li>\u4ee3\u7801\u5e7f\u6cdb\u4f7f\u7528\u4e86 #ifdef \u2026 #else \u2026 #endif \u7ed3\u6784\u3002\u8fd9\u4f7f\u5f97 SysRq \u7684\u529f\u80fd\u96c6\u53ef\u4ee5\u6839\u636e\u5185\u6838\u7684\u7f16\u8bd1\u914d\u7f6e\u8fdb\u884c\u88c1\u526a\u3002<\/li>\n<li>\u4f8b\u5982&#xff0c;\u53ea\u6709\u5728 CONFIG_SMP (\u591a\u6838\u652f\u6301) \u88ab\u542f\u7528\u65f6&#xff0c;sysrq_showallcpus_op (\u663e\u793a\u6240\u6709 CPU \u7684\u56de\u6eaf) \u76f8\u5173\u7684\u4ee3\u7801\u624d\u4f1a\u88ab\u7f16\u8bd1\u8fdb\u53bb\u3002\u540c\u6837&#xff0c;sysrq_showlocks_op \u4f9d\u8d56\u4e8e CONFIG_LOCKDEP (\u9501\u4f9d\u8d56\u68c0\u67e5\u5668)\u3002\u8fd9\u79cd\u8bbe\u8ba1\u786e\u4fdd\u4e86\u5728\u4e0d\u9700\u8981\u67d0\u4e9b\u529f\u80fd\u7684\u914d\u7f6e\u4e0b&#xff0c;\u76f8\u5173\u7684\u4ee3\u7801\u548c\u6570\u636e\u7ed3\u6784\u4e0d\u4f1a\u5360\u7528\u4efb\u4f55\u5185\u5b58\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_handle_loglevel &#8211; SysRq handler for changing console loglevel.<br \/>\n * &#064;param key: \u547d\u4ee4\u5b57\u7b26 (&#039;0&#039;-&#039;9&#039;).<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_handle_loglevel<\/span><span class=\"token punctuation\">(<\/span>u8 key<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\nu8 loglevel <span class=\"token operator\">&#061;<\/span> key <span class=\"token operator\">&#8211;<\/span> <span class=\"token char\">&#039;0&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e34\u65f6\u5c06\u65e5\u5fd7\u7ea7\u522b\u8bbe\u4e3a\u9ed8\u8ba4&#xff0c;\u4ee5\u786e\u4fdd pr_info \u80fd\u6253\u5370\u51fa\u6765\u3002<\/span><br \/>\nconsole_loglevel <span class=\"token operator\">&#061;<\/span> CONSOLE_LOGLEVEL_DEFAULT<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">pr_info<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Loglevel set to %u\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> loglevel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8bbe\u7f6e\u65b0\u7684\u65e5\u5fd7\u7ea7\u522b\u3002<\/span><br \/>\nconsole_loglevel <span class=\"token operator\">&#061;<\/span> loglevel<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ &#039;0&#039;-&#039;9&#039;\u952e\u7684\u64cd\u4f5c\u5b9a\u4e49<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_key_op<\/span> sysrq_loglevel_op <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>handler<span class=\"token operator\">&#061;<\/span> sysrq_handle_loglevel<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>help_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;loglevel(0-9)&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>action_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Changing Loglevel&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>enable_mask<span class=\"token operator\">&#061;<\/span> SYSRQ_ENABLE_LOG<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ &#8230; (\u5176\u4ed6 sysrq_handle_* \u51fd\u6570\u7684\u5b9e\u73b0&#xff0c;\u6bcf\u4e2a\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5177\u4f53\u7684\u64cd\u4f5c) &#8230;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_handle_reboot &#8211; SysRq handler for rebooting the system.<br \/>\n * &#064;param key: \u547d\u4ee4\u5b57\u7b26 &#039;b&#039;.<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_handle_reboot<\/span><span class=\"token punctuation\">(<\/span>u8 key<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token function\">lockdep_off<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5173\u95ed\u9501\u4f9d\u8d56\u68c0\u67e5&#xff0c;\u4ee5\u589e\u52a0\u5728\u9501\u6df7\u4e71\u72b6\u6001\u4e0b\u91cd\u542f\u7684\u6210\u529f\u7387\u3002<\/span><br \/>\n<span class=\"token function\">local_irq_enable<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u786e\u4fdd\u4e2d\u65ad\u662f\u5f00\u542f\u7684&#xff0c;\u67d0\u4e9b\u91cd\u542f\u8def\u5f84\u53ef\u80fd\u9700\u8981\u3002<\/span><br \/>\n<span class=\"token function\">emergency_restart<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8c03\u7528\u7d27\u6025\u91cd\u542f\u51fd\u6570\u3002<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ &#039;b&#039;\u952e\u7684\u64cd\u4f5c\u5b9a\u4e49<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_key_op<\/span> sysrq_reboot_op <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>handler<span class=\"token operator\">&#061;<\/span> sysrq_handle_reboot<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>help_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;reboot(b)&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>action_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Resetting&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>enable_mask<span class=\"token operator\">&#061;<\/span> SYSRQ_ENABLE_BOOT<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ &#8230; (sysrq_handle_sync, sysrq_handle_crash \u7b49\u51fd\u6570\u7684\u5b9e\u73b0) &#8230;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_handle_showstate &#8211; SysRq handler for showing task states.<br \/>\n * &#064;param key: \u547d\u4ee4\u5b57\u7b26 &#039;t&#039;.<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_handle_showstate<\/span><span class=\"token punctuation\">(<\/span>u8 key<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token function\">show_state<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6253\u5370\u6240\u6709\u4efb\u52a1\u7684\u72b6\u6001\u548c\u5806\u6808\u56de\u6eaf\u3002<\/span><br \/>\n<span class=\"token function\">show_all_workqueues<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6253\u5370\u6240\u6709\u5de5\u4f5c\u961f\u5217\u7684\u72b6\u6001\u3002<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ &#039;t&#039;\u952e\u7684\u64cd\u4f5c\u5b9a\u4e49<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_key_op<\/span> sysrq_showstate_op <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>handler<span class=\"token operator\">&#061;<\/span> sysrq_handle_showstate<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>help_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;show-task-states(t)&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>action_msg<span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;Show State&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>enable_mask<span class=\"token operator\">&#061;<\/span> SYSRQ_ENABLE_DUMP<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ &#8230; (\u5176\u4ed6\u64cd\u4f5c\u7684\u5b9a\u4e49) &#8230;<\/span><\/p>\n<p><span class=\"token comment\">\/* Key Operations table and lock *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token function\">DEFINE_SPINLOCK<\/span><span class=\"token punctuation\">(<\/span>sysrq_key_table_lock<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u7528\u4e8e\u4fdd\u62a4\u52a8\u6001\u6ce8\u518c\/\u6ce8\u9500\u64cd\u4f5c\u7684\u9501<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;var sysrq_key_table<br \/>\n * &#064;brief SysRq \u547d\u4ee4\u7684\u5206\u53d1\u8868\u3002<br \/>\n * \u8fd9\u662f\u4e00\u4e2a\u6307\u9488\u6570\u7ec4&#xff0c;\u5c06\u547d\u4ee4\u5b57\u7b26\u6620\u5c04\u5230\u5bf9\u5e94\u7684 sysrq_key_op \u7ed3\u6784\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_key_op<\/span> <span class=\"token operator\">*<\/span>sysrq_key_table<span class=\"token punctuation\">[<\/span><span class=\"token number\">62<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/* &#039;0&#039; to &#039;9&#039; *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_loglevel_op<span class=\"token punctuation\">,<\/span><\/p>\n<p><span class=\"token comment\">\/* &#039;a&#039; to &#039;z&#039; *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* a *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_reboot_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* b *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_crash_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* c *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showlocks_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* d *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_term_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* e *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_moom_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* f *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* g *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* h (help) *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_kill_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* i *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_thaw_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* j *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_SAK_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* k *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showallcpus_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* l *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showmem_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* m *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_unrt_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* n *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* o (off) *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showregs_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* p *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_show_timers_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* q *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_unraw_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* r *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_sync_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* s *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showstate_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* t *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_mountro_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* u *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* v *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_showstate_blocked_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* w *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* x *\/<\/span><br \/>\n<span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* y *\/<\/span><br \/>\n<span class=\"token operator\">&amp;<\/span>sysrq_ftrace_dump_op<span class=\"token punctuation\">,<\/span><span class=\"token comment\">\/* z *\/<\/span><\/p>\n<p><span class=\"token comment\">\/* &#8230; (\u5927\u5199\u5b57\u6bcd\u7684\u90e8\u5206\u5b9a\u4e49) &#8230; *\/<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>SysRq \u547d\u4ee4\u6267\u884c\u5f15\u64ce: __handle_sysrq<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u662f\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u7684\u6700\u7ec8\u6267\u884c\u5f15\u64ce\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;\u63a5\u6536\u4e00\u4e2a\u547d\u4ee4\u5b57\u7b26&#xff08;key&#xff09;&#xff0c;\u5728\u4e00\u4e2a\u53d7 RCU \u4fdd\u62a4\u7684\u4e0a\u4e0b\u6587\u4e2d&#xff0c;\u67e5\u627e\u4e0e\u8be5\u5b57\u7b26\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002\u5982\u679c\u627e\u5230&#xff0c;\u5b83\u4f1a\u9a8c\u8bc1\u8be5\u64cd\u4f5c\u662f\u5426\u88ab\u5f53\u524d\u7cfb\u7edf\u7684 SysRq \u63a9\u7801\u6240\u5141\u8bb8&#xff08;\u9664\u975e\u88ab\u544a\u77e5\u8df3\u8fc7\u68c0\u67e5&#xff09;&#xff0c;\u7136\u540e\u6253\u5370\u4e00\u6761\u4fe1\u606f\u6027\u6d88\u606f&#xff0c;\u5e76\u8c03\u7528\u8be5\u64cd\u4f5c\u7684\u6700\u7ec8\u5904\u7406\u51fd\u6570&#xff08;handler&#xff09;\u3002\u5982\u679c\u672a\u627e\u5230\u5bf9\u5e94\u7684\u64cd\u4f5c&#xff0c;\u5b83\u4f1a\u6253\u5370\u4e00\u4efd\u5305\u542b\u6240\u6709\u53ef\u7528\u547d\u4ee4\u7684\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>__handle_sysrq \u662f SysRq \u5e8f\u5217\u88ab\u8bc6\u522b\u540e\u7684\u7ec8\u70b9&#xff0c;\u662f\u6240\u6709\u9b54\u672f\u547d\u4ee4\u7684\u201c\u5206\u53d1\u4e2d\u5fc3\u201d\u3002\u5176\u5b9e\u73b0\u4f53\u73b0\u4e86\u5185\u6838\u5728\u6267\u884c\u6f5c\u5728\u5371\u9669\u64cd\u4f5c\u65f6\u7684\u591a\u91cd\u5b89\u5168\u548c\u540c\u6b65\u4fdd\u969c\u3002<\/p>\n<li>\n<p>\u6388\u6743\u68c0\u67e5 (check_mask):<\/p>\n<ul>\n<li>__handle_sysrq \u7684 check_mask \u53c2\u6570\u662f\u4e00\u4e2a\u5173\u952e\u7684\u8bbe\u8ba1\u3002\u5b83\u533a\u5206\u4e86\u4e24\u79cd\u8c03\u7528\u6765\u6e90&#xff1a; a. \u7269\u7406\u952e\u76d8\/\u63a7\u5236\u53f0: \u5f53\u7528\u6237\u901a\u8fc7 Alt&#043;SysRq&#043;Key \u89e6\u53d1\u65f6&#xff0c;sysrq_handle_keypress \u4f1a\u8c03\u7528 __handle_sysrq(c, true)\u3002check_mask &#061; true \u5f3a\u5236\u51fd\u6570\u68c0\u67e5 sysrq_on_mask(op_p-&gt;enable_mask)&#xff0c;\u5373\u8be5\u547d\u4ee4\u662f\u5426\u88ab \/proc\/sys\/kernel\/sysrq \u7684\u63a9\u7801\u6240\u5141\u8bb8\u3002 b. \/proc\/sysrq-trigger: \u5f53\u7528\u6237\u901a\u8fc7\u5411 \/proc\/sysrq-trigger \u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u6765\u89e6\u53d1\u547d\u4ee4\u65f6&#xff0c;\u5185\u6838\u4f1a\u8c03\u7528 __handle_sysrq(c, false)\u3002check_mask &#061; false \u4f1a\u7ed5\u8fc7 sysrq_on_mask \u68c0\u67e5&#xff0c;\u4f7f\u5f97 root \u7528\u6237\u603b\u80fd\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u6267\u884c\u4efb\u4f55 SysRq \u547d\u4ee4&#xff0c;\u5373\u4f7f\u5b83\u5bf9\u7269\u7406\u952e\u76d8\u662f\u7981\u7528\u7684\u3002\u8fd9\u4e3a\u8fdc\u7a0b\u7ba1\u7406\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u540e\u95e8\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5f3a\u5236\u63a7\u5236\u53f0\u8f93\u51fa:<\/p>\n<ul>\n<li>suppress_printk \u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf&#xff0c;\u53ef\u4ee5\u9759\u9ed8\u5185\u6838\u6d88\u606f\u3002__handle_sysrq \u9996\u5148\u4fdd\u5b58\u5176\u5f53\u524d\u503c&#xff0c;\u7136\u540e\u5f3a\u5236\u8bbe\u4e3a 0&#xff0c;\u5e76\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u6062\u590d\u3002<\/li>\n<li>printk_force_console_enter() \/ exit() \u662f\u4e00\u5bf9\u66f4\u5f3a\u7684\u539f\u8bed\u3002\u5b83\u786e\u4fdd\u4e86\u5728 __handle_sysrq \u5185\u90e8\u7684\u6240\u6709 printk \u8c03\u7528\u90fd\u4f1a\u7ed5\u8fc7\u65e5\u5fd7\u7f13\u51b2\u533a&#xff0c;\u76f4\u63a5\u3001\u540c\u6b65\u5730\u5199\u5165\u5230\u7269\u7406\u63a7\u5236\u53f0\u9a71\u52a8\u3002<\/li>\n<li>\u76ee\u7684: \u8fd9\u4e24\u9879\u63aa\u65bd\u786e\u4fdd\u4e86\u65e0\u8bba\u7cfb\u7edf\u5904\u4e8e\u4f55\u79cd\u72b6\u6001&#xff08;\u5373\u4f7f printk \u88ab\u7981\u7528\u6216\u65e5\u5fd7\u7cfb\u7edf\u5361\u6b7b&#xff09;&#xff0c;\u7528\u6237\u90fd\u80fd\u5728\u63a7\u5236\u53f0\u4e0a\u7acb\u5373\u770b\u5230 SysRq \u547d\u4ee4\u7684\u53cd\u9988&#xff0c;\u8fd9\u5728\u7cfb\u7edf\u6302\u8d77\u65f6\u81f3\u5173\u91cd\u8981\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>RCU \u5b89\u5168\u7684\u64cd\u4f5c\u8868\u8bbf\u95ee:<\/p>\n<ul>\n<li>\u6574\u4e2a\u6838\u5fc3\u903b\u8f91\u88ab rcu_sysrq_start() \/ end() \u548c rcu_read_lock() \/ unlock() \u5305\u56f4\u3002<\/li>\n<li>__sysrq_get_key_op(key) \u4f1a\u5728\u4e00\u4e2a\u5168\u5c40\u7684 sysrq_key_table \u6570\u7ec4\u4e2d\u67e5\u627e\u4e0e key \u5bf9\u5e94\u7684 sysrq_key_op \u7ed3\u6784\u3002<\/li>\n<li>\u76ee\u7684: \u4f7f\u7528 RCU \u4fdd\u62a4\u5bf9\u8fd9\u4e2a\u8868\u7684\u8bbf\u95ee&#xff0c;\u662f\u4e3a\u4e86\u5728\u7406\u8bba\u4e0a\u652f\u6301\u52a8\u6001\u52a0\u8f7d\/\u5378\u8f7d SysRq \u64cd\u4f5c\u6a21\u5757\u7684\u573a\u666f\u3002RCU \u4fdd\u8bc1\u4e86\u5728 rcu_read_lock \u4e34\u754c\u533a\u5185&#xff0c;op_p \u6307\u9488\u6240\u6307\u5411\u7684 sysrq_key_op \u7ed3\u6784\u662f\u7a33\u5b9a\u6709\u6548\u7684&#xff0c;\u4e0d\u4f1a\u88ab\u5e76\u53d1\u5730\u91ca\u653e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u5206\u53d1\u4e0e\u5e2e\u52a9\u4fe1\u606f:<\/p>\n<ul>\n<li>\u5982\u679c __sysrq_get_key_op \u6210\u529f\u627e\u5230\u4e86\u64cd\u4f5c op_p&#xff1a;\n<ul>\n<li>\u5728\u901a\u8fc7\u6388\u6743\u68c0\u67e5\u540e&#xff0c;\u5b83\u4f1a\u5148\u6253\u5370 op_p-&gt;action_msg&#xff08;\u4f8b\u5982 \u201cRebooting\u201d&#xff09;&#xff0c;\u7136\u540e\u8c03\u7528\u6838\u5fc3\u7684 op_p-&gt;handler(key) \u51fd\u6570\u6307\u9488&#xff0c;\u8fd9\u624d\u662f\u771f\u6b63\u6267\u884c\u91cd\u542f\u3001\u8f6c\u50a8\u4efb\u52a1\u7b49\u64cd\u4f5c\u7684\u5730\u65b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5982\u679c\u672a\u627e\u5230\u64cd\u4f5c op_p&#xff1a;\n<ul>\n<li>\u5b83\u4f1a\u8fdb\u5165\u5e2e\u52a9\u6a21\u5f0f&#xff0c;\u6253\u5370 &#034;HELP : &#034;&#xff0c;\u7136\u540e\u904d\u5386\u6574\u4e2a sysrq_key_table&#xff0c;\u5c06\u6240\u6709\u5df2\u6ce8\u518c\u64cd\u4f5c\u7684 help_msg \u6253\u5370\u51fa\u6765\u3002<\/li>\n<li>\u5185\u90e8\u7684 j \u5faa\u73af\u662f\u4e00\u4e2a\u53bb\u91cd\u673a\u5236&#xff0c;\u786e\u4fdd\u5bf9\u4e8e\u6709\u522b\u540d\u7684\u547d\u4ee4&#xff08;\u591a\u4e2a\u952e\u6307\u5411\u540c\u4e00\u4e2a\u64cd\u4f5c&#xff09;&#xff0c;\u5176\u5e2e\u52a9\u4fe1\u606f\u53ea\u88ab\u6253\u5370\u4e00\u6b21\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief __handle_sysrq &#8211; SysRq \u547d\u4ee4\u7684\u4e2d\u592e\u5904\u7406\u548c\u5206\u53d1\u51fd\u6570\u3002<br \/>\n * &#064;param key:        \u8981\u6267\u884c\u7684\u547d\u4ee4\u5b57\u7b26\u3002<br \/>\n * &#064;param check_mask: \u5982\u679c\u4e3a true&#xff0c;\u5219\u68c0\u67e5\u547d\u4ee4\u662f\u5426\u88ab sysrq \u63a9\u7801\u5141\u8bb8\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">__handle_sysrq<\/span><span class=\"token punctuation\">(<\/span>u8 key<span class=\"token punctuation\">,<\/span> bool check_mask<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_key_op<\/span> <span class=\"token operator\">*<\/span>op_p<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\/ &lt; \u6307\u5411\u67e5\u627e\u5230\u7684 SysRq \u64cd\u4f5c\u7684\u6307\u9488\u3002<\/span><br \/>\n<span class=\"token keyword\">int<\/span> orig_suppress_printk<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/\/ &lt; \u7528\u4e8e\u4fdd\u5b58\u539f\u59cb\u7684 printk \u6291\u5236\u72b6\u6001\u3002<\/span><br \/>\n<span class=\"token keyword\">int<\/span> i<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4fdd\u5b58\u5e76\u91cd\u7f6e printk \u6291\u5236\u72b6\u6001&#xff0c;\u786e\u4fdd SysRq \u8f93\u51fa\u603b\u662f\u53ef\u89c1\u3002<\/span><br \/>\norig_suppress_printk <span class=\"token operator\">&#061;<\/span> suppress_printk<span class=\"token punctuation\">;<\/span><br \/>\nsuppress_printk <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8fdb\u5165 SysRq \u7279\u6709\u7684 RCU \u8bfb\u4fa7\u4e34\u754c\u533a\u3002<\/span><br \/>\n<span class=\"token function\">rcu_sysrq_start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">rcu_read_lock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u8fdb\u5165\u5f3a\u5236\u63a7\u5236\u53f0\u8f93\u51fa\u6a21\u5f0f&#xff0c;\u4ee5\u4fbf SysRq \u5934\u90e8\u4fe1\u606f\u80fd\u663e\u793a\u51fa\u6765&#xff0c;<br \/>\n * \u4e3a\u7528\u6237\u63d0\u4f9b\u660e\u786e\u7684\u53cd\u9988\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token function\">printk_force_console_enter<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6839\u636e\u547d\u4ee4\u5b57\u7b26\u67e5\u627e\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002<\/span><br \/>\nop_p <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">__sysrq_get_key_op<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>op_p<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ \u5982\u679c\u627e\u5230\u4e86\u6709\u6548\u7684\u64cd\u4f5c&#8230;<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u662f\u5426\u5e94\u8be5\u68c0\u67e5\u542f\u7528\u7684\u64cd\u4f5c (\u6765\u81ea \/proc\/sysrq-trigger \u7684\u8c03\u7528\u4e0d\u5e94\u68c0\u67e5)&#xff0c;<br \/>\n * \u4ee5\u53ca\u88ab\u8c03\u7528\u7684\u64cd\u4f5c\u662f\u5426\u5df2\u542f\u7528&#xff1f;<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>check_mask <span class=\"token operator\">||<\/span> <span class=\"token function\">sysrq_on_mask<\/span><span class=\"token punctuation\">(<\/span>op_p<span class=\"token operator\">-&gt;<\/span>enable_mask<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u4e0d\u9700\u8981\u68c0\u67e5\u63a9\u7801&#xff0c;\u6216\u8005\u547d\u4ee4\u5df2\u88ab\u63a9\u7801\u5141\u8bb8&#8230;<\/span><br \/>\n<span class=\"token function\">pr_info<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;%s\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> op_p<span class=\"token operator\">-&gt;<\/span>action_msg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6253\u5370\u5c06\u8981\u6267\u884c\u7684\u52a8\u4f5c\u4fe1\u606f\u3002<\/span><br \/>\n<span class=\"token function\">printk_force_console_exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u9000\u51fa\u5f3a\u5236\u63a7\u5236\u53f0\u6a21\u5f0f&#xff0c;handler\u53ef\u80fd\u4f1a\u8017\u65f6\u3002<\/span><br \/>\nop_p<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">handler<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8c03\u7528\u771f\u6b63\u7684\u5904\u7406\u51fd\u6570&#xff01;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u547d\u4ee4\u88ab\u63a9\u7801\u7981\u7528&#8230;<\/span><br \/>\n<span class=\"token function\">pr_info<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;This sysrq operation is disabled.\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">printk_force_console_exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ \u5982\u679c\u6ca1\u6709\u627e\u5230\u4e0e key \u5bf9\u5e94\u7684\u64cd\u4f5c&#8230;<\/span><br \/>\n<span class=\"token function\">pr_info<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;HELP : &#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ &#8230;\u5219\u6253\u5370\u5e2e\u52a9\u4fe1\u606f\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u904d\u5386\u6574\u4e2a SysRq \u64cd\u4f5c\u8868\u3002<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span>i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token function\">ARRAY_SIZE<\/span><span class=\"token punctuation\">(<\/span>sysrq_key_table<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq_key_table<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">int<\/span> j<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u8fd9\u662f\u4e00\u4e2a\u53bb\u91cd\u5faa\u73af&#xff1a;\u786e\u4fdd\u6bcf\u4e2a\u552f\u4e00\u7684 handler \u7684\u5e2e\u52a9\u53ea\u88ab\u6253\u5370\u4e00\u6b21\u3002<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span>j <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> sysrq_key_table<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">!&#061;<\/span><br \/>\nsysrq_key_table<span class=\"token punctuation\">[<\/span>j<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> j<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>j <span class=\"token operator\">!&#061;<\/span> i<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4f7f\u7528 pr_cont \u5728\u540c\u4e00\u884c\u7ee7\u7eed\u6253\u5370\u3002<\/span><br \/>\n<span class=\"token function\">pr_cont<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;%s &#034;<\/span><span class=\"token punctuation\">,<\/span> sysrq_key_table<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token operator\">-&gt;<\/span>help_msg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token function\">pr_cont<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">printk_force_console_exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token function\">rcu_read_unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">rcu_sysrq_end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u6062\u590d\u539f\u59cb\u7684 printk \u6291\u5236\u72b6\u6001\u3002<\/span><br \/>\nsuppress_printk <span class=\"token operator\">&#061;<\/span> orig_suppress_printk<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq \u91cd\u7f6e\u5e8f\u5217\u68c0\u6d4b\u5668&#xff1a; sysrq_detect_reset_sequence<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u662f\u201c\u9b54\u672f SysRq\u201d\u590d\u4f4d\u529f\u80fd\u7684\u6838\u5fc3\u72b6\u6001\u673a\u548c\u4e8b\u4ef6\u68c0\u6d4b\u5668\u3002\u5176\u4e3b\u8981\u529f\u80fd\u662f&#xff1a;\u5728\u6bcf\u6b21\u6309\u952e\u4e8b\u4ef6\u53d1\u751f\u65f6\u88ab\u8c03\u7528&#xff0c;\u901a\u8fc7\u7cbe\u786e\u5730\u8ddf\u8e2a\u4e00\u4e2a\u9884\u5b9a\u4e49\u6309\u952e\u5e8f\u5217&#xff08;\u4f8b\u5982 Ctrl&#043;Alt&#043;Delete&#xff09;\u7684\u6309\u4e0b\u548c\u91ca\u653e\u72b6\u6001&#xff0c;\u6765\u5224\u65ad\u7528\u6237\u662f\u5426\u6b63\u5728\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u786c\u4ef6\u65e0\u5173\u7684\u7cfb\u7edf\u590d\u4f4d\u3002\u5b83\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4e25\u683c\u7684\u201c\u6240\u6709\u952e\u5fc5\u987b\u540c\u65f6\u6309\u4f4f\u201d\u7684\u903b\u8f91&#xff0c;\u5e76\u5305\u542b\u4e86\u9632\u6b62\u610f\u5916\u89e6\u53d1\u7684\u5b89\u5168\u673a\u5236\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u51fd\u6570\u662f\u4e00\u4e2a\u5178\u578b\u7684\u3001\u5728\u4e8b\u4ef6\u9a71\u52a8\u7684\u201c\u70ed\u8def\u5f84\u201d\u4e2d\u8fd0\u884c\u7684\u6709\u9650\u72b6\u6001\u673a&#xff08;FSM&#xff09;\u3002\u5b83\u7684\u72b6\u6001\u7531 state-&gt;reset_seq_cnt&#xff08;\u5f53\u524d\u6709\u591a\u5c11\u4e2a\u5e8f\u5217\u952e\u88ab\u6309\u4f4f&#xff09;\u548c state-&gt;reset_canceled&#xff08;\u5e8f\u5217\u68c0\u6d4b\u662f\u5426\u88ab\u4e34\u65f6\u7981\u7528&#xff09;\u5171\u540c\u5b9a\u4e49\u3002<\/p>\n<li>\n<p>\u201c\u95ef\u5165\u8005\u201d\u6309\u952e\u5bfc\u81f4\u5e8f\u5217\u5931\u6548:<\/p>\n<ul>\n<li>if (!test_bit(code, state-&gt;reset_keybit)): \u8fd9\u662f\u51fd\u6570\u7684\u7b2c\u4e00\u9053\u68c0\u67e5\u3002\u5b83\u5229\u7528 sysrq_parse_reset_sequence \u9884\u5148\u751f\u6210\u7684\u4f4d\u56fe&#xff0c;\u4ee5 O(1) \u7684\u6548\u7387\u5224\u65ad\u5f53\u524d\u6309\u4e0b\u7684\u952e\u662f\u5426\u5c5e\u4e8e\u9884\u5b9a\u4e49\u7684\u590d\u4f4d\u5e8f\u5217\u3002<\/li>\n<li>\u5982\u679c\u6309\u952e\u4e0d\u5c5e\u4e8e\u5e8f\u5217&#xff0c;\u5e76\u4e14\u5f53\u524d\u5df2\u7ecf\u6709\u90e8\u5206\u5e8f\u5217\u952e\u88ab\u6309\u4e0b&#xff08;state-&gt;reset_seq_cnt &gt; 0&#xff09;&#xff0c;\u90a3\u4e48\u8fd9\u4e2a\u201c\u95ef\u5165\u8005\u201d\u6309\u952e\u4f1a\u7acb\u5373\u89e6\u53d1\u4e24\u4e2a\u52a8\u4f5c&#xff1a; a. state-&gt;reset_canceled &#061; true;: \u5c06\u5e8f\u5217\u8bbe\u7f6e\u4e3a\u201c\u5df2\u53d6\u6d88\u201d\u72b6\u6001\u3002 b. timer_delete(&amp;state-&gt;keyreset_timer);: \u5982\u679c\u4e00\u4e2a\u6700\u7ec8\u7684\u590d\u4f4d\u5012\u8ba1\u65f6\u5df2\u7ecf\u542f\u52a8&#xff0c;\u7acb\u5373\u5c06\u5176\u53d6\u6d88\u3002<\/li>\n<li>\u539f\u7406: \u8fd9\u5b9e\u73b0\u4e86\u4e00\u4e2a\u975e\u5e38\u4e25\u683c\u7684\u5b89\u5168\u7b56\u7565&#xff1a;\u4e00\u65e6\u5f00\u59cb\u8f93\u5165\u590d\u4f4d\u5e8f\u5217&#xff0c;\u4efb\u4f55\u4e0d\u76f8\u5173\u7684\u6309\u952e\u90fd\u4f1a\u7acb\u5373\u4e2d\u6b62\u8be5\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u201c\u5e8f\u5217\u952e\u91ca\u653e\u201d\u5bfc\u81f4\u5012\u8ba1\u65f6\u4e2d\u6b62:<\/p>\n<ul>\n<li>else if (value &#061;&#061; 0): \u8fd9\u4e2a\u5206\u652f\u5904\u7406\u5c5e\u4e8e\u590d\u4f4d\u5e8f\u5217\u7684\u67d0\u4e2a\u952e\u88ab\u91ca\u653e\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>timer_delete(&amp;state-&gt;keyreset_timer);: \u91ca\u653e\u4efb\u4f55\u4e00\u4e2a\u5e8f\u5217\u952e\u90fd\u4f1a\u7acb\u5373\u53d6\u6d88\u590d\u4f4d\u5012\u8ba1\u65f6\u3002\u8fd9\u5f3a\u5236\u8981\u6c42\u7528\u6237\u5fc5\u987b\u540c\u65f6\u6309\u4f4f\u6240\u6709\u5e8f\u5217\u952e\u8fbe\u5230\u6307\u5b9a\u65f6\u957f\u624d\u80fd\u89e6\u53d1\u590d\u4f4d\u3002<\/li>\n<li>if (&#8211;state-&gt;reset_seq_cnt &#061;&#061; 0) state-&gt;reset_canceled &#061; false;: \u5b83\u9012\u51cf\u5f53\u524d\u6309\u4e0b\u7684\u5e8f\u5217\u952e\u8ba1\u6570\u3002\u5982\u679c\u8ba1\u6570\u5f52\u96f6&#xff08;\u610f\u5473\u7740\u6240\u6709\u5e8f\u5217\u952e\u90fd\u5df2\u88ab\u91ca\u653e&#xff09;&#xff0c;\u5b83\u4f1a\u6e05\u9664 reset_canceled \u6807\u5fd7&#xff0c;\u4ece\u800c\u91cd\u65b0\u4f7f\u80fd\u5bf9\u4e0b\u4e00\u6b21\u590d\u4f4d\u5e8f\u5217\u7684\u68c0\u6d4b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u201c\u5e8f\u5217\u952e\u96c6\u9f50\u201d\u89e6\u53d1\u6700\u7ec8\u52a8\u4f5c:<\/p>\n<ul>\n<li>else if (value &#061;&#061; 1): \u8fd9\u4e2a\u5206\u652f\u5904\u7406\u5c5e\u4e8e\u590d\u4f4d\u5e8f\u5217\u7684\u67d0\u4e2a\u952e\u88ab\u6309\u4e0b&#xff08;\u975e\u91cd\u590d&#xff09;\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>if (&#043;&#043;state-&gt;reset_seq_cnt &#061;&#061; state-&gt;reset_seq_len &amp;&amp; !state-&gt;reset_canceled): \u8fd9\u662f\u6210\u529f\u7684\u89e6\u53d1\u6761\u4ef6&#xff0c;\u5fc5\u987b\u540c\u65f6\u6ee1\u8db3&#xff1a; a. \u9012\u589e\u8ba1\u6570\u5668\u540e&#xff0c;\u5176\u503c\u6070\u597d\u7b49\u4e8e\u5e8f\u5217\u7684\u603b\u957f\u5ea6&#xff08;state-&gt;reset_seq_len&#xff09;\u3002 b. \u5e76\u4e14&#xff0c;\u5e8f\u5217\u5f53\u524d\u672a\u5904\u4e8e\u201c\u5df2\u53d6\u6d88\u201d\u72b6\u6001\u3002<\/li>\n<li>sysrq_handle_reset_request(state);: \u5982\u679c\u6761\u4ef6\u6ee1\u8db3&#xff0c;\u5219\u8c03\u7528\u4e00\u4e2a\u8f85\u52a9\u51fd\u6570&#xff08;\u672a\u5728\u6b64\u5904\u5c55\u793a&#xff09;\u3002\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u4f1a\u542f\u52a8\u4e00\u4e2a\u77ed\u6682\u7684\u5b9a\u65f6\u5668&#xff0c;\u5982\u679c\u5728\u8fd9\u4e2a\u5b9a\u65f6\u5668\u5230\u671f\u524d\u6ca1\u6709\u4efb\u4f55\u952e\u88ab\u91ca\u653e&#xff0c;\u5b9a\u65f6\u5668\u7684\u56de\u8c03\u51fd\u6570 sysrq_do_reset \u5c31\u4f1a\u88ab\u6267\u884c&#xff0c;\u4ece\u800c\u89e6\u53d1\u7cfb\u7edf\u91cd\u542f\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_detect_reset_sequence &#8211; \u68c0\u6d4b\u7528\u6237\u662f\u5426\u6b63\u5728\u8f93\u5165\u7cfb\u7edf\u590d\u4f4d\u6309\u952e\u5e8f\u5217\u3002<br \/>\n * &#064;param state: \u6307\u5411\u5f53\u524d\u8bbe\u5907\u7684 sysrq_state \u7ed3\u6784\u4f53\u3002<br \/>\n * &#064;param code:  \u6309\u952e\u7801\u3002<br \/>\n * &#064;param value: \u6309\u952e\u503c (1&#061;\u6309\u4e0b, 0&#061;\u91ca\u653e, 2&#061;\u91cd\u590d)\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_detect_reset_sequence<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>state<span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">int<\/span> code<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> value<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u68c0\u67e5\u5f53\u524d\u6309\u952e\u662f\u5426\u5c5e\u4e8e\u9884\u5b9a\u4e49\u7684\u590d\u4f4d\u5e8f\u5217\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token function\">test_bit<\/span><span class=\"token punctuation\">(<\/span>code<span class=\"token punctuation\">,<\/span> state<span class=\"token operator\">-&gt;<\/span>reset_keybit<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u6309\u4e0b\u4efb\u4f55\u4e0d\u5728\u590d\u4f4d\u5e8f\u5217\u4e2d\u7684\u952e\u90fd\u4f1a\u53d6\u6d88\u8be5\u5e8f\u5217\u3002<br \/>\n * \u540c\u65f6&#xff0c;\u4e5f\u53d6\u6d88\u5b9a\u65f6\u5668&#xff0c;\u4ee5\u9632\u5728\u8bf7\u6c42\u590d\u4f4d\u540e\u53c8\u6309\u4e0b\u4e86\u5176\u4ed6\u952e\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u662f\u4e00\u4e2a\u201c\u6309\u4e0b\u201d\u4e8b\u4ef6&#xff0c;\u5e76\u4e14\u6211\u4eec\u5df2\u5728\u5e8f\u5217\u4e2d&#xff08;\u8ba1\u6570\u5668&gt;0&#xff09;&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">&amp;&amp;<\/span> state<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u5c06\u5e8f\u5217\u6807\u8bb0\u4e3a\u5df2\u53d6\u6d88&#xff0c;\u5e76\u5220\u9664\u53ef\u80fd\u5df2\u542f\u52a8\u7684\u590d\u4f4d\u5b9a\u65f6\u5668\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_canceled <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">timer_delete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>state<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u6309\u952e\u91ca\u653e &#8211; \u590d\u4f4d\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u952e\u90fd\u9700\u8981\u88ab\u6301\u7eed\u6309\u4f4f&#xff0c;<br \/>\n * \u590d\u4f4d\u5012\u8ba1\u65f6\u624d\u4f1a\u751f\u6548\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u91ca\u653e\u4efb\u4f55\u4e00\u4e2a\u5e8f\u5217\u952e&#xff0c;\u90fd\u4f1a\u7acb\u5373\u53d6\u6d88\u590d\u4f4d\u5b9a\u65f6\u5668\u3002<\/span><br \/>\n<span class=\"token function\">timer_delete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>state<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5c06\u6309\u4e0b\u7684\u5e8f\u5217\u952e\u8ba1\u6570\u51cf\u4e00\u3002\u5982\u679c\u8ba1\u6570\u5f52\u96f6&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8212;<\/span>state<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u6e05\u9664\u201c\u5df2\u53d6\u6d88\u201d\u6807\u5fd7&#xff0c;\u4e3a\u4e0b\u4e00\u6b21\u5e8f\u5217\u68c0\u6d4b\u505a\u597d\u51c6\u5907\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_canceled <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>value <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/* \u6309\u952e\u6309\u4e0b&#xff0c;\u975e\u81ea\u52a8\u91cd\u590d *\/<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5c06\u6309\u4e0b\u7684\u5e8f\u5217\u952e\u8ba1\u6570\u52a0\u4e00\u3002\u5982\u679c\u8ba1\u6570\u7b49\u4e8e\u5e8f\u5217\u603b\u957f\u5ea6&#xff0c;\u5e76\u4e14\u5e8f\u5217\u672a\u88ab\u53d6\u6d88&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#043;&#043;<\/span>state<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt <span class=\"token operator\">&#061;&#061;<\/span> state<span class=\"token operator\">-&gt;<\/span>reset_seq_len <span class=\"token operator\">&amp;&amp;<\/span><br \/>\n    <span class=\"token operator\">!<\/span>state<span class=\"token operator\">-&gt;<\/span>reset_canceled<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u89e6\u53d1\u5904\u7406\u590d\u4f4d\u8bf7\u6c42\u7684\u4e0b\u4e00\u6b65&#xff08;\u901a\u5e38\u662f\u542f\u52a8\u6700\u7ec8\u7684\u5012\u8ba1\u65f6&#xff09;\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_handle_reset_request<\/span><span class=\"token punctuation\">(<\/span>state<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_handle_reset_request<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>state<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>state<span class=\"token operator\">-&gt;<\/span>reset_requested<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">__handle_sysrq<\/span><span class=\"token punctuation\">(<\/span>sysrq_xlate<span class=\"token punctuation\">[<\/span>KEY_B<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> false<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>sysrq_reset_downtime_ms<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">mod_timer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>state<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">,<\/span><br \/>\njiffies <span class=\"token operator\">&#043;<\/span> <span class=\"token function\">msecs_to_jiffies<\/span><span class=\"token punctuation\">(<\/span>sysrq_reset_downtime_ms<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n<span class=\"token function\">sysrq_do_reset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>state<span class=\"token operator\">-&gt;<\/span>keyreset_timer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq \u91cd\u7f6e\u5e8f\u5217\u89e3\u6790\u5668&#xff1a; sysrq_parse_reset_sequence<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u4e2d\u4e00\u4e2a\u5173\u952e\u7684\u9884\u5904\u7406\u548c\u72b6\u6001\u540c\u6b65\u51fd\u6570\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;\u5f53 SysRq \u7684\u5168\u5c40\u590d\u4f4d\u5e8f\u5217\u914d\u7f6e\u53d1\u751f\u53d8\u5316\u65f6&#xff0c;\u6b64\u51fd\u6570\u88ab\u8c03\u7528\u6765\u89e3\u6790\u8fd9\u4e2a\u5168\u5c40\u914d\u7f6e&#xff0c;\u5e76\u5c06\u5176\u7f13\u5b58\u5230\u4e00\u4e2a per-device \u7684 sysrq_state \u7ed3\u6784\u4f53\u4e2d\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d&#xff0c;\u5b83\u4f1a\u5c06\u5e8f\u5217\u4ece\u4e00\u4e2a keycode \u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u4e2a\u66f4\u9ad8\u6548\u7684\u4f4d\u56fe&#xff08;bitmap&#xff09;&#xff0c;\u5e76\u6267\u884c\u4e00\u9879\u91cd\u8981\u7684\u5b89\u5168\u68c0\u67e5&#xff0c;\u4ee5\u9632\u6b62\u5728\u7528\u6237\u5df2\u7ecf\u6309\u4e0b\u90e8\u5206\u590d\u4f4d\u952e\u7684\u60c5\u51b5\u4e0b\u610f\u5916\u89e6\u53d1\u7cfb\u7edf\u91cd\u542f\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u51fd\u6570\u662f SysRq \u590d\u4f4d\u529f\u80fd\u7684\u4e00\u4e2a\u6027\u80fd\u548c\u5b89\u5168\u4f18\u5316\u3002\u5b83\u907f\u514d\u4e86\u5728\u6bcf\u6b21\u6309\u952e\u65f6\u90fd\u53bb\u904d\u5386\u5168\u5c40\u914d\u7f6e\u6570\u7ec4&#xff0c;\u800c\u662f\u5c06\u914d\u7f6e\u201c\u7f16\u8bd1\u201d\u6210\u4e00\u79cd\u66f4\u9002\u5408\u5b9e\u65f6\u68c0\u6d4b\u7684\u683c\u5f0f\u3002<\/p>\n<li>\n<p>\u7248\u672c\u9a71\u52a8\u7684\u61d2\u60f0\u66f4\u65b0&#xff08;Lazy Update&#xff09;:<\/p>\n<ul>\n<li>\u6b64\u51fd\u6570\u5e76\u4e0d\u5728\u6bcf\u6b21\u6309\u952e\u65f6\u90fd\u88ab\u8c03\u7528\u3002\u5728 sysrq_handle_keypress \u4e2d&#xff0c;\u6709\u4e00\u4e2a if (state-&gt;reset_seq_version !&#061; sysrq_reset_seq_version) \u7684\u68c0\u67e5\u3002<\/li>\n<li>sysrq_reset_seq_version \u662f\u4e00\u4e2a\u5168\u5c40\u7248\u672c\u53f7&#xff0c;\u53ea\u6709\u5f53\u7cfb\u7edf\u7ba1\u7406\u5458\u901a\u8fc7 sysctl \u7b49\u65b9\u5f0f\u4fee\u6539\u4e86\u5168\u5c40 sysrq_reset_seq \u6570\u7ec4\u65f6&#xff0c;\u8fd9\u4e2a\u7248\u672c\u53f7\u624d\u4f1a\u589e\u52a0\u3002<\/li>\n<li>state-&gt;reset_seq_version \u5b58\u50a8\u4e86\u5f53\u524d sysrq_state \u4e0a\u4e00\u6b21\u540c\u6b65\u914d\u7f6e\u65f6\u7684\u7248\u672c\u53f7\u3002<\/li>\n<li>\u8fd9\u4e2a\u673a\u5236\u786e\u4fdd\u4e86 sysrq_parse_reset_sequence \u8fd9\u4e2a\u76f8\u5bf9\u8017\u65f6&#xff08;\u56e0\u4e3a\u5b83\u6709\u4e00\u4e2a\u5faa\u73af&#xff09;\u7684\u51fd\u6570&#xff0c;\u4ec5\u5728\u914d\u7f6e\u771f\u6b63\u53d1\u751f\u53d8\u5316\u65f6\u624d\u88ab\u6267\u884c\u4e00\u6b21&#xff0c;\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u201c\u61d2\u60f0\u66f4\u65b0\u201d\u6216\u201c\u7f13\u5b58\u5931\u6548\u201d\u7b56\u7565\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6570\u636e\u7ed3\u6784\u4f18\u5316&#xff08;\u6570\u7ec4\u5230 Bitmap&#xff09;:<\/p>\n<ul>\n<li>\u51fd\u6570\u7684\u6838\u5fc3\u662f\u4e00\u4e2a for \u5faa\u73af&#xff0c;\u5b83\u904d\u5386\u5168\u5c40\u7684 sysrq_reset_seq \u6570\u7ec4&#xff08;\u4e00\u4e2a keycode \u6570\u7ec4&#xff09;\u3002<\/li>\n<li>\u5bf9\u4e8e\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6709\u6548 key&#xff0c;\u5b83\u8c03\u7528 __set_bit(key, state-&gt;reset_keybit)\u3002<\/li>\n<li>state-&gt;reset_keybit \u662f\u4e00\u4e2a\u4f4d\u56fe\u3002\u8fd9\u4e2a\u8f6c\u6362\u7684\u610f\u4e49\u5728\u4e8e&#xff0c;\u540e\u7eed\u5728\u201c\u70ed\u8def\u5f84\u201d\u4e2d&#xff08;\u5373 sysrq_detect_reset_sequence \u51fd\u6570&#xff09;&#xff0c;\u68c0\u67e5\u4e00\u4e2a\u6309\u4e0b\u7684\u952e\u662f\u5426\u5c5e\u4e8e\u590d\u4f4d\u5e8f\u5217&#xff0c;\u5c31\u53ef\u4ee5\u4ece\u4e00\u4e2a O(N) \u7684\u6570\u7ec4\u641c\u7d22&#xff0c;\u53d8\u6210\u4e00\u4e2a O(1) \u7684 test_bit \u64cd\u4f5c&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u6027\u80fd\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5173\u952e\u7684\u5b89\u5168\u673a\u5236 (reset_canceled):<\/p>\n<ul>\n<li>\u5728\u6784\u5efa\u4f4d\u56fe\u7684\u540c\u65f6&#xff0c;\u51fd\u6570\u8fd8\u6267\u884c\u4e86 if (test_bit(key, state-&gt;key_down)) state-&gt;reset_seq_cnt&#043;&#043;;\u3002<\/li>\n<li>state-&gt;key_down \u662f\u4e00\u4e2a\u5b9e\u65f6\u8bb0\u5f55\u5f53\u524d\u8bbe\u5907\u4e0a\u6240\u6709\u88ab\u6309\u4e0b\u7684\u6309\u952e\u7684\u4f4d\u56fe\u3002<\/li>\n<li>\u8fd9\u4e2a\u68c0\u67e5\u7684\u542b\u4e49\u662f&#xff1a;\u201c\u5728\u540c\u6b65\u8fd9\u4e2a\u65b0\u7684\u590d\u4f4d\u5e8f\u5217\u914d\u7f6e\u7684\u65f6\u523b&#xff0c;\u5e8f\u5217\u4e2d\u7684\u67d0\u4e2a\u952e\u662f\u5426\u5df2\u7ecf\u88ab\u7528\u6237\u6309\u4e0b\u4e86&#xff1f;\u201d<\/li>\n<li>state-&gt;reset_canceled &#061; state-&gt;reset_seq_cnt !&#061; 0; \u8fd9\u4e00\u884c\u662f\u6700\u7ec8\u7684\u5b89\u5168\u7b56\u7565\u3002\u5982\u679c reset_seq_cnt \u4e0d\u4e3a\u96f6&#xff0c;\u610f\u5473\u7740\u5b58\u5728\u201c\u9884\u5148\u6309\u4e0b\u7684\u952e\u201d&#xff0c;\u6b64\u65f6 reset_canceled \u6807\u5fd7\u88ab\u8bbe\u7f6e\u3002<\/li>\n<li>sysrq_detect_reset_sequence \u51fd\u6570\u4f1a\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7&#xff0c;\u5982\u679c\u5b83\u88ab\u8bbe\u7f6e&#xff0c;\u5219\u5b8c\u5168\u7981\u7528\u590d\u4f4d\u5e8f\u5217\u7684\u68c0\u6d4b&#xff0c;\u76f4\u5230\u6240\u6709\u9884\u5148\u6309\u4e0b\u7684\u952e\u90fd\u88ab\u91ca\u653e\u3002\u8fd9\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62\u7528\u6237\u5728\u65e0\u610f\u4e2d&#xff08;\u4f8b\u5982&#xff0c;\u6b63\u5728\u6309 Ctrl&#043;Alt&#043;F1 \u5207\u6362\u7ec8\u7aef\u65f6&#xff09;\u610f\u5916\u5730\u89e6\u53d1\u4e00\u4e2a\u4ee5 Ctrl&#043;Alt \u5f00\u5934\u7684\u590d\u4f4d\u5e8f\u5217\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_parse_reset_sequence &#8211; \u89e3\u6790\u5168\u5c40\u590d\u4f4d\u5e8f\u5217\u5e76\u66f4\u65b0 per-device \u72b6\u6001\u3002<br \/>\n * &#064;param state: \u6307\u5411\u8981\u66f4\u65b0\u7684 sysrq_state \u7ed3\u6784\u4f53\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_parse_reset_sequence<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sysrq_state<\/span> <span class=\"token operator\">*<\/span>state<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">int<\/span> i<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">unsigned<\/span> <span class=\"token keyword\">short<\/span> key<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u91cd\u7f6e\u5f53\u524d\u5df2\u6309\u4e0b\u7684\u590d\u4f4d\u5e8f\u5217\u952e\u7684\u8ba1\u6570\u5668\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u904d\u5386\u5168\u5c40\u5b9a\u4e49\u7684\u590d\u4f4d\u5e8f\u5217\u6570\u7ec4\u3002<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span>i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> sysrq_reset_seq_len<span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\nkey <span class=\"token operator\">&#061;<\/span> sysrq_reset_seq<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5bf9 keycode \u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>key <span class=\"token operator\">&#061;&#061;<\/span> KEY_RESERVED <span class=\"token operator\">||<\/span> key <span class=\"token operator\">&gt;<\/span> KEY_MAX<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5c06\u8be5\u952e\u6dfb\u52a0\u5230 per-device \u7684\u590d\u4f4d\u952e\u4f4d\u56fe\u4e2d&#xff0c;\u7528\u4e8e\u5feb\u901f\u67e5\u627e\u3002<\/span><br \/>\n<span class=\"token function\">__set_bit<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> state<span class=\"token operator\">-&gt;<\/span>reset_keybit<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ \u66f4\u65b0 per-device \u7684\u5e8f\u5217\u957f\u5ea6\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_seq_len<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u68c0\u67e5\u8fd9\u4e2a\u5c5e\u4e8e\u590d\u4f4d\u5e8f\u5217\u7684\u952e&#xff0c;\u5f53\u524d\u662f\u5426\u5df2\u7ecf\u88ab\u6309\u4e0b\u4e86\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">test_bit<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> state<span class=\"token operator\">-&gt;<\/span>key_down<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u662f&#xff0c;\u5219\u589e\u52a0\u8ba1\u6570\u5668\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/*<br \/>\n * \u5728\u65e7\u7684\u6309\u952e\u88ab\u91ca\u653e\u4e4b\u524d&#xff0c;\u7981\u7528\u590d\u4f4d\u529f\u80fd\u3002<br \/>\n * \u8fd9\u662f\u4e00\u4e2a\u5b89\u5168\u63aa\u65bd&#xff0c;\u9632\u6b62\u610f\u5916\u89e6\u53d1\u3002<br \/>\n *\/<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_canceled <span class=\"token operator\">&#061;<\/span> state<span class=\"token operator\">-&gt;<\/span>reset_seq_cnt <span class=\"token operator\">!&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u66f4\u65b0 per-device \u7684\u7248\u672c\u53f7&#xff0c;\u8868\u793a\u5df2\u540c\u6b65\u5230\u6700\u65b0\u7684\u5168\u5c40\u914d\u7f6e\u3002<\/span><br \/>\nstate<span class=\"token operator\">-&gt;<\/span>reset_seq_version <span class=\"token operator\">&#061;<\/span> sysrq_reset_seq_version<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>SysRq Procfs \u89e6\u53d1\u63a5\u53e3&#xff1a;\/proc\/sysrq-trigger<\/h3>\n<p>\u672c\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u201c\u9b54\u672f SysRq\u201d\u529f\u80fd\u7684\u7a0b\u5e8f\u5316\u89e6\u53d1\u63a5\u53e3\u3002\u5176\u6838\u5fc3\u529f\u80fd\u662f&#xff1a;\u5728 \/proc \u6587\u4ef6\u7cfb\u7edf\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a sysrq-trigger \u7684\u53ea\u5199\u6587\u4ef6\u3002\u901a\u8fc7\u5411\u8fd9\u4e2a\u6587\u4ef6\u5199\u5165\u4e00\u4e2a\u547d\u4ee4\u5b57\u7b26&#xff0c;root \u7528\u6237\u53ef\u4ee5\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u89e6\u53d1\u4efb\u4f55 SysRq \u547d\u4ee4&#xff0c;\u5e76\u4e14\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u7ed5\u8fc7 \/proc\/sys\/kernel\/sysrq \u7684\u6743\u9650\u63a9\u7801\u68c0\u67e5\u3002\u5b83\u8fd8\u652f\u6301\u4e00\u4e2a\u7279\u6b8a\u7684\u201c\u6279\u91cf\u6a21\u5f0f\u201d&#xff0c;\u5141\u8bb8\u4e00\u6b21\u6027\u89e6\u53d1\u591a\u4e2a\u547d\u4ee4\u3002<\/p>\n<h4>\u5b9e\u73b0\u539f\u7406\u5206\u6790<\/h4>\n<p>\u6b64\u673a\u5236\u4e3a\u7cfb\u7edf\u7ba1\u7406\u5458\u548c\u81ea\u52a8\u5316\u811a\u672c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6bd4\u7269\u7406\u952e\u76d8\u66f4\u5f3a\u5927\u3001\u66f4\u7075\u6d3b\u7684 SysRq \u89e6\u53d1\u65b9\u5f0f\u3002\u5176\u5b9e\u73b0\u539f\u7406\u57fa\u4e8e procfs \u7684\u81ea\u5b9a\u4e49\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<li>\n<p>Procfs \u6587\u4ef6\u521b\u5efa (sysrq_init_procfs):<\/p>\n<ul>\n<li>sysrq_init_procfs \u51fd\u6570\u901a\u8fc7 proc_create \u5728 \/proc \u6839\u76ee\u5f55\u4e0b\u521b\u5efa sysrq-trigger \u6587\u4ef6\u3002<\/li>\n<li>\u6743\u9650: \u6743\u9650\u88ab\u8bbe\u7f6e\u4e3a S_IWUSR (0200)&#xff0c;\u610f\u5473\u7740\u53ea\u6709\u6587\u4ef6\u7684\u6240\u6709\u8005&#xff08;\u5373 root \u7528\u6237&#xff09;\u6709\u6743\u5199\u5165\u3002\u8fd9\u4e2a\u6587\u4ef6\u662f\u4e0d\u53ef\u8bfb\u7684\u3002<\/li>\n<li>\u7ed1\u5b9a\u64cd\u4f5c: proc_create \u5c06\u8fd9\u4e2a\u6587\u4ef6\u4e0e sysrq_trigger_proc_ops \u7ed3\u6784\u5173\u8054\u8d77\u6765&#xff0c;\u8be5\u7ed3\u6784\u6307\u5b9a\u4e86 write_sysrq_trigger \u4f5c\u4e3a\u5176\u5199\u64cd\u4f5c\u7684\u5904\u7406\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49\u5199\u64cd\u4f5c (write_sysrq_trigger):<\/p>\n<ul>\n<li>\u5f53 root \u7528\u6237\u6267\u884c echo &#039;b&#039; &gt; \/proc\/sysrq-trigger \u65f6&#xff0c;write_sysrq_trigger \u51fd\u6570\u88ab\u8c03\u7528\u3002<\/li>\n<li>\u5b89\u5168\u7684\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d: \u5b83\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u548c get_user(c, buf &#043; i) \u6765\u9010\u4e2a\u5b57\u7b26\u5730\u3001\u5b89\u5168\u5730\u4ece\u7528\u6237\u7a7a\u95f4\u7f13\u51b2\u533a\u62f7\u8d1d\u6570\u636e\u3002\u8fd9\u662f\u9632\u6b62\u5185\u6838\u88ab\u6076\u610f\u7528\u6237\u7a7a\u95f4\u6307\u9488\u6b3a\u9a97\u7684\u5173\u952e\u5b89\u5168\u63aa\u65bd\u3002<\/li>\n<li>\u9ed8\u8ba4\u884c\u4e3a&#xff08;\u5355\u547d\u4ee4&#xff09;: \u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;bulk \u6807\u5fd7\u4e3a false\u3002\u51fd\u6570\u5728\u5904\u7406\u5b8c\u7b2c\u4e00\u4e2a\u975e\u4e0b\u5212\u7ebf\u7684\u5b57\u7b26\u540e&#xff0c;\u5c31\u4f1a break \u9000\u51fa\u5faa\u73af\u3002\u8fd9\u610f\u5473\u7740 echo &#039;bcs&#039; \u548c echo &#039;b&#039; \u7684\u6548\u679c\u662f\u5b8c\u5168\u76f8\u540c\u7684&#xff0c;\u90fd\u53ea\u89e6\u53d1\u4e86 \u2018b\u2019 \u547d\u4ee4\u3002<\/li>\n<li>\u6279\u91cf\u6a21\u5f0f&#xff08;Bulk Mode&#xff09;: a. \u5982\u679c\u5199\u5165\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u4e0b\u5212\u7ebf _&#xff0c;bulk \u6807\u5fd7\u88ab\u8bbe\u4e3a true\u3002 b. \u4e0b\u5212\u7ebf\u672c\u8eab\u4e0d\u89e6\u53d1\u4efb\u4f55\u547d\u4ee4\u3002 c. \u5728\u6b64\u4e4b\u540e&#xff0c;if (!bulk) break; \u6761\u4ef6\u5c06\u4e0d\u518d\u6ee1\u8db3&#xff0c;\u5faa\u73af\u4f1a\u7ee7\u7eed\u5904\u7406\u540e\u7eed\u7684\u6240\u6709\u5b57\u7b26&#xff0c;\u5e76\u4f9d\u6b21\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u8c03\u7528 __handle_sysrq\u3002\u4f8b\u5982&#xff0c;echo &#039;_sub&#039; &gt; \/proc\/sysrq-trigger \u4f1a\u4f9d\u6b21\u89e6\u53d1 Sync, Unmount, \u548c Reboot\u3002<\/li>\n<li>\u7279\u6743\u8c03\u7528: \u6700\u5173\u952e\u7684\u4e00\u6b65\u662f __handle_sysrq(c, false)\u3002\u5b83\u8c03\u7528\u4e86 SysRq \u7684\u4e2d\u592e\u6267\u884c\u5f15\u64ce&#xff0c;\u5e76\u5c06 check_mask \u53c2\u6570\u8bbe\u4e3a false\u3002\u5982\u524d\u6587\u5206\u6790&#xff0c;\u8fd9\u4f1a\u5b8c\u5168\u7ed5\u8fc7 \/proc\/sys\/kernel\/sysrq \u4e2d\u8bbe\u7f6e\u7684\u6743\u9650\u63a9\u7801\u3002\u8fd9\u610f\u5473\u7740&#xff0c;\u5373\u4f7f\u7528\u6237\u901a\u8fc7 sysctl \u5b8c\u5168\u7981\u7528\u4e86\u7269\u7406\u952e\u76d8\u7684 SysRq \u529f\u80fd&#xff08;sysrq&#061;0&#xff09;&#xff0c;root \u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7 sysrq-trigger \u63a5\u53e3\u6267\u884c\u4efb\u4f55\u547d\u4ee4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316\u6d41\u7a0b (sysrq_init):<\/p>\n<ul>\n<li>\u6b64\u51fd\u6570\u901a\u8fc7 device_initcall \u6ce8\u518c&#xff0c;\u5728\u8bbe\u5907\u9a71\u52a8\u521d\u59cb\u5316\u9636\u6bb5\u88ab\u8c03\u7528\u3002<\/li>\n<li>\u5b83\u9996\u5148\u8c03\u7528 sysrq_init_procfs() \u6765\u521b\u5efa \/proc\/sysrq-trigger \u6587\u4ef6\u3002<\/li>\n<li>\u7136\u540e&#xff0c;\u5b83\u68c0\u67e5 sysrq_on()\u3002\u8fd9\u4e2a\u68c0\u67e5\u7684\u76ee\u7684\u662f&#xff0c;\u5982\u679c\u5185\u6838\u901a\u8fc7\u542f\u52a8\u53c2\u6570&#xff08;\u5982 sysrq_always_on&#061;1&#xff09;\u88ab\u914d\u7f6e\u4e3a\u9ed8\u8ba4\u5f00\u542f SysRq&#xff0c;\u90a3\u4e48\u5b83\u4f1a\u7acb\u5373\u8c03\u7528 sysrq_register_handler() \u6765\u6ce8\u518c\u7269\u7406\u952e\u76d8\u7684\u8f93\u5165\u53e5\u67c4\u3002\u8fd9\u786e\u4fdd\u4e86\u4e24\u79cd\u89e6\u53d1\u65b9\u5f0f\u90fd\u80fd\u5728\u7cfb\u7edf\u542f\u52a8\u540e\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>\u4ee3\u7801\u5206\u6790<\/h4>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">ifdef<\/span> <span class=\"token expression\">CONFIG_PROC_FS<\/span><\/span><br \/>\n<span class=\"token comment\">\/*<br \/>\n * \u5411 \/proc\/sysrq-trigger \u5199\u5165 &#039;C&#039; \u5c31\u50cf\u6309\u4e0b sysrq-C\u3002<br \/>\n * \u901a\u5e38&#xff0c;\u53ea\u6709\u5199\u5165\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4f1a\u88ab\u5904\u7406\u3002<br \/>\n * \u4f46\u662f&#xff0c;\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u4e0b\u5212\u7ebf&#xff0c;\u5219\u6240\u6709\u5b57\u7b26\u90fd\u4f1a\u88ab\u5904\u7406\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token comment\">\/**<br \/>\n * &#064;brief write_sysrq_trigger &#8211; \/proc\/sysrq-trigger \u7684\u5199\u64cd\u4f5c\u5904\u7406\u51fd\u6570\u3002<br \/>\n * &#064;param file:  \u6587\u4ef6\u5bf9\u8c61\u6307\u9488\u3002<br \/>\n * &#064;param buf:   \u6307\u5411\u7528\u6237\u7a7a\u95f4\u6570\u636e\u7f13\u51b2\u533a\u7684\u6307\u9488\u3002<br \/>\n * &#064;param count: \u8981\u5199\u5165\u7684\u5b57\u8282\u6570\u3002<br \/>\n * &#064;param ppos:  \u6587\u4ef6\u504f\u79fb\u91cf\u6307\u9488 (\u672a\u4f7f\u7528)\u3002<br \/>\n * &#064;return ssize_t: \u6210\u529f\u5219\u8fd4\u56de\u6d88\u8017\u7684\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7801\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token class-name\">ssize_t<\/span> <span class=\"token function\">write_sysrq_trigger<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file<\/span> <span class=\"token operator\">*<\/span>file<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> __user <span class=\"token operator\">*<\/span>buf<span class=\"token punctuation\">,<\/span><br \/>\n   <span class=\"token class-name\">size_t<\/span> count<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">loff_t<\/span> <span class=\"token operator\">*<\/span>ppos<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\nbool bulk <span class=\"token operator\">&#061;<\/span> false<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6279\u91cf\u6a21\u5f0f\u6807\u5fd7\u3002<\/span><br \/>\n<span class=\"token class-name\">size_t<\/span> i<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u904d\u5386\u7528\u6237\u5199\u5165\u7684\u6240\u6709\u5b57\u7b26\u3002<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span>i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> count<span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5b89\u5168\u5730\u4ece\u7528\u6237\u7a7a\u95f4\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">get_user<\/span><span class=\"token punctuation\">(<\/span>c<span class=\"token punctuation\">,<\/span> buf <span class=\"token operator\">&#043;<\/span> i<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span>EFAULT<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>c <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token char\">&#039;_&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5982\u679c\u5b57\u7b26\u662f\u4e0b\u5212\u7ebf&#xff0c;\u5219\u542f\u7528\u6279\u91cf\u6a21\u5f0f\u3002<\/span><br \/>\nbulk <span class=\"token operator\">&#061;<\/span> true<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5426\u5219&#xff0c;\u8c03\u7528 SysRq \u7684\u4e2d\u592e\u6267\u884c\u5f15\u64ce\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ check_mask \u8bbe\u4e3a false&#xff0c;\u4ee5\u7ed5\u8fc7 sysctl \u7684\u6743\u9650\u63a9\u7801\u68c0\u67e5\u3002<\/span><br \/>\n<span class=\"token function\">__handle_sysrq<\/span><span class=\"token punctuation\">(<\/span>c<span class=\"token punctuation\">,<\/span> false<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5982\u679c\u4e0d\u662f\u6279\u91cf\u6a21\u5f0f&#xff0c;\u5219\u5904\u7406\u5b8c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u540e\u7acb\u5373\u9000\u51fa\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>bulk<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u544a\u77e5\u4e0a\u5c42&#xff0c;\u6240\u6709\u5199\u5165\u7684\u5b57\u8282\u90fd\u5df2\u88ab\u201c\u6d88\u8017\u201d\u3002<\/span><br \/>\n<span class=\"token keyword\">return<\/span> count<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5b9a\u4e49 \/proc\/sysrq-trigger \u7684 proc \u6587\u4ef6\u64cd\u4f5c\u96c6\u3002<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">proc_ops<\/span> sysrq_trigger_proc_ops <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>proc_write<span class=\"token operator\">&#061;<\/span> write_sysrq_trigger<span class=\"token punctuation\">,<\/span> <span class=\"token comment\">\/\/ \u53ea\u5b9a\u4e49\u4e86\u5199\u64cd\u4f5c\u3002<\/span><br \/>\n<span class=\"token punctuation\">.<\/span>proc_lseek<span class=\"token operator\">&#061;<\/span> noop_llseek<span class=\"token punctuation\">,<\/span>         <span class=\"token comment\">\/\/ lseek \u662f\u4e00\u4e2a\u7a7a\u64cd\u4f5c\u3002<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_init_procfs &#8211; \u521d\u59cb\u5316 \/proc\/sysrq-trigger \u6587\u4ef6\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_init_procfs<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u521b\u5efa \/proc\/sysrq-trigger \u6587\u4ef6&#xff0c;\u6743\u9650\u4e3a 0200 (\u4ec5\u5c5e\u4e3b\u53ef\u5199)\u3002<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token function\">proc_create<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sysrq-trigger&#034;<\/span><span class=\"token punctuation\">,<\/span> S_IWUSR<span class=\"token punctuation\">,<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span><br \/>\n <span class=\"token operator\">&amp;<\/span>sysrq_trigger_proc_ops<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token function\">pr_err<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Failed to register proc interface\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">else<\/span> <span class=\"token comment\">\/\/ \u5982\u679c CONFIG_PROC_FS \u672a\u5b9a\u4e49<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u63d0\u4f9b\u4e00\u4e2a\u7a7a\u7684\u5185\u8054\u51fd\u6570&#xff0c;\u4ee5\u907f\u514d\u7f16\u8bd1\u9519\u8bef\u3002<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">inline<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">sysrq_init_procfs<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">endif<\/span> <span class=\"token comment\">\/* CONFIG_PROC_FS *\/<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * &#064;brief sysrq_init &#8211; SysRq \u5b50\u7cfb\u7edf\u7684\u4e3b\u8981\u521d\u59cb\u5316\u51fd\u6570\u4e4b\u4e00\u3002<br \/>\n * &#064;return int: \u59cb\u7ec8\u8fd4\u56de0\u3002<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">static<\/span> <span class=\"token keyword\">int<\/span> __init <span class=\"token function\">sysrq_init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u521d\u59cb\u5316 procfs \u63a5\u53e3\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_init_procfs<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5982\u679c SysRq \u529f\u80fd\u5728\u542f\u52a8\u65f6\u9ed8\u8ba4\u662f\u5f00\u542f\u7684&#8230;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">sysrq_on<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">\/\/ &#8230;\u5219\u7acb\u5373\u6ce8\u518c\u7269\u7406\u952e\u76d8\u7684\u8f93\u5165\u5904\u7406\u53e5\u67c4\u3002<\/span><br \/>\n<span class=\"token function\">sysrq_register_handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4f7f\u7528 device_initcall \u5b8f&#xff0c;\u786e\u4fdd\u8be5\u521d\u59cb\u5316\u51fd\u6570\u5728\u8bbe\u5907\u9a71\u52a8\u521d\u59cb\u5316\u9636\u6bb5\u88ab\u8c03\u7528\u3002<\/span><br \/>\n<span class=\"token function\">device_initcall<\/span><span class=\"token punctuation\">(<\/span>sysrq_init<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>title: sysrq categories:<br \/>\nlinuxdriverstty tags:linuxdriverstty abbrlink: 2db0ec3 date: 2025-10-15 13:52:27 https:\/\/github.com\/wdfk-prog\/linux-study \u6587\u7ae0\u76ee\u5f55 drivers\/tty\/sysrq.c \u5185\u6838\u9b54\u6cd5\u952e(Magic SysRq Key) \u7cfb\u7edf\u7684\u7ec8\u6781\u5e94\u6025\u4e0e\u8c03\u8bd5\u5de5\u5177\u5386\u53f2\u4e0e\u80cc\u666f\u8fd9\u9879\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u4ec0\u4e48\u7279\u5b9a\u95ee\u9898\u800c\u8bde\u751f\u7684&#xff1f;<\/p>\n","protected":false},"author":2,"featured_media":73357,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,371,305],"topic":[],"class_list":["post-73358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-371","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/73358.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"title: sysrq categories: linuxdriverstty tags:linuxdriverstty abbrlink: 2db0ec3 date: 2025-10-15 13:52:27 https:\/\/github.com\/wdfk-prog\/linux-study \u6587\u7ae0\u76ee\u5f55 drivers\/tty\/sysrq.c \u5185\u6838\u9b54\u6cd5\u952e(Magic SysRq Key) \u7cfb\u7edf\u7684\u7ec8\u6781\u5e94\u6025\u4e0e\u8c03\u8bd5\u5de5\u5177\u5386\u53f2\u4e0e\u80cc\u666f\u8fd9\u9879\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u4ec0\u4e48\u7279\u5b9a\u95ee\u9898\u800c\u8bde\u751f\u7684&#xff1f;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/73358.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-07T07:35:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207073525-6986eb3d0c39c.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"24 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73358.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/73358.html\",\"name\":\"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-07T07:35:26+00:00\",\"dateModified\":\"2026-02-07T07:35:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/73358.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/73358.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73358.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 -- [drivers][tty]sysrq\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/73358.html","og_locale":"zh_CN","og_type":"article","og_title":"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"title: sysrq categories: linuxdriverstty tags:linuxdriverstty abbrlink: 2db0ec3 date: 2025-10-15 13:52:27 https:\/\/github.com\/wdfk-prog\/linux-study \u6587\u7ae0\u76ee\u5f55 drivers\/tty\/sysrq.c \u5185\u6838\u9b54\u6cd5\u952e(Magic SysRq Key) \u7cfb\u7edf\u7684\u7ec8\u6781\u5e94\u6025\u4e0e\u8c03\u8bd5\u5de5\u5177\u5386\u53f2\u4e0e\u80cc\u666f\u8fd9\u9879\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u4ec0\u4e48\u7279\u5b9a\u95ee\u9898\u800c\u8bde\u751f\u7684&#xff1f;","og_url":"https:\/\/www.wsisp.com\/helps\/73358.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-07T07:35:26+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207073525-6986eb3d0c39c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"24 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/73358.html","url":"https:\/\/www.wsisp.com\/helps\/73358.html","name":"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 - [drivers][tty]sysrq - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-07T07:35:26+00:00","dateModified":"2026-02-07T07:35:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/73358.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/73358.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/73358.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"[Linux]\u5b66\u4e60\u7b14\u8bb0\u7cfb\u5217 -- [drivers][tty]sysrq"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=73358"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73358\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/73357"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=73358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=73358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=73358"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=73358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}