Browse code

Catch sigpipe

git-svn: trunk@3052

Nigel Horne authored on 2007/05/03 05:56:44
Showing 1 changed files
... ...
@@ -35,6 +35,7 @@
35 35
 #include <unistd.h>
36 36
 #include <pthread.h>
37 37
 #include <string.h>
38
+#include <signal.h>
38 39
 
39 40
 #define	PORT	3310
40 41
 #define	LEN	128
... ...
@@ -53,7 +54,8 @@ static struct machine {
53 53
 } machines[] = {
54 54
 	{	"eric",		0,	-1	},
55 55
 	/*{	"motorola",	0,	-1	},*/
56
-	{	"ultra60",	0,	-1	},
56
+	/*{	"ultra60",	0,	-1	},*/
57
+	{	"mac",		0,	-1	},
57 58
 	{	"localhost",	0,	-1	},
58 59
 	{	NULL,		0,	-1	}
59 60
 };
... ...
@@ -90,6 +92,9 @@ main(int argc, char **argv)
90 90
 		if(m->sock < 0)
91 91
 			fprintf(stderr, "%s is down\n", m->name);
92 92
 	}
93
+
94
+	signal(SIGPIPE, SIG_IGN);
95
+
93 96
 	while(*++argv)
94 97
 		dir(*argv);
95 98
 
... ...
@@ -174,7 +179,7 @@ dir(const char *dirname)
174 174
 			pthread_create(&tids[nthreads], NULL, scan, &args[nthreads]);
175 175
 			nthreads++;
176 176
 		}
177
-		printf("Scanning %s\n", name);
177
+		/*printf("Scanning %s\n", name);*/
178 178
 		founddiffs = 0;
179 179
 		while(--nthreads >= 0)
180 180
 			/* TODO: timeout */
... ...
@@ -199,8 +204,8 @@ dir(const char *dirname)
199 199
 			}
200 200
 		}
201 201
 
202
-		if(!founddiffs)
203
-			printf("%s passed\n", name);
202
+		/*if(!founddiffs)
203
+			printf("%s passed\n", name);*/
204 204
 	}
205 205
 	closedir(d);
206 206
 }
... ...
@@ -308,8 +313,6 @@ scan(void *v)
308 308
 		return NULL;
309 309
 	}
310 310
 
311
-	shutdown(sock, SHUT_RD);
312
-
313 311
 	nbytes = recv(m->sock, buf, sizeof(buf), 0);
314 312
 	if(nbytes <= 0) {
315 313
 		perror(m->name);
... ...
@@ -339,12 +342,22 @@ scan(void *v)
339 339
 		fclose(fin);
340 340
 		return NULL;
341 341
 	}
342
-	while((buflen = fread(buf, 1, sizeof(buf), fin)) > 0)
343
-		if(send(sock, buf, buflen, 0) != (ssize_t)buflen) {
344
-			/* Proably hit scanstream len */
345
-			perror(m->name);
342
+
343
+	shutdown(sock, SHUT_RD);
344
+
345
+	while((buflen = fread(buf, 1, sizeof(buf), fin)) > 0) {
346
+		ssize_t sent = send(sock, buf, buflen, 0);
347
+
348
+		if(sent != (ssize_t)buflen) {
349
+			/* Probably hit scanstream len */
350
+			if(sent < 0)
351
+				perror(m->name);
352
+			else
353
+				fprintf(stderr, "%s: only sent %d bytes of %d to %s\n",
354
+					args->filename, sent, buflen, m->name);
346 355
 			break;
347 356
 		}
357
+	}
348 358
 
349 359
 	close(sock);
350 360
 	fclose(fin);
... ...
@@ -355,7 +368,7 @@ scan(void *v)
355 355
 		perror(m->name);
356 356
 		return NULL;
357 357
 	}
358
-	args->ret[nbytes - 1] = '\0';	/* remove the trailing \n */
358
+	args->ret[(nbytes) ? (nbytes - 1) : 0] = '\0';	/* remove the trailing \n */
359 359
 
360 360
 	return NULL;
361 361
 }