forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd.c
3001 lines (2601 loc) · 87.4 KB
/
fd.c
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Server-side file descriptor management
*
* Copyright (C) 2000, 2003 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#ifdef HAVE_LINUX_MAJOR_H
#include <linux/major.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
/* Work around a conflict with Solaris' system list defined in sys/list.h. */
#define list SYSLIST
#define list_next SYSLIST_NEXT
#define list_prev SYSLIST_PREV
#define list_head SYSLIST_HEAD
#define list_tail SYSLIST_TAIL
#define list_move_tail SYSLIST_MOVE_TAIL
#define list_remove SYSLIST_REMOVE
#include <sys/vfs.h>
#undef list
#undef list_next
#undef list_prev
#undef list_head
#undef list_tail
#undef list_move_tail
#undef list_remove
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_SYS_EVENT_H
#include <sys/event.h>
#undef LIST_INIT
#undef LIST_ENTRY
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <sys/stat.h>
#include <sys/time.h>
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif defined(MAJOR_IN_SYSMACROS)
#include <sys/sysmacros.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "object.h"
#include "file.h"
#include "handle.h"
#include "process.h"
#include "request.h"
#include "winternl.h"
#include "winioctl.h"
#include "ddk/wdm.h"
#if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
# include <sys/epoll.h>
# define USE_EPOLL
#elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
# define USE_EPOLL
# define EPOLLIN POLLIN
# define EPOLLOUT POLLOUT
# define EPOLLERR POLLERR
# define EPOLLHUP POLLHUP
# define EPOLL_CTL_ADD 1
# define EPOLL_CTL_DEL 2
# define EPOLL_CTL_MOD 3
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event
{
uint32_t events;
epoll_data_t data;
};
static inline int epoll_create( int size )
{
return syscall( 254 /*NR_epoll_create*/, size );
}
static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
{
return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
}
static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
{
return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
}
#endif /* linux && __i386__ && HAVE_STDINT_H */
#if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
# include <port.h>
# define USE_EVENT_PORTS
#endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
/* Because of the stupid Posix locking semantics, we need to keep
* track of all file descriptors referencing a given file, and not
* close a single one until all the locks are gone (sigh).
*/
/* file descriptor object */
/* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
struct closed_fd
{
struct list entry; /* entry in inode closed list */
int unix_fd; /* the unix file descriptor */
int unlink; /* whether to unlink on close: -1 - implicit FILE_DELETE_ON_CLOSE, 1 - explicit disposition */
char *unix_name; /* name to unlink on close, points to parent fd unix_name */
};
struct fd
{
struct object obj; /* object header */
const struct fd_ops *fd_ops; /* file descriptor operations */
struct inode *inode; /* inode that this fd belongs to */
struct list inode_entry; /* entry in inode fd list */
struct closed_fd *closed; /* structure to store the unix fd at destroy time */
struct object *user; /* object using this file descriptor */
struct list locks; /* list of locks on this fd */
unsigned int access; /* file access (FILE_READ_DATA etc.) */
unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
unsigned int sharing; /* file sharing mode */
char *unix_name; /* unix file name */
WCHAR *nt_name; /* NT file name */
data_size_t nt_namelen; /* length of NT file name */
int unix_fd; /* unix file descriptor */
unsigned int no_fd_status;/* status to return when unix_fd is -1 */
unsigned int cacheable :1;/* can the fd be cached on the client side? */
unsigned int signaled :1; /* is the fd signaled? */
unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
int poll_index; /* index of fd in poll array */
struct async_queue read_q; /* async readers of this fd */
struct async_queue write_q; /* async writers of this fd */
struct async_queue wait_q; /* other async waiters of this fd */
struct completion *completion; /* completion object attached to this fd */
apc_param_t comp_key; /* completion key to set in completion events */
unsigned int comp_flags; /* completion flags */
};
static void fd_dump( struct object *obj, int verbose );
static void fd_destroy( struct object *obj );
static const struct object_ops fd_ops =
{
sizeof(struct fd), /* size */
&no_type, /* type */
fd_dump, /* dump */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
fd_destroy /* destroy */
};
/* device object */
#define DEVICE_HASH_SIZE 7
#define INODE_HASH_SIZE 17
struct device
{
struct object obj; /* object header */
struct list entry; /* entry in device hash list */
dev_t dev; /* device number */
int removable; /* removable device? (or -1 if unknown) */
struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
};
static void device_dump( struct object *obj, int verbose );
static void device_destroy( struct object *obj );
static const struct object_ops device_ops =
{
sizeof(struct device), /* size */
&no_type, /* type */
device_dump, /* dump */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
device_destroy /* destroy */
};
/* inode object */
struct inode
{
struct object obj; /* object header */
struct list entry; /* inode hash list entry */
struct device *device; /* device containing this inode */
ino_t ino; /* inode number */
struct list open; /* list of open file descriptors */
struct list locks; /* list of file locks */
struct list closed; /* list of file descriptors to close at destroy time */
};
static void inode_dump( struct object *obj, int verbose );
static void inode_destroy( struct object *obj );
static const struct object_ops inode_ops =
{
sizeof(struct inode), /* size */
&no_type, /* type */
inode_dump, /* dump */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
inode_destroy /* destroy */
};
/* file lock object */
struct file_lock
{
struct object obj; /* object header */
struct fd *fd; /* fd owning this lock */
struct list fd_entry; /* entry in list of locks on a given fd */
struct list inode_entry; /* entry in inode list of locks */
int shared; /* shared lock? */
file_pos_t start; /* locked region is interval [start;end) */
file_pos_t end;
struct process *process; /* process owning this lock */
struct list proc_entry; /* entry in list of locks owned by the process */
};
static void file_lock_dump( struct object *obj, int verbose );
static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
static const struct object_ops file_lock_ops =
{
sizeof(struct file_lock), /* size */
&no_type, /* type */
file_lock_dump, /* dump */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
file_lock_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
no_destroy /* destroy */
};
#define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
#define FILE_POS_T_MAX (~(file_pos_t)0)
static file_pos_t max_unix_offset = OFF_T_MAX;
#define DUMP_LONG_LONG(val) do { \
if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
else \
fprintf( stderr, "%lx", (unsigned long)(val) ); \
} while (0)
/****************************************************************/
/* timeouts support */
struct timeout_user
{
struct list entry; /* entry in sorted timeout list */
abstime_t when; /* timeout expiry */
timeout_callback callback; /* callback function */
void *private; /* callback private data */
};
static struct list abs_timeout_list = LIST_INIT(abs_timeout_list); /* sorted absolute timeouts list */
static struct list rel_timeout_list = LIST_INIT(rel_timeout_list); /* sorted relative timeouts list */
timeout_t current_time;
timeout_t monotonic_time;
struct _KUSER_SHARED_DATA *user_shared_data = NULL;
static const int user_shared_data_timeout = 16;
static void atomic_store_ulong(volatile ULONG *ptr, ULONG value)
{
/* on x86 there should be total store order guarantees, so volatile is
* enough to ensure the stores aren't reordered by the compiler, and then
* they will always be seen in-order from other CPUs. On other archs, we
* need atomic intrinsics to guarantee that. */
#if defined(__i386__) || defined(__x86_64__)
*ptr = value;
#else
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST);
#endif
}
static void atomic_store_long(volatile LONG *ptr, LONG value)
{
#if defined(__i386__) || defined(__x86_64__)
*ptr = value;
#else
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST);
#endif
}
static void set_user_shared_data_time(void)
{
timeout_t tick_count = monotonic_time / 10000;
static timeout_t last_timezone_update;
timeout_t timezone_bias;
struct tm *tm;
time_t now;
if (monotonic_time - last_timezone_update > TICKS_PER_SEC)
{
now = time( NULL );
tm = gmtime( &now );
timezone_bias = mktime( tm ) - now;
tm = localtime( &now );
if (tm->tm_isdst) timezone_bias -= 3600;
timezone_bias *= TICKS_PER_SEC;
atomic_store_long(&user_shared_data->TimeZoneBias.High2Time, timezone_bias >> 32);
atomic_store_ulong(&user_shared_data->TimeZoneBias.LowPart, timezone_bias);
atomic_store_long(&user_shared_data->TimeZoneBias.High1Time, timezone_bias >> 32);
last_timezone_update = monotonic_time;
}
atomic_store_long(&user_shared_data->SystemTime.High2Time, current_time >> 32);
atomic_store_ulong(&user_shared_data->SystemTime.LowPart, current_time);
atomic_store_long(&user_shared_data->SystemTime.High1Time, current_time >> 32);
atomic_store_long(&user_shared_data->InterruptTime.High2Time, monotonic_time >> 32);
atomic_store_ulong(&user_shared_data->InterruptTime.LowPart, monotonic_time);
atomic_store_long(&user_shared_data->InterruptTime.High1Time, monotonic_time >> 32);
atomic_store_long(&user_shared_data->TickCount.High2Time, tick_count >> 32);
atomic_store_ulong(&user_shared_data->TickCount.LowPart, tick_count);
atomic_store_long(&user_shared_data->TickCount.High1Time, tick_count >> 32);
atomic_store_ulong(&user_shared_data->TickCountLowDeprecated, tick_count);
}
void set_current_time(void)
{
static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
struct timeval now;
gettimeofday( &now, NULL );
current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
monotonic_time = monotonic_counter();
if (user_shared_data) set_user_shared_data_time();
}
/* add a timeout user */
struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
{
struct timeout_user *user;
struct list *ptr;
if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
user->when = timeout_to_abstime( when );
user->callback = func;
user->private = private;
/* Now insert it in the linked list */
if (user->when > 0)
{
LIST_FOR_EACH( ptr, &abs_timeout_list )
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
if (timeout->when >= user->when) break;
}
}
else
{
LIST_FOR_EACH( ptr, &rel_timeout_list )
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
if (timeout->when <= user->when) break;
}
}
list_add_before( ptr, &user->entry );
return user;
}
/* remove a timeout user */
void remove_timeout_user( struct timeout_user *user )
{
list_remove( &user->entry );
free( user );
}
/* return a text description of a timeout for debugging purposes */
const char *get_timeout_str( timeout_t timeout )
{
static char buffer[64];
long secs, nsecs;
if (!timeout) return "0";
if (timeout == TIMEOUT_INFINITE) return "infinite";
if (timeout < 0) /* relative */
{
secs = -timeout / TICKS_PER_SEC;
nsecs = -timeout % TICKS_PER_SEC;
sprintf( buffer, "+%ld.%07ld", secs, nsecs );
}
else /* absolute */
{
secs = (timeout - current_time) / TICKS_PER_SEC;
nsecs = (timeout - current_time) % TICKS_PER_SEC;
if (nsecs < 0)
{
nsecs += TICKS_PER_SEC;
secs--;
}
if (secs >= 0)
sprintf( buffer, "%x%08x (+%ld.%07ld)",
(unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
else
sprintf( buffer, "%x%08x (-%ld.%07ld)",
(unsigned int)(timeout >> 32), (unsigned int)timeout,
-(secs + 1), TICKS_PER_SEC - nsecs );
}
return buffer;
}
/****************************************************************/
/* poll support */
static struct fd **poll_users; /* users array */
static struct pollfd *pollfd; /* poll fd array */
static int nb_users; /* count of array entries actually in use */
static int active_users; /* current number of active users */
static int allocated_users; /* count of allocated entries in the array */
static struct fd **freelist; /* list of free entries in the array */
static int get_next_timeout(void);
static inline void fd_poll_event( struct fd *fd, int event )
{
fd->fd_ops->poll_event( fd, event );
}
#ifdef USE_EPOLL
static int epoll_fd = -1;
static inline void init_epoll(void)
{
epoll_fd = epoll_create( 128 );
}
/* set the events that epoll waits for on this fd; helper for set_fd_events */
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
{
struct epoll_event ev;
int ctl;
if (epoll_fd == -1) return;
if (events == -1) /* stop waiting on this fd completely */
{
if (pollfd[user].fd == -1) return; /* already removed */
ctl = EPOLL_CTL_DEL;
}
else if (pollfd[user].fd == -1)
{
ctl = EPOLL_CTL_ADD;
}
else
{
if (pollfd[user].events == events) return; /* nothing to do */
ctl = EPOLL_CTL_MOD;
}
ev.events = events;
memset(&ev.data, 0, sizeof(ev.data));
ev.data.u32 = user;
if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
{
if (errno == ENOMEM) /* not enough memory, give up on epoll */
{
close( epoll_fd );
epoll_fd = -1;
}
else perror( "epoll_ctl" ); /* should not happen */
}
}
static inline void remove_epoll_user( struct fd *fd, int user )
{
if (epoll_fd == -1) return;
if (pollfd[user].fd != -1)
{
struct epoll_event dummy;
epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
}
}
static inline void main_loop_epoll(void)
{
int i, ret, timeout;
struct epoll_event events[128];
assert( POLLIN == EPOLLIN );
assert( POLLOUT == EPOLLOUT );
assert( POLLERR == EPOLLERR );
assert( POLLHUP == EPOLLHUP );
if (epoll_fd == -1) return;
while (active_users)
{
timeout = get_next_timeout();
if (!active_users) break; /* last user removed by a timeout */
if (epoll_fd == -1) break; /* an error occurred with epoll */
ret = epoll_wait( epoll_fd, events, ARRAY_SIZE( events ), timeout );
set_current_time();
/* put the events into the pollfd array first, like poll does */
for (i = 0; i < ret; i++)
{
int user = events[i].data.u32;
pollfd[user].revents = events[i].events;
}
/* read events from the pollfd array, as set_fd_events may modify them */
for (i = 0; i < ret; i++)
{
int user = events[i].data.u32;
if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
}
}
}
#elif defined(HAVE_KQUEUE)
static int kqueue_fd = -1;
static inline void init_epoll(void)
{
kqueue_fd = kqueue();
}
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
{
struct kevent ev[2];
if (kqueue_fd == -1) return;
EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)(long)user );
EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)(long)user );
if (events == -1) /* stop waiting on this fd completely */
{
if (pollfd[user].fd == -1) return; /* already removed */
ev[0].flags |= EV_DELETE;
ev[1].flags |= EV_DELETE;
}
else if (pollfd[user].fd == -1)
{
ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
}
else
{
if (pollfd[user].events == events) return; /* nothing to do */
ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
}
if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
{
if (errno == ENOMEM) /* not enough memory, give up on kqueue */
{
close( kqueue_fd );
kqueue_fd = -1;
}
else perror( "kevent" ); /* should not happen */
}
}
static inline void remove_epoll_user( struct fd *fd, int user )
{
if (kqueue_fd == -1) return;
if (pollfd[user].fd != -1)
{
struct kevent ev[2];
EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
}
}
static inline void main_loop_epoll(void)
{
int i, ret, timeout;
struct kevent events[128];
if (kqueue_fd == -1) return;
while (active_users)
{
timeout = get_next_timeout();
if (!active_users) break; /* last user removed by a timeout */
if (kqueue_fd == -1) break; /* an error occurred with kqueue */
if (timeout != -1)
{
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), &ts );
}
else ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), NULL );
set_current_time();
/* put the events into the pollfd array first, like poll does */
for (i = 0; i < ret; i++)
{
long user = (long)events[i].udata;
pollfd[user].revents = 0;
}
for (i = 0; i < ret; i++)
{
long user = (long)events[i].udata;
if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
}
/* read events from the pollfd array, as set_fd_events may modify them */
for (i = 0; i < ret; i++)
{
long user = (long)events[i].udata;
if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
pollfd[user].revents = 0;
}
}
}
#elif defined(USE_EVENT_PORTS)
static int port_fd = -1;
static inline void init_epoll(void)
{
port_fd = port_create();
}
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
{
int ret;
if (port_fd == -1) return;
if (events == -1) /* stop waiting on this fd completely */
{
if (pollfd[user].fd == -1) return; /* already removed */
port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
}
else if (pollfd[user].fd == -1)
{
ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
}
else
{
if (pollfd[user].events == events) return; /* nothing to do */
ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
}
if (ret == -1)
{
if (errno == ENOMEM) /* not enough memory, give up on port_associate */
{
close( port_fd );
port_fd = -1;
}
else perror( "port_associate" ); /* should not happen */
}
}
static inline void remove_epoll_user( struct fd *fd, int user )
{
if (port_fd == -1) return;
if (pollfd[user].fd != -1)
{
port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
}
}
static inline void main_loop_epoll(void)
{
int i, nget, ret, timeout;
port_event_t events[128];
if (port_fd == -1) return;
while (active_users)
{
timeout = get_next_timeout();
nget = 1;
if (!active_users) break; /* last user removed by a timeout */
if (port_fd == -1) break; /* an error occurred with event completion */
if (timeout != -1)
{
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, &ts );
}
else ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, NULL );
if (ret == -1) break; /* an error occurred with event completion */
set_current_time();
/* put the events into the pollfd array first, like poll does */
for (i = 0; i < nget; i++)
{
long user = (long)events[i].portev_user;
pollfd[user].revents = events[i].portev_events;
}
/* read events from the pollfd array, as set_fd_events may modify them */
for (i = 0; i < nget; i++)
{
long user = (long)events[i].portev_user;
if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
/* if we are still interested, reassociate the fd */
if (pollfd[user].fd != -1) {
port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
}
}
}
}
#else /* HAVE_KQUEUE */
static inline void init_epoll(void) { }
static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
static inline void remove_epoll_user( struct fd *fd, int user ) { }
static inline void main_loop_epoll(void) { }
#endif /* USE_EPOLL */
/* add a user in the poll array and return its index, or -1 on failure */
static int add_poll_user( struct fd *fd )
{
int ret;
if (freelist)
{
ret = freelist - poll_users;
freelist = (struct fd **)poll_users[ret];
}
else
{
if (nb_users == allocated_users)
{
struct fd **newusers;
struct pollfd *newpoll;
int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
{
if (allocated_users)
poll_users = newusers;
else
free( newusers );
return -1;
}
poll_users = newusers;
pollfd = newpoll;
if (!allocated_users) init_epoll();
allocated_users = new_count;
}
ret = nb_users++;
}
pollfd[ret].fd = -1;
pollfd[ret].events = 0;
pollfd[ret].revents = 0;
poll_users[ret] = fd;
active_users++;
return ret;
}
/* remove a user from the poll list */
static void remove_poll_user( struct fd *fd, int user )
{
assert( user >= 0 );
assert( poll_users[user] == fd );
remove_epoll_user( fd, user );
pollfd[user].fd = -1;
pollfd[user].events = 0;
pollfd[user].revents = 0;
poll_users[user] = (struct fd *)freelist;
freelist = &poll_users[user];
active_users--;
}
/* process pending timeouts and return the time until the next timeout, in milliseconds */
static int get_next_timeout(void)
{
int ret = user_shared_data ? user_shared_data_timeout : -1;
if (!list_empty( &abs_timeout_list ) || !list_empty( &rel_timeout_list ))
{
struct list expired_list, *ptr;
/* first remove all expired timers from the list */
list_init( &expired_list );
while ((ptr = list_head( &abs_timeout_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
if (timeout->when <= current_time)
{
list_remove( &timeout->entry );
list_add_tail( &expired_list, &timeout->entry );
}
else break;
}
while ((ptr = list_head( &rel_timeout_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
if (-timeout->when <= monotonic_time)
{
list_remove( &timeout->entry );
list_add_tail( &expired_list, &timeout->entry );
}
else break;
}
/* now call the callback for all the removed timers */
while ((ptr = list_head( &expired_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
list_remove( &timeout->entry );
timeout->callback( timeout->private );
free( timeout );
}
if ((ptr = list_head( &abs_timeout_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
timeout_t diff = (timeout->when - current_time + 9999) / 10000;
if (diff > INT_MAX) diff = INT_MAX;
else if (diff < 0) diff = 0;
if (ret == -1 || diff < ret) ret = diff;
}
if ((ptr = list_head( &rel_timeout_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
timeout_t diff = (-timeout->when - monotonic_time + 9999) / 10000;
if (diff > INT_MAX) diff = INT_MAX;
else if (diff < 0) diff = 0;
if (ret == -1 || diff < ret) ret = diff;
}
}
return ret;
}
/* server main poll() loop */
void main_loop(void)
{
int i, ret, timeout;
set_current_time();
server_start_time = current_time;
main_loop_epoll();
/* fall through to normal poll loop */
while (active_users)
{
timeout = get_next_timeout();
if (!active_users) break; /* last user removed by a timeout */