Mercurial > noffle
comparison src/protocol.c @ 144:8b9366fc1361 noffle
[svn] Added timeout to Prt_getLn to avoid Noffle hanging if the
connection breaks down during a fetch.
author | enz |
---|---|
date | Sat, 23 Sep 2000 11:40:35 +0100 |
parents | 3c71e28c8eef |
children | 1c7303c71f66 |
comparison
equal
deleted
inserted
replaced
143:7400a8e9d5ba | 144:8b9366fc1361 |
---|---|
1 /* | 1 /* |
2 protocol.c | 2 protocol.c |
3 | 3 |
4 $Id: protocol.c 183 2000-07-25 12:14:54Z bears $ | 4 $Id: protocol.c 217 2000-09-23 10:40:35Z enz $ |
5 */ | 5 */ |
6 | 6 |
7 #if HAVE_CONFIG_H | 7 #if HAVE_CONFIG_H |
8 #include <config.h> | 8 #include <config.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <ctype.h> | 12 #include <ctype.h> |
13 #include <netdb.h> | 13 #include <netdb.h> |
14 #include <signal.h> | |
14 #include <sys/types.h> | 15 #include <sys/types.h> |
15 #include <sys/utsname.h> | 16 #include <sys/utsname.h> |
17 #include <unistd.h> | |
16 #include "common.h" | 18 #include "common.h" |
17 #include "dynamicstring.h" | 19 #include "dynamicstring.h" |
18 #include "log.h" | 20 #include "log.h" |
19 #include "over.h" | 21 #include "over.h" |
20 #include "util.h" | 22 #include "util.h" |
21 #include "protocol.h" | 23 #include "protocol.h" |
22 #include "portable.h" | 24 #include "portable.h" |
23 | 25 |
24 Bool | 26 static void |
25 Prt_getLn( Str line, FILE *f ) | 27 readAlarm( int sig ) |
28 { | |
29 UNUSED( sig ); | |
30 | |
31 return; | |
32 } | |
33 | |
34 static sig_t | |
35 installSignalHandler( int sig, sig_t handler ) | |
36 { | |
37 struct sigaction act, oldAct; | |
38 | |
39 act.sa_handler = handler; | |
40 sigemptyset( &act.sa_mask ); | |
41 act.sa_flags = 0; | |
42 if ( sig == SIGALRM ) | |
43 act.sa_flags |= SA_INTERRUPT; | |
44 else | |
45 act.sa_flags |= SA_RESTART; | |
46 if ( sigaction( sig, &act, &oldAct ) < 0 ) | |
47 return SIG_ERR; | |
48 return oldAct.sa_handler; | |
49 } | |
50 | |
51 Bool | |
52 Prt_getLn( Str line, FILE *f, int timeoutSeconds ) | |
26 { | 53 { |
27 size_t len; | 54 size_t len; |
28 | 55 char *ret; |
56 sig_t oldHandler = NULL; | |
57 | |
58 if ( timeoutSeconds >= 0 ) | |
59 { | |
60 oldHandler = installSignalHandler( SIGALRM, readAlarm ); | |
61 if ( oldHandler == SIG_ERR ) | |
62 { | |
63 Log_err( "Prt_getLn: signal failed." ); | |
64 return FALSE; | |
65 } | |
66 if ( alarm( timeoutSeconds ) != 0 ) | |
67 Log_err( "Prt_getLn: Alarm was already set." ); | |
68 } | |
29 /* | 69 /* |
30 We also accept lines ending with "\n" instead of "\r\n", some | 70 We also accept lines ending with "\n" instead of "\r\n", some |
31 clients wrongly send such lines. | 71 clients wrongly send such lines. |
32 */ | 72 */ |
33 if ( ! fgets( line, MAXCHAR, f ) ) | 73 ret = fgets( line, MAXCHAR, f ); |
34 { | 74 if ( timeoutSeconds >= 0 ) |
35 Log_dbg( "Prt_getLn failed" ); | 75 { |
76 alarm( 0 ); | |
77 installSignalHandler( SIGALRM, oldHandler ); | |
78 } | |
79 if ( ret == NULL ) | |
36 return FALSE; | 80 return FALSE; |
37 } | |
38 len = strlen( line ); | 81 len = strlen( line ); |
39 if ( line[ len - 1 ] == '\n' ) | 82 if ( line[ len - 1 ] == '\n' ) |
40 { | 83 { |
41 line[ len - 1 ] = '\0'; | 84 line[ len - 1 ] = '\0'; |
42 if ( line[ len - 2 ] == '\r' ) | 85 if ( line[ len - 2 ] == '\r' ) |
45 Log_dbg( "[R] %s", line ); | 88 Log_dbg( "[R] %s", line ); |
46 return TRUE; | 89 return TRUE; |
47 } | 90 } |
48 | 91 |
49 Bool | 92 Bool |
50 Prt_getTxtLn( Str line, Bool *err, FILE *f ) | 93 Prt_getTxtLn( Str line, Bool *err, FILE *f, int timeoutSeconds ) |
51 { | 94 { |
52 Str buf; | 95 Str buf; |
53 | 96 |
54 if ( ! Prt_getLn( buf, f ) ) | 97 if ( ! Prt_getLn( buf, f, timeoutSeconds ) ) |
55 { | 98 { |
56 Log_err( "Cannot get text line" ); | 99 Log_err( "Cannot get text line" ); |
57 *err = TRUE; | 100 *err = TRUE; |
58 return FALSE; | 101 return FALSE; |
59 } | 102 } |