LCOV - code coverage report
Current view: top level - lib/tevent - tevent_timed.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 149 166 89.8 %
Date: 2024-04-21 15:09:00 Functions: 17 18 94.4 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    common events code for timed events
       5             : 
       6             :    Copyright (C) Andrew Tridgell        2003-2006
       7             :    Copyright (C) Stefan Metzmacher      2005-2009
       8             : 
       9             :      ** NOTE! The following LGPL license applies to the tevent
      10             :      ** library. This does NOT imply that all of Samba is released
      11             :      ** under the LGPL
      12             : 
      13             :    This library is free software; you can redistribute it and/or
      14             :    modify it under the terms of the GNU Lesser General Public
      15             :    License as published by the Free Software Foundation; either
      16             :    version 3 of the License, or (at your option) any later version.
      17             : 
      18             :    This library is distributed in the hope that it will be useful,
      19             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      21             :    Lesser General Public License for more details.
      22             : 
      23             :    You should have received a copy of the GNU Lesser General Public
      24             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      25             : */
      26             : 
      27             : #include "replace.h"
      28             : #include "system/time.h"
      29             : #define TEVENT_DEPRECATED 1
      30             : #include "tevent.h"
      31             : #include "tevent_internal.h"
      32             : #include "tevent_util.h"
      33             : 
      34             : /**
      35             :   compare two timeval structures.
      36             :   Return -1 if tv1 < tv2
      37             :   Return 0 if tv1 == tv2
      38             :   Return 1 if tv1 > tv2
      39             : */
      40   538036179 : int tevent_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
      41             : {
      42   538036179 :         if (tv1->tv_sec  > tv2->tv_sec)  return 1;
      43   352271917 :         if (tv1->tv_sec  < tv2->tv_sec)  return -1;
      44   145479356 :         if (tv1->tv_usec > tv2->tv_usec) return 1;
      45   140603355 :         if (tv1->tv_usec < tv2->tv_usec) return -1;
      46   130977745 :         return 0;
      47             : }
      48             : 
      49             : /**
      50             :   return a zero timeval
      51             : */
      52   618220985 : struct timeval tevent_timeval_zero(void)
      53             : {
      54    40921544 :         struct timeval tv;
      55   618220985 :         tv.tv_sec = 0;
      56   618220985 :         tv.tv_usec = 0;
      57   618220985 :         return tv;
      58             : }
      59             : 
      60             : /**
      61             :   return a timeval for the current time
      62             : */
      63   332130715 : struct timeval tevent_timeval_current(void)
      64             : {
      65    31427187 :         struct timeval tv;
      66   332130715 :         gettimeofday(&tv, NULL);
      67   332130715 :         return tv;
      68             : }
      69             : 
      70             : /**
      71             :   return a timeval struct with the given elements
      72             : */
      73    29103719 : struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs)
      74             : {
      75      431429 :         struct timeval tv;
      76    29103719 :         tv.tv_sec = secs;
      77    29103719 :         tv.tv_usec = usecs;
      78    29103719 :         return tv;
      79             : }
      80             : 
      81             : /**
      82             :   return the difference between two timevals as a timeval
      83             :   if tv1 comes after tv2, then return a zero timeval
      84             :   (this is *tv2 - *tv1)
      85             : */
      86   176168972 : struct timeval tevent_timeval_until(const struct timeval *tv1,
      87             :                                     const struct timeval *tv2)
      88             : {
      89    28972502 :         struct timeval t;
      90   176168972 :         if (tevent_timeval_compare(tv1, tv2) >= 0) {
      91     1851670 :                 return tevent_timeval_zero();
      92             :         }
      93   174317302 :         t.tv_sec = tv2->tv_sec - tv1->tv_sec;
      94   174317302 :         if (tv1->tv_usec > tv2->tv_usec) {
      95   140084068 :                 t.tv_sec--;
      96   140084068 :                 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
      97             :         } else {
      98    34233234 :                 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
      99             :         }
     100   174317302 :         return t;
     101             : }
     102             : 
     103             : /**
     104             :   return true if a timeval is zero
     105             : */
     106  1384524239 : bool tevent_timeval_is_zero(const struct timeval *tv)
     107             : {
     108  1384524239 :         return tv->tv_sec == 0 && tv->tv_usec == 0;
     109             : }
     110             : 
     111    35537485 : struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
     112             :                                   uint32_t usecs)
     113             : {
     114    35537485 :         struct timeval tv2 = *tv;
     115    35537485 :         tv2.tv_sec += secs;
     116    35537485 :         tv2.tv_usec += usecs;
     117    35537485 :         tv2.tv_sec += tv2.tv_usec / 1000000;
     118    35537485 :         tv2.tv_usec = tv2.tv_usec % 1000000;
     119             : 
     120    35537485 :         return tv2;
     121             : }
     122             : 
     123             : /**
     124             :   return a timeval in the future with a specified offset
     125             : */
     126    35530675 : struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs)
     127             : {
     128    35530675 :         struct timeval tv = tevent_timeval_current();
     129    35530675 :         return tevent_timeval_add(&tv, secs, usecs);
     130             : }
     131             : 
     132             : /*
     133             :   destroy a timed event
     134             : */
     135   419881417 : static int tevent_common_timed_destructor(struct tevent_timer *te)
     136             : {
     137   419881417 :         if (te->destroyed) {
     138      100702 :                 tevent_common_check_double_free(te, "tevent_timer double free");
     139      100702 :                 goto done;
     140             :         }
     141   419780715 :         te->destroyed = true;
     142             : 
     143   419780715 :         if (te->event_ctx == NULL) {
     144       91735 :                 return 0;
     145             :         }
     146             : 
     147   419686894 :         TEVENT_DEBUG(te->event_ctx, TEVENT_DEBUG_TRACE,
     148             :                      "Destroying timer event %p \"%s\"\n",
     149             :                      te, te->handler_name);
     150             : 
     151   419686894 :         if (te->event_ctx->last_zero_timer == te) {
     152       25852 :                 te->event_ctx->last_zero_timer = DLIST_PREV(te);
     153             :         }
     154             : 
     155   419686894 :         tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
     156   419686894 :         DLIST_REMOVE(te->event_ctx->timer_events, te);
     157             : 
     158   419686894 :         te->event_ctx = NULL;
     159   419787596 : done:
     160   419787596 :         if (te->busy) {
     161   200951669 :                 return -1;
     162             :         }
     163   213075951 :         te->wrapper = NULL;
     164             : 
     165   213075951 :         return 0;
     166             : }
     167             : 
     168   453523492 : static void tevent_common_insert_timer(struct tevent_context *ev,
     169             :                                        struct tevent_timer *te,
     170             :                                        bool optimize_zero)
     171             : {
     172   453523492 :         struct tevent_timer *prev_te = NULL;
     173             : 
     174   453523492 :         if (te->destroyed) {
     175           0 :                 tevent_abort(ev, "tevent_timer use after free");
     176           0 :                 return;
     177             :         }
     178             : 
     179             :         /* keep the list ordered */
     180   453523492 :         if (optimize_zero && tevent_timeval_is_zero(&te->next_event)) {
     181             :                 /*
     182             :                  * Some callers use zero tevent_timer
     183             :                  * instead of tevent_immediate events.
     184             :                  *
     185             :                  * As these can happen very often,
     186             :                  * we remember the last zero timer
     187             :                  * in the list.
     188             :                  */
     189   204823564 :                 prev_te = ev->last_zero_timer;
     190   204823564 :                 ev->last_zero_timer = te;
     191             :         } else {
     192     6134136 :                 struct tevent_timer *cur_te;
     193             : 
     194             :                 /*
     195             :                  * we traverse the list from the tail
     196             :                  * because it's much more likely that
     197             :                  * timers are added at the end of the list
     198             :                  */
     199   248699928 :                 for (cur_te = DLIST_TAIL(ev->timer_events);
     200   287679999 :                      cur_te != NULL;
     201    39241911 :                      cur_te = DLIST_PREV(cur_te))
     202             :                 {
     203     6433701 :                         int ret;
     204             : 
     205             :                         /*
     206             :                          * if the new event comes before the current
     207             :                          * we continue searching
     208             :                          */
     209   255157129 :                         ret = tevent_timeval_compare(&te->next_event,
     210   248723428 :                                                      &cur_te->next_event);
     211   248723428 :                         if (ret < 0) {
     212    39241911 :                                 continue;
     213             :                         }
     214             : 
     215   203631896 :                         break;
     216             :                 }
     217             : 
     218   242565792 :                 prev_te = cur_te;
     219             :         }
     220             : 
     221   453523492 :         tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_ATTACH);
     222   453523492 :         DLIST_ADD_AFTER(ev->timer_events, te, prev_te);
     223             : }
     224             : 
     225             : /*
     226             :   add a timed event
     227             :   return NULL on failure (memory allocation error)
     228             : */
     229   419917166 : static struct tevent_timer *tevent_common_add_timer_internal(
     230             :                                         struct tevent_context *ev,
     231             :                                         TALLOC_CTX *mem_ctx,
     232             :                                         struct timeval next_event,
     233             :                                         tevent_timer_handler_t handler,
     234             :                                         void *private_data,
     235             :                                         const char *handler_name,
     236             :                                         const char *location,
     237             :                                         bool optimize_zero)
     238             : {
     239    11639898 :         struct tevent_timer *te;
     240             : 
     241   419917207 :         te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
     242   419917166 :         if (te == NULL) return NULL;
     243             : 
     244   419917166 :         *te = (struct tevent_timer) {
     245             :                 .event_ctx      = ev,
     246             :                 .next_event     = next_event,
     247             :                 .handler        = handler,
     248             :                 .private_data   = private_data,
     249             :                 .handler_name   = handler_name,
     250             :                 .location       = location,
     251             :         };
     252             : 
     253   419917166 :         if (ev->timer_events == NULL) {
     254    76649901 :                 ev->last_zero_timer = NULL;
     255             :         }
     256             : 
     257   419917166 :         tevent_common_insert_timer(ev, te, optimize_zero);
     258             : 
     259   419917166 :         talloc_set_destructor(te, tevent_common_timed_destructor);
     260             : 
     261             : 
     262   419917166 :         TEVENT_DEBUG(ev, TEVENT_DEBUG_TRACE,
     263             :                      "Added timed event \"%s\": %p\n",
     264             :                      handler_name, te);
     265   408277268 :         return te;
     266             : }
     267             : 
     268           0 : struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
     269             :                                              TALLOC_CTX *mem_ctx,
     270             :                                              struct timeval next_event,
     271             :                                              tevent_timer_handler_t handler,
     272             :                                              void *private_data,
     273             :                                              const char *handler_name,
     274             :                                              const char *location)
     275             : {
     276             :         /*
     277             :          * do not use optimization, there are broken Samba
     278             :          * versions which use tevent_common_add_timer()
     279             :          * without using tevent_common_loop_timer_delay(),
     280             :          * it just uses DLIST_REMOVE(ev->timer_events, te)
     281             :          * and would leave ev->last_zero_timer behind.
     282             :          */
     283           0 :         return tevent_common_add_timer_internal(ev, mem_ctx, next_event,
     284             :                                                 handler, private_data,
     285             :                                                 handler_name, location,
     286             :                                                 false);
     287             : }
     288             : 
     289   419917166 : struct tevent_timer *tevent_common_add_timer_v2(struct tevent_context *ev,
     290             :                                                 TALLOC_CTX *mem_ctx,
     291             :                                                 struct timeval next_event,
     292             :                                                 tevent_timer_handler_t handler,
     293             :                                                 void *private_data,
     294             :                                                 const char *handler_name,
     295             :                                                 const char *location)
     296             : {
     297             :         /*
     298             :          * Here we turn on last_zero_timer optimization
     299             :          */
     300   419917166 :         return tevent_common_add_timer_internal(ev, mem_ctx, next_event,
     301             :                                                 handler, private_data,
     302             :                                                 handler_name, location,
     303             :                                                 true);
     304             : }
     305             : 
     306    33606326 : void tevent_update_timer(struct tevent_timer *te, struct timeval next_event)
     307             : {
     308    33606326 :         struct tevent_context *ev = te->event_ctx;
     309             : 
     310    33606326 :         if (ev->last_zero_timer == te) {
     311           0 :                 te->event_ctx->last_zero_timer = DLIST_PREV(te);
     312             :         }
     313    33606326 :         tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
     314    33606326 :         DLIST_REMOVE(ev->timer_events, te);
     315             : 
     316    33606326 :         te->next_event = next_event;
     317             : 
     318             :         /*
     319             :          * Not doing the zero_timer optimization. This is for new code
     320             :          * that should know about immediates.
     321             :          */
     322    33606326 :         tevent_common_insert_timer(ev, te, false);
     323    33606326 : }
     324             : 
     325   206649310 : int tevent_common_invoke_timer_handler(struct tevent_timer *te,
     326             :                                        struct timeval current_time,
     327             :                                        bool *removed)
     328             : {
     329   206649310 :         struct tevent_context *handler_ev = te->event_ctx;
     330             : 
     331   206649310 :         if (removed != NULL) {
     332           0 :                 *removed = false;
     333             :         }
     334             : 
     335   206649310 :         if (te->event_ctx == NULL) {
     336           0 :                 return 0;
     337             :         }
     338             : 
     339             :         /*
     340             :          * We need to remove the timer from the list before calling the
     341             :          * handler because in a semi-async inner event loop called from the
     342             :          * handler we don't want to come across this event again -- vl
     343             :          */
     344   206649310 :         if (te->event_ctx->last_zero_timer == te) {
     345   204772732 :                 te->event_ctx->last_zero_timer = DLIST_PREV(te);
     346             :         }
     347   206649310 :         DLIST_REMOVE(te->event_ctx->timer_events, te);
     348             : 
     349   206649310 :         TEVENT_DEBUG(te->event_ctx, TEVENT_DEBUG_TRACE,
     350             :                      "Running timer event %p \"%s\"\n",
     351             :                      te, te->handler_name);
     352             : 
     353             :         /*
     354             :          * If the timed event was registered for a zero current_time,
     355             :          * then we pass a zero timeval here too! To avoid the
     356             :          * overhead of gettimeofday() calls.
     357             :          *
     358             :          * otherwise we pass the current time
     359             :          */
     360   206649310 :         te->busy = true;
     361   206649310 :         if (te->wrapper != NULL) {
     362           4 :                 handler_ev = te->wrapper->wrap_ev;
     363             : 
     364           4 :                 tevent_wrapper_push_use_internal(handler_ev, te->wrapper);
     365           4 :                 te->wrapper->ops->before_timer_handler(
     366           0 :                                         te->wrapper->wrap_ev,
     367           0 :                                         te->wrapper->private_state,
     368           0 :                                         te->wrapper->main_ev,
     369             :                                         te,
     370             :                                         te->next_event,
     371             :                                         current_time,
     372             :                                         te->handler_name,
     373             :                                         te->location);
     374             :         }
     375   206649310 :         tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_BEFORE_HANDLER);
     376   206649310 :         te->handler(handler_ev, te, current_time, te->private_data);
     377   206649228 :         if (te->wrapper != NULL) {
     378           4 :                 te->wrapper->ops->after_timer_handler(
     379           0 :                                         te->wrapper->wrap_ev,
     380           0 :                                         te->wrapper->private_state,
     381           0 :                                         te->wrapper->main_ev,
     382             :                                         te,
     383             :                                         te->next_event,
     384             :                                         current_time,
     385             :                                         te->handler_name,
     386             :                                         te->location);
     387           4 :                 tevent_wrapper_pop_use_internal(handler_ev, te->wrapper);
     388             :         }
     389   206649228 :         te->busy = false;
     390             : 
     391   206649228 :         TEVENT_DEBUG(te->event_ctx, TEVENT_DEBUG_TRACE,
     392             :                      "Ending timer event %p \"%s\"\n",
     393             :                      te, te->handler_name);
     394             : 
     395             :         /* The callback was already called when freed from the handler. */
     396   206649228 :         if (!te->destroyed) {
     397       38366 :                 tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
     398             :         }
     399             : 
     400   206649228 :         te->wrapper = NULL;
     401   206649228 :         te->event_ctx = NULL;
     402   206649228 :         talloc_set_destructor(te, NULL);
     403   206649228 :         TALLOC_FREE(te);
     404             : 
     405   206649228 :         if (removed != NULL) {
     406           0 :                 *removed = true;
     407             :         }
     408             : 
     409   200889164 :         return 0;
     410             : }
     411             : /*
     412             :   do a single event loop using the events defined in ev
     413             : 
     414             :   return the delay until the next timed event,
     415             :   or zero if a timed event was triggered
     416             : */
     417   409720087 : struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
     418             : {
     419   409720087 :         struct timeval current_time = tevent_timeval_zero();
     420   409720087 :         struct tevent_timer *te = ev->timer_events;
     421    35150861 :         int ret;
     422             : 
     423   409720087 :         if (!te) {
     424             :                 /* have a default tick time of 30 seconds. This guarantees
     425             :                    that code that uses its own timeout checking will be
     426             :                    able to proceed eventually */
     427    29008425 :                 return tevent_timeval_set(30, 0);
     428             :         }
     429             : 
     430             :         /*
     431             :          * work out the right timeout for the next timed event
     432             :          *
     433             :          * avoid the syscall to gettimeofday() if the timed event should
     434             :          * be triggered directly
     435             :          *
     436             :          * if there's a delay till the next timed event, we're done
     437             :          * with just returning the delay
     438             :          */
     439   380711662 :         if (!tevent_timeval_is_zero(&te->next_event)) {
     440    28972494 :                 struct timeval delay;
     441             : 
     442   175914022 :                 current_time = tevent_timeval_current();
     443             : 
     444   175914022 :                 delay = tevent_timeval_until(&current_time, &te->next_event);
     445   175914022 :                 if (!tevent_timeval_is_zero(&delay)) {
     446   174062352 :                         return delay;
     447             :                 }
     448             :         }
     449             : 
     450             :         /*
     451             :          * ok, we have a timed event that we'll process ...
     452             :          */
     453   206649310 :         ret = tevent_common_invoke_timer_handler(te, current_time, NULL);
     454   206649228 :         if (ret != 0) {
     455           0 :                 tevent_abort(ev, "tevent_common_invoke_timer_handler() failed");
     456             :         }
     457             : 
     458   206649228 :         return tevent_timeval_zero();
     459             : }
     460             : 
     461      139202 : void tevent_timer_set_tag(struct tevent_timer *te, uint64_t tag)
     462             : {
     463      139202 :         if (te == NULL) {
     464           0 :                 return;
     465             :         }
     466             : 
     467      139202 :         te->tag = tag;
     468             : }
     469             : 
     470        6326 : uint64_t tevent_timer_get_tag(const struct tevent_timer *te)
     471             : {
     472        6326 :         if (te == NULL) {
     473           0 :                 return 0;
     474             :         }
     475             : 
     476        6326 :         return te->tag;
     477             : }

Generated by: LCOV version 1.14