5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global variables --------------------------------------------------*/
34 /*----- Static variables --------------------------------------------------*/
37 #define T_INTERVAL MIN(1)
39 static unsigned iv_nreasons
= 0;
40 static struct timeval iv_next
= { 0, 0 };
41 static int lpdone
= 0;
43 /*----- The interval timer ------------------------------------------------*/
45 /* --- @interval@ --- *
47 * Arguments: @struct timeval *tv@ = time when called
48 * @void *v@ = boring pointer
52 * Use: Called periodically to do housekeeping tasks.
55 static void interval(struct timeval
*tv
, void *v
)
57 T( trace(T_PEER
, "peer: interval timer"); )
59 rand_seed(RAND_GLOBAL
, MAXHASHSZ
);
61 iv_next
.tv_sec
+= T_INTERVAL
;
62 sel_addtimer(&sel
, &it
, &iv_next
, interval
, v
);
65 /* --- @iv_addreason@ --- *
71 * Use: Adds an `interval timer reason'; if there are no others, the
72 * interval timer is engaged.
75 void iv_addreason(void)
81 if (TV_CMP(&tv
, >=, &iv_next
)) interval(&tv
, 0);
82 else sel_addtimer(&sel
, &it
, &iv_next
, interval
, 0);
87 /* --- @iv_rmreason@ --- *
93 * Use: Removes an interval timer reason; if there are none left, the
94 * interval timer is disengaged.
97 void iv_rmreason(void)
99 assert(iv_nreasons
); iv_nreasons
--;
100 if (!iv_nreasons
) sel_rmtimer(&it
);
103 /*----- The main loop -----------------------------------------------------*/
105 /* --- @lp_init@ --- *
111 * Use: Initializes the main loop. Most importantly, this sets up
112 * the select multiplexor that everything else hooks onto.
117 gettimeofday(&iv_next
, 0); iv_next
.tv_sec
+= T_INTERVAL
;
118 signal(SIGPIPE
, SIG_IGN
);
123 /* --- @lp_end@ --- *
129 * Use: Requests an exit from the main loop.
132 void lp_end(void) { lpdone
= 1; }
134 /* --- @lp_run@ --- *
138 * Returns: Zero on successful termination; @-1@ if things went wrong.
140 * Use: Cranks the main loop until it should be cranked no more.
150 if (!sel_select(&sel
)) nerr
= 0;
151 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
152 a_warn("SERVER", "select-error", "?ERRNO", A_END
);
155 a_warn("ABORT", "repeated-select-errors", A_END
);
164 /*----- That's all, folks -------------------------------------------------*/