server/admin.c: Remove spurious `ping' in usage message.
[tripe] / server / tripe.c
1 /* -*-c-*-
2 *
3 * Main program
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
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.
16 *
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
20 * for more details.
21 *
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/>.
24 */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Global variables --------------------------------------------------*/
31
32 sel_state sel;
33
34 /*----- Static variables --------------------------------------------------*/
35
36 static sel_timer it;
37 #define T_INTERVAL MIN(1)
38
39 static unsigned iv_nreasons = 0;
40 static struct timeval iv_next = { 0, 0 };
41 static int lpdone = 0;
42
43 /*----- The interval timer ------------------------------------------------*/
44
45 /* --- @interval@ --- *
46 *
47 * Arguments: @struct timeval *tv@ = time when called
48 * @void *v@ = boring pointer
49 *
50 * Returns: ---
51 *
52 * Use: Called periodically to do housekeeping tasks.
53 */
54
55 static void interval(struct timeval *tv, void *v)
56 {
57 T( trace(T_PEER, "peer: interval timer"); )
58 iv_next = *tv;
59 rand_seed(RAND_GLOBAL, MAXHASHSZ);
60 p_interval();
61 iv_next.tv_sec += T_INTERVAL;
62 sel_addtimer(&sel, &it, &iv_next, interval, v);
63 }
64
65 /* --- @iv_addreason@ --- *
66 *
67 * Arguments: ---
68 *
69 * Returns: ---
70 *
71 * Use: Adds an `interval timer reason'; if there are no others, the
72 * interval timer is engaged.
73 */
74
75 void iv_addreason(void)
76 {
77 struct timeval tv;
78
79 if (!iv_nreasons) {
80 gettimeofday(&tv, 0);
81 if (TV_CMP(&tv, >=, &iv_next)) interval(&tv, 0);
82 else sel_addtimer(&sel, &it, &iv_next, interval, 0);
83 }
84 iv_nreasons++;
85 }
86
87 /* --- @iv_rmreason@ --- *
88 *
89 * Arguments: ---
90 *
91 * Returns: ---
92 *
93 * Use: Removes an interval timer reason; if there are none left, the
94 * interval timer is disengaged.
95 */
96
97 void iv_rmreason(void)
98 {
99 assert(iv_nreasons); iv_nreasons--;
100 if (!iv_nreasons) sel_rmtimer(&it);
101 }
102
103 /*----- The main loop -----------------------------------------------------*/
104
105 /* --- @lp_init@ --- *
106 *
107 * Arguments: ---
108 *
109 * Returns: ---
110 *
111 * Use: Initializes the main loop. Most importantly, this sets up
112 * the select multiplexor that everything else hooks onto.
113 */
114
115 void lp_init(void)
116 {
117 gettimeofday(&iv_next, 0); iv_next.tv_sec += T_INTERVAL;
118 signal(SIGPIPE, SIG_IGN);
119 sel_init(&sel);
120 sig_init(&sel);
121 }
122
123 /* --- @lp_end@ --- *
124 *
125 * Arguments: ---
126 *
127 * Returns: ---
128 *
129 * Use: Requests an exit from the main loop.
130 */
131
132 void lp_end(void) { lpdone = 1; }
133
134 /* --- @lp_run@ --- *
135 *
136 * Arguments: ---
137 *
138 * Returns: Zero on successful termination; @-1@ if things went wrong.
139 *
140 * Use: Cranks the main loop until it should be cranked no more.
141 */
142
143 int lp_run(void)
144 {
145 int nerr = 0;
146
147 for (;;) {
148 a_preselect();
149 if (lpdone) break;
150 if (!sel_select(&sel)) nerr = 0;
151 else if (errno != EINTR && errno != EAGAIN) {
152 a_warn("SERVER", "select-error", "?ERRNO", A_END);
153 nerr++;
154 if (nerr > 8) {
155 a_warn("ABORT", "repeated-select-errors", A_END);
156 abort();
157 }
158 }
159 }
160 lpdone = 0;
161 return (0);
162 }
163
164 /*----- That's all, folks -------------------------------------------------*/