aboutsummaryrefslogtreecommitdiff
path: root/networking/netstat.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-12-03 10:45:14 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-12-03 10:45:14 +0000
commit7221c8c22dd527700204eb5cc4d0651af4273f4f (patch)
tree9f45c168efd2a83c7a700dfc2161e9fa2823a222 /networking/netstat.c
parent2bec5139964ebf814ffddbedd526651361be69f8 (diff)
downloadbusybox-w32-7221c8c22dd527700204eb5cc4d0651af4273f4f.tar.gz
busybox-w32-7221c8c22dd527700204eb5cc4d0651af4273f4f.tar.bz2
busybox-w32-7221c8c22dd527700204eb5cc4d0651af4273f4f.zip
lineedit: reduce stack usage
netstat: reduce stack usage; fix handling of NULs in unix socket filenames static.has_inode 1 - -1 do_info 119 116 -3 deinit_S 60 51 -9 unix_do_one 578 451 -127 parse_and_put_prompt 966 825 -141 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 0/4 up/down: 0/-281) Total: -281 bytes
Diffstat (limited to 'networking/netstat.c')
-rw-r--r--networking/netstat.c70
1 files changed, 37 insertions, 33 deletions
diff --git a/networking/netstat.c b/networking/netstat.c
index 11f141947..d86c2ff5e 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -148,7 +148,7 @@ static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int
148 return host_port; 148 return host_port;
149} 149}
150 150
151static void tcp_do_one(int lnr, const char *line) 151static void tcp_do_one(int lnr, char *line)
152{ 152{
153 char local_addr[64], rem_addr[64]; 153 char local_addr[64], rem_addr[64];
154 char more[512]; 154 char more[512];
@@ -201,7 +201,7 @@ static void tcp_do_one(int lnr, const char *line)
201 } 201 }
202} 202}
203 203
204static void udp_do_one(int lnr, const char *line) 204static void udp_do_one(int lnr, char *line)
205{ 205{
206 char local_addr[64], rem_addr[64]; 206 char local_addr[64], rem_addr[64];
207 const char *state_str; 207 const char *state_str;
@@ -283,7 +283,7 @@ static void udp_do_one(int lnr, const char *line)
283 } 283 }
284} 284}
285 285
286static void raw_do_one(int lnr, const char *line) 286static void raw_do_one(int lnr, char *line)
287{ 287{
288 char local_addr[64], rem_addr[64]; 288 char local_addr[64], rem_addr[64];
289 char more[512]; 289 char more[512];
@@ -339,31 +339,37 @@ static void raw_do_one(int lnr, const char *line)
339 } 339 }
340} 340}
341 341
342static void unix_do_one(int nr, const char *line) 342static void unix_do_one(int nr, char *line)
343{ 343{
344 static smallint has_inode = 0;
345
346 char path[PATH_MAX], ss_flags[32];
347 const char *ss_proto, *ss_state, *ss_type;
348 int num, state, type, inode;
349 void *d;
350 unsigned long refcnt, proto, unix_flags; 344 unsigned long refcnt, proto, unix_flags;
345 unsigned long inode;
346 int type, state;
347 int num, path_ofs;
348 void *d;
349 const char *ss_proto, *ss_state, *ss_type;
350 char ss_flags[32];
351 351
352 if (nr == 0) { 352 if (nr == 0)
353 if (strstr(line, "Inode")) 353 return; /* skip header */
354 has_inode = 1; 354
355 return; 355 {
356 char *last = last_char_is(line, '\n');
357 if (last)
358 *last = '\0';
356 } 359 }
357 path[0] = '\0'; 360
358 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s", 361 /* 2.6.15 may report lines like "... @/tmp/fam-user-^@^@^@^@^@^@^@..."
359 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path); 362 * (those ^@ are NUL bytes). fgets sees them as tons of empty lines. */
360 if (num < 6) { 363 if (!line[0])
361 bb_error_msg("warning, got bogus unix line");
362 return; 364 return;
363 }
364 if (!has_inode)
365 sprintf(path, "%d", inode);
366 365
366 path_ofs = 0; /* paranoia */
367 num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %n",
368 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, &path_ofs);
369 if (num < 7) {
370 bb_error_msg("got bogus unix line '%s'", line);
371 return;
372 }
367 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) { 373 if ((flags & (NETSTAT_LISTENING|NETSTAT_CONNECTED)) != (NETSTAT_LISTENING|NETSTAT_CONNECTED)) {
368 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) { 374 if ((state == SS_UNCONNECTED) && (unix_flags & SO_ACCEPTCON)) {
369 if (!(flags & NETSTAT_LISTENING)) 375 if (!(flags & NETSTAT_LISTENING))
@@ -439,13 +445,9 @@ static void unix_do_one(int nr, const char *line)
439 strcat(ss_flags, "N "); 445 strcat(ss_flags, "N ");
440 strcat(ss_flags, "]"); 446 strcat(ss_flags, "]");
441 447
442 printf("%-5s %-6ld %-11s %-10s %-13s ", 448 printf("%-5s %-6ld %-11s %-10s %-13s %6lu %s\n",
443 ss_proto, refcnt, ss_flags, ss_type, ss_state); 449 ss_proto, refcnt, ss_flags, ss_type, ss_state, inode,
444 if (has_inode) 450 line + path_ofs);
445 printf("%-6d ", inode);
446 else
447 printf("- ");
448 puts(path);
449} 451}
450 452
451#define _PATH_PROCNET_UDP "/proc/net/udp" 453#define _PATH_PROCNET_UDP "/proc/net/udp"
@@ -456,10 +458,11 @@ static void unix_do_one(int nr, const char *line)
456#define _PATH_PROCNET_RAW6 "/proc/net/raw6" 458#define _PATH_PROCNET_RAW6 "/proc/net/raw6"
457#define _PATH_PROCNET_UNIX "/proc/net/unix" 459#define _PATH_PROCNET_UNIX "/proc/net/unix"
458 460
459static void do_info(const char *file, const char *name, void (*proc)(int, const char *)) 461static void do_info(const char *file, const char *name, void (*proc)(int, char *))
460{ 462{
461 int lnr = 0; 463 int lnr;
462 FILE *procinfo; 464 FILE *procinfo;
465 char *buffer;
463 466
464 procinfo = fopen(file, "r"); 467 procinfo = fopen(file, "r");
465 if (procinfo == NULL) { 468 if (procinfo == NULL) {
@@ -470,13 +473,14 @@ static void do_info(const char *file, const char *name, void (*proc)(int, const
470 } 473 }
471 return; 474 return;
472 } 475 }
476 lnr = 0;
473 do { 477 do {
474 char *buffer = xmalloc_fgets(procinfo); 478 buffer = xmalloc_fgets(procinfo);
475 if (buffer) { 479 if (buffer) {
476 (proc)(lnr++, buffer); 480 (proc)(lnr++, buffer);
477 free(buffer); 481 free(buffer);
478 } 482 }
479 } while (!feof(procinfo)); 483 } while (buffer);
480 fclose(procinfo); 484 fclose(procinfo);
481} 485}
482 486