Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fhandler_timerfd.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7e22329677f011e77d4ea4209fa78165570753c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* fhandler_timerfd.cc: fhandler for timerfd, public timerfd API

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "path.h"
#include "fhandler.h"
#include "pinfo.h"
#include "dtable.h"
#include "cygheap.h"
#include "timerfd.h"
#include <sys/timerfd.h>
#include <cygwin/signal.h>

fhandler_timerfd::fhandler_timerfd () :
  fhandler_base ()
{
}

char *
fhandler_timerfd::get_proc_fd_name (char *buf)
{
  return strcpy (buf, "anon_inode:[timerfd]");
}

/* The timers connected to a descriptor are stored on the cygheap
   together with their fhandler. */

int
fhandler_timerfd::timerfd (clockid_t clock_id, int flags)
{
  timerfd_tracker *tfd = (timerfd_tracker *)
			 ccalloc (HEAP_FHANDLER, 1, sizeof (timerfd_tracker));
  if (!tfd)
    {
      set_errno (ENOMEM);
      return -1;
    }
  new (tfd) timerfd_tracker ();
  int ret = tfd->create (clock_id);
  if (ret < 0)
    {
      cfree (tfd);
      set_errno (-ret);
      return -1;
    }
  if (flags & TFD_NONBLOCK)
    set_nonblocking (true);
  if (flags & TFD_CLOEXEC)
    set_close_on_exec (true);
  nohandle (true);
  set_unique_id ();
  set_ino (get_unique_id ());
  set_flags (O_RDWR | O_BINARY);
  timerid = (timer_t) tfd;
  return 0;
}

int
fhandler_timerfd::settime (int flags, const struct itimerspec *new_value,
			   struct itimerspec *old_value)
{
  int ret = -1;

  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      ret = tfd->settime (flags, new_value, old_value);
      if (ret < 0)
	{
	  set_errno (-ret);
	  ret = -1;
	}
    }
  __except (EFAULT) {}
  __endtry
  return ret;
}

int
fhandler_timerfd::gettime (struct itimerspec *ovalue)
{
  int ret = -1;

  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      ret = tfd->gettime (ovalue);
      if (ret < 0)
	{
	  set_errno (-ret);
	  ret = -1;
	}
    }
  __except (EFAULT) {}
  __endtry
  return ret;
}

int __reg2
fhandler_timerfd::fstat (struct stat *buf)
{
  int ret = fhandler_base::fstat (buf);
  if (!ret)
    {
      buf->st_mode = S_IRUSR | S_IWUSR;
      buf->st_dev = FH_TIMERFD;
      buf->st_ino = get_unique_id ();
    }
  return ret;
}

void __reg3
fhandler_timerfd::read (void *ptr, size_t& len)
{
  if (len < sizeof (LONG64))
    {
      set_errno (EINVAL);
      len = (size_t) -1;
      return;
    }

  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      LONG64 ret = tfd->wait (is_nonblocking ());
      if (ret < 0)
	{
	  set_errno (-ret);
	  __leave;
	}
      *(PLONG64) ptr = ret;
      len = sizeof (LONG64);
      return;
    }
  __except (EFAULT) {}
  __endtry
  len = (size_t) -1;
  return;
}

ssize_t __stdcall
fhandler_timerfd::write (const void *, size_t)
{
  set_errno (EINVAL);
  return -1;
}

HANDLE
fhandler_timerfd::get_timerfd_handle ()
{
  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      return tfd->get_timerfd_handle ();
    }
  __except (EFAULT) {}
  __endtry
  return NULL;
}

int
fhandler_timerfd::dup (fhandler_base *child, int flags)
{
  int ret = fhandler_base::dup (child, flags);

  if (!ret)
    {
      fhandler_timerfd *fhc = (fhandler_timerfd *) child;
      __try
	{
	  timerfd_tracker *tfd = (timerfd_tracker *) fhc->timerid;
	  tfd->dup ();
	  ret = 0;
	}
      __except (EFAULT) {}
      __endtry
    }
  return ret;
}

int
fhandler_timerfd::ioctl (unsigned int cmd, void *p)
{
  int ret = -1;
  uint64_t exp_cnt;

  switch (cmd)
    {
    case TFD_IOC_SET_TICKS:
      __try
	{
	  timerfd_tracker *tfd = (timerfd_tracker *) timerid;

	  exp_cnt = *(uint64_t *) p;
	  ret = tfd->ioctl_set_ticks (exp_cnt);
	  if (ret < 0)
	    set_errno (-ret);
	}
      __except (EFAULT) {}
      __endtry
      break;
    default:
      ret = fhandler_base::ioctl (cmd, p);
      break;
    }
  syscall_printf ("%d = ioctl_timerfd(%x, %p)", ret, cmd, p);
  return ret;
}

void
fhandler_timerfd::fixup_after_fork (HANDLE)
{
  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      tfd->fixup_after_fork ();
    }
  __except (EFAULT) {}
  __endtry
}

void
fhandler_timerfd::fixup_after_exec ()
{
  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      tfd->init_fixup_after_fork_exec ();
      if (close_on_exec ())
	timerfd_tracker::dtor (tfd);
      else
	tfd->fixup_after_exec ();
    }
  __except (EFAULT) {}
  __endtry
}

int
fhandler_timerfd::close ()
{
  int ret = -1;

  __try
    {
      timerfd_tracker *tfd = (timerfd_tracker *) timerid;
      timerfd_tracker::dtor (tfd);
      ret = 0;
    }
  __except (EFAULT) {}
  __endtry
  return ret;
}

extern "C" int
timerfd_create (clockid_t clock_id, int flags)
{
  int ret = -1;
  fhandler_timerfd *fh;

  debug_printf ("timerfd_create (%lu, %y)", clock_id, flags);

  if (clock_id != CLOCK_REALTIME
      && clock_id != CLOCK_MONOTONIC
      && clock_id != CLOCK_BOOTTIME)
    {
      set_errno (EINVAL);
      goto done;
    }
  if ((flags & ~(TFD_NONBLOCK | TFD_CLOEXEC)) != 0)
    {
      set_errno (EINVAL);
      goto done;
    }

    {
      /* Create new timerfd descriptor. */
      cygheap_fdnew fd;

      if (fd < 0)
        goto done;
      fh = (fhandler_timerfd *) build_fh_dev (*timerfd_dev);
      if (fh && fh->timerfd (clock_id, flags) == 0)
        {
          fd = fh;
          if (fd <= 2)
            set_std_handle (fd);
          ret = fd;
        }
      else
        delete fh;
    }

done:
  syscall_printf ("%R = timerfd_create (%lu, %y)", ret, clock_id, flags);
  return ret;
}

extern "C" int
timerfd_settime (int fd_in, int flags, const struct itimerspec *value,
		 struct itimerspec *ovalue)
{
  if ((flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) != 0)
    {
      set_errno (EINVAL);
      return -1;
    }

  cygheap_fdget fd (fd_in);
  if (fd < 0)
    return -1;
  fhandler_timerfd *fh = fd->is_timerfd ();
  if (!fh)
    {
      set_errno (EINVAL);
      return -1;
    }
  return fh->settime (flags, value, ovalue);
}

extern "C" int
timerfd_gettime (int fd_in, struct itimerspec *ovalue)
{
  cygheap_fdget fd (fd_in);
  if (fd < 0)
    return -1;
  fhandler_timerfd *fh = fd->is_timerfd ();
  if (!fh)
    {
      set_errno (EINVAL);
      return -1;
    }
  return fh->gettime (ovalue);
}