]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blobdiff - misc/ss.c
ss: simplify code
[lisovros/iproute2_canprio.git] / misc / ss.c
index 38eed29db150f1b757d30334cbb8665a695fb97d..5414f7557ab8c92dd6ba9541021778bd2b766f5f 100644 (file)
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -34,7 +34,9 @@
 #include "SNAPSHOT.h"
 
 #include <netinet/tcp.h>
+#include <linux/sock_diag.h>
 #include <linux/inet_diag.h>
+#include <linux/unix_diag.h>
 
 int resolve_hosts = 0;
 int resolve_services = 1;
@@ -195,90 +197,153 @@ static FILE *ephemeral_ports_open(void)
        return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
 }
 
-int find_users(unsigned ino, char *buf, int buflen)
+struct user_ent {
+       struct user_ent *next;
+       unsigned int    ino;
+       int             pid;
+       int             fd;
+       char            process[0];
+};
+
+#define USER_ENT_HASH_SIZE     256
+struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
+
+static int user_ent_hashfn(unsigned int ino)
 {
-       char pattern[64];
-       int  pattern_len;
-       char *ptr = buf;
-       char name[1024];
-       DIR *dir;
-       struct dirent *d;
-       int cnt = 0;
-       int nameoff;
+       int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
 
-       if (!ino)
-               return 0;
+       return val & (USER_ENT_HASH_SIZE - 1);
+}
+
+static void user_ent_add(unsigned int ino, const char *process, int pid, int fd)
+{
+       struct user_ent *p, **pp;
+       int str_len;
+
+       str_len = strlen(process) + 1;
+       p = malloc(sizeof(struct user_ent) + str_len);
+       if (!p)
+               abort();
+       p->next = NULL;
+       p->ino = ino;
+       p->pid = pid;
+       p->fd = fd;
+       strcpy(p->process, process);
+
+       pp = &user_ent_hash[user_ent_hashfn(ino)];
+       p->next = *pp;
+       *pp = p;
+}
 
-       sprintf(pattern, "socket:[%u]", ino);
-       pattern_len = strlen(pattern);
+static void user_ent_hash_build(void)
+{
+       const char *root = getenv("PROC_ROOT") ? : "/proc/";
+       struct dirent *d;
+       char name[1024];
+       int nameoff;
+       DIR *dir;
 
-       strncpy(name, getenv("PROC_ROOT") ? : "/proc/", sizeof(name)/2);
-       name[sizeof(name)/2] = 0;
-       if (strlen(name) == 0 ||
-           name[strlen(name)-1] != '/')
+       strcpy(name, root);
+       if (strlen(name) == 0 || name[strlen(name)-1] != '/')
                strcat(name, "/");
+
        nameoff = strlen(name);
-       if ((dir = opendir(name)) == NULL)
-               return 0;
+
+       dir = opendir(name);
+       if (!dir)
+               return;
 
        while ((d = readdir(dir)) != NULL) {
-               DIR *dir1;
                struct dirent *d1;
-               int pid;
-               int pos;
-               char crap;
                char process[16];
+               int pid, pos;
+               DIR *dir1;
+               char crap;
 
                if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
                        continue;
 
-               sprintf(name+nameoff, "%d/fd/", pid);
+               sprintf(name + nameoff, "%d/fd/", pid);
                pos = strlen(name);
                if ((dir1 = opendir(name)) == NULL)
                        continue;
 
-               process[0] = 0;
+               process[0] = '\0';
 
                while ((d1 = readdir(dir1)) != NULL) {
-                       int fd, n;
+                       const char *pattern = "socket:[";
+                       unsigned int ino;
                        char lnk[64];
+                       int fd;
+                       ssize_t link_len;
 
                        if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
                                continue;
 
                        sprintf(name+pos, "%d", fd);
-                       n = readlink(name, lnk, sizeof(lnk)-1);
-                       if (n != pattern_len ||
-                           memcmp(lnk, pattern, n))
+
+                       link_len = readlink(name, lnk, sizeof(lnk)-1);
+                       if (link_len == -1)
                                continue;
+                       lnk[link_len] = '\0';
 
-                       if (ptr-buf >= buflen-1)
-                               break;
+                       if (strncmp(lnk, pattern, strlen(pattern)))
+                               continue;
 
-                       if (process[0] == 0) {
+                       sscanf(lnk, "socket:[%u]", &ino);
+
+                       if (process[0] == '\0') {
                                char tmp[1024];
                                FILE *fp;
-                               snprintf(tmp, sizeof(tmp), "%s/%d/stat",
-                                        getenv("PROC_ROOT") ? : "/proc", pid);
+
+                               snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
                                if ((fp = fopen(tmp, "r")) != NULL) {
                                        fscanf(fp, "%*d (%[^)])", process);
                                        fclose(fp);
                                }
                        }
 
-                       snprintf(ptr, buflen-(ptr-buf), "(\"%s\",%d,%d),", process, pid, fd);
-                       ptr += strlen(ptr);
-                       cnt++;
+                       user_ent_add(ino, process, pid, fd);
                }
                closedir(dir1);
        }
        closedir(dir);
+}
+
+int find_users(unsigned ino, char *buf, int buflen)
+{
+       struct user_ent *p;
+       int cnt = 0;
+       char *ptr;
+
+       if (!ino)
+               return 0;
+
+       p = user_ent_hash[user_ent_hashfn(ino)];
+       ptr = buf;
+       while (p) {
+               if (p->ino != ino)
+                       goto next;
+
+               if (ptr - buf >= buflen - 1)
+                       break;
+
+               snprintf(ptr, buflen - (ptr - buf),
+                        "(\"%s\",%d,%d),",
+                        p->process, p->pid, p->fd);
+               ptr += strlen(ptr);
+               cnt++;
+
+       next:
+               p = p->next;
+       }
+
        if (ptr != buf)
-               ptr[-1] = 0;
+               ptr[-1] = '\0';
+
        return cnt;
 }
 
-
 /* Get stats from slab */
 
 struct slabstat
@@ -461,6 +526,7 @@ void init_service_resolver(void)
                                }
                        }
                }
+               pclose(fp);
        }
 }
 
@@ -746,7 +812,7 @@ static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
        {
                if (!(*bytecode=malloc(4))) abort();
                ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
-               return 8;
+               return 4;
        }
                case SSF_DCOND:
                case SSF_SCOND:
@@ -1299,10 +1365,12 @@ static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r)
                                printf(" sack");
                        if (info->tcpi_options & TCPI_OPT_ECN)
                                printf(" ecn");
+                       if (info->tcpi_options & TCPI_OPT_ECN_SEEN)
+                               printf(" ecnseen");
                }
 
                if (tb[INET_DIAG_CONG])
-                       printf("%s", (char *) RTA_DATA(tb[INET_DIAG_CONG]));
+                       printf(" %s", (char *) RTA_DATA(tb[INET_DIAG_CONG]));
 
                if (info->tcpi_options & TCPI_OPT_WSCALE)
                        printf(" wscale:%d,%d", info->tcpi_snd_wscale,
@@ -1393,9 +1461,10 @@ static int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f)
                if (r->idiag_uid)
                        printf(" uid:%u", (unsigned)r->idiag_uid);
                printf(" ino:%u", r->idiag_inode);
-               printf(" sk:%08x", r->id.idiag_cookie[0]);
+               printf(" sk:");
                if (r->id.idiag_cookie[1] != 0)
                        printf("%08x", r->id.idiag_cookie[1]);
+               printf("%08x", r->id.idiag_cookie[0]);
        }
        if (show_mem || show_tcpinfo) {
                printf("\n\t");
@@ -1494,6 +1563,7 @@ static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype)
                }
                if (status == 0) {
                        fprintf(stderr, "EOF on netlink\n");
+                       close(fd);
                        return 0;
                }
 
@@ -1503,13 +1573,16 @@ static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype)
                h = (struct nlmsghdr*)buf;
                while (NLMSG_OK(h, status)) {
                        int err;
+                       struct inet_diag_msg *r = NLMSG_DATA(h);
 
                        if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
                            h->nlmsg_seq != 123456)
                                goto skip_it;
 
-                       if (h->nlmsg_type == NLMSG_DONE)
+                       if (h->nlmsg_type == NLMSG_DONE) {
+                               close(fd);
                                return 0;
+                       }
                        if (h->nlmsg_type == NLMSG_ERROR) {
                                struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
                                if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
@@ -1518,12 +1591,19 @@ static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype)
                                        errno = -err->error;
                                        perror("TCPDIAG answers");
                                }
+                               close(fd);
                                return 0;
                        }
                        if (!dump_fp) {
+                               if (!(f->families & (1<<r->idiag_family))) {
+                                       h = NLMSG_NEXT(h, status);
+                                       continue;
+                               }
                                err = tcp_show_sock(h, NULL);
-                               if (err < 0)
+                               if (err < 0) {
+                                       close(fd);
                                        return err;
+                               }
                        }
 
 skip_it:
@@ -1538,6 +1618,7 @@ skip_it:
                        exit(1);
                }
        }
+       close(fd);
        return 0;
 }
 
@@ -1921,6 +2002,157 @@ void unix_list_print(struct unixstat *list, struct filter *f)
        }
 }
 
+static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
+{
+       struct unix_diag_msg *r = NLMSG_DATA(nlh);
+       struct rtattr *tb[UNIX_DIAG_MAX+1];
+       char name[128];
+       int peer_ino;
+       int rqlen;
+
+       parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
+                    nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
+
+       if (netid_width)
+               printf("%-*s ", netid_width,
+                               r->udiag_type == SOCK_STREAM ? "u_str" : "u_dgr");
+       if (state_width)
+               printf("%-*s ", state_width, sstate_name[r->udiag_state]);
+
+       if (tb[UNIX_DIAG_RQLEN])
+               rqlen = *(int *)RTA_DATA(tb[UNIX_DIAG_RQLEN]);
+       else
+               rqlen = 0;
+
+       printf("%-6d %-6d ", rqlen, 0);
+
+       if (tb[UNIX_DIAG_NAME]) {
+               int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
+
+               memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
+               name[len] = '\0';
+               if (name[0] == '\0')
+                       name[0] = '@';
+       } else
+               sprintf(name, "*");
+
+       if (tb[UNIX_DIAG_PEER])
+               peer_ino = *(int *)RTA_DATA(tb[UNIX_DIAG_PEER]);
+       else
+               peer_ino = 0;
+
+       printf("%*s %-*d %*s %-*d",
+                       addr_width, name,
+                       serv_width, r->udiag_ino,
+                       addr_width, "*", /* FIXME */
+                       serv_width, peer_ino);
+
+       if (show_users) {
+               char ubuf[4096];
+               if (find_users(r->udiag_ino, ubuf, sizeof(ubuf)) > 0)
+                       printf(" users:(%s)", ubuf);
+       }
+
+       printf("\n");
+
+       return 0;
+}
+
+static int unix_show_netlink(struct filter *f, FILE *dump_fp)
+{
+       int fd;
+       struct {
+               struct nlmsghdr nlh;
+               struct unix_diag_req r;
+       } req;
+       char    buf[8192];
+
+       if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
+               return -1;
+
+       memset(&req, 0, sizeof(req));
+       req.nlh.nlmsg_len = sizeof(req);
+       req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
+       req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
+       req.nlh.nlmsg_seq = 123456;
+
+       req.r.sdiag_family = AF_UNIX;
+       req.r.udiag_states = f->states;
+       req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
+
+       if (send(fd, &req, sizeof(req), 0) < 0) {
+               close(fd);
+               return -1;
+       }
+
+       while (1) {
+               ssize_t status;
+               struct nlmsghdr *h;
+               struct sockaddr_nl nladdr;
+               socklen_t slen = sizeof(nladdr);
+
+               status = recvfrom(fd, buf, sizeof(buf), 0,
+                                 (struct sockaddr *) &nladdr, &slen);
+               if (status < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       perror("OVERRUN");
+                       continue;
+               }
+               if (status == 0) {
+                       fprintf(stderr, "EOF on netlink\n");
+                       goto close_it;
+               }
+
+               if (dump_fp)
+                       fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
+
+               h = (struct nlmsghdr*)buf;
+               while (NLMSG_OK(h, status)) {
+                       int err;
+
+                       if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
+                           h->nlmsg_seq != 123456)
+                               goto skip_it;
+
+                       if (h->nlmsg_type == NLMSG_DONE)
+                               goto close_it;
+
+                       if (h->nlmsg_type == NLMSG_ERROR) {
+                               struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
+                               if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
+                                       fprintf(stderr, "ERROR truncated\n");
+                               } else {
+                                       errno = -err->error;
+                                       if (errno != ENOENT)
+                                               fprintf(stderr, "UDIAG answers %d\n", errno);
+                               }
+                               close(fd);
+                               return -1;
+                       }
+                       if (!dump_fp) {
+                               err = unix_show_sock(h, f);
+                               if (err < 0) {
+                                       close(fd);
+                                       return err;
+                               }
+                       }
+
+skip_it:
+                       h = NLMSG_NEXT(h, status);
+               }
+
+               if (status) {
+                       fprintf(stderr, "!!!Remnant of size %zd\n", status);
+                       exit(1);
+               }
+       }
+
+close_it:
+       close(fd);
+       return 0;
+}
+
 int unix_show(struct filter *f)
 {
        FILE *fp;
@@ -1930,6 +2162,10 @@ int unix_show(struct filter *f)
        int  cnt;
        struct unixstat *list = NULL;
 
+       if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
+           && unix_show_netlink(f, NULL) == 0)
+               return 0;
+
        if ((fp = net_unix_open()) == NULL)
                return -1;
        fgets(buf, sizeof(buf)-1, fp);
@@ -1990,7 +2226,7 @@ int unix_show(struct filter *f)
                        cnt = 0;
                }
        }
-
+       fclose(fp);
        if (list) {
                unix_list_print(list, f);
                unix_list_free(list);
@@ -2325,12 +2561,9 @@ int print_summary(void)
        return 0;
 }
 
-
-static void usage(void) __attribute__((noreturn));
-
-static void usage(void)
+static void _usage(FILE *dest)
 {
-       fprintf(stderr,
+       fprintf(dest,
 "Usage: ss [ OPTIONS ]\n"
 "       ss [ OPTIONS ] [ FILTER ]\n"
 "   -h, --help         this message\n"
@@ -2356,12 +2589,26 @@ static void usage(void)
 "   -x, --unix         display only Unix domain sockets\n"
 "   -f, --family=FAMILY display sockets of type FAMILY\n"
 "\n"
-"   -A, --query=QUERY\n"
+"   -A, --query=QUERY, --socket=QUERY\n"
 "       QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
 "\n"
+"   -D, --diag=FILE     Dump raw information about TCP sockets to FILE\n"
 "   -F, --filter=FILE   read filter information from FILE\n"
 "       FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
                );
+}
+
+static void help(void) __attribute__((noreturn));
+static void help(void)
+{
+       _usage(stdout);
+       exit(0);
+}
+
+static void usage(void) __attribute__((noreturn));
+static void usage(void)
+{
+       _usage(stderr);
        exit(-1);
 }
 
@@ -2413,8 +2660,9 @@ static const struct option long_opts[] = {
        { "packet", 0, 0, '0' },
        { "family", 1, 0, 'f' },
        { "socket", 1, 0, 'A' },
+       { "query", 1, 0, 'A' },
        { "summary", 0, 0, 's' },
-       { "diag", 0, 0, 'D' },
+       { "diag", 1, 0, 'D' },
        { "filter", 1, 0, 'F' },
        { "version", 0, 0, 'V' },
        { "help", 0, 0, 'h' },
@@ -2460,6 +2708,7 @@ int main(int argc, char *argv[])
                        break;
                case 'p':
                        show_users++;
+                       user_ent_hash_build();
                        break;
                case 'd':
                        current_filter.dbs |= (1<<DCCP_DB);
@@ -2485,7 +2734,7 @@ int main(int argc, char *argv[])
                        current_filter.states = SS_ALL;
                        break;
                case 'l':
-                       current_filter.states = (1<<SS_LISTEN);
+                       current_filter.states = (1<<SS_LISTEN) | (1<<SS_CLOSE);
                        break;
                case '4':
                        preferred_family = AF_INET;
@@ -2508,7 +2757,7 @@ int main(int argc, char *argv[])
                        else if (strcmp(optarg, "netlink") == 0)
                                preferred_family = AF_NETLINK;
                        else if (strcmp(optarg, "help") == 0)
-                               usage();
+                               help();
                        else {
                                fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
                                usage();
@@ -2590,6 +2839,7 @@ int main(int argc, char *argv[])
                        exit(0);
                case 'h':
                case '?':
+                       help();
                default:
                        usage();
                }
@@ -2622,9 +2872,7 @@ int main(int argc, char *argv[])
                int mask2;
                if (preferred_family == AF_INET ||
                    preferred_family == AF_INET6) {
-                       mask2= (1<<TCP_DB);
-                       if (!do_default)
-                               mask2 = (1<<UDP_DB)|(1<<RAW_DB);
+                       mask2= current_filter.dbs;
                } else if (preferred_family == AF_PACKET) {
                        mask2 = PACKET_DBM;
                } else if (preferred_family == AF_UNIX) {