-O finished: prepend "finished: " marker when tfcrypt done (useful for logs).
[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         xexit(0);
34 }
35
36 void handle_sigtstp(int signal)
37 {
38         if (signal == SIGTSTP) {
39                 tfc_useconds freeze_start, freeze_end;
40
41                 tfc_getcurtime(&freeze_start);
42                 kill(getpid(), SIGSTOP);
43                 tfc_getcurtime(&freeze_end);
44                 total_time -= (freeze_end - freeze_start);
45         }
46 }
47
48 void print_crypt_status(int signal)
49 {
50         tfc_fsize wr_speed;
51         double seconds, human_totalproc_src, human_totalproc_dst, human_totalwrit_dst, human_wr_speed;
52         int src_scale_idx, dst_scale_idx, wri_scale_idx, wr_speed_scale;
53         const char *oper_mode, *inplace;
54         static tfc_yesno last;
55
56         if (last == YES) return;
57         if (signal == 0 || signal == -1) last = YES;
58
59         switch (do_edcrypt) {
60                 case TFC_DO_ENCRYPT: oper_mode = "encrypted"; break;
61                 case TFC_DO_DECRYPT: oper_mode = "decrypted"; break;
62                 default:
63                         if (ctr_mode == TFC_MODE_PLAIN) oper_mode = "written";
64                         else if (ctr_mode == TFC_MODE_SKSUM) oper_mode = "hashed";
65                         else oper_mode = NULL;
66                         break;
67         }
68
69         if (signal == SIGINT || signal == SIGTERM) {
70                 do_stop = YES;
71         }
72
73         tfc_getcurtime(&current_time);
74         total_time += (current_time - delta_time);
75         seconds = TFC_UTODSECS(current_time - delta_time);
76         wr_speed = delta_processed / seconds;
77         tfc_describescale(total_processed_src, &human_totalproc_src, &src_scale_idx);
78         tfc_describescale(total_processed_dst, &human_totalproc_dst, &dst_scale_idx);
79         tfc_describescale(total_written_dst, &human_totalwrit_dst, &wri_scale_idx);
80         tfc_describescale(wr_speed, &human_wr_speed, &wr_speed_scale);
81
82         if (bench_timer) {
83                 tfc_say("done!");
84                 tfc_say("%s %s benchmark results:", progname, tfc_modename(ctr_mode));
85                 tfc_nfsay(stdout, "%s %llu (%.2f%s) bytes, "
86                         "avg. speed %llu (%.2f%s) B/s, time %.4fs.",
87                         oper_mode,
88                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
89                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), seconds);
90                 tfc_esay("\n");
91                 xexit(0);
92         }
93
94         if (do_statline_dynamic == YES) inplace = "\033[2K\r";
95         else inplace = "";
96
97         if (do_less_stats == YES) {
98                 tfc_nfsay(stderr, "%s%s%s:"
99                         " %s %.2f%s,"
100                         " %.2f%s B/s @%s",
101                         inplace, (last && show_when_done) ? "finished: " : "", progname,
102                         oper_mode,
103                         human_totalproc_dst, tfc_getscale(dst_scale_idx),
104                         human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
105         }
106         else {
107                 if (ctr_mode <= TFC_MODE_PLAIN) tfc_nfsay(stderr, "%s%s%s: read: %llu (%.2f%s),"
108                         " %s %llu (%.2f%s) bytes,"
109                         " (%llu (%.2f%s) B/s), time %s",
110                         inplace, (last && show_when_done) ? "finished: " : "", progname,
111                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
112                         oper_mode,
113                         total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
114                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
115                 else tfc_nfsay(stderr, "%s%s%s: read: %llu (%.2f%s),"
116                         " %s %s %llu (%.2f%s) bytes,"
117                         " written %llu (%.2f%s) bytes,"
118                         " (%llu (%.2f%s) B/s), time %s",
119                         inplace, (last && show_when_done) ? "finished: " : "", progname,
120                         total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
121                         tfc_modename(ctr_mode), oper_mode,
122                         total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
123                         total_written_dst, human_totalwrit_dst, tfc_getscale(wri_scale_idx),
124                         wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
125         }
126
127         if (do_stop == NO && do_statline_dynamic == NO) tfc_esay("\n");
128         if (last) tfc_esay("\n");
129         statline_was_shown = YES;
130
131         if ((signal == SIGINT || signal == SIGTERM) && do_stop == YES) {
132                 tfc_esay("\n");
133                 exit_sigterm(signal);
134         }
135
136         delta_processed = 0;
137         tfc_getcurtime(&delta_time);
138
139         handle_sigtstp(signal);
140
141         if (status_timer) setup_next_alarm(status_timer);
142 }
143
144 void change_status_width(int signal)
145 {
146         if (do_less_stats == YES) do_less_stats = NO;
147         else if (do_less_stats == NO) do_less_stats = YES;
148 }
149
150 void change_status_timer(int signal)
151 {
152         static tfc_useconds reset_timer;
153         tfc_useconds t;
154
155         tfc_getcurtime(&t);
156         if (reset_timer && (t - reset_timer) < TFC_DTOUSECS(0.1)) status_timer = 0;
157         reset_timer = t;
158
159         if (status_timer == 0) status_timer = TFC_DTOUSECS(0.25);
160         else status_timer *= 2;
161
162         if (verbose) tfc_esay("%s: status timer was changed to %.2fs",
163                 progname, TFC_UTODSECS(status_timer));
164         setup_next_alarm(status_timer);
165 }
166
167 void setup_next_alarm(tfc_useconds useconds)
168 {
169         struct itimerval it;
170
171         memset(&it, 0, sizeof(struct itimerval));
172         it.it_value.tv_sec = useconds / 1000000;
173         it.it_value.tv_usec = useconds % 1000000;
174         setitimer(ITIMER_REAL, &it, NULL);
175 }