Skip to content

Commit

Permalink
Add self-termination for user mode.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
simo5 committed May 23, 2022
1 parent b9b8884 commit b8f5f4a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
6 changes: 0 additions & 6 deletions src/gp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ void fini_server(void)
closelog();
}

static void break_loop(verto_ctx *vctx, verto_ev *ev UNUSED)
{
GPDEBUG("Exiting after receiving a signal\n");
verto_break(vctx);
}

verto_ctx *init_event_loop(void)
{
verto_ctx *vctx;
Expand Down
5 changes: 5 additions & 0 deletions src/gp_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ struct gssproxy_ctx {
struct gp_workers *workers;
verto_ctx *vctx;
verto_ev *sock_ev; /* default socket event */

bool terminate; /* program runs while this is false */
time_t term_timeout;
verto_ev *term_ev; /* termination ev in user mode */
};

struct gp_sock_ctx {
Expand Down Expand Up @@ -105,6 +109,7 @@ void init_server(bool daemonize, int userproxy, int *wait_fd);
void init_done(int wait_fd);
void fini_server(void);
verto_ctx *init_event_loop(void);
void break_loop(verto_ctx *, verto_ev *);
void init_proc_nfsd(struct gp_config *cfg);
void write_pid(void);
int drop_privs(struct gp_config *cfg);
Expand Down
3 changes: 3 additions & 0 deletions src/gp_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ int init_activation_socket(struct gssproxy_ctx *gpctx,
_sock_ctx->fd = fd;

*sock_ctx = _sock_ctx;
} else {
/* disable self termination as we are not socket activated */
gpctx->term_timeout = 0;
}

done:
Expand Down
46 changes: 44 additions & 2 deletions src/gssproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,43 @@ static void hup_handler(verto_ctx *vctx, verto_ev *ev UNUSED)
return;
}

void break_loop(verto_ctx *vctx UNUSED, verto_ev *ev)
{
if (ev == gpctx->term_ev) {
gpctx->term_ev = NULL;
}
GPDEBUG("Exiting!\n");
gpctx->terminate = true;
}

static void idle_handler(verto_ctx *vctx)
{
/* we've been called, this means some event just fired,
* restart the timeout handler */

if (gpctx->term_timeout == 0) {
/* self termination is disabled */
return;
}

verto_del(gpctx->term_ev);

/* Add self-termination timeout */
gpctx->term_ev = verto_add_timeout(vctx, VERTO_EV_FLAG_NONE,
break_loop, gpctx->term_timeout);
if (!gpctx->term_ev) {
GPDEBUG("Failed to register timeout event!\n");
}
}

static void do_loop(verto_ctx *vctx)
{
while(gpctx->terminate == false) {
verto_run_once(vctx);
idle_handler(vctx);
}
}

static void init_event(verto_ctx *vctx UNUSED, verto_ev *ev UNUSED)
{
GPDEBUG("Initialization complete.\n");
Expand All @@ -195,6 +232,7 @@ int main(int argc, const char *argv[])
int opt_debug_level = 0;
int opt_syslog_status = 0;
int opt_userproxy = 0;
int opt_idle_timeout = 1000;
verto_ctx *vctx;
verto_ev *ev;
int wait_fd;
Expand Down Expand Up @@ -226,6 +264,8 @@ int main(int argc, const char *argv[])
_("Enable GSSAPI status logging to syslog"), NULL}, \
{"version", '\0', POPT_ARG_NONE, &opt_version, 0, \
_("Print version number and exit"), NULL }, \
{"idle-timeout", '\0', POPT_ARG_INT, &opt_idle_timeout, 0, \
_("Set idle timeout for user mode (default: 1000s)"), NULL },
{"extract-ccache", '\0', POPT_ARG_STRING|POPT_ARGFLAG_DOC_HIDDEN, \
&opt_extract_ccache, 0, \
_("Extract a gssproxy encrypted ccache"), NULL },
Expand Down Expand Up @@ -299,6 +339,8 @@ int main(int argc, const char *argv[])
write_pid();
}

gpctx->term_timeout = opt_idle_timeout * 1000;

vctx = init_event_loop();
if (!vctx) {
fprintf(stderr, "Failed to initialize event loop. "
Expand All @@ -308,8 +350,8 @@ int main(int argc, const char *argv[])
}
gpctx->vctx = vctx;

/* Add SIGHUP here so that gpctx is in scope for the handler */
if (!opt_userproxy) {
/* Add SIGHUP here so that gpctx is in scope for the handler */
ev = verto_add_signal(vctx, VERTO_EV_FLAG_PERSIST,
hup_handler, SIGHUP);
if (!ev) {
Expand Down Expand Up @@ -366,7 +408,7 @@ int main(int argc, const char *argv[])
goto cleanup;
}

verto_run(vctx);
do_loop(vctx);
verto_free(vctx);

gp_workers_free(gpctx->workers);
Expand Down

0 comments on commit b8f5f4a

Please sign in to comment.