2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief I/O Managment (Derived from Cheops-NG)
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
35 #include <sys/ioctl.h>
37 #include "asterisk/io.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
48 * Kept for each file descriptor
51 ast_io_cb callback
; /* What is to be called */
52 void *data
; /* Data to be passed */
53 int *id
; /* ID number */
56 /* These two arrays are keyed with
57 the same index. it's too bad that
58 pollfd doesn't have a callback field
59 or something like that. They grow as
60 needed, by GROW_SHRINK_SIZE structures
63 #define GROW_SHRINK_SIZE 512
65 /* Global variables are now in a struct in order to be
70 /* Associated I/O records */
72 /* First available fd */
74 /* Maximum available fd */
75 unsigned int maxfdcnt
;
76 /* Currently used io callback */
78 /* Whether something has been deleted */
82 struct io_context
*io_context_create(void)
84 /* Create an I/O context */
85 struct io_context
*tmp
;
86 if ((tmp
= ast_malloc(sizeof(*tmp
)))) {
89 tmp
->maxfdcnt
= GROW_SHRINK_SIZE
/2;
90 tmp
->current_ioc
= -1;
91 if (!(tmp
->fds
= ast_calloc(1, (GROW_SHRINK_SIZE
/ 2) * sizeof(*tmp
->fds
)))) {
95 if (!(tmp
->ior
= ast_calloc(1, (GROW_SHRINK_SIZE
/ 2) * sizeof(*tmp
->ior
)))) {
105 void io_context_destroy(struct io_context
*ioc
)
107 /* Free associated memory with an I/O context */
115 static int io_grow(struct io_context
*ioc
)
118 * Grow the size of our arrays. Return 0 on success or
122 DEBUG(ast_log(LOG_DEBUG
, "io_grow()\n"));
123 ioc
->maxfdcnt
+= GROW_SHRINK_SIZE
;
124 if ((tmp
= ast_realloc(ioc
->ior
, (ioc
->maxfdcnt
+ 1) * sizeof(*ioc
->ior
)))) {
126 if ((tmp
= ast_realloc(ioc
->fds
, (ioc
->maxfdcnt
+ 1) * sizeof(*ioc
->fds
)))) {
130 * Failed to allocate enough memory for the pollfd. Not
131 * really any need to shrink back the iorec's as we'll
132 * probably want to grow them again soon when more memory
133 * is available, and then they'll already be the right size
135 ioc
->maxfdcnt
-= GROW_SHRINK_SIZE
;
140 * Memory allocation failure. We return to the old size, and
143 ioc
->maxfdcnt
-= GROW_SHRINK_SIZE
;
149 int *ast_io_add(struct io_context
*ioc
, int fd
, ast_io_cb callback
, short events
, void *data
)
152 * Add a new I/O entry for this file descriptor
153 * with the given event mask, to call callback with
154 * data as an argument. Returns NULL on failure.
157 DEBUG(ast_log(LOG_DEBUG
, "ast_io_add()\n"));
158 if (ioc
->fdcnt
>= ioc
->maxfdcnt
) {
160 * We don't have enough space for this entry. We need to
161 * reallocate maxfdcnt poll fd's and io_rec's, or back out now.
168 * At this point, we've got sufficiently large arrays going
169 * and we can make an entry for it in the pollfd and io_r
172 ioc
->fds
[ioc
->fdcnt
].fd
= fd
;
173 ioc
->fds
[ioc
->fdcnt
].events
= events
;
174 ioc
->fds
[ioc
->fdcnt
].revents
= 0;
175 ioc
->ior
[ioc
->fdcnt
].callback
= callback
;
176 ioc
->ior
[ioc
->fdcnt
].data
= data
;
177 if (!(ioc
->ior
[ioc
->fdcnt
].id
= ast_malloc(sizeof(*ioc
->ior
[ioc
->fdcnt
].id
)))) {
178 /* Bonk if we couldn't allocate an int */
181 *(ioc
->ior
[ioc
->fdcnt
].id
) = ioc
->fdcnt
;
182 ret
= ioc
->ior
[ioc
->fdcnt
].id
;
187 int *ast_io_change(struct io_context
*ioc
, int *id
, int fd
, ast_io_cb callback
, short events
, void *data
)
189 if (*id
< ioc
->fdcnt
) {
191 ioc
->fds
[*id
].fd
= fd
;
193 ioc
->ior
[*id
].callback
= callback
;
195 ioc
->fds
[*id
].events
= events
;
197 ioc
->ior
[*id
].data
= data
;
203 static int io_shrink(struct io_context
*ioc
)
208 * Bring the fields from the very last entry to cover over
209 * the entry we are removing, then decrease the size of the
212 for (getfrom
= 0; getfrom
< ioc
->fdcnt
; getfrom
++) {
213 if (ioc
->ior
[getfrom
].id
) {
214 /* In use, save it */
215 if (getfrom
!= putto
) {
216 ioc
->fds
[putto
] = ioc
->fds
[getfrom
];
217 ioc
->ior
[putto
] = ioc
->ior
[getfrom
];
218 *(ioc
->ior
[putto
].id
) = putto
;
225 /* FIXME: We should free some memory if we have lots of unused
230 int ast_io_remove(struct io_context
*ioc
, int *_id
)
234 ast_log(LOG_WARNING
, "Asked to remove NULL?\n");
237 for (x
= 0; x
< ioc
->fdcnt
; x
++) {
238 if (ioc
->ior
[x
].id
== _id
) {
239 /* Free the int immediately and set to NULL so we know it's unused now */
240 free(ioc
->ior
[x
].id
);
241 ioc
->ior
[x
].id
= NULL
;
242 ioc
->fds
[x
].events
= 0;
243 ioc
->fds
[x
].revents
= 0;
245 if (ioc
->current_ioc
== -1)
251 ast_log(LOG_NOTICE
, "Unable to remove unknown id %p\n", _id
);
255 int ast_io_wait(struct io_context
*ioc
, int howlong
)
258 * Make the poll call, and call
259 * the callbacks for anything that needs
265 DEBUG(ast_log(LOG_DEBUG
, "ast_io_wait()\n"));
266 res
= poll(ioc
->fds
, ioc
->fdcnt
, howlong
);
271 origcnt
= ioc
->fdcnt
;
272 for(x
= 0; x
< origcnt
; x
++) {
273 /* Yes, it is possible for an entry to be deleted and still have an
274 event waiting if it occurs after the original calling id */
275 if (ioc
->fds
[x
].revents
&& ioc
->ior
[x
].id
) {
276 /* There's an event waiting */
277 ioc
->current_ioc
= *ioc
->ior
[x
].id
;
278 if (ioc
->ior
[x
].callback
) {
279 if (!ioc
->ior
[x
].callback(ioc
->ior
[x
].id
, ioc
->fds
[x
].fd
, ioc
->fds
[x
].revents
, ioc
->ior
[x
].data
)) {
280 /* Time to delete them since they returned a 0 */
281 ast_io_remove(ioc
, ioc
->ior
[x
].id
);
284 ioc
->current_ioc
= -1;
293 void ast_io_dump(struct io_context
*ioc
)
296 * Print some debugging information via
297 * the logger interface
300 ast_log(LOG_DEBUG
, "Asterisk IO Dump: %d entries, %d max entries\n", ioc
->fdcnt
, ioc
->maxfdcnt
);
301 ast_log(LOG_DEBUG
, "================================================\n");
302 ast_log(LOG_DEBUG
, "| ID FD Callback Data Events |\n");
303 ast_log(LOG_DEBUG
, "+------+------+-----------+-----------+--------+\n");
304 for (x
= 0; x
< ioc
->fdcnt
; x
++) {
305 ast_log(LOG_DEBUG
, "| %.4d | %.4d | %p | %p | %.6x |\n",
308 ioc
->ior
[x
].callback
,
312 ast_log(LOG_DEBUG
, "================================================\n");
315 /* Unrelated I/O functions */
317 int ast_hide_password(int fd
)
324 res
= tcgetattr(fd
, &tios
);
327 old
= tios
.c_lflag
& (ECHO
| ECHONL
);
328 tios
.c_lflag
&= ~ECHO
;
329 tios
.c_lflag
|= ECHONL
;
330 res
= tcsetattr(fd
, TCSAFLUSH
, &tios
);
336 int ast_restore_tty(int fd
, int oldstate
)
342 res
= tcgetattr(fd
, &tios
);
345 tios
.c_lflag
&= ~(ECHO
| ECHONL
);
346 tios
.c_lflag
|= oldstate
;
347 res
= tcsetattr(fd
, TCSAFLUSH
, &tios
);
353 int ast_get_termcols(int fd
)
361 if ( ioctl(fd
, TIOCGWINSZ
, &win
) != -1 ) {
362 if ( !cols
&& win
.ws_col
> 0 )
363 cols
= (int) win
.ws_col
;
365 /* assume 80 characters if the ioctl fails for some reason */