- Timestamp:
- Nov 17, 2008, 8:17:22 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_port2proc.c
r193 r195 60 60 #define FIL__ _("sh_port2proc.c") 61 61 62 struct sock_store { 63 unsigned long sock; 64 size_t pid; 65 char * path; 66 char * user; 67 struct sock_store * next; 68 }; 69 70 /* /proc: 71 * linux: /proc/pid/exe 72 * freebsd: /proc/pid/file 73 * solaris10: /proc/pid/path/a.out 74 */ 75 static void get_user_and_path (struct sock_store * add) 76 { 77 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len); 78 79 char path[128]; 80 char * buf; 81 struct stat sbuf; 82 int len; 83 char * tmp; 84 85 sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid); 86 87 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode)) 88 { 89 goto linkread; 90 } 91 92 sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid); 93 94 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode)) 95 { 96 goto linkread; 97 } 98 99 sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid); 100 101 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode)) 102 { 103 goto linkread; 104 } 105 106 return; 107 108 linkread: 109 110 buf = SH_ALLOC(PATH_MAX); 111 len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */ 112 len = (len >= PATH_MAX) ? (PATH_MAX-1) : len; 113 114 if (len > 0) 115 { 116 buf[len] = '\0'; 117 add->path = buf; 118 } 119 else 120 { 121 SH_FREE(buf); 122 } 123 124 add->user = SH_ALLOC(USER_MAX); 125 tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX); 126 127 if (!tmp) 128 sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid); 129 130 return; 131 } 132 62 133 #endif 63 134 … … 92 163 struct sock_store * next; 93 164 }; 94 95 /* /proc:96 * linux: /proc/pid/exe97 * freebsd: /proc/pid/file98 * solaris10: /proc/pid/path/a.out99 */100 static void get_user_and_path (struct sock_store * add)101 {102 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);103 104 char path[128];105 char * buf;106 struct stat sbuf;107 int len;108 char * tmp;109 110 sl_snprintf (path, sizeof(path), "/proc/%ld/exe", (unsigned long) add->pid);111 112 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))113 {114 goto linkread;115 }116 117 sl_snprintf (path, sizeof(path), "/proc/%ld/file", (unsigned long) add->pid);118 119 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))120 {121 goto linkread;122 }123 124 sl_snprintf (path, sizeof(path), "/proc/%ld/path/a.out", (unsigned long) add->pid);125 126 if (0 == retry_lstat(FIL__, __LINE__, path, &sbuf) && S_ISLNK(sbuf.st_mode))127 {128 goto linkread;129 }130 131 return;132 133 linkread:134 135 buf = SH_ALLOC(PATH_MAX);136 len = readlink(path, buf, PATH_MAX); /* flawfinder: ignore */137 len = (len >= PATH_MAX) ? (PATH_MAX-1) : len;138 139 if (len > 0)140 {141 buf[len] = '\0';142 add->path = buf;143 }144 else145 {146 SH_FREE(buf);147 }148 149 add->user = SH_ALLOC(USER_MAX);150 tmp = sh_unix_getUIDname (SH_ERR_ALL, sbuf.st_uid, add->user, USER_MAX);151 152 if (!tmp)153 sl_snprintf (add->user, USER_MAX, "%ld", (unsigned long) sbuf.st_uid);154 155 return;156 }157 165 158 166 #if defined(__linux__) … … 441 449 static int opt_c = 0; /* Show connected sockets */ 442 450 static int opt_l = 1; /* Show listening sockets */ 443 static int opt_u = 0; /* Show Unix domain sockets */444 451 static int opt_v = 0; /* Verbose mode */ 445 452 … … 467 474 if (len > 0) 468 475 { 469 void xbuf = SH_ALLOC(len); 470 memcpy(xbuf, buf, len0); 471 SH_FREE(buf); 476 void * xbuf = SH_ALLOC(len); 477 if (buf) 478 { 479 if (len0 <= len) 480 memcpy(xbuf, buf, len0); 481 else 482 memset(xbuf, '\0', len); 483 SH_FREE(buf); 484 } 472 485 return xbuf; 473 486 } … … 542 555 543 556 buf = NULL; 544 bufsize = 8192;557 bufsize = 1000; 545 558 bufsize0 = bufsize; 546 559 retry = 5; … … 552 565 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0) 553 566 break; 554 567 if (errno == ENOENT) 555 568 goto out; 556 569 if (errno != ENOMEM) 557 570 { 558 571 SH_MUTEX_LOCK(mutex_thread_nolog); 559 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN, 572 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 573 0, MSG_E_SUBGEN, 560 574 _("sysctlbyname()"), 561 575 _("gather_inet")); 562 576 SH_MUTEX_UNLOCK(mutex_thread_nolog); 577 SH_FREE(buf); 563 578 return; 564 579 } … … 674 689 } 675 690 out: 676 free(buf); 691 if (buf) 692 SH_FREE(buf); 677 693 } 678 694 … … 689 705 if (errno != ENOMEM) 690 706 { 707 volatile int status = errno; 691 708 SH_MUTEX_LOCK(mutex_thread_nolog); 692 709 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, … … 702 719 if (errno != ENOMEM) 703 720 { 721 volatile int status = errno; 704 722 SH_MUTEX_LOCK(mutex_thread_nolog); 705 723 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN, … … 727 745 if (errno != ESRCH) 728 746 { 747 volatile int status = errno; 729 748 SH_MUTEX_LOCK(mutex_thread_nolog); 730 749 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_SUBGEN, … … 741 760 char * user, size_t userlen) 742 761 { 762 int n, hash; 743 763 struct xfile *xf; 744 764 struct in_addr * haddr; 765 struct sock * s; 745 766 746 767 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) { … … 756 777 break; 757 778 779 if (!s) 780 continue; 781 782 /* fprintf(stderr, "FIXME: %d %d, %d %d, %d %d, %d, %d\n", s->proto, proto, 783 s->family, AF_INET, 784 sport, ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port), 785 (int) xf->xf_uid, (int)xf->xf_pid); 786 */ 787 758 788 if (s->proto != proto) 759 789 continue; … … 762 792 continue; 763 793 764 if (sport != ntohs(((struct sockaddr_in *) s)->sin_port))794 if (sport != ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port)) 765 795 continue; 766 796 767 haddr = &((struct sockaddr_in *)s)->sin_addr; 768 if (haddr.s_addr == saddr->s_addr) 797 haddr = &((struct sockaddr_in *)(&s->laddr))->sin_addr; 798 799 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*haddr)); */ 800 /* fprintf(stderr, "FIXME: %s\n", inet_ntoa(*saddr)); */ 801 802 if (haddr->s_addr == saddr->s_addr || inet_lnaof(*saddr) == INADDR_ANY || inet_lnaof(*haddr) == INADDR_ANY) 769 803 { 770 char * tmp = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen); 771 if (!tmp) 772 sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid); 773 return sh_util_strdup(getprocname(xf->xf_pid)); 804 struct sock_store try; 805 806 try.pid = xf->xf_pid; 807 try.path = NULL; 808 try.user = NULL; 809 get_user_and_path (&try); /* Try to get info from /proc */ 810 811 if (try.path == NULL) 812 { 813 extern char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len); 814 char * tmp = sh_unix_getUIDname (SH_ERR_ALL, xf->xf_uid, user, userlen); 815 if (!tmp) 816 sl_snprintf (user, userlen, "%ld", (unsigned long) xf->xf_uid); 817 return sh_util_strdup(getprocname(xf->xf_pid)); 818 } 819 else 820 { 821 sl_strlcpy(user, try.user, userlen); 822 SH_FREE(try.user); 823 return try.path; 824 } 774 825 } 775 826 } 827 sl_strlcpy(user, "-", userlen); 828 return sh_util_strdup("-"); 776 829 } 777 830 … … 798 851 799 852 for (i = 0; i < HASHSIZE; ++i) 800 sockdel(sockhash[i]); 853 { 854 sockdel(sockhash[i]); 855 sockhash[i] = NULL; 856 } 801 857 802 858 /* Inet connections
Note:
See TracChangeset
for help on using the changeset viewer.