Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/sockstat/sockstat.c
Show First 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | |||||
struct addr { | struct addr { | ||||
struct sockaddr_storage address; | struct sockaddr_storage address; | ||||
unsigned int encaps_port; | unsigned int encaps_port; | ||||
int state; | int state; | ||||
struct addr *next; | struct addr *next; | ||||
}; | }; | ||||
struct sock { | struct sock { | ||||
void *socket; | kvaddr_t socket; | ||||
void *pcb; | kvaddr_t pcb; | ||||
int shown; | int shown; | ||||
int vflag; | int vflag; | ||||
int family; | int family; | ||||
int proto; | int proto; | ||||
int state; | int state; | ||||
const char *protoname; | const char *protoname; | ||||
char stack[TCP_FUNCTION_NAME_LEN_MAX]; | char stack[TCP_FUNCTION_NAME_LEN_MAX]; | ||||
struct addr *laddr; | struct addr *laddr; | ||||
▲ Show 20 Lines • Show All 663 Lines • ▼ Show 20 Lines | for (;;) { | ||||
xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); | xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); | ||||
if (xug >= exug) | if (xug >= exug) | ||||
break; | break; | ||||
xup = (struct xunpcb *)xug; | xup = (struct xunpcb *)xug; | ||||
if (xup->xu_len != sizeof(*xup)) { | if (xup->xu_len != sizeof(*xup)) { | ||||
warnx("struct xunpcb size mismatch"); | warnx("struct xunpcb size mismatch"); | ||||
goto out; | goto out; | ||||
} | } | ||||
if ((xup->unp_conn == NULL && !opt_l) || | if ((xup->unp_conn == 0 && !opt_l) || | ||||
(xup->unp_conn != NULL && !opt_c)) | (xup->unp_conn != 0 && !opt_c)) | ||||
continue; | continue; | ||||
if ((sock = calloc(1, sizeof(*sock))) == NULL) | if ((sock = calloc(1, sizeof(*sock))) == NULL) | ||||
err(1, "malloc()"); | err(1, "malloc()"); | ||||
if ((laddr = calloc(1, sizeof *laddr)) == NULL) | if ((laddr = calloc(1, sizeof *laddr)) == NULL) | ||||
err(1, "malloc()"); | err(1, "malloc()"); | ||||
if ((faddr = calloc(1, sizeof *faddr)) == NULL) | if ((faddr = calloc(1, sizeof *faddr)) == NULL) | ||||
err(1, "malloc()"); | err(1, "malloc()"); | ||||
sock->socket = xup->xu_socket.xso_so; | sock->socket = xup->xu_socket.xso_so; | ||||
sock->pcb = xup->xu_unpp; | sock->pcb = xup->xu_unpp; | ||||
sock->proto = proto; | sock->proto = proto; | ||||
sock->family = AF_UNIX; | sock->family = AF_UNIX; | ||||
sock->protoname = protoname; | sock->protoname = protoname; | ||||
if (xup->xu_addr.sun_family == AF_UNIX) | if (xup->xu_addr.sun_family == AF_UNIX) | ||||
laddr->address = | laddr->address = | ||||
*(struct sockaddr_storage *)(void *)&xup->xu_addr; | *(struct sockaddr_storage *)(void *)&xup->xu_addr; | ||||
else if (xup->unp_conn != NULL) | else if (xup->unp_conn != 0) | ||||
*(void **)&(faddr->address) = xup->unp_conn; | *(kvaddr_t*)&(faddr->address) = xup->unp_conn; | ||||
laddr->next = NULL; | laddr->next = NULL; | ||||
faddr->next = NULL; | faddr->next = NULL; | ||||
sock->laddr = laddr; | sock->laddr = laddr; | ||||
sock->faddr = faddr; | sock->faddr = faddr; | ||||
hash = (int)((uintptr_t)sock->socket % HASHSIZE); | hash = (int)((uintptr_t)sock->socket % HASHSIZE); | ||||
sock->next = sockhash[hash]; | sock->next = sockhash[hash]; | ||||
sockhash[hash] = sock; | sockhash[hash] = sock; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | default: | ||||
return "UNKNOWN"; | return "UNKNOWN"; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
displaysock(struct sock *s, int pos) | displaysock(struct sock *s, int pos) | ||||
{ | { | ||||
void *p; | kvaddr_t p; | ||||
int hash, first, offset; | int hash, first, offset; | ||||
struct addr *laddr, *faddr; | struct addr *laddr, *faddr; | ||||
struct sock *s_tmp; | struct sock *s_tmp; | ||||
while (pos < 29) | while (pos < 29) | ||||
pos += xprintf(" "); | pos += xprintf(" "); | ||||
pos += xprintf("%s", s->protoname); | pos += xprintf("%s", s->protoname); | ||||
if (s->vflag & INP_IPV4) | if (s->vflag & INP_IPV4) | ||||
Show All 29 Lines | case AF_UNIX: | ||||
errx(1, "laddr = %p or faddr = %p is NULL", | errx(1, "laddr = %p or faddr = %p is NULL", | ||||
(void *)laddr, (void *)faddr); | (void *)laddr, (void *)faddr); | ||||
/* server */ | /* server */ | ||||
if (laddr->address.ss_len > 0) { | if (laddr->address.ss_len > 0) { | ||||
pos += printaddr(&laddr->address); | pos += printaddr(&laddr->address); | ||||
break; | break; | ||||
} | } | ||||
/* client */ | /* client */ | ||||
p = *(void **)&(faddr->address); | p = *(kvaddr_t*)&(faddr->address); | ||||
if (p == NULL) { | if (p == 0) { | ||||
pos += xprintf("(not connected)"); | pos += xprintf("(not connected)"); | ||||
offset += opt_w ? 92 : 44; | offset += opt_w ? 92 : 44; | ||||
break; | break; | ||||
} | } | ||||
pos += xprintf("-> "); | pos += xprintf("-> "); | ||||
for (hash = 0; hash < HASHSIZE; ++hash) { | for (hash = 0; hash < HASHSIZE; ++hash) { | ||||
for (s_tmp = sockhash[hash]; | for (s_tmp = sockhash[hash]; | ||||
s_tmp != NULL; | s_tmp != NULL; | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | if (opt_s) { | ||||
printf(" %-12s", "CONN STATE"); | printf(" %-12s", "CONN STATE"); | ||||
} | } | ||||
if (opt_S) | if (opt_S) | ||||
printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK"); | printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK"); | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
setpassent(1); | setpassent(1); | ||||
for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) { | for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) { | ||||
if (xf->xf_data == NULL) | if (xf->xf_data == 0) | ||||
continue; | continue; | ||||
if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid)) | if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid)) | ||||
continue; | continue; | ||||
hash = (int)((uintptr_t)xf->xf_data % HASHSIZE); | hash = (int)((uintptr_t)xf->xf_data % HASHSIZE); | ||||
for (s = sockhash[hash]; s != NULL; s = s->next) { | for (s = sockhash[hash]; s != NULL; s = s->next) { | ||||
if ((void *)s->socket != xf->xf_data) | if (s->socket != xf->xf_data) | ||||
continue; | continue; | ||||
if (!check_ports(s)) | if (!check_ports(s)) | ||||
continue; | continue; | ||||
s->shown = 1; | s->shown = 1; | ||||
pos = 0; | pos = 0; | ||||
if ((pwd = getpwuid(xf->xf_uid)) == NULL) | if ((pwd = getpwuid(xf->xf_uid)) == NULL) | ||||
pos += xprintf("%lu ", (u_long)xf->xf_uid); | pos += xprintf("%lu ", (u_long)xf->xf_uid); | ||||
else | else | ||||
▲ Show 20 Lines • Show All 148 Lines • Show Last 20 Lines |