ss: simplify code
Rather than copy-pasting code using sendmsg/recvmsg, use the simpler send() and recv() system calls.
This commit is contained in:
parent
c51577cd13
commit
2728f598bb
64
misc/ss.c
64
misc/ss.c
|
|
@ -2061,67 +2061,38 @@ static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
|
||||||
static int unix_show_netlink(struct filter *f, FILE *dump_fp)
|
static int unix_show_netlink(struct filter *f, FILE *dump_fp)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
struct sockaddr_nl nladdr;
|
|
||||||
struct {
|
struct {
|
||||||
struct nlmsghdr nlh;
|
struct nlmsghdr nlh;
|
||||||
struct unix_diag_req r;
|
struct unix_diag_req r;
|
||||||
} req;
|
} req;
|
||||||
struct msghdr msg;
|
|
||||||
char buf[8192];
|
char buf[8192];
|
||||||
struct iovec iov[3];
|
|
||||||
|
|
||||||
if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
|
if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
memset(&nladdr, 0, sizeof(nladdr));
|
memset(&req, 0, sizeof(req));
|
||||||
nladdr.nl_family = AF_NETLINK;
|
|
||||||
|
|
||||||
req.nlh.nlmsg_len = sizeof(req);
|
req.nlh.nlmsg_len = sizeof(req);
|
||||||
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
|
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
|
||||||
req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
|
req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
|
||||||
req.nlh.nlmsg_pid = 0;
|
|
||||||
req.nlh.nlmsg_seq = 123456;
|
req.nlh.nlmsg_seq = 123456;
|
||||||
memset(&req.r, 0, sizeof(req.r));
|
|
||||||
req.r.sdiag_family = AF_UNIX;
|
req.r.sdiag_family = AF_UNIX;
|
||||||
req.r.sdiag_protocol = 0; /* ignored */
|
|
||||||
req.r.udiag_states = f->states;
|
req.r.udiag_states = f->states;
|
||||||
req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
|
req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
|
||||||
|
|
||||||
iov[0] = (struct iovec){
|
if (send(fd, &req, sizeof(req), 0) < 0) {
|
||||||
.iov_base = &req,
|
|
||||||
.iov_len = sizeof(req)
|
|
||||||
};
|
|
||||||
|
|
||||||
msg = (struct msghdr) {
|
|
||||||
.msg_name = (void*)&nladdr,
|
|
||||||
.msg_namelen = sizeof(nladdr),
|
|
||||||
.msg_iov = iov,
|
|
||||||
.msg_iovlen = f->f ? 3 : 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (sendmsg(fd, &msg, 0) < 0) {
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
iov[0] = (struct iovec){
|
|
||||||
.iov_base = buf,
|
|
||||||
.iov_len = sizeof(buf)
|
|
||||||
};
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int status;
|
ssize_t status;
|
||||||
struct nlmsghdr *h;
|
struct nlmsghdr *h;
|
||||||
|
struct sockaddr_nl nladdr;
|
||||||
|
socklen_t slen = sizeof(nladdr);
|
||||||
|
|
||||||
msg = (struct msghdr) {
|
status = recvfrom(fd, buf, sizeof(buf), 0,
|
||||||
(void*)&nladdr, sizeof(nladdr),
|
(struct sockaddr *) &nladdr, &slen);
|
||||||
iov, 1,
|
|
||||||
NULL, 0,
|
|
||||||
0
|
|
||||||
};
|
|
||||||
|
|
||||||
status = recvmsg(fd, &msg, 0);
|
|
||||||
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -2130,8 +2101,7 @@ static int unix_show_netlink(struct filter *f, FILE *dump_fp)
|
||||||
}
|
}
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
fprintf(stderr, "EOF on netlink\n");
|
fprintf(stderr, "EOF on netlink\n");
|
||||||
close(fd);
|
goto close_it;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dump_fp)
|
if (dump_fp)
|
||||||
|
|
@ -2145,10 +2115,9 @@ static int unix_show_netlink(struct filter *f, FILE *dump_fp)
|
||||||
h->nlmsg_seq != 123456)
|
h->nlmsg_seq != 123456)
|
||||||
goto skip_it;
|
goto skip_it;
|
||||||
|
|
||||||
if (h->nlmsg_type == NLMSG_DONE) {
|
if (h->nlmsg_type == NLMSG_DONE)
|
||||||
close(fd);
|
goto close_it;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (h->nlmsg_type == NLMSG_ERROR) {
|
if (h->nlmsg_type == NLMSG_ERROR) {
|
||||||
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
|
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
|
||||||
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
|
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
|
||||||
|
|
@ -2172,15 +2141,14 @@ static int unix_show_netlink(struct filter *f, FILE *dump_fp)
|
||||||
skip_it:
|
skip_it:
|
||||||
h = NLMSG_NEXT(h, status);
|
h = NLMSG_NEXT(h, status);
|
||||||
}
|
}
|
||||||
if (msg.msg_flags & MSG_TRUNC) {
|
|
||||||
fprintf(stderr, "Message truncated\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (status) {
|
if (status) {
|
||||||
fprintf(stderr, "!!!Remnant of size %d\n", status);
|
fprintf(stderr, "!!!Remnant of size %zd\n", status);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close_it:
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue