2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <sys/resource.h>
23 #include <common/defaults.h>
24 #include <common/error.h>
28 unsigned int poll_max_size
;
31 * Create pollfd data structure.
33 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
35 if (events
== NULL
|| size
<= 0) {
36 ERR("Wrong arguments for poll create");
40 /* Don't bust the limit here */
41 if (size
> poll_max_size
) {
45 /* This *must* be freed by using lttng_poll_free() */
46 events
->events
= zmalloc(size
* sizeof(struct pollfd
));
47 if (events
->events
== NULL
) {
48 perror("zmalloc struct pollfd");
52 events
->events_size
= size
;
62 * Add fd to pollfd data structure with requested events.
64 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
70 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
71 ERR("Bad compat poll add arguments");
75 /* Reallocate pollfd structure by a factor of 2 if needed. */
76 if (events
->nb_fd
>= events
->events_size
) {
77 new_size
= 2 * events
->events_size
;
78 ptr
= realloc(events
->events
, new_size
* sizeof(struct pollfd
));
80 perror("realloc poll add");
84 events
->events_size
= new_size
;
87 events
->events
[events
->nb_fd
].fd
= fd
;
88 events
->events
[events
->nb_fd
].events
= req_events
;
91 DBG("fd %d of %d added to pollfd", fd
, events
->nb_fd
);
100 * Remove a fd from the pollfd structure.
102 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
104 int new_size
, i
, count
= 0;
105 struct pollfd
*old
= NULL
, *new = NULL
;
107 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
108 ERR("Wrong arguments for poll del");
112 old
= events
->events
;
113 new_size
= events
->events_size
- 1;
115 /* Safety check on size */
116 if (new_size
> poll_max_size
) {
117 new_size
= poll_max_size
;
120 new = zmalloc(new_size
* sizeof(struct pollfd
));
122 perror("zmalloc poll del");
126 for (i
= 0; i
< events
->events_size
; i
++) {
127 /* Don't put back the fd we want to delete */
128 if (old
[i
].fd
!= fd
) {
129 new[count
].fd
= old
[i
].fd
;
130 new[count
].events
= old
[i
].events
;
135 events
->events_size
= new_size
;
136 events
->events
= new;
148 * Wait on poll() with timeout. Blocking call.
150 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
154 if (events
== NULL
|| events
->events
== NULL
||
155 events
->events_size
< events
->nb_fd
) {
156 ERR("poll wait arguments error");
160 ret
= poll(events
->events
, events
->nb_fd
, timeout
);
162 /* At this point, every error is fatal */
174 * Setup poll set maximum size.
176 void compat_poll_set_max_size(void)
182 poll_max_size
= DEFAULT_POLL_SIZE
;
184 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
186 perror("getrlimit poll RLIMIT_NOFILE");
190 poll_max_size
= lim
.rlim_cur
;
191 if (poll_max_size
<= 0) {
192 /* Extra precaution */
193 poll_max_size
= DEFAULT_POLL_SIZE
;
196 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.033955 seconds and 5 git commands to generate.