OSDN Git Service

Merge pull request #3616 from Hourier/Change-Artifacts-Spoiler-Signature
[hengbandforosx/hengbandosx.git] / src / io / signal-handlers.cpp
1 /*!
2  * @file signal-handlers.c
3  * @brief シグナルハンドラの管理 / Controlling signal handlers
4  * @date 2020/02/23
5  * @author Hourier
6  */
7
8 #include "io/signal-handlers.h"
9 #include "cmd-io/cmd-dump.h"
10 #include "core/game-closer.h"
11 #include "floor/floor-events.h"
12 #include "game-option/cheat-options.h"
13 #include "io/write-diary.h"
14 #include "monster-floor/monster-lite.h"
15 #include "save/save.h"
16 #include "system/player-type-definition.h"
17 #include "system/system-variables.h"
18 #include "term/term-color-types.h"
19 #include "world/world.h"
20
21 int16_t signal_count; /* Hack -- Count interupts */
22
23 #include <signal.h>
24
25 /*!
26  * @brief OSからのシグナルを受けてサスペンド状態に入る /
27  * Handle signals -- suspend
28  * @param sig 受け取ったシグナル
29  * @details
30  * Actually suspend the game, and then resume cleanly
31  */
32 static void handle_signal_suspend(int sig)
33 {
34     (void)signal(sig, SIG_IGN);
35 #ifdef SIGSTOP
36     term_fresh();
37     term_xtra(TERM_XTRA_ALIVE, 0);
38     (void)kill(0, SIGSTOP);
39     term_xtra(TERM_XTRA_ALIVE, 1);
40     term_redraw();
41     term_fresh();
42 #endif
43     (void)signal(sig, handle_signal_suspend);
44 }
45
46 /*!
47  * @brief OSからのシグナルを受けて中断、終了する /
48  * Handle signals -- simple (interrupt and quit)
49  * @param sig 受け取ったシグナル
50  * @details
51  * <pre>
52  * This function was causing a *huge* number of problems, so it has
53  * been simplified greatly.  We keep a global variable which counts
54  * the number of times the user attempts to kill the process, and
55  * we commit suicide if the user does this a certain number of times.
56  * We attempt to give "feedback" to the user as he approaches the
57  * suicide thresh-hold, but without penalizing accidental keypresses.
58  * To prevent messy accidents, we should reset this global variable
59  * whenever the user enters a keypress, or something like that.
60  * </pre>
61  * @todo ここにPlayerTypeを追加すると関数ポインタ周りの収拾がつかなくなるので保留
62  */
63 static void handle_signal_simple(int sig)
64 {
65     (void)signal(sig, SIG_IGN);
66     if (!w_ptr->character_generated || w_ptr->character_saved) {
67         quit(nullptr);
68     }
69
70     signal_count++;
71     if (p_ptr->is_dead) {
72         p_ptr->died_from = _("強制終了", "Abortion");
73         forget_lite(p_ptr->current_floor_ptr);
74         forget_view(p_ptr->current_floor_ptr);
75         clear_mon_lite(p_ptr->current_floor_ptr);
76         close_game(p_ptr);
77         quit(_("強制終了", "interrupt"));
78     } else if (signal_count >= 5) {
79         p_ptr->died_from = _("強制終了中", "Interrupting");
80         forget_lite(p_ptr->current_floor_ptr);
81         forget_view(p_ptr->current_floor_ptr);
82         clear_mon_lite(p_ptr->current_floor_ptr);
83         p_ptr->playing = false;
84         if (!cheat_immortal) {
85             p_ptr->is_dead = true;
86         }
87         p_ptr->leaving = true;
88         close_game(p_ptr);
89         quit(_("強制終了", "interrupt"));
90     } else if (signal_count >= 4) {
91         term_xtra(TERM_XTRA_NOISE, 0);
92         term_erase(0, 0);
93         term_putstr(0, 0, -1, TERM_WHITE, _("熟慮の上の自殺!", "Contemplating suicide!"));
94         term_fresh();
95     } else if (signal_count >= 2) {
96         term_xtra(TERM_XTRA_NOISE, 0);
97     }
98
99     (void)signal(sig, handle_signal_simple);
100 }
101
102 /*!
103  * @brief OSからのシグナルを受けて強制終了する /
104  * Handle signal -- abort, kill, etc
105  * @param sig 受け取ったシグナル
106  * @details
107  * <pre>
108  * This function was causing a *huge* number of problems, so it has
109  * been simplified greatly.  We keep a global variable which counts
110  * the number of times the user attempts to kill the process, and
111  * we commit suicide if the user does this a certain number of times.
112  * We attempt to give "feedback" to the user as he approaches the
113  * suicide thresh-hold, but without penalizing accidental keypresses.
114  * To prevent messy accidents, we should reset this global variable
115  * whenever the user enters a keypress, or something like that.
116  * </pre>
117  * @todo ここにp_ptrを追加すると関数ポインタ周りの収拾がつかなくなるので保留
118  */
119 static void handle_signal_abort(int sig)
120 {
121     const auto &[wid, hgt] = term_get_size();
122     (void)signal(sig, SIG_IGN);
123     if (!w_ptr->character_generated || w_ptr->character_saved) {
124         quit(nullptr);
125     }
126
127     forget_lite(p_ptr->current_floor_ptr);
128     forget_view(p_ptr->current_floor_ptr);
129     clear_mon_lite(p_ptr->current_floor_ptr);
130
131     term_erase(0, hgt - 1);
132     term_putstr(0, hgt - 1, -1, TERM_RED, _("恐ろしいソフトのバグが飛びかかってきた!", "A gruesome software bug LEAPS out at you!"));
133
134     term_putstr(45, hgt - 1, -1, TERM_RED, _("緊急セーブ...", "Panic save..."));
135
136     exe_write_diary(p_ptr, DiaryKind::GAMESTART, 0, _("----ゲーム異常終了----", "-- Tried Panic Save and Aborted Game --"));
137     term_fresh();
138
139     p_ptr->panic_save = 1;
140     p_ptr->died_from = _("(緊急セーブ)", "(panic save)");
141
142     signals_ignore_tstp();
143
144     if (save_player(p_ptr, SaveType::CLOSE_GAME)) {
145         term_putstr(45, hgt - 1, -1, TERM_RED, _("緊急セーブ成功!", "Panic save succeeded!"));
146     } else {
147         term_putstr(45, hgt - 1, -1, TERM_RED, _("緊急セーブ失敗!", "Panic save failed!"));
148     }
149
150     term_fresh();
151     quit(_("ソフトのバグ", "software bug"));
152 }
153
154 /*!
155  * @brief OSからのSIGTSTPシグナルを無視する関数 /
156  * Ignore SIGTSTP signals (keyboard suspend)
157  * @details
158  */
159 void signals_ignore_tstp(void)
160 {
161 #ifdef SIGTSTP
162     (void)signal(SIGTSTP, SIG_IGN);
163 #endif
164 }
165
166 /*!
167  * @brief OSからのSIGTSTPシグナルハンドラ /
168  * Handle SIGTSTP signals (keyboard suspend)
169  * @details
170  */
171 void signals_handle_tstp(void)
172 {
173 #ifdef SIGTSTP
174     (void)signal(SIGTSTP, handle_signal_suspend);
175 #endif
176 }
177
178 /*!
179  * @brief OSからのシグナルハンドルを初期化する /
180  * Prepare to handle the relevant signals
181  * @details
182  */
183 void signals_init(void)
184 {
185 #ifdef SIGHUP
186     (void)signal(SIGHUP, SIG_IGN);
187 #endif
188
189 #ifdef SIGTSTP
190     (void)signal(SIGTSTP, handle_signal_suspend);
191 #endif
192
193 #ifdef SIGINT
194     (void)signal(SIGINT, handle_signal_simple);
195 #endif
196
197 #ifdef SIGQUIT
198     (void)signal(SIGQUIT, handle_signal_simple);
199 #endif
200
201 #ifdef SIGFPE
202     (void)signal(SIGFPE, handle_signal_abort);
203 #endif
204
205 #ifdef SIGILL
206     (void)signal(SIGILL, handle_signal_abort);
207 #endif
208
209 #ifdef SIGTRAP
210     (void)signal(SIGTRAP, handle_signal_abort);
211 #endif
212
213 #ifdef SIGIOT
214     (void)signal(SIGIOT, handle_signal_abort);
215 #endif
216
217 #ifdef SIGBUS
218     (void)signal(SIGBUS, handle_signal_abort);
219 #endif
220
221 #ifdef SIGSEGV
222     (void)signal(SIGSEGV, handle_signal_abort);
223 #endif
224
225 #ifdef SIGTERM
226     (void)signal(SIGTERM, handle_signal_abort);
227 #endif
228
229 #ifdef SIGPIPE
230     (void)signal(SIGPIPE, handle_signal_abort);
231 #endif
232
233 #ifdef SIGEMT
234     (void)signal(SIGEMT, handle_signal_abort);
235 #endif
236
237 #ifdef SIGDANGER
238     (void)signal(SIGDANGER, handle_signal_abort);
239 #endif
240
241 #ifdef SIGSYS
242     (void)signal(SIGSYS, handle_signal_abort);
243 #endif
244
245 #ifdef SIGXCPU
246     (void)signal(SIGXCPU, handle_signal_abort);
247 #endif
248
249 #ifdef SIGPWR
250     (void)signal(SIGPWR, handle_signal_abort);
251 #endif
252 }