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