2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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 with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <sys/resource.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
27 unsigned int poll_max_size
;
30 * Create pollfd data structure.
32 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
34 if (events
== NULL
|| size
<= 0) {
35 ERR("Wrong arguments for poll create");
39 /* Don't bust the limit here */
40 if (size
> poll_max_size
) {
44 /* This *must* be freed by using lttng_poll_free() */
45 events
->events
= zmalloc(size
* sizeof(struct pollfd
));
46 if (events
->events
== NULL
) {
47 perror("zmalloc struct pollfd");
51 events
->events_size
= size
;
61 * Add fd to pollfd data structure with requested events.
63 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
69 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
70 ERR("Bad compat poll add arguments");
74 /* Reallocate pollfd structure by a factor of 2 if needed. */
75 if (events
->nb_fd
>= events
->events_size
) {
76 new_size
= 2 * events
->events_size
;
77 ptr
= realloc(events
->events
, new_size
* sizeof(struct pollfd
));
79 perror("realloc poll add");
83 events
->events_size
= new_size
;
86 events
->events
[events
->nb_fd
].fd
= fd
;
87 events
->events
[events
->nb_fd
].events
= req_events
;
90 DBG("fd %d of %d added to pollfd", fd
, events
->nb_fd
);
99 * Remove a fd from the pollfd structure.
101 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
103 int new_size
, i
, count
= 0;
104 struct pollfd
*old
= NULL
, *new = NULL
;
106 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
107 ERR("Wrong arguments for poll del");
111 old
= events
->events
;
112 new_size
= events
->events_size
- 1;
114 /* Safety check on size */
115 if (new_size
> poll_max_size
) {
116 new_size
= poll_max_size
;
119 new = zmalloc(new_size
* sizeof(struct pollfd
));
121 perror("zmalloc poll del");
125 for (i
= 0; i
< events
->events_size
; i
++) {
126 /* Don't put back the fd we want to delete */
127 if (old
[i
].fd
!= fd
) {
128 new[count
].fd
= old
[i
].fd
;
129 new[count
].events
= old
[i
].events
;
134 events
->events_size
= new_size
;
135 events
->events
= new;
147 * Wait on poll() with timeout. Blocking call.
149 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
153 if (events
== NULL
|| events
->events
== NULL
||
154 events
->events_size
< events
->nb_fd
) {
155 ERR("poll wait arguments error");
159 ret
= poll(events
->events
, events
->nb_fd
, timeout
);
161 /* At this point, every error is fatal */
173 * Setup poll set maximum size.
175 void compat_poll_set_max_size(void)
181 poll_max_size
= DEFAULT_POLL_SIZE
;
183 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
185 perror("getrlimit poll RLIMIT_NOFILE");
189 poll_max_size
= lim
.rlim_cur
;
190 if (poll_max_size
<= 0) {
191 /* Extra precaution */
192 poll_max_size
= DEFAULT_POLL_SIZE
;
195 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.035676 seconds and 5 git commands to generate.