diff options
author | Pavel Březina <pbrezina@redhat.com> | 2012-12-14 13:15:53 +0100 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2012-12-18 17:15:22 +0100 |
commit | 260366c37cafab011cf48760eaf04282098ef800 (patch) | |
tree | a32eb1bd2e466445346d8ec60c36b24d114a176f | |
parent | e148ba6dbb26a0eac162ac9a2ff550da2c883808 (diff) | |
download | sssd-260366c37cafab011cf48760eaf04282098ef800.tar.gz sssd-260366c37cafab011cf48760eaf04282098ef800.tar.bz2 sssd-260366c37cafab011cf48760eaf04282098ef800.zip |
add sdap_sudo_schedule_refresh()
Reduces amount of code duplication.
-rw-r--r-- | src/providers/ldap/sdap_sudo.c | 114 | ||||
-rw-r--r-- | src/providers/ldap/sdap_sudo.h | 6 |
2 files changed, 77 insertions, 43 deletions
diff --git a/src/providers/ldap/sdap_sudo.c b/src/providers/ldap/sdap_sudo.c index 28938207..2c6c7712 100644 --- a/src/providers/ldap/sdap_sudo.c +++ b/src/providers/ldap/sdap_sudo.c @@ -88,6 +88,13 @@ static void sdap_sudo_periodical_full_refresh_done(struct tevent_req *req); static void sdap_sudo_periodical_smart_refresh_done(struct tevent_req *req); +static int sdap_sudo_schedule_refresh(TALLOC_CTX *mem_ctx, + struct sdap_sudo_ctx *sudo_ctx, + enum sdap_sudo_refresh_type refresh, + tevent_req_fn callback, + time_t delay, + time_t timeout); + static int sdap_sudo_schedule_full_refresh(struct sdap_sudo_ctx *sudo_ctx, time_t delay); @@ -200,12 +207,11 @@ static void sdap_sudo_get_hostinfo_done(struct tevent_req *req) static int sdap_sudo_setup_periodical_refresh(struct sdap_sudo_ctx *sudo_ctx) { struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx; - struct tevent_req *req; time_t smart_default; time_t smart_interval; time_t full_interval; time_t last_full; - struct timeval tv; + time_t delay; int ret; smart_interval = dp_opt_get_int(id_ctx->opts->basic, @@ -252,7 +258,7 @@ static int sdap_sudo_setup_periodical_refresh(struct sdap_sudo_ctx *sudo_ctx) * clients requesting sudo information won't get an * immediate reply with no entries */ - tv = tevent_timeval_current(); + delay = 0; } else { /* At least one update has previously run, * so clients will get cached data. @@ -262,24 +268,19 @@ static int sdap_sudo_setup_periodical_refresh(struct sdap_sudo_ctx *sudo_ctx) */ /* delay at least by 10s */ - tv = tevent_timeval_current_ofs(10, 0); + delay = 10; } - req = sdap_sudo_timer_send(sudo_ctx, id_ctx->be->ev, sudo_ctx, - tv, full_interval, - sdap_sudo_full_refresh_send); - if (req == NULL) { + ret = sdap_sudo_schedule_refresh(sudo_ctx, sudo_ctx, + SDAP_SUDO_REFRESH_FULL, + sdap_sudo_periodical_first_refresh_done, + delay, full_interval); + if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, ("Unable to schedule full refresh of sudo " "rules! Periodical updates will not work!\n")); - return ENOMEM; + return ret; } - tevent_req_set_callback(req, sdap_sudo_periodical_first_refresh_done, - sudo_ctx); - - DEBUG(SSSDBG_TRACE_FUNC, ("Full refresh scheduled at: %lld\n", - (long long)tv.tv_sec)); - return EOK; } @@ -1111,27 +1112,62 @@ schedule: } } -static int sdap_sudo_schedule_full_refresh(struct sdap_sudo_ctx *sudo_ctx, - time_t delay) +static int sdap_sudo_schedule_refresh(TALLOC_CTX *mem_ctx, + struct sdap_sudo_ctx *sudo_ctx, + enum sdap_sudo_refresh_type refresh, + tevent_req_fn callback, + time_t delay, + time_t timeout) { struct tevent_req *req = NULL; - struct timeval tv; + sdap_sudo_timer_fn_t send_fn; + const char *name = NULL; + struct timeval when; - /* schedule new refresh */ - tv = tevent_timeval_current_ofs(delay, 0); - req = sdap_sudo_timer_send(sudo_ctx, sudo_ctx->id_ctx->be->ev, sudo_ctx, - tv, delay, sdap_sudo_full_refresh_send); + when = tevent_timeval_current_ofs(delay, 0); + + switch (refresh) { + case SDAP_SUDO_REFRESH_FULL: + send_fn = sdap_sudo_full_refresh_send; + name = "Full refresh"; + break; + case SDAP_SUDO_REFRESH_SMART: + send_fn = sdap_sudo_smart_refresh_send; + name = "Smart refresh"; + break; + case SDAP_SUDO_REFRESH_RULES: + DEBUG(SSSDBG_OP_FAILURE, ("Rules refresh can't be scheduled!\n")); + return EINVAL; + } + + req = sdap_sudo_timer_send(mem_ctx, sudo_ctx->id_ctx->be->ev, sudo_ctx, + when, timeout, send_fn); if (req == NULL) { - DEBUG(SSSDBG_OP_FAILURE, ("Unable to schedule full refresh of sudo " - "rules!\n")); return ENOMEM; } - tevent_req_set_callback(req, sdap_sudo_periodical_full_refresh_done, - sudo_ctx); + tevent_req_set_callback(req, callback, sudo_ctx); + + DEBUG(SSSDBG_TRACE_FUNC, ("%s scheduled at: %lld\n", + name, (long long)when.tv_sec)); + + return EOK; +} + +static int sdap_sudo_schedule_full_refresh(struct sdap_sudo_ctx *sudo_ctx, + time_t delay) +{ + int ret; - DEBUG(SSSDBG_TRACE_FUNC, ("Full refresh scheduled at: %lld\n", - (long long)tv.tv_sec)); + ret = sdap_sudo_schedule_refresh(sudo_ctx, sudo_ctx, + SDAP_SUDO_REFRESH_FULL, + sdap_sudo_periodical_full_refresh_done, + delay, delay); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, ("Unable to schedule full refresh of sudo " + "rules!\n")); + return ret; + } return EOK; } @@ -1139,25 +1175,17 @@ static int sdap_sudo_schedule_full_refresh(struct sdap_sudo_ctx *sudo_ctx, static int sdap_sudo_schedule_smart_refresh(struct sdap_sudo_ctx *sudo_ctx, time_t delay) { - struct sdap_id_ctx *id_ctx = sudo_ctx->id_ctx; - struct tevent_req *req = NULL; - struct timeval tv; + int ret; - /* schedule new refresh */ - tv = tevent_timeval_current_ofs(delay, 0); - req = sdap_sudo_timer_send(sudo_ctx, id_ctx->be->ev, sudo_ctx, - tv, delay, sdap_sudo_smart_refresh_send); - if (req == NULL) { + ret = sdap_sudo_schedule_refresh(sudo_ctx, sudo_ctx, + SDAP_SUDO_REFRESH_SMART, + sdap_sudo_periodical_smart_refresh_done, + delay, delay); + if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, ("Unable to schedule smart refresh of sudo " "rules!\n")); - return ENOMEM; + return ret; } - tevent_req_set_callback(req, sdap_sudo_periodical_smart_refresh_done, - sudo_ctx); - - DEBUG(SSSDBG_TRACE_FUNC, ("Smart refresh scheduled at: %lld\n", - (long long)tv.tv_sec)); - return EOK; } diff --git a/src/providers/ldap/sdap_sudo.h b/src/providers/ldap/sdap_sudo.h index f9988d61..5a12afd5 100644 --- a/src/providers/ldap/sdap_sudo.h +++ b/src/providers/ldap/sdap_sudo.h @@ -33,6 +33,12 @@ struct sdap_sudo_ctx { bool full_refresh_done; }; +enum sdap_sudo_refresh_type { + SDAP_SUDO_REFRESH_FULL, + SDAP_SUDO_REFRESH_SMART, + SDAP_SUDO_REFRESH_RULES +}; + /* Common functions from ldap_sudo.c */ void sdap_sudo_handler(struct be_req *breq); int sdap_sudo_init(struct be_ctx *be_ctx, |