50ef37472dc0a94db9eec258f21ce632f7d615d3
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>
26 unsigned int poll_max_size
;
29 * Create pollfd data structure.
31 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
33 if (events
== NULL
|| size
<= 0) {
34 ERR("Wrong arguments for poll create");
38 /* Don't bust the limit here */
39 if (size
> poll_max_size
) {
43 /* This *must* be freed by using lttng_poll_free() */
44 events
->events
= zmalloc(size
* sizeof(struct pollfd
));
45 if (events
->events
== NULL
) {
46 perror("zmalloc struct pollfd");
50 events
->events_size
= size
;
60 * Add fd to pollfd data structure with requested events.
62 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
68 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
69 ERR("Bad compat poll add arguments");
73 /* Reallocate pollfd structure by a factor of 2 if needed. */
74 if (events
->nb_fd
>= events
->events_size
) {
75 new_size
= 2 * events
->events_size
;
76 ptr
= realloc(events
->events
, new_size
* sizeof(struct pollfd
));
78 perror("realloc poll add");
82 events
->events_size
= new_size
;
85 events
->events
[events
->nb_fd
].fd
= fd
;
86 events
->events
[events
->nb_fd
].events
= req_events
;
89 DBG("fd %d of %d added to pollfd", fd
, events
->nb_fd
);
98 * Remove a fd from the pollfd structure.
100 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
102 int new_size
, i
, count
= 0;
103 struct pollfd
*old
= NULL
, *new = NULL
;
105 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
106 ERR("Wrong arguments for poll del");
110 old
= events
->events
;
111 new_size
= events
->events_size
- 1;
113 /* Safety check on size */
114 if (new_size
> poll_max_size
) {
115 new_size
= poll_max_size
;
118 new = zmalloc(new_size
* sizeof(struct pollfd
));
120 perror("zmalloc poll del");
124 for (i
= 0; i
< events
->events_size
; i
++) {
125 /* Don't put back the fd we want to delete */
126 if (old
[i
].fd
!= fd
) {
127 new[count
].fd
= old
[i
].fd
;
128 new[count
].events
= old
[i
].events
;
133 events
->events_size
= new_size
;
134 events
->events
= new;
146 * Wait on poll() with timeout. Blocking call.
148 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
)
152 if (events
== NULL
|| events
->events
== NULL
||
153 events
->events_size
< events
->nb_fd
) {
154 ERR("poll wait arguments error");
158 ret
= poll(events
->events
, events
->nb_fd
, timeout
);
160 /* At this point, every error is fatal */
172 * Setup poll set maximum size.
174 void compat_poll_set_max_size(void)
180 poll_max_size
= LTTNG_POLL_DEFAULT_SIZE
;
182 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
184 perror("getrlimit poll RLIMIT_NOFILE");
188 poll_max_size
= lim
.rlim_cur
;
189 if (poll_max_size
<= 0) {
190 /* Extra precaution */
191 poll_max_size
= LTTNG_POLL_DEFAULT_SIZE
;
194 DBG("poll set max size set to %u", poll_max_size
);
This page took 0.032813 seconds and 3 git commands to generate.