Skip to content

Commit d122248

Browse files
committed
Add self-termination for user mode.
Self termination kicks in only if the daemon is socket activated, so that the user daemon eventually turns itself off when not used. The default timeout is 1000 seconds but can be changed via the command line switch --idle-timeout Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent bf92a59 commit d122248

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

src/gp_init.c

-6
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ void fini_server(void)
113113
closelog();
114114
}
115115

116-
static void break_loop(verto_ctx *vctx, verto_ev *ev UNUSED)
117-
{
118-
GPDEBUG("Exiting after receiving a signal\n");
119-
verto_break(vctx);
120-
}
121-
122116
verto_ctx *init_event_loop(void)
123117
{
124118
verto_ctx *vctx;

src/gp_proxy.h

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ struct gssproxy_ctx {
7474
struct gp_workers *workers;
7575
verto_ctx *vctx;
7676
verto_ev *sock_ev; /* default socket event */
77+
78+
bool terminate; /* program runs while this is false */
79+
time_t term_timeout;
80+
verto_ev *term_ev; /* termination ev in user mode */
7781
};
7882

7983
struct gp_sock_ctx {
@@ -105,6 +109,7 @@ void init_server(bool daemonize, int userproxy, int *wait_fd);
105109
void init_done(int wait_fd);
106110
void fini_server(void);
107111
verto_ctx *init_event_loop(void);
112+
void break_loop(verto_ctx *, verto_ev *);
108113
void init_proc_nfsd(struct gp_config *cfg);
109114
void write_pid(void);
110115
int drop_privs(struct gp_config *cfg);

src/gp_socket.c

+3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ int init_activation_socket(struct gssproxy_ctx *gpctx,
217217
_sock_ctx->fd = fd;
218218

219219
*sock_ctx = _sock_ctx;
220+
} else {
221+
/* disable self termination as we are not socket activated */
222+
gpctx->term_timeout = 0;
220223
}
221224

222225
done:

src/gssproxy.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,43 @@ static void hup_handler(verto_ctx *vctx, verto_ev *ev UNUSED)
180180
return;
181181
}
182182

183+
void break_loop(verto_ctx *vctx UNUSED, verto_ev *ev)
184+
{
185+
if (ev == gpctx->term_ev) {
186+
gpctx->term_ev = NULL;
187+
}
188+
GPDEBUG("Exiting!\n");
189+
gpctx->terminate = true;
190+
}
191+
192+
static void idle_handler(verto_ctx *vctx)
193+
{
194+
/* we've been called, this means some event just fired,
195+
* restart the timeout handler */
196+
197+
if (gpctx->term_timeout == 0) {
198+
/* self termination is disabled */
199+
return;
200+
}
201+
202+
verto_del(gpctx->term_ev);
203+
204+
/* Add self-termination timeout */
205+
gpctx->term_ev = verto_add_timeout(vctx, VERTO_EV_FLAG_NONE,
206+
break_loop, gpctx->term_timeout);
207+
if (!gpctx->term_ev) {
208+
GPDEBUG("Failed to register timeout event!\n");
209+
}
210+
}
211+
212+
static void do_loop(verto_ctx *vctx)
213+
{
214+
while(gpctx->terminate == false) {
215+
verto_run_once(vctx);
216+
idle_handler(vctx);
217+
}
218+
}
219+
183220
static void init_event(verto_ctx *vctx UNUSED, verto_ev *ev UNUSED)
184221
{
185222
GPDEBUG("Initialization complete.\n");
@@ -195,6 +232,7 @@ int main(int argc, const char *argv[])
195232
int opt_debug_level = 0;
196233
int opt_syslog_status = 0;
197234
int opt_userproxy = 0;
235+
int opt_idle_timeout = 1000;
198236
verto_ctx *vctx;
199237
verto_ev *ev;
200238
int wait_fd;
@@ -226,6 +264,8 @@ int main(int argc, const char *argv[])
226264
_("Enable GSSAPI status logging to syslog"), NULL}, \
227265
{"version", '\0', POPT_ARG_NONE, &opt_version, 0, \
228266
_("Print version number and exit"), NULL }, \
267+
{"idle-timeout", '\0', POPT_ARG_INT, &opt_idle_timeout, 0, \
268+
_("Set idle timeout for user mode (default: 1000s)"), NULL },
229269
{"extract-ccache", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, \
230270
&opt_extract_ccache, 0, \
231271
_("Extract a gssproxy encrypted ccache"), NULL },
@@ -299,6 +339,8 @@ int main(int argc, const char *argv[])
299339
write_pid();
300340
}
301341

342+
gpctx->term_timeout = opt_idle_timeout * 1000;
343+
302344
vctx = init_event_loop();
303345
if (!vctx) {
304346
fprintf(stderr, "Failed to initialize event loop. "
@@ -308,8 +350,8 @@ int main(int argc, const char *argv[])
308350
}
309351
gpctx->vctx = vctx;
310352

311-
/* Add SIGHUP here so that gpctx is in scope for the handler */
312353
if (!opt_userproxy) {
354+
/* Add SIGHUP here so that gpctx is in scope for the handler */
313355
ev = verto_add_signal(vctx, VERTO_EV_FLAG_PERSIST,
314356
hup_handler, SIGHUP);
315357
if (!ev) {
@@ -366,7 +408,7 @@ int main(int argc, const char *argv[])
366408
goto cleanup;
367409
}
368410

369-
verto_run(vctx);
411+
do_loop(vctx);
370412
verto_free(vctx);
371413

372414
gp_workers_free(gpctx->workers);

0 commit comments

Comments
 (0)