51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_signal.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
6  *
7  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "tfcrypt.h"
30
31 void exit_sigterm(int signal)
32 {
33         tfc_esay("\n");
34         xexit(0);
35 }
36
37 void handle_sigtstp(int signal)
38 {
39         if (signal == SIGTSTP) {
40                 tfc_useconds freeze_start, freeze_end;
41
42                 tfc_getcurtime(&freeze_start);
43                 kill(getpid(), SIGSTOP);
44                 tfc_getcurtime(&freeze_end);
45                 total_time -= (freeze_end - freeze_start);
46         }
47 }
48
49 void print_crypt_status(int signal)
50 {
51         tfc_fsize wr_speed;
52         double seconds, human_totalproc_src, human_totalproc_dst, human_totalwrit_dst, human_wr_speed;
53         int src_scale_idx, dst_scale_idx, wri_scale_idx, wr_speed_scale;
54         const char *oper_mode, *inplace;
55         static tfc_yesno last;
56         tfc_yesno finished = NO;
57
58         if (last == YES) return;
59         if (signal == TFC_SIGLAST) {
60                 last = YES;
61                 finished = YES;
62         }
63
64         switch (do_edcrypt) {
65                 case TFC_DO_ENCRYPT: oper_mode = "encrypted"; break;
66                 case TFC_DO_DECRYPT: oper_mode = "decrypted"; break;
67                 default:
68                         if (ctr_mode == TFC_MODE_PLAIN) oper_mode = "written";
69                         else if (ctr_mode == TFC_MODE_XOR) oper_mode = "xored";
70                         else if (ctr_mode == TFC_MODE_SKSUM) oper_mode = "hashed";
71                         else oper_mode = NULL;
72                         break;
73         }
74
75         if (signal == SIGINT || signal == SIGTERM) {
76                 do_stop = YES;
77         }
78
79         tfc_getcurtime(&current_time);
80         total_time += (current_time - delta_time);
81         seconds = TFC_UTODSECS(current_time - delta_time);
82         wr_speed = delta_processed / seconds;
83         tfc_describescale(total_processed_src, &human_totalproc_src, &src_scale_idx);
84         tfc_describescale(total_processed_dst, &human_totalproc_dst, &dst_scale_idx);
85         tfc_describescale(total_written_dst, &human_totalwrit_dst, &wri_scale_idx);
86         tfc_describescale(wr_speed, &human_wr_speed, &wr_speed_scale);
87
88         if (bench_timer) {
89                 tfc_say("done!");
90                 tfc_say("%s %s benchmark results:", tfc_format_pid(progname), tfc_modename(ctr_mode));
91                 tfc_nfsay(stdout, "%s %llu (%.2f%s) bytes, "
92                         "avg. speed %llu (%.2f%s) B/s, time %.4fs.",
93                         oper_mode,
94                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
95                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), seconds);
96                 xexit(0);
97         }
98
99         if (do_statline_dynamic == YES) inplace = "\033[2K\r";
100         else inplace = "";
101
102         if (do_less_stats == YES) {
103                 tfc_nfsay(stderr, "%s%s%s:"
104                         " %s %.2f%s,"
105                         " %.2f%s B/s @%s",
106                         inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
107                         oper_mode,
108                         human_totalproc_dst, tfc_getscale(dst_scale_idx),
109                         human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
110         }
111         else {
112                 if (ctr_mode <= TFC_MODE_PLAIN) tfc_nfsay(stderr, "%s%s%s: read: %llu (%.2f%s),"
113                         " %s %llu (%.2f%s) bytes,"
114                         " (%llu (%.2f%s) B/s), time %s",
115                         inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
116                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
117                         oper_mode,
118                         total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
119                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
120                 else tfc_nfsay(stderr, "%s%s%s: read: %llu (%.2f%s),"
121                         " %s %s %llu (%.2f%s) bytes,"
122                         " written %llu (%.2f%s) bytes,"
123                         " (%llu (%.2f%s) B/s), time %s",
124                         inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
125                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
126                         tfc_modename(ctr_mode), oper_mode,
127                         total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
128                         total_written_dst, human_totalwrit_dst, tfc_getscale(wri_scale_idx),
129                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
130         }
131
132         if (do_stop == NO && do_statline_dynamic == NO) tfc_esay("\n");
133         else if (signal == TFC_SIGLAST || signal == TFC_SIGERR) tfc_esay("\n");
134         statline_was_shown = YES;
135
136         if ((signal == SIGINT || signal == SIGTERM) && do_stop == YES) exit_sigterm(signal);
137
138         delta_processed = 0;
139         tfc_getcurtime(&delta_time);
140
141         handle_sigtstp(signal);
142
143         if (status_timer) setup_next_alarm(status_timer);
144 }
145
146 void change_status_width(int signal)
147 {
148         if (do_less_stats == YES) do_less_stats = NO;
149         else if (do_less_stats == NO) do_less_stats = YES;
150 }
151
152 void change_status_timer(int signal)
153 {
154         static tfc_useconds reset_timer;
155         tfc_useconds t;
156
157         tfc_getcurtime(&t);
158         if (reset_timer && (t - reset_timer) < TFC_DTOUSECS(0.1)) status_timer = 0;
159         reset_timer = t;
160
161         if (status_timer == 0) status_timer = TFC_DTOUSECS(0.25);
162         else status_timer *= 2;
163
164         if (verbose) tfc_esay("%s: status timer was changed to %.2fs",
165                 tfc_format_pid(progname), TFC_UTODSECS(status_timer));
166         setup_next_alarm(status_timer);
167 }
168
169 void setup_next_alarm(tfc_useconds useconds)
170 {
171         struct itimerval it;
172
173         memset(&it, 0, sizeof(struct itimerval));
174         it.it_value.tv_sec = useconds / 1000000;
175         it.it_value.tv_usec = useconds % 1000000;
176         setitimer(ITIMER_REAL, &it, NULL);
177 }