Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Jun 2012 09:13:01 +0400
From:      Stanislav Zaharov <root.vagner@gmail.com>
To:        Eitan Adler <lists@eitanadler.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: sockstat.c
Message-ID:  <CAFDDX6_DhOOm8oC_wTPz5JttLbsCZ9mDHQWtooG8qi0JHjnDaA@mail.gmail.com>
In-Reply-To: <CAF6rxgns6VSKk=zyLQNvv0m_ZGB3w15bsVOeQFtDT0mKpcaamw@mail.gmail.com>
References:  <CAFDDX6_eYwfrV6et4w%2B2%2Bcy3hGuUTY4y3Rj8BHcU_cLOTdWL%2Bg@mail.gmail.com> <CAF6rxgns6VSKk=zyLQNvv0m_ZGB3w15bsVOeQFtDT0mKpcaamw@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
Sorry, patch now as an attachment

On Thu, Jun 21, 2012 at 3:55 AM, Eitan Adler <lists@eitanadler.com> wrote:

> On 20 June 2012 16:48, Stanislav Zaharov <root.vagner@gmail.com> wrote:
> > Hi all!
> > I changed the sockstat.c, function display() - added a resize of fields,
> > depending on the length of the data. Can I suggest you look at the
> > resulting source
> > code?
>
> no patch is attached. Can you resend or upload somewhere and give the url?
>
>
> --
> Eitan Adler
>



-- 
Dear                          ,



Respectfully,
Stanislav Putrya
System & network administrator
LTD "RMK Kovsh"
icq: 328585847
e-mail: root.vagner@gmail.com
e-mail: vagner_rider@bk.ru

[-- Attachment #2 --]
*** /usr/src/usr.bin/sockstat/sockstat.c	2011-11-11 08:20:22.000000000 +0400
--- /usr/src/sockstat.c	2012-06-21 09:04:33.000000000 +0400
***************
*** 57,73 ****
--- 57,76 ----
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  
+ #define MAXLEN 100
+ 
  static int	 opt_4;		/* Show IPv4 sockets */
  static int	 opt_6;		/* Show IPv6 sockets */
  static int	 opt_c;		/* Show connected sockets */
  static int	 opt_L;		/* Don't show IPv4 or IPv6 loopback sockets */
  static int	 opt_l;		/* Show listening sockets */
  static int	 opt_u;		/* Show Unix domain sockets */
  static int	 opt_v;		/* Verbose mode */
+ static int	 opt_r;		/* Resize mode */
  
  /*
   * Default protocols to use if no -P was defined.
   */
  static const char *default_protos[] = {"tcp", "udp", "divert" };
***************
*** 93,102 ****
--- 96,107 ----
  	struct sockaddr_storage laddr;
  	struct sockaddr_storage faddr;
  	struct sock *next;
  };
  
+ struct memory_type *mtp;
+ 
  #define HASHSIZE 1009
  static struct sock *sockhash[HASHSIZE];
  
  static struct xfile *xfiles;
  static int nxfiles;
***************
*** 568,590 ****
  	if (CHK_PORT(port))
  		return (1);
  	return (0);
  }
  
  static void
  display(void)
  {
  	struct passwd *pwd;
  	struct xfile *xf;
  	struct sock *s;
  	void *p;
! 	int hash, n, pos;
  
- 	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
- 	    "USER", "COMMAND", "PID", "FD", "PROTO",
- 	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
  	setpassent(1);
  	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
  		if (xf->xf_data == NULL)
  			continue;
  		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
  		for (s = sockhash[hash]; s != NULL; s = s->next)
--- 573,696 ----
  	if (CHK_PORT(port))
  		return (1);
  	return (0);
  }
  
+ static void 
+ display_resize(int *pcl)
+ {
+ 	struct passwd *pwd;
+ 	struct xfile *xf;
+ 	struct sock *s;
+ 	int hash, n, lnname, maxlnname, lnuid, maxlnuid, lnpid, maxlnpid, lnfd, maxlnfd, lnproto, maxlnproto;
+ 	int i;
+ 	const int delim=3;
+ 	char buf[100];
+ 
+ 	maxlnproto = maxlnfd = maxlnpid = maxlnuid = maxlnname = 0;	
+ 		for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
+ 			if (xf->xf_data == NULL)
+ 				continue;
+ 			hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
+ 			for (s = sockhash[hash]; s != NULL; s = s->next)
+ 				if ((void *)s->socket == xf->xf_data)
+ 					break;
+ 			if (s == NULL)
+ 				continue;
+ 			if (!check_ports(s))
+ 				continue;
+ 			if ((pwd = getpwuid(xf->xf_uid)) == NULL) {
+ 				sprintf(buf, "%d", xf->xf_uid);
+ 				lnuid = strlen(buf);
+ 				memset(buf, 0, sizeof(buf));
+ 			}
+ 			else
+ 				lnuid = strlen(pwd->pw_name);
+ 			lnuid = lnuid < MAXLEN ? lnuid:MAXLEN;	
+ 			maxlnuid = lnuid > maxlnuid ? lnuid:maxlnuid;
+ 
+ 			lnname = strlen(getprocname(xf->xf_pid));
+ 			lnname = lnname < MAXLEN ? lnname:MAXLEN;
+ 			maxlnname = lnname > maxlnname ? lnname:maxlnname;
+ 
+ 			sprintf(buf, "%d", xf->xf_pid);
+ 			lnpid = strlen(buf);
+ 			memset(buf, 0, sizeof(buf));
+ 			maxlnpid = lnpid > maxlnpid ? lnpid:maxlnpid;
+ 
+ 			sprintf(buf, "%d", xf->xf_fd);
+ 			lnfd = strlen(buf);
+ 			memset(buf, 0, sizeof(buf));
+ 			maxlnfd = lnfd > maxlnfd ? lnfd:maxlnfd;
+ 
+ 			lnproto = strlen(s->protoname);
+ 			maxlnproto = lnproto > maxlnproto ? lnproto:maxlnproto;
+ 		}
+ 		for ( i=0; i<7; ++i) {
+ 			switch(i) {
+ 			    case 0: *pcl = maxlnuid+delim;
+ 				    break;
+ 			    case 1: *(pcl+i) = *(pcl+(i-1))+maxlnname+delim;
+ 				    break;
+ 			    case 2: *(pcl+i) = *(pcl+(i-1))+maxlnpid+delim;
+ 				    break;
+ 			    case 3: *(pcl+i) = *(pcl+(i-1))+maxlnfd+delim;
+ 				    break;
+ 			    case 4: *(pcl+i) = *(pcl+(i-1))+maxlnproto+delim;
+ 				    break;
+ 			    case 5: *(pcl+i) = *(pcl+(i-1))+17+delim;
+ 				    break;
+ 			    case 6: *(pcl+i) = *(pcl+(i-1))+delim;
+ 				    break;
+ 			    default: break;	    
+ 		        }
+ 		}
+ }
+ 
  static void
  display(void)
  {
  	struct passwd *pwd;
  	struct xfile *xf;
  	struct sock *s;
  	void *p;
! 	int hash, n, headpos, pos;
! 	int col[7]={9, 20, 26, 29, 36, 58, 70}, *pcl;
! 	pcl=col;
  
  	setpassent(1);
+ //	Get max length for word	
+ 	if (opt_r == 1 ) {
+ 		headpos = 0;
+ 		display_resize(col);
+ 		headpos += xprintf("%.4s", "USER");
+ 		while (headpos < *pcl)
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.7s", "COMMAND");
+ 		while (headpos < *(pcl+1))
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.7s", "PID");	
+ 		while (headpos < *(pcl+2))
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.2s", "FD");	
+ 		while (headpos < *(pcl+3))
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.5s", "PROTO");	
+ 		while (headpos < *(pcl+4))
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.13s", "LOCAL ADDRESS");	
+ 		while (headpos < *(pcl+6))
+ 			headpos += xprintf(" ");
+ 		headpos += xprintf("%.15s", "FOREIGN ADDRESS");	
+ 		xprintf("\n");
+ 	}
+ 	else {
+ 		*(pcl+6) = *(pcl+5);
+ 		printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
+ 		    "USER", "COMMAND", "PID", "FD", "PROTO",
+ 		    "LOCAL ADDRESS", "FOREIGN ADDRESS");
+ 	};	    
  	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
  		if (xf->xf_data == NULL)
  			continue;
  		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
  		for (s = sockhash[hash]; s != NULL; s = s->next)
***************
*** 596,631 ****
  			continue;
  		pos = 0;
  		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
  			pos += xprintf("%lu ", (u_long)xf->xf_uid);
  		else
! 			pos += xprintf("%s ", pwd->pw_name);
! 		while (pos < 9)
  			pos += xprintf(" ");
! 		pos += xprintf("%.10s", getprocname(xf->xf_pid));
! 		while (pos < 20)
  			pos += xprintf(" ");
  		pos += xprintf("%lu ", (u_long)xf->xf_pid);
! 		while (pos < 26)
  			pos += xprintf(" ");
  		pos += xprintf("%d ", xf->xf_fd);
! 		while (pos < 29)
  			pos += xprintf(" ");
  		pos += xprintf("%s", s->protoname);
  		if (s->vflag & INP_IPV4)
  			pos += xprintf("4 ");
  		if (s->vflag & INP_IPV6)
  			pos += xprintf("6 ");
! 		while (pos < 36)
  			pos += xprintf(" ");
  		switch (s->family) {
  		case AF_INET:
  		case AF_INET6:
  			pos += printaddr(s->family, &s->laddr);
! 			if (s->family == AF_INET6 && pos >= 58)
  				pos += xprintf(" ");
! 			while (pos < 58)
  				pos += xprintf(" ");
  			pos += printaddr(s->family, &s->faddr);
  			break;
  		case AF_UNIX:
  			/* server */
--- 702,740 ----
  			continue;
  		pos = 0;
  		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
  			pos += xprintf("%lu ", (u_long)xf->xf_uid);
  		else
! 			pos += xprintf("%.100s ", pwd->pw_name);
! 		while (pos < *pcl)
  			pos += xprintf(" ");
! 		if (opt_r == 1 )	
! 			pos += xprintf("%.100s ", getprocname(xf->xf_pid));
! 		else
! 			pos += xprintf("%.10s", getprocname(xf->xf_pid));
! 		while (pos < *(pcl+1))
  			pos += xprintf(" ");
  		pos += xprintf("%lu ", (u_long)xf->xf_pid);
! 		while (pos < *(pcl+2))
  			pos += xprintf(" ");
  		pos += xprintf("%d ", xf->xf_fd);
! 		while (pos < *(pcl+3))
  			pos += xprintf(" ");
  		pos += xprintf("%s", s->protoname);
  		if (s->vflag & INP_IPV4)
  			pos += xprintf("4 ");
  		if (s->vflag & INP_IPV6)
  			pos += xprintf("6 ");
! 		while (pos < *(pcl+4))
  			pos += xprintf(" ");
  		switch (s->family) {
  		case AF_INET:
  		case AF_INET6:
  			pos += printaddr(s->family, &s->laddr);
! 			if (s->family == AF_INET6 && pos >= *(pcl+4))
  				pos += xprintf(" ");
! 			while (pos < *(pcl+6))
  				pos += xprintf(" ");
  			pos += printaddr(s->family, &s->faddr);
  			break;
  		case AF_UNIX:
  			/* server */
***************
*** 681,701 ****
  
  static void
  usage(void)
  {
  	fprintf(stderr,
! 	    "Usage: sockstat [-46cLlu] [-p ports] [-P protocols]\n");
  	exit(1);
  }
  
  int
  main(int argc, char *argv[])
  {
  	int protos_defined = -1;
  	int o, i;
  
! 	while ((o = getopt(argc, argv, "46cLlp:P:uv")) != -1)
  		switch (o) {
  		case '4':
  			opt_4 = 1;
  			break;
  		case '6':
--- 790,810 ----
  
  static void
  usage(void)
  {
  	fprintf(stderr,
! 	    "Usage: sockstat [-46cLlur] [-p ports] [-P protocols]\n");
  	exit(1);
  }
  
  int
  main(int argc, char *argv[])
  {
  	int protos_defined = -1;
  	int o, i;
  
! 	while ((o = getopt(argc, argv, "46cLlp:P:uv:r")) != -1)
  		switch (o) {
  		case '4':
  			opt_4 = 1;
  			break;
  		case '6':
***************
*** 711,720 ****
--- 820,832 ----
  			opt_l = 1;
  			break;
  		case 'p':
  			parse_ports(optarg);
  			break;
+ 		case 'r':
+ 			opt_r = 1;
+ 			break;
  		case 'P':
  			protos_defined = parse_protos(optarg);
  			break;
  		case 'u':
  			opt_u = 1;

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFDDX6_DhOOm8oC_wTPz5JttLbsCZ9mDHQWtooG8qi0JHjnDaA>