fs-poll.c
Go to the documentation of this file.
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "uv-common.h"
24 
25 #ifdef _WIN32
26 #include "win/internal.h"
27 #include "win/handle-inl.h"
28 #define uv__make_close_pending(h) uv_want_endgame((h)->loop, (h))
29 #else
30 #include "unix/internal.h"
31 #endif
32 
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 struct poll_ctx {
40  unsigned int interval;
45  uv_fs_t fs_req; /* TODO(bnoordhuis) mark fs_req internal */
47  struct poll_ctx* previous; /* context from previous start()..stop() period */
48  char path[1]; /* variable length */
49 };
50 
51 static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b);
52 static void poll_cb(uv_fs_t* req);
53 static void timer_cb(uv_timer_t* timer);
54 static void timer_close_cb(uv_handle_t* handle);
55 
57 
58 
60  uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_POLL);
61  handle->poll_ctx = NULL;
62  return 0;
63 }
64 
65 
68  const char* path,
69  unsigned int interval) {
70  struct poll_ctx* ctx;
71  uv_loop_t* loop;
72  size_t len;
73  int err;
74 
76  return 0;
77 
78  loop = handle->loop;
79  len = strlen(path);
80  ctx = uv__calloc(1, sizeof(*ctx) + len);
81 
82  if (ctx == NULL)
83  return UV_ENOMEM;
84 
85  ctx->loop = loop;
86  ctx->poll_cb = cb;
87  ctx->interval = interval ? interval : 1;
88  ctx->start_time = uv_now(loop);
89  ctx->parent_handle = handle;
90  memcpy(ctx->path, path, len + 1);
91 
92  err = uv_timer_init(loop, &ctx->timer_handle);
93  if (err < 0)
94  goto error;
95 
96  ctx->timer_handle.flags |= UV_HANDLE_INTERNAL;
97  uv__handle_unref(&ctx->timer_handle);
98 
99  err = uv_fs_stat(loop, &ctx->fs_req, ctx->path, poll_cb);
100  if (err < 0)
101  goto error;
102 
103  if (handle->poll_ctx != NULL)
104  ctx->previous = handle->poll_ctx;
105  handle->poll_ctx = ctx;
107 
108  return 0;
109 
110 error:
111  uv__free(ctx);
112  return err;
113 }
114 
115 
117  struct poll_ctx* ctx;
118 
120  return 0;
121 
122  ctx = handle->poll_ctx;
123  assert(ctx != NULL);
124  assert(ctx->parent_handle == handle);
125 
126  /* Close the timer if it's active. If it's inactive, there's a stat request
127  * in progress and poll_cb will take care of the cleanup.
128  */
129  if (uv_is_active((uv_handle_t*)&ctx->timer_handle))
130  uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
131 
133 
134  return 0;
135 }
136 
137 
139  struct poll_ctx* ctx;
140  size_t required_len;
141 
142  if (!uv_is_active((uv_handle_t*)handle)) {
143  *size = 0;
144  return UV_EINVAL;
145  }
146 
147  ctx = handle->poll_ctx;
148  assert(ctx != NULL);
149 
150  required_len = strlen(ctx->path);
151  if (required_len >= *size) {
152  *size = required_len + 1;
153  return UV_ENOBUFS;
154  }
155 
156  memcpy(buffer, ctx->path, required_len);
157  *size = required_len;
158  buffer[required_len] = '\0';
159 
160  return 0;
161 }
162 
163 
166 
167  if (handle->poll_ctx == NULL)
169 }
170 
171 
172 static void timer_cb(uv_timer_t* timer) {
173  struct poll_ctx* ctx;
174 
176  assert(ctx->parent_handle != NULL);
177  assert(ctx->parent_handle->poll_ctx == ctx);
178  ctx->start_time = uv_now(ctx->loop);
179 
180  if (uv_fs_stat(ctx->loop, &ctx->fs_req, ctx->path, poll_cb))
181  abort();
182 }
183 
184 
185 static void poll_cb(uv_fs_t* req) {
187  struct poll_ctx* ctx;
190 
191  ctx = container_of(req, struct poll_ctx, fs_req);
192  handle = ctx->parent_handle;
193 
195  goto out;
196 
197  if (req->result != 0) {
198  if (ctx->busy_polling != req->result) {
199  ctx->poll_cb(ctx->parent_handle,
200  req->result,
201  &ctx->statbuf,
202  &zero_statbuf);
203  ctx->busy_polling = req->result;
204  }
205  goto out;
206  }
207 
208  statbuf = &req->statbuf;
209 
210  if (ctx->busy_polling != 0)
211  if (ctx->busy_polling < 0 || !statbuf_eq(&ctx->statbuf, statbuf))
212  ctx->poll_cb(ctx->parent_handle, 0, &ctx->statbuf, statbuf);
213 
214  ctx->statbuf = *statbuf;
215  ctx->busy_polling = 1;
216 
217 out:
219 
221  uv_close((uv_handle_t*)&ctx->timer_handle, timer_close_cb);
222  return;
223  }
224 
225  /* Reschedule timer, subtract the delay from doing the stat(). */
226  interval = ctx->interval;
227  interval -= (uv_now(ctx->loop) - ctx->start_time) % interval;
228 
229  if (uv_timer_start(&ctx->timer_handle, timer_cb, interval, 0))
230  abort();
231 }
232 
233 
235  struct poll_ctx* ctx;
236  struct poll_ctx* it;
237  struct poll_ctx* last;
239 
241  handle = ctx->parent_handle;
242  if (ctx == handle->poll_ctx) {
243  handle->poll_ctx = ctx->previous;
244  if (handle->poll_ctx == NULL && uv__is_closing(handle))
246  } else {
247  for (last = handle->poll_ctx, it = last->previous;
248  it != ctx;
249  last = it, it = it->previous) {
250  assert(last->previous != NULL);
251  }
252  last->previous = ctx->previous;
253  }
254  uv__free(ctx);
255 }
256 
257 
258 static int statbuf_eq(const uv_stat_t* a, const uv_stat_t* b) {
259  return a->st_ctim.tv_nsec == b->st_ctim.tv_nsec
260  && a->st_mtim.tv_nsec == b->st_mtim.tv_nsec
261  && a->st_birthtim.tv_nsec == b->st_birthtim.tv_nsec
262  && a->st_ctim.tv_sec == b->st_ctim.tv_sec
263  && a->st_mtim.tv_sec == b->st_mtim.tv_sec
264  && a->st_birthtim.tv_sec == b->st_birthtim.tv_sec
265  && a->st_size == b->st_size
266  && a->st_mode == b->st_mode
267  && a->st_uid == b->st_uid
268  && a->st_gid == b->st_gid
269  && a->st_ino == b->st_ino
270  && a->st_dev == b->st_dev
271  && a->st_flags == b->st_flags
272  && a->st_gen == b->st_gen;
273 }
274 
275 
276 #if defined(_WIN32)
277 
278 #include "win/internal.h"
279 #include "win/handle-inl.h"
280 
282  assert(handle->flags & UV_HANDLE_CLOSING);
283  assert(!(handle->flags & UV_HANDLE_CLOSED));
285 }
286 
287 #endif /* _WIN32 */
uv__is_closing
#define uv__is_closing(h)
Definition: uv-common.h:238
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
poll_ctx::statbuf
uv_stat_t statbuf
Definition: fs-poll.c:46
timer_cb
static void timer_cb(uv_timer_t *timer)
Definition: fs-poll.c:172
UV_HANDLE_CLOSED
@ UV_HANDLE_CLOSED
Definition: uv-common.h:67
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
regen-readme.it
it
Definition: regen-readme.py:15
ctx
Definition: benchmark-async.c:30
poll_ctx::poll_cb
uv_fs_poll_cb poll_cb
Definition: fs-poll.c:43
uv_now
UV_EXTERN uint64_t uv_now(const uv_loop_t *)
Definition: uv-common.c:537
uv_fs_stat
UV_EXTERN int uv_fs_stat(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
Definition: unix/fs.c:1954
uv-common.h
string.h
error
grpc_error_handle error
Definition: retry_filter.cc:499
error_ref_leak.err
err
Definition: error_ref_leak.py:35
poll_ctx::busy_polling
int busy_polling
Definition: fs-poll.c:39
uv__handle_close
#define uv__handle_close(handle)
Definition: handle-inl.h:76
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
check_documentation.path
path
Definition: check_documentation.py:57
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
poll_ctx::previous
struct poll_ctx * previous
Definition: fs-poll.c:47
poll_ctx
Definition: fs-poll.c:37
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
poll_ctx::loop
uv_loop_t * loop
Definition: fs-poll.c:42
uv_fs_s
Definition: uv.h:1294
uv__handle_unref
#define uv__handle_unref(h)
Definition: uv-common.h:266
uv_fs_poll_start
int uv_fs_poll_start(uv_fs_poll_t *handle, uv_fs_poll_cb cb, const char *path, unsigned int interval)
Definition: fs-poll.c:66
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
timer_close_cb
static void timer_close_cb(uv_handle_t *handle)
Definition: fs-poll.c:234
poll_ctx::start_time
uint64_t start_time
Definition: fs-poll.c:41
zero_statbuf
static uv_stat_t zero_statbuf
Definition: fs-poll.c:56
fs_req
Definition: test-thread.c:41
req
static uv_connect_t req
Definition: test-connection-fail.c:30
uv__make_close_pending
void uv__make_close_pending(uv_handle_t *handle)
Definition: unix/core.c:212
uv_fs_poll_stop
int uv_fs_poll_stop(uv_fs_poll_t *handle)
Definition: fs-poll.c:116
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
uv_is_active
UV_EXTERN int uv_is_active(const uv_handle_t *handle)
Definition: unix/core.c:418
UV_HANDLE_INTERNAL
@ UV_HANDLE_INTERNAL
Definition: uv-common.h:70
poll_ctx::path
char path[1]
Definition: fs-poll.c:48
statbuf_eq
static int statbuf_eq(const uv_stat_t *a, const uv_stat_t *b)
Definition: fs-poll.c:258
uv_fs_poll_init
int uv_fs_poll_init(uv_loop_t *loop, uv_fs_poll_t *handle)
Definition: fs-poll.c:59
uv__calloc
void * uv__calloc(size_t count, size_t size)
Definition: uv-common.c:92
uv__free
void uv__free(void *ptr)
Definition: uv-common.c:81
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
uv_timer_s
Definition: uv.h:850
poll_cb
static void poll_cb(uv_fs_t *req)
Definition: fs-poll.c:185
uv__handle_init
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:284
poll_ctx::timer_handle
uv_timer_t timer_handle
Definition: fs-poll.c:44
uv.h
ctx::loop
uv_loop_t loop
Definition: benchmark-async.c:31
UV_HANDLE_CLOSING
@ UV_HANDLE_CLOSING
Definition: uv-common.h:66
uv_fs_poll_cb
void(* uv_fs_poll_cb)(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr)
Definition: uv.h:371
internal.h
uv_stat_t
Definition: uv.h:346
uv_fs_poll_s
Definition: uv.h:1544
poll_ctx::interval
unsigned int interval
Definition: fs-poll.c:40
uv_fs_poll_getpath
int uv_fs_poll_getpath(uv_fs_poll_t *handle, char *buffer, size_t *size)
Definition: fs-poll.c:138
uv__fs_poll_close
void uv__fs_poll_close(uv_fs_poll_t *handle)
Definition: fs-poll.c:164
poll_ctx::fs_req
uv_fs_t fs_req
Definition: fs-poll.c:45
uv_fs_req_cleanup
UV_EXTERN void uv_fs_req_cleanup(uv_fs_t *req)
Definition: unix/fs.c:2024
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
poll_ctx::parent_handle
uv_fs_poll_t * parent_handle
Definition: fs-poll.c:38
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
uv_loop_s
Definition: uv.h:1767
internal.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
uv__handle_start
#define uv__handle_start(h)
Definition: uv-common.h:241
timer_handle
static uv_timer_t timer_handle
Definition: benchmark-loop-count.c:32
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv__fs_poll_endgame
void uv__fs_poll_endgame(uv_loop_t *loop, uv_fs_poll_t *handle)
handle-inl.h
uv__handle_stop
#define uv__handle_stop(h)
Definition: uv-common.h:249
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:25