TMP: add a ctdb snapshot of current ctdb master (git://git.samba.org/ctdb.git) to...
[obnox/samba/samba-obnox.git] / ctdb / lib / tevent / tevent_util.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    Copyright (C) Jelmer Vernooij 2005
6
7      ** NOTE! The following LGPL license applies to the tevent
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 3 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "replace.h"
26 #include "talloc.h"
27 #include "tevent.h"
28 #include "tevent_internal.h"
29 #include "tevent_util.h"
30 #include <fcntl.h>
31
32 /**
33   return the number of elements in a string list
34 */
35 size_t ev_str_list_length(const char **list)
36 {
37         size_t ret;
38         for (ret=0;list && list[ret];ret++) /* noop */ ;
39         return ret;
40 }
41
42 /**
43   add an entry to a string list
44 */
45 const char **ev_str_list_add(const char **list, const char *s)
46 {
47         size_t len = ev_str_list_length(list);
48         const char **ret;
49
50         ret = talloc_realloc(NULL, list, const char *, len+2);
51         if (ret == NULL) return NULL;
52
53         ret[len] = talloc_strdup(ret, s);
54         if (ret[len] == NULL) return NULL;
55
56         ret[len+1] = NULL;
57
58         return ret;
59 }
60
61
62 /**
63  Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
64  else
65   if SYSV use O_NDELAY
66   if BSD use FNDELAY
67 **/
68
69 int ev_set_blocking(int fd, bool set)
70 {
71         int val;
72 #ifdef O_NONBLOCK
73 #define FLAG_TO_SET O_NONBLOCK
74 #else
75 #ifdef SYSV
76 #define FLAG_TO_SET O_NDELAY
77 #else /* BSD */
78 #define FLAG_TO_SET FNDELAY
79 #endif
80 #endif
81
82         if((val = fcntl(fd, F_GETFL, 0)) == -1)
83                 return -1;
84         if(set) /* Turn blocking on - ie. clear nonblock flag */
85                 val &= ~FLAG_TO_SET;
86         else
87                 val |= FLAG_TO_SET;
88         return fcntl( fd, F_SETFL, val);
89 #undef FLAG_TO_SET
90 }
91
92 static struct timeval tevent_before_wait_ts;
93 static struct timeval tevent_after_wait_ts;
94
95 /*
96  * measure the time difference between multiple arrivals
97  * to the point where we wait for new events to come in
98  *
99  * allows to measure how long it takes to work on a 
100  * event
101  */
102 void tevent_before_wait(struct event_context *ev) {
103
104         struct timeval diff;
105         struct timeval now = tevent_timeval_current();
106
107         if (!tevent_timeval_is_zero(&tevent_after_wait_ts)) {
108                 diff = tevent_timeval_until(&tevent_after_wait_ts, &now);
109                 if (diff.tv_sec > 3) {
110                         tevent_debug(ev, TEVENT_DEBUG_ERROR,  __location__ 
111                                      " Handling event took %d seconds!",
112                                      (int) diff.tv_sec);
113                 }
114         }
115
116         tevent_before_wait_ts = tevent_timeval_current();
117
118 }
119
120 /*
121  * measure how long the select()/epoll() call took
122  *
123  * allows to measure how long we are waiting for new events
124  */
125 void tevent_after_wait(struct event_context *ev) {
126
127         struct timeval diff;
128         struct timeval now = tevent_timeval_current();
129
130         if (!tevent_timeval_is_zero(&tevent_before_wait_ts)) {
131                 diff = tevent_timeval_until(&tevent_before_wait_ts, &now);
132                 if (diff.tv_sec > 3) {
133                         tevent_debug(ev, TEVENT_DEBUG_FATAL,  __location__
134                                      " No event for %d seconds!",
135                                      (int) diff.tv_sec);
136                 }
137         }
138
139         tevent_after_wait_ts = tevent_timeval_current();
140
141 }