]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/ss.c
Use libbsd for strlcpy if available
[mirror_iproute2.git] / misc / ss.c
index 3ed7e66962f3a2a243a0578e19d5c7fa375bfd8f..c472fbd9a8790d1b779a6fa8d96995dfc8cd42d6 100644 (file)
--- a/misc/ss.c
+++ b/misc/ss.c
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
+#include <sys/sysmacros.h>
 #include <netinet/in.h>
 #include <string.h>
+#ifdef HAVE_LIBBSD
+#include <bsd/string.h>
+#endif
 #include <errno.h>
 #include <netdb.h>
 #include <arpa/inet.h>
@@ -474,7 +478,6 @@ static FILE *generic_proc_open(const char *env, const char *name)
                                                        "net/packet")
 #define net_netlink_open()     generic_proc_open("PROC_NET_NETLINK", \
                                                        "net/netlink")
-#define slabinfo_open()                generic_proc_open("PROC_SLABINFO", "slabinfo")
 #define net_sockstat_open()    generic_proc_open("PROC_NET_SOCKSTAT", \
                                                        "net/sockstat")
 #define net_sockstat6_open()   generic_proc_open("PROC_NET_SOCKSTAT6", \
@@ -728,67 +731,6 @@ next:
        return cnt;
 }
 
-/* Get stats from slab */
-
-struct slabstat {
-       int socks;
-       int tcp_ports;
-       int tcp_tws;
-       int tcp_syns;
-       int skbs;
-};
-
-static struct slabstat slabstat;
-
-static int get_slabstat(struct slabstat *s)
-{
-       char buf[256];
-       FILE *fp;
-       int cnt;
-       static int slabstat_valid;
-       static const char * const slabstat_ids[] = {
-               "sock",
-               "tcp_bind_bucket",
-               "tcp_tw_bucket",
-               "tcp_open_request",
-               "skbuff_head_cache",
-       };
-
-       if (slabstat_valid)
-               return 0;
-
-       memset(s, 0, sizeof(*s));
-
-       fp = slabinfo_open();
-       if (!fp)
-               return -1;
-
-       cnt = sizeof(*s)/sizeof(int);
-
-       if (!fgets(buf, sizeof(buf), fp)) {
-               fclose(fp);
-               return -1;
-       }
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               int i;
-
-               for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) {
-                       if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
-                               sscanf(buf, "%*s%d", ((int *)s) + i);
-                               cnt--;
-                               break;
-                       }
-               }
-               if (cnt <= 0)
-                       break;
-       }
-
-       slabstat_valid = 1;
-
-       fclose(fp);
-       return 0;
-}
-
 static unsigned long long cookie_sk_get(const uint32_t *cookie)
 {
        return (((unsigned long long)cookie[1] << 31) << 1) | cookie[0];
@@ -1038,6 +980,7 @@ static int buf_update(int len)
 }
 
 /* Append content to buffer as part of the current field */
+__attribute__((format(printf, 1, 2)))
 static void out(const char *fmt, ...)
 {
        struct column *f = current_field;
@@ -1155,7 +1098,7 @@ static void print_header(void)
 {
        while (!field_is_last(current_field)) {
                if (!current_field->disabled)
-                       out(current_field->header);
+                       out("%s", current_field->header);
                field_next();
        }
 }
@@ -1320,7 +1263,7 @@ static void render(void)
        while (token) {
                /* Print left delimiter only if we already started a line */
                if (line_started++)
-                       printed = printf("%s", current_field->ldelim);
+                       printed = printf("%s", f->ldelim);
                else
                        printed = 0;
 
@@ -3216,8 +3159,7 @@ static int kill_inet_sock(struct nlmsghdr *h, void *arg, struct sockstat *s)
        return rtnl_talk(rth, &req.nlh, NULL);
 }
 
-static int show_one_inet_sock(const struct sockaddr_nl *addr,
-               struct nlmsghdr *h, void *arg)
+static int show_one_inet_sock(struct nlmsghdr *h, void *arg)
 {
        int err;
        struct inet_diag_arg *diag_arg = arg;
@@ -3372,7 +3314,7 @@ static int tcp_show(struct filter *f)
 {
        FILE *fp = NULL;
        char *buf = NULL;
-       int bufsize = 64*1024;
+       int bufsize = 1024*1024;
 
        if (!filter_af_get(f, AF_INET) && !filter_af_get(f, AF_INET6))
                return 0;
@@ -3387,27 +3329,6 @@ static int tcp_show(struct filter *f)
                return 0;
 
        /* Sigh... We have to parse /proc/net/tcp... */
-
-
-       /* Estimate amount of sockets and try to allocate
-        * huge buffer to read all the table at one read.
-        * Limit it by 16MB though. The assumption is: as soon as
-        * kernel was able to hold information about N connections,
-        * it is able to give us some memory for snapshot.
-        */
-       if (1) {
-               get_slabstat(&slabstat);
-
-               int guess = slabstat.socks+slabstat.tcp_syns;
-
-               if (f->states&(1<<SS_TIME_WAIT))
-                       guess += slabstat.tcp_tws;
-               if (guess > (16*1024*1024)/128)
-                       guess = (16*1024*1024)/128;
-               guess *= 128;
-               if (guess > bufsize)
-                       bufsize = guess;
-       }
        while (bufsize >= 64*1024) {
                if ((buf = malloc(bufsize)) != NULL)
                        break;
@@ -3629,8 +3550,7 @@ static void unix_stats_print(struct sockstat *s, struct filter *f)
        proc_ctx_print(s);
 }
 
-static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
-               void *arg)
+static int unix_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        struct filter *f = (struct filter *)arg;
        struct unix_diag_msg *r = NLMSG_DATA(nlh);
@@ -3687,6 +3607,21 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
                        out(" %c-%c",
                            mask & 1 ? '-' : '<', mask & 2 ? '-' : '>');
                }
+               if (tb[UNIX_DIAG_VFS]) {
+                       struct unix_diag_vfs *uv = RTA_DATA(tb[UNIX_DIAG_VFS]);
+
+                       out(" ino:%u dev:%u/%u", uv->udiag_vfs_ino, major(uv->udiag_vfs_dev),
+                                                minor(uv->udiag_vfs_dev));
+               }
+               if (tb[UNIX_DIAG_ICONS]) {
+                       int len = RTA_PAYLOAD(tb[UNIX_DIAG_ICONS]);
+                       __u32 *peers = RTA_DATA(tb[UNIX_DIAG_ICONS]);
+                       int i;
+
+                       out(" peers:");
+                       for (i = 0; i < len / sizeof(__u32); i++)
+                               out(" %u", peers[i]);
+               }
        }
 
        return 0;
@@ -3724,6 +3659,8 @@ static int unix_show_netlink(struct filter *f)
        req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
        if (show_mem)
                req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
+       if (show_details)
+               req.r.udiag_show |= UDIAG_SHOW_VFS | UDIAG_SHOW_ICONS;
 
        return handle_netlink_request(f, &req.nlh, sizeof(req), unix_show_sock);
 }
@@ -3907,8 +3844,7 @@ static void packet_show_ring(struct packet_diag_ring *ring)
        out(",features:0x%x", ring->pdr_features);
 }
 
-static int packet_show_sock(const struct sockaddr_nl *addr,
-               struct nlmsghdr *nlh, void *arg)
+static int packet_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        const struct filter *f = arg;
        struct packet_diag_msg *r = NLMSG_DATA(nlh);
@@ -4197,8 +4133,7 @@ static int netlink_show_one(struct filter *f,
        return 0;
 }
 
-static int netlink_show_sock(const struct sockaddr_nl *addr,
-               struct nlmsghdr *nlh, void *arg)
+static int netlink_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        struct filter *f = (struct filter *)arg;
        struct netlink_diag_msg *r = NLMSG_DATA(nlh);
@@ -4321,8 +4256,7 @@ static void vsock_stats_print(struct sockstat *s, struct filter *f)
        proc_ctx_print(s);
 }
 
-static int vsock_show_sock(const struct sockaddr_nl *addr,
-                          struct nlmsghdr *nlh, void *arg)
+static int vsock_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        struct filter *f = (struct filter *)arg;
        struct vsock_diag_msg *r = NLMSG_DATA(nlh);
@@ -4375,8 +4309,7 @@ static void tipc_sock_addr_print(struct rtattr *net_addr, struct rtattr *id)
 
 }
 
-static int tipc_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
-                         void *arg)
+static int tipc_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        struct rtattr *stat[TIPC_NLA_SOCK_STAT_MAX + 1] = {};
        struct rtattr *attrs[TIPC_NLA_SOCK_MAX + 1] = {};
@@ -4464,8 +4397,7 @@ struct sock_diag_msg {
        __u8 sdiag_family;
 };
 
-static int generic_show_sock(const struct sockaddr_nl *addr,
-               struct nlmsghdr *nlh, void *arg)
+static int generic_show_sock(struct nlmsghdr *nlh, void *arg)
 {
        struct sock_diag_msg *r = NLMSG_DATA(nlh);
        struct inet_diag_arg inet_arg = { .f = arg, .protocol = IPPROTO_MAX };
@@ -4475,19 +4407,19 @@ static int generic_show_sock(const struct sockaddr_nl *addr,
        case AF_INET:
        case AF_INET6:
                inet_arg.rth = inet_arg.f->rth_for_killing;
-               ret = show_one_inet_sock(addr, nlh, &inet_arg);
+               ret = show_one_inet_sock(nlh, &inet_arg);
                break;
        case AF_UNIX:
-               ret = unix_show_sock(addr, nlh, arg);
+               ret = unix_show_sock(nlh, arg);
                break;
        case AF_PACKET:
-               ret = packet_show_sock(addr, nlh, arg);
+               ret = packet_show_sock(nlh, arg);
                break;
        case AF_NETLINK:
-               ret = netlink_show_sock(addr, nlh, arg);
+               ret = netlink_show_sock(nlh, arg);
                break;
        case AF_VSOCK:
-               ret = vsock_show_sock(addr, nlh, arg);
+               ret = vsock_show_sock(nlh, arg);
                break;
        default:
                ret = -1;
@@ -4666,23 +4598,15 @@ static int print_summary(void)
        if (get_snmp_int("Tcp:", "CurrEstab", &tcp_estab) < 0)
                perror("ss: get_snmpstat");
 
-       get_slabstat(&slabstat);
-
-       printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
+       printf("Total: %d\n", s.socks);
 
-       printf("TCP:   %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
-              s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
-              tcp_estab,
-              s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
-              s.tcp_orphans,
-              slabstat.tcp_syns,
-              s.tcp_tws, slabstat.tcp_tws,
-              slabstat.tcp_ports
-              );
+       printf("TCP:   %d (estab %d, closed %d, orphaned %d, timewait %d)\n",
+              s.tcp_total + s.tcp_tws, tcp_estab,
+              s.tcp_total - (s.tcp4_hashed + s.tcp6_hashed - s.tcp_tws),
+              s.tcp_orphans, s.tcp_tws);
 
        printf("\n");
        printf("Transport Total     IP        IPv6\n");
-       printf("*         %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
        printf("RAW       %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
        printf("UDP       %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
        printf("TCP       %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);