| OLD | NEW |
| 1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // | 2 // |
| 3 /// \file signals.c | 3 /// \file signals.c |
| 4 /// \brief Handling signals to abort operation | 4 /// \brief Handling signals to abort operation |
| 5 // | 5 // |
| 6 // Author: Lasse Collin | 6 // Author: Lasse Collin |
| 7 // | 7 // |
| 8 // This file has been put into the public domain. | 8 // This file has been put into the public domain. |
| 9 // You can do whatever you want with this file. | 9 // You can do whatever you want with this file. |
| 10 // | 10 // |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 #ifdef SIGXFSZ | 64 #ifdef SIGXFSZ |
| 65 SIGXFSZ, | 65 SIGXFSZ, |
| 66 #endif | 66 #endif |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 // Mask of the signals for which we have established a signal handler. | 69 // Mask of the signals for which we have established a signal handler. |
| 70 sigemptyset(&hooked_signals); | 70 sigemptyset(&hooked_signals); |
| 71 for (size_t i = 0; i < ARRAY_SIZE(sigs); ++i) | 71 for (size_t i = 0; i < ARRAY_SIZE(sigs); ++i) |
| 72 sigaddset(&hooked_signals, sigs[i]); | 72 sigaddset(&hooked_signals, sigs[i]); |
| 73 | 73 |
| 74 #ifdef SIGALRM |
| 75 // Add also the signals from message.c to hooked_signals. |
| 76 for (size_t i = 0; message_progress_sigs[i] != 0; ++i) |
| 77 sigaddset(&hooked_signals, message_progress_sigs[i]); |
| 78 #endif |
| 79 |
| 74 struct sigaction sa; | 80 struct sigaction sa; |
| 75 | 81 |
| 76 // All the signals that we handle we also blocked while the signal | 82 // All the signals that we handle we also blocked while the signal |
| 77 // handler runs. | 83 // handler runs. |
| 78 sa.sa_mask = hooked_signals; | 84 sa.sa_mask = hooked_signals; |
| 79 | 85 |
| 80 // Don't set SA_RESTART, because we want EINTR so that we can check | 86 // Don't set SA_RESTART, because we want EINTR so that we can check |
| 81 // for user_abort and cleanup before exiting. We block the signals | 87 // for user_abort and cleanup before exiting. We block the signals |
| 82 // for which we have established a handler when we don't want EINTR. | 88 // for which we have established a handler when we don't want EINTR. |
| 83 sa.sa_flags = 0; | 89 sa.sa_flags = 0; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 141 } |
| 136 #endif | 142 #endif |
| 137 | 143 |
| 138 | 144 |
| 139 extern void | 145 extern void |
| 140 signals_exit(void) | 146 signals_exit(void) |
| 141 { | 147 { |
| 142 const int sig = exit_signal; | 148 const int sig = exit_signal; |
| 143 | 149 |
| 144 if (sig != 0) { | 150 if (sig != 0) { |
| 151 #if defined(TUKLIB_DOSLIKE) || defined(__VMS) |
| 152 // Don't raise(), set only exit status. This avoids |
| 153 // printing unwanted message about SIGINT when the user |
| 154 // presses C-c. |
| 155 set_exit_status(E_ERROR); |
| 156 #else |
| 145 struct sigaction sa; | 157 struct sigaction sa; |
| 146 sa.sa_handler = SIG_DFL; | 158 sa.sa_handler = SIG_DFL; |
| 147 sigfillset(&sa.sa_mask); | 159 sigfillset(&sa.sa_mask); |
| 148 sa.sa_flags = 0; | 160 sa.sa_flags = 0; |
| 149 sigaction(sig, &sa, NULL); | 161 sigaction(sig, &sa, NULL); |
| 150 raise(exit_signal); | 162 raise(exit_signal); |
| 163 #endif |
| 151 } | 164 } |
| 152 | 165 |
| 153 return; | 166 return; |
| 154 } | 167 } |
| 155 | 168 |
| 156 #else | 169 #else |
| 157 | 170 |
| 158 // While Windows has some very basic signal handling functions as required | 171 // While Windows has some very basic signal handling functions as required |
| 159 // by C89, they are not really used, and e.g. SIGINT doesn't work exactly | 172 // by C89, they are not really used, and e.g. SIGINT doesn't work exactly |
| 160 // the way it does on POSIX (Windows creates a new thread for the signal | 173 // the way it does on POSIX (Windows creates a new thread for the signal |
| 161 // handler). Instead, we use SetConsoleCtrlHandler() to catch user | 174 // handler). Instead, we use SetConsoleCtrlHandler() to catch user |
| 162 // pressing C-c, because that seems to be the recommended way to do it. | 175 // pressing C-c, because that seems to be the recommended way to do it. |
| 163 // | 176 // |
| 164 // NOTE: This doesn't work under MSYS. Trying with SIGINT doesn't work | 177 // NOTE: This doesn't work under MSYS. Trying with SIGINT doesn't work |
| 165 // either even if it appeared to work at first. So test using Windows | 178 // either even if it appeared to work at first. So test using Windows |
| 166 // console window. | 179 // console window. |
| 167 | 180 |
| 168 static BOOL WINAPI | 181 static BOOL WINAPI |
| 169 signal_handler(DWORD type lzma_attribute((unused))) | 182 signal_handler(DWORD type lzma_attribute((__unused__))) |
| 170 { | 183 { |
| 171 // Since we don't get a signal number which we could raise() at | 184 // Since we don't get a signal number which we could raise() at |
| 172 // signals_exit() like on POSIX, just set the exit status to | 185 // signals_exit() like on POSIX, just set the exit status to |
| 173 // indicate an error, so that we cannot return with zero exit status. | 186 // indicate an error, so that we cannot return with zero exit status. |
| 174 set_exit_status(E_ERROR); | 187 set_exit_status(E_ERROR); |
| 175 user_abort = true; | 188 user_abort = true; |
| 176 return TRUE; | 189 return TRUE; |
| 177 } | 190 } |
| 178 | 191 |
| 179 | 192 |
| 180 extern void | 193 extern void |
| 181 signals_init(void) | 194 signals_init(void) |
| 182 { | 195 { |
| 183 if (!SetConsoleCtrlHandler(&signal_handler, TRUE)) | 196 if (!SetConsoleCtrlHandler(&signal_handler, TRUE)) |
| 184 message_signal_handler(); | 197 message_signal_handler(); |
| 185 | 198 |
| 186 return; | 199 return; |
| 187 } | 200 } |
| 188 | 201 |
| 189 #endif | 202 #endif |
| OLD | NEW |