Version 0.1.10 from FTP
[asterisk/asterisk.git] / asterisk.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Top level source file for asterisk
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <asterisk/cli.h>
19 #include <asterisk/channel.h>
20 #include <asterisk/ulaw.h>
21 #include <asterisk/alaw.h>
22 #include <asterisk/callerid.h>
23 #include <asterisk/module.h>
24 #include <asterisk/image.h>
25 #include <asterisk/tdd.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <sched.h>
30 #include <asterisk/io.h>
31 #include <pthread.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <sys/select.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <readline/readline.h>
38 #include <readline/history.h>
39 #include "asterisk.h"
40
41 #define AST_MAX_CONNECTS 128
42 #define NUM_MSGS 64
43
44 int option_verbose=0;
45 int option_debug=0;
46 int option_nofork=0;
47 int option_quiet=0;
48 int option_console=0;
49 int option_highpriority=0;
50 int option_remote=0;
51 int option_exec=0;
52 int option_initcrypto=0;
53 int fully_booted = 0;
54
55 static int ast_socket = -1;             /* UNIX Socket for allowing remote control */
56 static int ast_consock = -1;            /* UNIX Socket for controlling another asterisk */
57 static int mainpid;
58 struct console {
59         int fd;                                 /* File descriptor */
60         int p[2];                               /* Pipe */
61         pthread_t t;                    /* Thread of handler */
62 };
63
64 struct console consoles[AST_MAX_CONNECTS];
65
66 char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE;
67
68 static int fdprint(int fd, char *s)
69 {
70         return write(fd, s, strlen(s) + 1);
71 }
72
73 static void network_verboser(char *s, int pos, int replace, int complete)
74 {
75         int x;
76         for (x=0;x<AST_MAX_CONNECTS; x++) {
77                 if (consoles[x].fd > -1) 
78                         fdprint(consoles[x].p[1], s);
79         }
80 }
81
82 static pthread_t lthread;
83
84 static void *netconsole(void *vconsole)
85 {
86         struct console *con = vconsole;
87         char hostname[256];
88         char tmp[512];
89         int res;
90         int max;
91         fd_set rfds;
92         
93         if (gethostname(hostname, sizeof(hostname)))
94                 strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
95         snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, mainpid, ASTERISK_VERSION);
96         fdprint(con->fd, tmp);
97         for(;;) {
98                 FD_ZERO(&rfds); 
99                 FD_SET(con->fd, &rfds);
100                 FD_SET(con->p[0], &rfds);
101                 max = con->fd;
102                 if (con->p[0] > max)
103                         max = con->p[0];
104                 res = select(max + 1, &rfds, NULL, NULL, NULL);
105                 if (res < 0) {
106                         ast_log(LOG_WARNING, "select returned < 0: %s\n", strerror(errno));
107                         continue;
108                 }
109                 if (FD_ISSET(con->fd, &rfds)) {
110                         res = read(con->fd, tmp, sizeof(tmp));
111                         if (res < 1) {
112                                 break;
113                         }
114                         tmp[res] = 0;
115                         ast_cli_command(con->fd, tmp);
116                 }
117                 if (FD_ISSET(con->p[0], &rfds)) {
118                         res = read(con->p[0], tmp, sizeof(tmp));
119                         if (res < 1) {
120                                 ast_log(LOG_ERROR, "read returned %d\n", res);
121                                 break;
122                         }
123                         res = write(con->fd, tmp, res);
124                         if (res < 1)
125                                 break;
126                 }
127         }
128         if (option_verbose > 2) 
129                 ast_verbose(VERBOSE_PREFIX_3 "Remote UNIX connection disconnected\n");
130         close(con->fd);
131         close(con->p[0]);
132         close(con->p[1]);
133         con->fd = -1;
134         
135         return NULL;
136 }
137
138 static void *listener(void *unused)
139 {
140         struct sockaddr_un sun;
141         int s;
142         int len;
143         int x;
144         int flags;
145         pthread_attr_t attr;
146         pthread_attr_init(&attr);
147         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
148         for(;;) {
149                 len = sizeof(sun);
150                 s = accept(ast_socket, (struct sockaddr *)&sun, &len);
151                 if (s < 0) {
152                         ast_log(LOG_WARNING, "Accept retured %d: %s\n", s, strerror(errno));
153                 } else {
154                         for (x=0;x<AST_MAX_CONNECTS;x++) {
155                                 if (consoles[x].fd < 0) {
156                                         if (socketpair(AF_LOCAL, SOCK_STREAM, 0, consoles[x].p)) {
157                                                 ast_log(LOG_ERROR, "Unable to create pipe: %s\n", strerror(errno));
158                                                 consoles[x].fd = -1;
159                                                 fdprint(s, "Server failed to create pipe\n");
160                                                 close(s);
161                                                 break;
162                                         }
163                                         flags = fcntl(consoles[x].p[1], F_GETFL);
164                                         fcntl(consoles[x].p[1], F_SETFL, flags | O_NONBLOCK);
165                                         consoles[x].fd = s;
166                                         if (pthread_create(&consoles[x].t, &attr, netconsole, &consoles[x])) {
167                                                 ast_log(LOG_ERROR, "Unable to spawn thread to handle connection\n");
168                                                 consoles[x].fd = -1;
169                                                 fdprint(s, "Server failed to spawn thread\n");
170                                                 close(s);
171                                         }
172                                         break;
173                                 }
174                         }
175                         if (x >= AST_MAX_CONNECTS) {
176                                 fdprint(s, "No more connections allowed\n");
177                                 ast_log(LOG_WARNING, "No more connections allowed\n");
178                                 close(s);
179                         } else if (consoles[x].fd > -1) {
180                                 if (option_verbose > 2) 
181                                         ast_verbose(VERBOSE_PREFIX_3 "Remote UNIX connection\n");
182                         }
183                 }
184         }
185         return NULL;
186 }
187
188 static int ast_makesocket(void)
189 {
190         struct sockaddr_un sun;
191         int res;
192         int x;
193         for (x=0;x<AST_MAX_CONNECTS;x++)        
194                 consoles[x].fd = -1;
195         unlink(AST_SOCKET);
196         ast_socket = socket(PF_LOCAL, SOCK_STREAM, 0);
197         if (ast_socket < 0) {
198                 ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
199                 return -1;
200         }               
201         memset(&sun, 0, sizeof(sun));
202         sun.sun_family = AF_LOCAL;
203         strncpy(sun.sun_path, AST_SOCKET, sizeof(sun.sun_path)-1);
204         res = bind(ast_socket, (struct sockaddr *)&sun, sizeof(sun));
205         if (res) {
206                 ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", AST_SOCKET, strerror(errno));
207                 close(ast_socket);
208                 ast_socket = -1;
209                 return -1;
210         }
211         res = listen(ast_socket, 2);
212         if (res < 0) {
213                 ast_log(LOG_WARNING, "Unable to listen on socket %s: %s\n", AST_SOCKET, strerror(errno));
214                 close(ast_socket);
215                 ast_socket = -1;
216                 return -1;
217         }
218         ast_register_verbose(network_verboser);
219         pthread_create(&lthread, NULL, listener, NULL);
220         return 0;
221 }
222
223 static int ast_tryconnect(void)
224 {
225         struct sockaddr_un sun;
226         int res;
227         ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
228         if (ast_consock < 0) {
229                 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
230                 return 0;
231         }
232         memset(&sun, 0, sizeof(sun));
233         sun.sun_family = AF_LOCAL;
234         strncpy(sun.sun_path, AST_SOCKET, sizeof(sun.sun_path)-1);
235         res = connect(ast_consock, (struct sockaddr *)&sun, sizeof(sun));
236         if (res) {
237                 close(ast_consock);
238                 ast_consock = -1;
239                 return 0;
240         } else
241                 return 1;
242 }
243
244 static void urg_handler(int num)
245 {
246         /* Called by soft_hangup to interrupt the select, read, or other
247            system call.  We don't actually need to do anything though.  */
248         if (option_debug) 
249                 ast_log(LOG_DEBUG, "Urgent handler\n");
250         signal(num, urg_handler);
251         return;
252 }
253
254 static void hup_handler(int num)
255 {
256         if (option_verbose > 1) 
257                 ast_verbose(VERBOSE_PREFIX_2 "Received HUP signal -- Reloading configs\n");
258         ast_module_reload();
259 }
260
261
262 static void pipe_handler(int num)
263 {
264         /* Ignore sigpipe */
265 }
266 static void set_title(char *text)
267 {
268         /* Set an X-term or screen title */
269         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
270                 fprintf(stdout, "\033]2;%s\007", text);
271 }
272
273 static void set_icon(char *text)
274 {
275         if (getenv("TERM") && strstr(getenv("TERM"), "xterm"))
276                 fprintf(stdout, "\033]1;%s\007", text);
277 }
278
279 static int set_priority(int pri)
280 {
281         struct sched_param sched;
282         memset(&sched, 0, sizeof(sched));
283         /* We set ourselves to a high priority, that we might pre-empt everything
284            else.  If your PBX has heavy activity on it, this is a good thing.  */
285         if (pri) {  
286                 sched.sched_priority = 10;
287                 if (sched_setscheduler(0, SCHED_RR, &sched)) {
288                         ast_log(LOG_WARNING, "Unable to set high priority\n");
289                         return -1;
290                 } else
291                         if (option_verbose)
292                                 ast_verbose("Set to realtime thread\n");
293         } else {
294                 sched.sched_priority = 0;
295                 if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
296                         ast_log(LOG_WARNING, "Unable to set normal priority\n");
297                         return -1;
298                 }
299         }
300         return 0;
301 }
302
303 static void quit_handler(int num)
304 {
305         char filename[80] = "";
306         if (option_console || option_remote) {
307                 if (getenv("HOME")) 
308                         snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
309                 if (strlen(filename))
310                         write_history(filename);
311                 rl_callback_handler_remove();
312         }
313         /* Called on exit */
314         if (option_verbose && option_console)
315                 ast_verbose("Asterisk ending (%d).\n", num);
316         else if (option_debug)
317                 ast_log(LOG_DEBUG, "Asterisk ending (%d).\n", num);
318         if (ast_socket > -1)
319                 close(ast_socket);
320         if (ast_consock > -1)
321                 close(ast_consock);
322         if (ast_socket > -1)
323                 unlink(AST_SOCKET);
324         
325         exit(0);
326 }
327
328 static pthread_t consolethread = -1;
329
330 static void console_verboser(char *s, int pos, int replace, int complete)
331 {
332         /* Return to the beginning of the line */
333         if (!pos)
334                 fprintf(stdout, "\r");
335         fputs(s + pos,stdout);
336         fflush(stdout);
337         if (complete)
338         /* Wake up a select()ing console */
339                 pthread_kill(consolethread, SIGURG);
340 }
341
342 static void consolehandler(char *s)
343 {
344         /* Called when readline data is available */
345         if (s && strlen(s))
346                 add_history(s);
347         /* Give the console access to the shell */
348         if (s) {
349                 if (s[0] == '!') {
350                         if (s[1])
351                                 system(s+1);
352                         else
353                                 system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
354                 } else 
355                 ast_cli_command(STDOUT_FILENO, s);
356                 if (!strcasecmp(s, "help"))
357                         fprintf(stdout, "          !<command>   Executes a given shell command\n");
358         } else
359                 fprintf(stdout, "\nUse \"quit\" to exit\n");
360 }
361
362
363 static char cmd[1024];
364
365 static void remoteconsolehandler(char *s)
366 {
367         /* Called when readline data is available */
368         if (s && strlen(s))
369                 add_history(s);
370         /* Give the console access to the shell */
371         if (s) {
372                 if (s[0] == '!') {
373                         if (s[1])
374                                 system(s+1);
375                         else
376                                 system(getenv("SHELL") ? getenv("SHELL") : "/bin/sh");
377                 } else 
378                 strncpy(cmd, s, sizeof(cmd)-1);
379                 if (!strcasecmp(s, "help"))
380                         fprintf(stdout, "          !<command>   Executes a given shell command\n");
381                 if (!strcasecmp(s, "quit"))
382                         quit_handler(0);
383         } else
384                 fprintf(stdout, "\nUse \"quit\" to exit\n");
385 }
386
387 static char quit_help[] = 
388 "Usage: quit\n"
389 "       Exits Asterisk.\n";
390
391 static char shutdown_help[] = 
392 "Usage: shutdown\n"
393 "       Shuts down a running Asterisk PBX.\n";
394
395 static int handle_quit(int fd, int argc, char *argv[])
396 {
397         if (argc != 1)
398                 return RESULT_SHOWUSAGE;
399         quit_handler(0);
400         return RESULT_SUCCESS;
401 }
402
403 #define ASTERISK_PROMPT "*CLI> "
404
405 #define ASTERISK_PROMPT2 "%s*CLI> "
406
407 static struct ast_cli_entry quit =      { { "quit", NULL }, handle_quit, "Exit Asterisk", quit_help };
408
409 static struct ast_cli_entry astshutdown =       { { "shutdown", NULL }, handle_quit, "Shut down an Asterisk PBX", shutdown_help };
410
411 static char *cli_generator(char *text, int state)
412 {
413         return ast_cli_generator(rl_line_buffer, text, state);
414 }
415
416 static char *console_cli_generator(char *text, int state)
417 {
418         char buf[1024];
419         int res;
420 #if 0
421         fprintf(stderr, "Searching for '%s', %s %d\n", rl_line_buffer, text, state);
422 #endif  
423         snprintf(buf, sizeof(buf),"_COMMAND COMPLETE \"%s\" \"%s\" %d", rl_line_buffer, text, state); 
424         fdprint(ast_consock, buf);
425         res = read(ast_consock, buf, sizeof(buf));
426         buf[res] = '\0';
427 #if 0
428         printf("res is %d, buf is '%s'\n", res, buf);
429 #endif  
430         if (strncmp(buf, "NULL", 4))
431                 return strdup(buf);
432         else
433                 return NULL;
434 }
435
436 static void ast_remotecontrol(char * data)
437 {
438         char buf[80];
439         int res;
440         int max;
441         int lastpos = 0;
442         fd_set rfds;
443         char filename[80] = "";
444         char *hostname;
445         char *cpid;
446         char *version;
447         int pid;
448         int lastclear=0;
449         int oldstatus=0;
450         char tmp[80];
451         read(ast_consock, buf, sizeof(buf));
452         if (data) {
453                         write(ast_consock, data, strlen(data) + 1);
454                         return;
455         }
456         hostname = strtok(buf, "/");
457         cpid = strtok(NULL, "/");
458         version = strtok(NULL, "/");
459         if (!version)
460                 version = "<Version Unknown>";
461         strtok(hostname, ".");
462         if (cpid)
463                 pid = atoi(cpid);
464         else
465                 pid = -1;
466         snprintf(tmp, sizeof(tmp), "set verbose atleast %d", option_verbose);
467         fdprint(ast_consock, tmp);
468         ast_verbose("Connected to Asterisk %s currently running on %s (pid = %d)\n", version, hostname, pid);
469         snprintf(tmp, sizeof(tmp), ASTERISK_PROMPT2, hostname);
470         if (getenv("HOME")) 
471                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
472         if (strlen(filename))
473                 read_history(filename);
474     ast_cli_register(&quit);
475     ast_cli_register(&astshutdown);
476         rl_callback_handler_install(tmp, remoteconsolehandler);
477         rl_completion_entry_function = (Function *)console_cli_generator;
478         for(;;) {
479                 FD_ZERO(&rfds);
480                 FD_SET(ast_consock, &rfds);
481                 FD_SET(STDIN_FILENO, &rfds);
482                 max = ast_consock;
483                 if (STDIN_FILENO > max)
484                         max = STDIN_FILENO;
485                 res = select(max + 1, &rfds, NULL, NULL, NULL);
486                 if (res < 0) {
487                         if (errno == EINTR)
488                                 continue;
489                         ast_log(LOG_ERROR, "select failed: %s\n", strerror(errno));
490                         break;
491                 }
492                 if (FD_ISSET(STDIN_FILENO, &rfds)) {
493                         rl_callback_read_char();
494                         if (strlen(cmd)) {
495                                 res = write(ast_consock, cmd, strlen(cmd) + 1);
496                                 if (res < 1) {
497                                         ast_log(LOG_WARNING, "Unable to write: %s\n", strerror(errno));
498                                         break;
499                                 }
500                                 strcpy(cmd, "");
501                         }
502                 }
503                 if (FD_ISSET(ast_consock, &rfds)) {
504                         res = read(ast_consock, buf, sizeof(buf));
505                         if (res < 1)
506                                 break;
507                         buf[res] = 0;
508                         /* If someone asks for a pass code, hide the password */
509                         if (!memcmp(buf, ">>>>", 4)) {
510                                 printf("Ooh, i should hide password!\n");
511                                 if (!lastclear) {
512                                         oldstatus = ast_hide_password(STDIN_FILENO);
513                                         printf("Oldstatus = %d\n", oldstatus);
514                                 }
515                                 lastclear = 1;
516                         } else if (lastclear) {
517                                 ast_restore_tty(STDIN_FILENO, oldstatus);
518                                 lastclear = 0;
519                         }
520                         if (!lastpos)
521                                 write(STDOUT_FILENO, "\r", 2);
522                         write(STDOUT_FILENO, buf, res);
523                         if ((buf[res-1] == '\n') || (buf[res-2] == '\n')) {
524                                 rl_forced_update_display();
525                                 lastpos = 0;
526                         } else {
527                                 lastpos = 1;
528                         }
529                 }
530         }
531         printf("\nDisconnected from Asterisk server\n");
532 }
533
534 int main(int argc, char *argv[])
535 {
536         char c;
537         fd_set rfds;
538         int res;
539         int pid;
540         char filename[80] = "";
541         char hostname[256];
542         char * xarg = NULL;
543         sigset_t sigs;
544
545         if (gethostname(hostname, sizeof(hostname)))
546                 strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
547         mainpid = getpid();
548         ast_ulaw_init();
549         ast_alaw_init();
550         callerid_init();
551         tdd_init();
552         if (getenv("HOME")) 
553                 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME"));
554         /* Check if we're root */
555         if (geteuid()) {
556                 ast_log(LOG_ERROR, "Must be run as root\n");
557                 exit(1);
558         }
559         /* Check for options */
560         while((c=getopt(argc, argv, "fdvqprcix:")) != EOF) {
561                 switch(c) {
562                 case 'd':
563                         option_debug++;
564                         option_nofork++;
565                         break;
566                 case 'c':
567                         option_console++;
568                         option_nofork++;
569                         break;
570                 case 'f':
571                         option_nofork++;
572                         break;
573                 case 'r':
574                         option_remote++;
575                         option_nofork++;
576                         break;
577                 case 'p':
578                         option_highpriority++;
579                         break;
580                 case 'v':
581                         option_verbose++;
582                         option_nofork++;
583                         break;
584                 case 'q':
585                         option_quiet++;
586                         break;
587                 case 'x':
588                         option_exec++;
589                         xarg = optarg;
590                         break;
591                 case 'i':
592                         option_initcrypto++;
593                         break;
594                 case '?':
595                         exit(1);
596                 }
597         }
598         
599         if (ast_tryconnect()) {
600                 /* One is already running */
601                 if (option_remote) {
602                         if (option_exec) {
603                                 ast_remotecontrol(xarg);
604                                 quit_handler(0);
605                                 exit(0);
606                         }
607                         ast_register_verbose(console_verboser);
608                         ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2001 Linux Support Services, Inc.\n");
609                         ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n");
610                         ast_verbose( "=========================================================================\n");
611                         ast_remotecontrol(NULL);
612                         quit_handler(0);
613                         exit(0);
614                 } else {
615                         ast_log(LOG_ERROR, "Asterisk already running on %s.  Use 'asterisk -r' to connect.\n", AST_SOCKET);
616                         exit(1);
617                 }
618         } else if (option_remote || option_exec) {
619                 ast_log(LOG_ERROR, "Unable to connect to remote asterisk\n");
620                 exit(1);
621         }
622
623         if (!option_verbose && !option_debug && !option_nofork && !option_console) {
624                 pid = fork();
625                 if (pid < 0) {
626                         ast_log(LOG_ERROR, "Unable to fork(): %s\n", strerror(errno));
627                         exit(1);
628                 }
629                 if (pid) 
630                         exit(0);
631         }
632
633         ast_makesocket();
634         sigemptyset(&sigs);
635         sigaddset(&sigs, SIGHUP);
636         sigaddset(&sigs, SIGTERM);
637         sigaddset(&sigs, SIGINT);
638         sigaddset(&sigs, SIGPIPE);
639         sigaddset(&sigs, SIGWINCH);
640         pthread_sigmask(SIG_BLOCK, &sigs, NULL);
641         if (option_console || option_verbose || option_remote)
642                 ast_register_verbose(console_verboser);
643         /* Print a welcome message if desired */
644         if (option_verbose || option_console) {
645                 ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2001 Linux Support Services, Inc.\n");
646                 ast_verbose( "Written by Mark Spencer <markster@linux-support.net>\n");
647                 ast_verbose( "=========================================================================\n");
648         }
649         if (option_console && !option_verbose) 
650                 ast_verbose("[ Booting...");
651         signal(SIGURG, urg_handler);
652         signal(SIGINT, quit_handler);
653         signal(SIGTERM, quit_handler);
654         signal(SIGHUP, hup_handler);
655         signal(SIGPIPE, pipe_handler);
656         if (set_priority(option_highpriority))
657                 exit(1);
658         if (init_logger())
659                 exit(1);
660         if (ast_image_init())
661                 exit(1);
662         if (load_pbx())
663                 exit(1);
664         if (load_modules())
665                 exit(1);
666         if (init_framer())
667                 exit(1);
668         /* We might have the option of showing a console, but for now just
669            do nothing... */
670         if (option_console && !option_verbose)
671                 ast_verbose(" ]\n");
672         if (option_verbose || option_console)
673                 ast_verbose( "Asterisk Ready.\n");
674         fully_booted = 1;
675         pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
676         ast_cli_register(&astshutdown);
677         if (option_console) {
678                 /* Console stuff now... */
679                 /* Register our quit function */
680                 char title[256];
681                 set_icon("Asterisk");
682                 snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %d)", hostname, mainpid);
683                 set_title(title);
684             ast_cli_register(&quit);
685                 consolethread = pthread_self();
686                 if (strlen(filename))
687                         read_history(filename);
688                 rl_callback_handler_install(ASTERISK_PROMPT, consolehandler);
689                 rl_completion_entry_function = (Function *)cli_generator;
690                 for(;;) {
691                         FD_ZERO(&rfds);
692                         FD_SET(STDIN_FILENO, &rfds);
693                         res = select(STDIN_FILENO + 1, &rfds, NULL, NULL, NULL);
694                         if (res > 0) {
695                                 rl_callback_read_char();
696                         } else if (res < 1) {
697                                 rl_forced_update_display();
698                         }
699         
700                 }       
701         } else {
702                 /* Do nothing */
703                 select(0,NULL,NULL,NULL,NULL);
704         }
705         return 0;
706 }