Fossil SCM

The "ui" and "server" commands no longer quit if they cannot open TCP port 8080. They keep trying with consecutive ports until they find one that works - up to 100 ports.

drh 2008-11-10 01:13 trunk
Commit d8ceb4ad476342bedd30559eb679a7118a8833a2
3 files changed +39 -19 +7 -6 +32 -17
+39 -19
--- src/cgi.c
+++ src/cgi.c
@@ -1182,13 +1182,13 @@
11821182
** The parent never returns from this procedure.
11831183
**
11841184
** Return 0 to each child as it runs. If unable to establish a
11851185
** listening socket, return non-zero.
11861186
*/
1187
-int cgi_http_server(int iPort, char *zBrowser){
1187
+int cgi_http_server(int mnPort, int mxPort, char *zBrowser){
11881188
#ifdef __MINGW32__
1189
- fprintf(stderr,"server not yet available in windows version of fossil\n");
1189
+ /* Use win32_http_server() instead */
11901190
exit(1);
11911191
#else
11921192
int listener; /* The server socket */
11931193
int connection; /* A socket for each individual connection */
11941194
fd_set readfds; /* Set of file descriptors for select() */
@@ -1196,29 +1196,49 @@
11961196
int child; /* PID of the child process */
11971197
int nchildren = 0; /* Number of child processes */
11981198
struct timeval delay; /* How long to wait inside select() */
11991199
struct sockaddr_in inaddr; /* The socket address */
12001200
int opt = 1; /* setsockopt flag */
1201
-
1202
- memset(&inaddr, 0, sizeof(inaddr));
1203
- inaddr.sin_family = AF_INET;
1204
- inaddr.sin_addr.s_addr = INADDR_ANY;
1205
- inaddr.sin_port = htons(iPort);
1206
- listener = socket(AF_INET, SOCK_STREAM, 0);
1207
- if( listener<0 ){
1208
- return 1;
1209
- }
1210
-
1211
- /* if we can't terminate nicely, at least allow the socket to be reused */
1212
- setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
1213
-
1214
- if( bind(listener, (struct sockaddr*)&inaddr, sizeof(inaddr))<0 ){
1215
- close(listener);
1216
- return 1;
1217
- }
1201
+ int iPort = mnPort;
1202
+
1203
+ while( iPort<mxPort ){
1204
+ memset(&inaddr, 0, sizeof(inaddr));
1205
+ inaddr.sin_family = AF_INET;
1206
+ inaddr.sin_addr.s_addr = INADDR_ANY;
1207
+ inaddr.sin_port = htons(iPort);
1208
+ listener = socket(AF_INET, SOCK_STREAM, 0);
1209
+ if( listener<0 ){
1210
+ iPort++;
1211
+ continue;
1212
+ }
1213
+
1214
+ /* if we can't terminate nicely, at least allow the socket to be reused */
1215
+ setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
1216
+
1217
+ if( bind(listener, (struct sockaddr*)&inaddr, sizeof(inaddr))<0 ){
1218
+ close(listener);
1219
+ iPort++;
1220
+ continue;
1221
+ }
1222
+ break;
1223
+ }
1224
+ if( iPort>mxPort ){
1225
+ if( mnPort==mxPort ){
1226
+ fossil_fatal("unable to open listening socket on ports %d", mnPort);
1227
+ }else{
1228
+ fossil_fatal("unable to open listening socket on any"
1229
+ " ports %d..%d", mnPort, mxPort);
1230
+ }
1231
+ }
1232
+ if( iPort>mxPort ) return 1;
12181233
listen(listener,10);
1234
+ if( iPort>mnPort ){
1235
+ printf("Listening for HTTP requests on TCP port %d\n", iPort);
1236
+ fflush(stdout);
1237
+ }
12191238
if( zBrowser ){
1239
+ zBrowser = mprintf(zBrowser, iPort);
12201240
system(zBrowser);
12211241
}
12221242
while( 1 ){
12231243
if( nchildren>MAX_PARALLEL ){
12241244
/* Slow down if connections are arriving too fast */
12251245
--- src/cgi.c
+++ src/cgi.c
@@ -1182,13 +1182,13 @@
1182 ** The parent never returns from this procedure.
1183 **
1184 ** Return 0 to each child as it runs. If unable to establish a
1185 ** listening socket, return non-zero.
1186 */
1187 int cgi_http_server(int iPort, char *zBrowser){
1188 #ifdef __MINGW32__
1189 fprintf(stderr,"server not yet available in windows version of fossil\n");
1190 exit(1);
1191 #else
1192 int listener; /* The server socket */
1193 int connection; /* A socket for each individual connection */
1194 fd_set readfds; /* Set of file descriptors for select() */
@@ -1196,29 +1196,49 @@
1196 int child; /* PID of the child process */
1197 int nchildren = 0; /* Number of child processes */
1198 struct timeval delay; /* How long to wait inside select() */
1199 struct sockaddr_in inaddr; /* The socket address */
1200 int opt = 1; /* setsockopt flag */
1201
1202 memset(&inaddr, 0, sizeof(inaddr));
1203 inaddr.sin_family = AF_INET;
1204 inaddr.sin_addr.s_addr = INADDR_ANY;
1205 inaddr.sin_port = htons(iPort);
1206 listener = socket(AF_INET, SOCK_STREAM, 0);
1207 if( listener<0 ){
1208 return 1;
1209 }
1210
1211 /* if we can't terminate nicely, at least allow the socket to be reused */
1212 setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
1213
1214 if( bind(listener, (struct sockaddr*)&inaddr, sizeof(inaddr))<0 ){
1215 close(listener);
1216 return 1;
1217 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1218 listen(listener,10);
 
 
 
 
1219 if( zBrowser ){
 
1220 system(zBrowser);
1221 }
1222 while( 1 ){
1223 if( nchildren>MAX_PARALLEL ){
1224 /* Slow down if connections are arriving too fast */
1225
--- src/cgi.c
+++ src/cgi.c
@@ -1182,13 +1182,13 @@
1182 ** The parent never returns from this procedure.
1183 **
1184 ** Return 0 to each child as it runs. If unable to establish a
1185 ** listening socket, return non-zero.
1186 */
1187 int cgi_http_server(int mnPort, int mxPort, char *zBrowser){
1188 #ifdef __MINGW32__
1189 /* Use win32_http_server() instead */
1190 exit(1);
1191 #else
1192 int listener; /* The server socket */
1193 int connection; /* A socket for each individual connection */
1194 fd_set readfds; /* Set of file descriptors for select() */
@@ -1196,29 +1196,49 @@
1196 int child; /* PID of the child process */
1197 int nchildren = 0; /* Number of child processes */
1198 struct timeval delay; /* How long to wait inside select() */
1199 struct sockaddr_in inaddr; /* The socket address */
1200 int opt = 1; /* setsockopt flag */
1201 int iPort = mnPort;
1202
1203 while( iPort<mxPort ){
1204 memset(&inaddr, 0, sizeof(inaddr));
1205 inaddr.sin_family = AF_INET;
1206 inaddr.sin_addr.s_addr = INADDR_ANY;
1207 inaddr.sin_port = htons(iPort);
1208 listener = socket(AF_INET, SOCK_STREAM, 0);
1209 if( listener<0 ){
1210 iPort++;
1211 continue;
1212 }
1213
1214 /* if we can't terminate nicely, at least allow the socket to be reused */
1215 setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
1216
1217 if( bind(listener, (struct sockaddr*)&inaddr, sizeof(inaddr))<0 ){
1218 close(listener);
1219 iPort++;
1220 continue;
1221 }
1222 break;
1223 }
1224 if( iPort>mxPort ){
1225 if( mnPort==mxPort ){
1226 fossil_fatal("unable to open listening socket on ports %d", mnPort);
1227 }else{
1228 fossil_fatal("unable to open listening socket on any"
1229 " ports %d..%d", mnPort, mxPort);
1230 }
1231 }
1232 if( iPort>mxPort ) return 1;
1233 listen(listener,10);
1234 if( iPort>mnPort ){
1235 printf("Listening for HTTP requests on TCP port %d\n", iPort);
1236 fflush(stdout);
1237 }
1238 if( zBrowser ){
1239 zBrowser = mprintf(zBrowser, iPort);
1240 system(zBrowser);
1241 }
1242 while( 1 ){
1243 if( nchildren>MAX_PARALLEL ){
1244 /* Slow down if connections are arriving too fast */
1245
+7 -6
--- src/main.c
+++ src/main.c
@@ -704,11 +704,11 @@
704704
**
705705
** The "ui" command automatically starts a web browser after initializing
706706
** the web server.
707707
*/
708708
void cmd_webserver(void){
709
- int iPort;
709
+ int iPort, mxPort;
710710
const char *zPort;
711711
char *zBrowser;
712712
char *zBrowserCmd = 0;
713713
714714
g.thTrace = find_option("th-trace", 0, 0)!=0;
@@ -721,26 +721,27 @@
721721
db_must_be_within_tree();
722722
}else{
723723
db_open_repository(g.argv[2]);
724724
}
725725
if( zPort ){
726
- iPort = atoi(zPort);
726
+ iPort = mxPort = atoi(zPort);
727727
}else{
728728
iPort = db_get_int("http-port", 8080);
729
+ mxPort = iPort+100;
729730
}
730731
#ifndef __MINGW32__
731732
/* Unix implementation */
732733
if( g.argv[1][0]=='u' ){
733734
#if !defined(__DARWIN__) && !defined(__APPLE__)
734735
zBrowser = db_get("web-browser", "firefox");
735736
#else
736737
zBrowser = db_get("web-browser", "open");
737738
#endif
738
- zBrowserCmd = mprintf("%s http://localhost:%d/ &", zBrowser, iPort);
739
+ zBrowserCmd = mprintf("%s http://localhost:%%d/ &", zBrowser);
739740
}
740741
db_close();
741
- if( cgi_http_server(iPort, zBrowserCmd) ){
742
+ if( cgi_http_server(iPort, mxPort, zBrowserCmd) ){
742743
fossil_fatal("unable to listen on TCP socket %d", iPort);
743744
}
744745
g.httpIn = stdin;
745746
g.httpOut = stdout;
746747
if( g.fHttpTrace ){
@@ -756,11 +757,11 @@
756757
process_one_web_page();
757758
#else
758759
/* Win32 implementation */
759760
if( g.argv[1][0]=='u' ){
760761
zBrowser = db_get("web-browser", "start");
761
- zBrowserCmd = mprintf("%s http://127.0.0.1:%d/", zBrowser, iPort);
762
+ zBrowserCmd = mprintf("%s http://127.0.0.1:%%d/", zBrowser);
762763
}
763764
db_close();
764
- win32_http_server(iPort, zBrowserCmd);
765
+ win32_http_server(iPort, mxPort, zBrowserCmd);
765766
#endif
766767
}
767768
--- src/main.c
+++ src/main.c
@@ -704,11 +704,11 @@
704 **
705 ** The "ui" command automatically starts a web browser after initializing
706 ** the web server.
707 */
708 void cmd_webserver(void){
709 int iPort;
710 const char *zPort;
711 char *zBrowser;
712 char *zBrowserCmd = 0;
713
714 g.thTrace = find_option("th-trace", 0, 0)!=0;
@@ -721,26 +721,27 @@
721 db_must_be_within_tree();
722 }else{
723 db_open_repository(g.argv[2]);
724 }
725 if( zPort ){
726 iPort = atoi(zPort);
727 }else{
728 iPort = db_get_int("http-port", 8080);
 
729 }
730 #ifndef __MINGW32__
731 /* Unix implementation */
732 if( g.argv[1][0]=='u' ){
733 #if !defined(__DARWIN__) && !defined(__APPLE__)
734 zBrowser = db_get("web-browser", "firefox");
735 #else
736 zBrowser = db_get("web-browser", "open");
737 #endif
738 zBrowserCmd = mprintf("%s http://localhost:%d/ &", zBrowser, iPort);
739 }
740 db_close();
741 if( cgi_http_server(iPort, zBrowserCmd) ){
742 fossil_fatal("unable to listen on TCP socket %d", iPort);
743 }
744 g.httpIn = stdin;
745 g.httpOut = stdout;
746 if( g.fHttpTrace ){
@@ -756,11 +757,11 @@
756 process_one_web_page();
757 #else
758 /* Win32 implementation */
759 if( g.argv[1][0]=='u' ){
760 zBrowser = db_get("web-browser", "start");
761 zBrowserCmd = mprintf("%s http://127.0.0.1:%d/", zBrowser, iPort);
762 }
763 db_close();
764 win32_http_server(iPort, zBrowserCmd);
765 #endif
766 }
767
--- src/main.c
+++ src/main.c
@@ -704,11 +704,11 @@
704 **
705 ** The "ui" command automatically starts a web browser after initializing
706 ** the web server.
707 */
708 void cmd_webserver(void){
709 int iPort, mxPort;
710 const char *zPort;
711 char *zBrowser;
712 char *zBrowserCmd = 0;
713
714 g.thTrace = find_option("th-trace", 0, 0)!=0;
@@ -721,26 +721,27 @@
721 db_must_be_within_tree();
722 }else{
723 db_open_repository(g.argv[2]);
724 }
725 if( zPort ){
726 iPort = mxPort = atoi(zPort);
727 }else{
728 iPort = db_get_int("http-port", 8080);
729 mxPort = iPort+100;
730 }
731 #ifndef __MINGW32__
732 /* Unix implementation */
733 if( g.argv[1][0]=='u' ){
734 #if !defined(__DARWIN__) && !defined(__APPLE__)
735 zBrowser = db_get("web-browser", "firefox");
736 #else
737 zBrowser = db_get("web-browser", "open");
738 #endif
739 zBrowserCmd = mprintf("%s http://localhost:%%d/ &", zBrowser);
740 }
741 db_close();
742 if( cgi_http_server(iPort, mxPort, zBrowserCmd) ){
743 fossil_fatal("unable to listen on TCP socket %d", iPort);
744 }
745 g.httpIn = stdin;
746 g.httpOut = stdout;
747 if( g.fHttpTrace ){
@@ -756,11 +757,11 @@
757 process_one_web_page();
758 #else
759 /* Win32 implementation */
760 if( g.argv[1][0]=='u' ){
761 zBrowser = db_get("web-browser", "start");
762 zBrowserCmd = mprintf("%s http://127.0.0.1:%%d/", zBrowser);
763 }
764 db_close();
765 win32_http_server(iPort, mxPort, zBrowserCmd);
766 #endif
767 }
768
+32 -17
--- src/winhttp.c
+++ src/winhttp.c
@@ -134,37 +134,52 @@
134134
135135
/*
136136
** Start a listening socket and process incoming HTTP requests on
137137
** that socket.
138138
*/
139
-void win32_http_server(int iPort, char *zBrowser){
139
+void win32_http_server(int mnPort, int mxPort, char *zBrowser){
140140
WSADATA wd;
141141
SOCKET s;
142142
SOCKADDR_IN addr;
143143
int idCnt = 0;
144
+ int iPort = mnPort;
144145
145146
if( WSAStartup(MAKEWORD(1,1), &wd) ){
146147
fossil_fatal("unable to initialize winsock");
147148
}
148
- zTempPrefix = mprintf("fossil_server_P%d_", iPort);
149
- s = socket(AF_INET, SOCK_STREAM, 0);
150
- if( s==INVALID_SOCKET ){
151
- fossil_fatal("unable to create a socket");
152
- }
153
- addr.sin_family = AF_INET;
154
- addr.sin_port = htons(iPort);
155
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
156
- if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
157
- closesocket(s);
158
- fossil_fatal("unable to bind");
159
- }
160
- if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
161
- closesocket(s);
162
- fossil_fatal("unable to listen");
163
- }
149
+ while( iPort<mxPort ){
150
+ s = socket(AF_INET, SOCK_STREAM, 0);
151
+ if( s==INVALID_SOCKET ){
152
+ fossil_fatal("unable to create a socket");
153
+ }
154
+ addr.sin_family = AF_INET;
155
+ addr.sin_port = htons(iPort);
156
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
157
+ if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
158
+ closesocket(s);
159
+ iPort++;
160
+ continue;
161
+ }
162
+ if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
163
+ closesocket(s);
164
+ iPort++;
165
+ continue;
166
+ }
167
+ break;
168
+ }
169
+ if( iPort>mxPort ){
170
+ if( mnPort==mxPort ){
171
+ fossil_fatal("unable to open listening socket on ports %d", mnPort);
172
+ }else{
173
+ fossil_fatal("unable to open listening socket on any"
174
+ " ports %d..%d", mnPort, mxPort);
175
+ }
176
+ }
177
+ zTempPrefix = mprintf("fossil_server_P%d_", iPort);
164178
printf("Listening for HTTP requests on TCP port %d\n", iPort);
165179
if( zBrowser ){
180
+ zBrowser = mprintf(zBrowser, iPort);
166181
printf("Launch webbrowser: %s\n", zBrowser);
167182
system(zBrowser);
168183
}
169184
printf("Type Ctrl-C to stop the HTTP server\n");
170185
for(;;){
171186
--- src/winhttp.c
+++ src/winhttp.c
@@ -134,37 +134,52 @@
134
135 /*
136 ** Start a listening socket and process incoming HTTP requests on
137 ** that socket.
138 */
139 void win32_http_server(int iPort, char *zBrowser){
140 WSADATA wd;
141 SOCKET s;
142 SOCKADDR_IN addr;
143 int idCnt = 0;
 
144
145 if( WSAStartup(MAKEWORD(1,1), &wd) ){
146 fossil_fatal("unable to initialize winsock");
147 }
148 zTempPrefix = mprintf("fossil_server_P%d_", iPort);
149 s = socket(AF_INET, SOCK_STREAM, 0);
150 if( s==INVALID_SOCKET ){
151 fossil_fatal("unable to create a socket");
152 }
153 addr.sin_family = AF_INET;
154 addr.sin_port = htons(iPort);
155 addr.sin_addr.s_addr = htonl(INADDR_ANY);
156 if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
157 closesocket(s);
158 fossil_fatal("unable to bind");
159 }
160 if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
161 closesocket(s);
162 fossil_fatal("unable to listen");
163 }
 
 
 
 
 
 
 
 
 
 
 
 
 
164 printf("Listening for HTTP requests on TCP port %d\n", iPort);
165 if( zBrowser ){
 
166 printf("Launch webbrowser: %s\n", zBrowser);
167 system(zBrowser);
168 }
169 printf("Type Ctrl-C to stop the HTTP server\n");
170 for(;;){
171
--- src/winhttp.c
+++ src/winhttp.c
@@ -134,37 +134,52 @@
134
135 /*
136 ** Start a listening socket and process incoming HTTP requests on
137 ** that socket.
138 */
139 void win32_http_server(int mnPort, int mxPort, char *zBrowser){
140 WSADATA wd;
141 SOCKET s;
142 SOCKADDR_IN addr;
143 int idCnt = 0;
144 int iPort = mnPort;
145
146 if( WSAStartup(MAKEWORD(1,1), &wd) ){
147 fossil_fatal("unable to initialize winsock");
148 }
149 while( iPort<mxPort ){
150 s = socket(AF_INET, SOCK_STREAM, 0);
151 if( s==INVALID_SOCKET ){
152 fossil_fatal("unable to create a socket");
153 }
154 addr.sin_family = AF_INET;
155 addr.sin_port = htons(iPort);
156 addr.sin_addr.s_addr = htonl(INADDR_ANY);
157 if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
158 closesocket(s);
159 iPort++;
160 continue;
161 }
162 if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
163 closesocket(s);
164 iPort++;
165 continue;
166 }
167 break;
168 }
169 if( iPort>mxPort ){
170 if( mnPort==mxPort ){
171 fossil_fatal("unable to open listening socket on ports %d", mnPort);
172 }else{
173 fossil_fatal("unable to open listening socket on any"
174 " ports %d..%d", mnPort, mxPort);
175 }
176 }
177 zTempPrefix = mprintf("fossil_server_P%d_", iPort);
178 printf("Listening for HTTP requests on TCP port %d\n", iPort);
179 if( zBrowser ){
180 zBrowser = mprintf(zBrowser, iPort);
181 printf("Launch webbrowser: %s\n", zBrowser);
182 system(zBrowser);
183 }
184 printf("Type Ctrl-C to stop the HTTP server\n");
185 for(;;){
186

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button