chiark / gitweb /
server/admin.c: Remove spurious `ping' in usage message.
[tripe] / server / tripe.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Main program
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
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.
e04c2d50 16 *
11ad66c2
MW
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.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
410c8acf 24 */
25
410c8acf 26/*----- Header files ------------------------------------------------------*/
27
28#include "tripe.h"
29
30/*----- Global variables --------------------------------------------------*/
31
32sel_state sel;
410c8acf 33
34/*----- Static variables --------------------------------------------------*/
35
36static sel_timer it;
37#define T_INTERVAL MIN(1)
38
a7abb429
MW
39static unsigned iv_nreasons = 0;
40static struct timeval iv_next = { 0, 0 };
2173bd49 41static int lpdone = 0;
a7abb429
MW
42
43/*----- The interval timer ------------------------------------------------*/
410c8acf 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
42da2a58 55static void interval(struct timeval *tv, void *v)
410c8acf 56{
410c8acf 57 T( trace(T_PEER, "peer: interval timer"); )
a7abb429 58 iv_next = *tv;
b5c45da1 59 rand_seed(RAND_GLOBAL, MAXHASHSZ);
410c8acf 60 p_interval();
a7abb429
MW
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
75void 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++;
410c8acf 85}
86
a7abb429
MW
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
97void iv_rmreason(void)
98{
99 assert(iv_nreasons); iv_nreasons--;
100 if (!iv_nreasons) sel_rmtimer(&it);
101}
102
c9aded9f
MW
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
115void lp_init(void)
116{
c9aded9f
MW
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
2173bd49
MW
123/* --- @lp_end@ --- *
124 *
125 * Arguments: ---
126 *
127 * Returns: ---
128 *
129 * Use: Requests an exit from the main loop.
130 */
131
132void lp_end(void) { lpdone = 1; }
133
c9aded9f
MW
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
143int lp_run(void)
144{
145 int nerr = 0;
146
147 for (;;) {
148 a_preselect();
2173bd49 149 if (lpdone) break;
c9aded9f
MW
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 }
2173bd49 160 lpdone = 0;
c9aded9f
MW
161 return (0);
162}
163
410c8acf 164/*----- That's all, folks -------------------------------------------------*/