LCOV - code coverage report
Current view: top level - lib/tevent - tevent_fd.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 58 72 80.6 %
Date: 2021-09-23 10:06:22 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    common events code for fd events
       5             : 
       6             :    Copyright (C) Stefan Metzmacher 2009
       7             : 
       8             :      ** NOTE! The following LGPL license applies to the tevent
       9             :      ** library. This does NOT imply that all of Samba is released
      10             :      ** under the LGPL
      11             : 
      12             :    This library is free software; you can redistribute it and/or
      13             :    modify it under the terms of the GNU Lesser General Public
      14             :    License as published by the Free Software Foundation; either
      15             :    version 3 of the License, or (at your option) any later version.
      16             : 
      17             :    This library is distributed in the hope that it will be useful,
      18             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      19             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      20             :    Lesser General Public License for more details.
      21             : 
      22             :    You should have received a copy of the GNU Lesser General Public
      23             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      24             : */
      25             : 
      26             : #include "replace.h"
      27             : #define TEVENT_DEPRECATED 1
      28             : #include "tevent.h"
      29             : #include "tevent_internal.h"
      30             : #include "tevent_util.h"
      31             : 
      32    41996019 : int tevent_common_fd_destructor(struct tevent_fd *fde)
      33             : {
      34    41996019 :         if (fde->destroyed) {
      35     7383128 :                 tevent_common_check_double_free(fde, "tevent_fd double free");
      36     7383128 :                 goto done;
      37             :         }
      38    34612891 :         fde->destroyed = true;
      39             : 
      40    34612891 :         if (fde->event_ctx) {
      41     9944998 :                 tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_DETACH);
      42     9944998 :                 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
      43             :         }
      44             : 
      45    34612891 :         if (fde->close_fn) {
      46     6198214 :                 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
      47     6198214 :                 fde->fd = -1;
      48     6198214 :                 fde->close_fn = NULL;
      49             :         }
      50             : 
      51    34612891 :         fde->event_ctx = NULL;
      52    41996019 : done:
      53    41996019 :         if (fde->busy) {
      54    16212696 :                 return -1;
      55             :         }
      56    25707186 :         fde->wrapper = NULL;
      57             : 
      58    25707186 :         return 0;
      59             : }
      60             : 
      61    34636168 : struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
      62             :                                        int fd, uint16_t flags,
      63             :                                        tevent_fd_handler_t handler,
      64             :                                        void *private_data,
      65             :                                        const char *handler_name,
      66             :                                        const char *location)
      67             : {
      68             :         struct tevent_fd *fde;
      69             : 
      70             :         /* tevent will crash later on select() if we save
      71             :          * a negative file descriptor. Better to fail here
      72             :          * so that consumers will be able to debug it
      73             :          */
      74    34636168 :         if (fd < 0) return NULL;
      75             : 
      76    34636168 :         fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
      77    34636168 :         if (!fde) return NULL;
      78             : 
      79    34636168 :         *fde = (struct tevent_fd) {
      80             :                 .event_ctx      = ev,
      81             :                 .fd             = fd,
      82             :                 .flags          = flags,
      83             :                 .handler        = handler,
      84             :                 .private_data   = private_data,
      85             :                 .handler_name   = handler_name,
      86             :                 .location       = location,
      87             :         };
      88             : 
      89    34636168 :         tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_ATTACH);
      90    34636168 :         DLIST_ADD(ev->fd_events, fde);
      91             : 
      92    34636168 :         talloc_set_destructor(fde, tevent_common_fd_destructor);
      93             : 
      94             : 
      95    34636168 :         return fde;
      96             : }
      97    81400044 : uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
      98             : {
      99    81400044 :         return fde->flags;
     100             : }
     101             : 
     102           0 : void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
     103             : {
     104           0 :         if (fde->flags == flags) return;
     105           0 :         fde->flags = flags;
     106             : }
     107             : 
     108     6252452 : void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
     109             :                                    tevent_fd_close_fn_t close_fn)
     110             : {
     111     6252452 :         fde->close_fn = close_fn;
     112     6252452 : }
     113             : 
     114   152648365 : int tevent_common_invoke_fd_handler(struct tevent_fd *fde, uint16_t flags,
     115             :                                     bool *removed)
     116             : {
     117   152648365 :         struct tevent_context *handler_ev = fde->event_ctx;
     118             : 
     119   152648365 :         if (removed != NULL) {
     120           0 :                 *removed = false;
     121             :         }
     122             : 
     123   152648365 :         if (fde->event_ctx == NULL) {
     124           0 :                 return 0;
     125             :         }
     126             : 
     127   152648365 :         fde->busy = true;
     128   152648365 :         if (fde->wrapper != NULL) {
     129           4 :                 handler_ev = fde->wrapper->wrap_ev;
     130             : 
     131           4 :                 tevent_wrapper_push_use_internal(handler_ev, fde->wrapper);
     132           4 :                 fde->wrapper->ops->before_fd_handler(
     133           0 :                                         fde->wrapper->wrap_ev,
     134           0 :                                         fde->wrapper->private_state,
     135           0 :                                         fde->wrapper->main_ev,
     136             :                                         fde,
     137             :                                         flags,
     138             :                                         fde->handler_name,
     139             :                                         fde->location);
     140             :         }
     141   152648365 :         tevent_trace_fd_callback(fde->event_ctx, fde, TEVENT_EVENT_TRACE_BEFORE_HANDLER);
     142   152648365 :         fde->handler(handler_ev, fde, flags, fde->private_data);
     143   152581487 :         if (fde->wrapper != NULL) {
     144           4 :                 fde->wrapper->ops->after_fd_handler(
     145           0 :                                         fde->wrapper->wrap_ev,
     146           0 :                                         fde->wrapper->private_state,
     147           0 :                                         fde->wrapper->main_ev,
     148             :                                         fde,
     149             :                                         flags,
     150             :                                         fde->handler_name,
     151             :                                         fde->location);
     152           4 :                 tevent_wrapper_pop_use_internal(handler_ev, fde->wrapper);
     153             :         }
     154   152581487 :         fde->busy = false;
     155             : 
     156   152581487 :         if (fde->destroyed) {
     157     8838925 :                 talloc_set_destructor(fde, NULL);
     158     8838925 :                 TALLOC_FREE(fde);
     159     8838925 :                 if (removed != NULL) {
     160           0 :                         *removed = true;
     161             :                 }
     162             :         }
     163             : 
     164   151035574 :         return 0;
     165             : }
     166             : 
     167           5 : void tevent_fd_set_tag(struct tevent_fd *fde, uint64_t tag)
     168             : {
     169           5 :         if (fde == NULL) {
     170           0 :                 return;
     171             :         }
     172             : 
     173           5 :         fde->tag = tag;
     174             : }
     175             : 
     176          15 : uint64_t tevent_fd_get_tag(const struct tevent_fd *fde)
     177             : {
     178          15 :         if (fde == NULL) {
     179           0 :                 return 0;
     180             :         }
     181             : 
     182          15 :         return fde->tag;
     183             : }

Generated by: LCOV version 1.13