From patchwork Wed May 25 02:51:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860685 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4E5CC433F5 for ; Wed, 25 May 2022 02:52:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243697AbiEYCwF (ORCPT ); Tue, 24 May 2022 22:52:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243695AbiEYCwD (ORCPT ); Tue, 24 May 2022 22:52:03 -0400 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FB3415FCB for ; Tue, 24 May 2022 19:52:01 -0700 (PDT) Received: by mail-qt1-x831.google.com with SMTP id g21so7643237qtg.5 for ; Tue, 24 May 2022 19:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JnonedHAdyfrX6TlDYWWRsxrhqD0olRWt8hKAYQiBw4=; b=RU6PGyhlGpPWWmLnMkqPUQ33LO9k0IsN++A+KejvbwD/4mXXEMPMk0xw62u0Zf9bo8 oYjdzTmdaZFmhiAKmeIcb9EH32LHZV1OIeAy/81TkSEIKwUcDCPNgXTRqVr5Y/J+M8iq xrp04NcMfTkzYoQ6WSj6VZWJZzHlYKS7j5hNk6LlZ6PAGO4QrblN/0mIrF17JK/PAK9g vqOn8LJO7klGjmD22bKGqKVLTSnNOJ097zA8+xzEQF8BInYUOHJbHHGS3rGC2mPKlPEw 0dX9eHJ5L2/JbivSpcPdT4N7NeVTZHIUXUYSxBf+kPdfk90fqe6M85hd4cQcDh0nH/cn gWag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JnonedHAdyfrX6TlDYWWRsxrhqD0olRWt8hKAYQiBw4=; b=HoYiX0GYxIWUQGQHXUSJnWMsQbsXDuNob8UNKj11pnVLP4hlNs3AIxW4WA6IyXLDo8 O8fZn+en3hx1h5KSuZTxW+9gbOO9Bxp641h3fVP5FbS/g+WQNXZq+Ua77dLAn/WoqQuJ DOl2058bUFlqrxqNnaZseUQ7jOGBVSDujuKkLguQIIAhKYxUQQzCr3yQPuCyNdpLQGg1 dlLjzY1hr076zR4/uvdpdrJ7nZ6p+NxFcJO/AEi/txGptv/7VWzNjtGk5mQwQM79kk2S ASdAus8j6h7gmxTDhDU6f1pDcqbOmBXlT21k8WGR9CwGyYqdgy0FSA4IO6ffpQBtWct3 i/0Q== X-Gm-Message-State: AOAM5311Wfb0HVhLHj5wpc/Nmn1Jn5T4nQ8uE/l+7bzdr+mfebJIBDkO VUgUwq0VPKl+UBMt5nD0kQ== X-Google-Smtp-Source: ABdhPJwsLd/XFa+rFLXkkbwAaunxLIj0vnIcptuJVogADJJnDO7fi1x5oIYl/zscsCp8JnS3SlhnMw== X-Received: by 2002:a05:622a:15c8:b0:2f3:d7ae:bae6 with SMTP id d8-20020a05622a15c800b002f3d7aebae6mr23023325qty.106.1653447120539; Tue, 24 May 2022 19:52:00 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id z193-20020a3765ca000000b0069fc13ce207sm542314qkb.56.2022.05.24.19.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:52:00 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 1/7] ss: Use assignment-suppression character in sscanf() Date: Tue, 24 May 2022 19:51:48 -0700 Message-Id: <5f0a2388e77ae0388a92505c0a24b4e9d00570fb.1653446538.git.peilin.ye@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye Use the '*' assignment-suppression character, instead of an inappropriately named temporary variable. Signed-off-by: Peilin Ye --- misc/ss.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 4b3ca9c4e86b..aa9da7e45e53 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -621,9 +621,8 @@ static void user_ent_hash_build(void) char *p; int pid, pos; DIR *dir1; - char crap; - if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1) + if (sscanf(d->d_name, "%d%*c", &pid) != 1) continue; if (getpidcon(pid, &pid_context) != 0) @@ -647,7 +646,7 @@ static void user_ent_hash_build(void) ssize_t link_len; char tmp[1024]; - if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1) + if (sscanf(d1->d_name, "%d%*c", &fd) != 1) continue; snprintf(name+pos, sizeof(name) - pos, "%d", fd); From patchwork Wed May 25 02:52:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860686 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C178C433F5 for ; Wed, 25 May 2022 02:52:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243705AbiEYCwT (ORCPT ); Tue, 24 May 2022 22:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243699AbiEYCwS (ORCPT ); Tue, 24 May 2022 22:52:18 -0400 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 940D334B9F for ; Tue, 24 May 2022 19:52:17 -0700 (PDT) Received: by mail-qk1-x736.google.com with SMTP id 14so10320140qkl.6 for ; Tue, 24 May 2022 19:52:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UP/VYgEFC7/NG5lOowYSDeCzE3G0VZTx41Klfsp8O4o=; b=gdB6Dai3QurMDAdCN1N3fj1hUMOG5d/Ssxa7S4ZA7PNaS7mDY92BvC1L6M2c1Fdjaa MKOtM7c8tvemJG+PFA5bQ2CtI7FDkCGp8VzG74YBalyKJbVKxLLyJUSMMtjkKI4GtAMG gZ5TOHlH3ZlQmBhqdtwwcDIS6CyJnG3IQC4petVgc0QFt+TWUNAJ/7jqEFxSzpCK0H8j +16HqG7Jaa3DwfKBRA6gayofhIwtOBGYuqWgxFmNw5no2CXPRBvasRvEaPRAAkbx8bBi MCGtjzjmWGug7h5/Ou8v2JzcsrgVPiSfWnTF4SO8OSe2eQXsJJP17MiPTzcASQP4ux82 HEDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UP/VYgEFC7/NG5lOowYSDeCzE3G0VZTx41Klfsp8O4o=; b=78Tt05g+tP9SR/zxKnHm9tiBlwb7GIwkWviCuIib54XixG/KLnVRXXlN/9W0SiRSII 8tqoMunXQmN9H6cctp4RteWQSi7Wwhj2VBtIVClHC3jfTvNXVpCqUTyR+EcfOPABvKZP kx3/zoBmlRhbDBbXZA1F3YnncT2LErx8V9a+TA8Jf049TdI+adS/bMZELB7VYHcBelTV H1/iLrM25je57MLAtXL2Xp547UaKoHo1p7m4kh6s7Ie43Y6L4K8ZfQdfhV+NWTEoCsuk gV40UeD7CpJ9c5+ZhCr3vveCB7weDqvgwAc7Sc9+GnTh84ec1VX9JecuE1nptI8HoF0Z wWKA== X-Gm-Message-State: AOAM533dGFsavXKm1TbpG3JcTBPYUSEABK2sguMc4+2nbuOw5FHQJt8P Silv7pmTJ5IGIqD/hU9iG3BBKQfG1w== X-Google-Smtp-Source: ABdhPJwo6icl+YL6EvmO9ra8Y2bqJrqvXykooEeh6Si+ke4zal+73I5YSwW5p1IiUy02jmurFi6vZw== X-Received: by 2002:a05:620a:2807:b0:67d:6349:2577 with SMTP id f7-20020a05620a280700b0067d63492577mr18996186qkp.785.1653447136760; Tue, 24 May 2022 19:52:16 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id v1-20020ac85781000000b002f3d23cf87esm758835qta.27.2022.05.24.19.52.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:52:16 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 2/7] ss: Remove unnecessary stack variable 'p' in user_ent_hash_build() Date: Tue, 24 May 2022 19:52:09 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye Commit 116ac9270b6d ("ss: Add support for retrieving SELinux contexts") added an unnecessary stack variable, 'char *p', in user_ent_hash_build(). Delete it for readability. Signed-off-by: Peilin Ye --- misc/ss.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index aa9da7e45e53..bccf01bb5efa 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -618,7 +618,6 @@ static void user_ent_hash_build(void) while ((d = readdir(dir)) != NULL) { struct dirent *d1; char process[16]; - char *p; int pid, pos; DIR *dir1; @@ -636,7 +635,6 @@ static void user_ent_hash_build(void) } process[0] = '\0'; - p = process; while ((d1 = readdir(dir1)) != NULL) { const char *pattern = "socket:["; @@ -667,18 +665,18 @@ static void user_ent_hash_build(void) if (getfilecon(tmp, &sock_context) <= 0) sock_context = strdup(no_ctx); - if (*p == '\0') { + if (process[0] == '\0') { FILE *fp; snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid); if ((fp = fopen(tmp, "r")) != NULL) { - if (fscanf(fp, "%*d (%[^)])", p) < 1) + if (fscanf(fp, "%*d (%[^)])", process) < 1) ; /* ignore */ fclose(fp); } } - user_ent_add(ino, p, pid, fd, + user_ent_add(ino, process, pid, fd, pid_context, sock_context); freecon(sock_context); } From patchwork Wed May 25 02:52:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860688 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BF7CC433EF for ; Wed, 25 May 2022 02:52:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243708AbiEYCwc (ORCPT ); Tue, 24 May 2022 22:52:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243710AbiEYCwb (ORCPT ); Tue, 24 May 2022 22:52:31 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E6CF46140 for ; Tue, 24 May 2022 19:52:30 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id p63so2248241qkf.0 for ; Tue, 24 May 2022 19:52:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QIFYihfLoVNjGYO37YZAC90MNsOnmPZ04BsxoO1pbGA=; b=Jo8bOZAE2FEJJ62amfJVycIYsX/+E4BNr8IOExw+jeuaQsP8vZHamz867DShbpSDNW Tn0WDjaG/rB00k8+ro21wei9gC2LY+fmpEonwqArDiVT5pRgenIM7UR2aO1EvFMS2CZ8 0SH8T6YbM7hCDgc0+ODUMcnW7GIesPjqTcmOQX83xL8J430WmcxgBoKcZ66HQ5tLWDE4 BIqCxckGeSj3M0cg1OIhlCkGnNCMtc13cq4FmM4KItG9aFK6EF5nZokAqbXNPkeebXKQ pc4uJ1zLURmupgZb1stRGTYcMQJMXdQHWLhwrx5ozRsOKZ/+zpCQ9BI/9XuQ8u07Ty4G hJ6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QIFYihfLoVNjGYO37YZAC90MNsOnmPZ04BsxoO1pbGA=; b=NPr2PAJiphOxzQrS54koUj9H1UccA5gkO1wHCGnOyV2TTOgcYMaTuO8d7umQIIS0Zk FHPGxmQ9Lf5YMEVXUuX+ZxQj4gxfX83fuVNj+u2rsaiylrCkb/FJWkvnia607dlndt5P 02yGlNcBQdUvx/H96P1qGF815pmDJp0euQkSiv5iApqPMhEhdwZ/XTPoFPX5R7CPR7L8 Sjxcw9ooVUO5uEPXLSY10FfXXEnSTx8ZlBhEK8YMym0W/yKt6YDNunZABrtVwc36csxV co5Pxwtaqxk/tKm85v07hDQAbel2JRQL8LPbDaHT4Nvdq/UZNbuThN41cJn3guGaiI5r pF8A== X-Gm-Message-State: AOAM533ho46bmNcnpUgVASA6ELRT5z1g6Lj+5FxXnkMCgB8hSS0G/JS7 K3b77yVFnd5WNbgCFqTKhQ== X-Google-Smtp-Source: ABdhPJwQOQlIegwRxUYtrRDCSv+XkbsCzUan0LIOsNTTC3PFWS1wtp2ByeZ43JFtnkThxDpW4iJY5w== X-Received: by 2002:a05:620a:29cb:b0:6a0:9df:ab46 with SMTP id s11-20020a05620a29cb00b006a009dfab46mr19099112qkp.629.1653447149540; Tue, 24 May 2022 19:52:29 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id w71-20020a37624a000000b006a03cbb1323sm567403qkb.65.2022.05.24.19.52.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:52:29 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 3/7] ss: Do not call user_ent_hash_build() more than once Date: Tue, 24 May 2022 19:52:21 -0700 Message-Id: <546e9515257ecb00f09baea80ead5e9311c07f75.1653446538.git.peilin.ye@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye Call user_ent_hash_build() once after the getopt_long() loop if -p, -z or -Z is used. Signed-off-by: Peilin Ye --- misc/ss.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index bccf01bb5efa..dd7b67a76255 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -596,13 +596,6 @@ static void user_ent_hash_build(void) char *pid_context; char *sock_context; const char *no_ctx = "unavailable"; - static int user_ent_hash_build_init; - - /* If show_users & show_proc_ctx set only do this once */ - if (user_ent_hash_build_init != 0) - return; - - user_ent_hash_build_init = 1; strlcpy(name, root, sizeof(name)); @@ -5509,7 +5502,6 @@ int main(int argc, char *argv[]) break; case 'p': show_users++; - user_ent_hash_build(); break; case 'b': show_options = 1; @@ -5644,7 +5636,6 @@ int main(int argc, char *argv[]) exit(1); } show_proc_ctx++; - user_ent_hash_build(); break; case 'N': if (netns_switch(optarg)) @@ -5679,6 +5670,9 @@ int main(int argc, char *argv[]) } } + if (show_users || show_proc_ctx || show_sock_ctx) + user_ent_hash_build(); + argc -= optind; argv += optind; From patchwork Wed May 25 02:52:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860689 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B76C7C433F5 for ; Wed, 25 May 2022 02:52:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243711AbiEYCww (ORCPT ); Tue, 24 May 2022 22:52:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242767AbiEYCwv (ORCPT ); Tue, 24 May 2022 22:52:51 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C51D13FBC for ; Tue, 24 May 2022 19:52:50 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id 135so13937097qkm.4 for ; Tue, 24 May 2022 19:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iQcr8uJ3zas3vFGD/G3yrt66vkozC5l14AvlElgIioY=; b=pu+opcm2t2eRb3NRH18waXAHqTG7Pj3KA4U9+VZPxJe8NciodW85k6U9QkMZGL0Y6l SSJZq2qrsINBTbUYHxxuMU0r+dWhStANUA8Q07ETVHJMaBWUHbMbmNLGgl0fsmr7pg/M y4I/vMCM4+QM3k2fXyx4w7ux4xzT/3INvxgXvNAiKPg4qRo/qBeM2ifKfvs16ey68RYA UiNs4m3JlpC884HlEuIcEFPGJLnfw+pF4uXSX0MqlJXH2YIdQXFttlX0JrT5y3vhcKN2 VT/jAhDd4SHOxtmjzIR07OPMHU6mtL3IT331GxV8c89s+7/pJONYqVqmENzipJ+mj9pV 6PIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iQcr8uJ3zas3vFGD/G3yrt66vkozC5l14AvlElgIioY=; b=HRmnBMsNZtWv0RWTI69dJFV+zABqfrb3zGHlJqKz5+qntZgf8YnVulK3x8kWcGLaRv WSgOvtsOxsql1GNw140C/uV5BeOxoQr/9C179njaeReamrD0E1ZZ6M9qhTrwnhsrKACV dTde1Snk6Wy0sNwYAXy43DExTfP1DdvSqH0F/lyxt55oJmCmrHOTAzW01DKcQ5z9yNaE u/55OM16sj8i225rBBLv+ZlDMMgGPn5w64CkTkG1kSs0TSq1LH3U6NAnIvRS4ML8ZcHa 5gTXfcx4AsrFbaGU/Ow9l+2bt4Snh8Xdyr7FssZFP7ns4M2HLK3oL42f77y9W+VNTw7E 0YwA== X-Gm-Message-State: AOAM532iwEGrsV0Xly3uGNdOeGUeYdo8u7XNbyGNb7aT5oW/Ox5FmFz5 ZPUpQr5ZCAxamMGMZufySA== X-Google-Smtp-Source: ABdhPJxyzXAPzWnyFT/HcWdA2jedF2YmLNWRAAsqaN1x+8TeRi5MwDZhPYqYGq5r54zu5+Wm89QF8g== X-Received: by 2002:a05:620a:e01:b0:69f:6042:7fa3 with SMTP id y1-20020a05620a0e0100b0069f60427fa3mr18318470qkm.524.1653447169677; Tue, 24 May 2022 19:52:49 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id l9-20020a05622a174900b002f39b99f687sm804727qtk.33.2022.05.24.19.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:52:49 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 4/7] ss: Delete unnecessary call to snprintf() in user_ent_hash_build() Date: Tue, 24 May 2022 19:52:36 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye 'name' is already $PROC_ROOT/$PID/fd/$FD there, no need to rebuild the string. Signed-off-by: Peilin Ye --- misc/ss.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index dd7b67a76255..ec14d746c669 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -652,10 +652,7 @@ static void user_ent_hash_build(void) sscanf(lnk, "socket:[%u]", &ino); - snprintf(tmp, sizeof(tmp), "%s/%d/fd/%s", - root, pid, d1->d_name); - - if (getfilecon(tmp, &sock_context) <= 0) + if (getfilecon(name, &sock_context) <= 0) sock_context = strdup(no_ctx); if (process[0] == '\0') { From patchwork Wed May 25 02:53:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860690 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B033C433F5 for ; Wed, 25 May 2022 02:53:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242322AbiEYCxR (ORCPT ); Tue, 24 May 2022 22:53:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237327AbiEYCxQ (ORCPT ); Tue, 24 May 2022 22:53:16 -0400 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDB993E0FB for ; Tue, 24 May 2022 19:53:14 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id ej7so15573480qvb.13 for ; Tue, 24 May 2022 19:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uBhCzmqzgb010Pbxq+nybhXov3w41PJ4SfGCmM0apCI=; b=FqRln8F6zL7lK5lkoTxROXgO/CNJGZs5I7rMTVabesE9+MJlpq6aEZ4LMeiu9MHyFn XwLx8CyK1N24k/hbfsCvGZ/d2bhJpx7mevaHt0CCDh9BsYEO3O+v9R1nYhdVk/I0rrDv 96xXfoOo3Gia/HOWUxZQ5+kBQt6awlGtPLjwWHeq3HlKT887DAjRFL726ZfsES1GSWXO br8C8CkV77zliBRBhNf3+FTcW5AwIl3i/LvXKW/f1vfGAmZrz3rx6K8+ybRUWISKDHPK 8OMsMFh7gLM/lneW/FyMJ0c5sq/M2M7FOZWWIjGhagOLcejMEq79uS+ivJrEAm3kFI+t H1oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uBhCzmqzgb010Pbxq+nybhXov3w41PJ4SfGCmM0apCI=; b=NDELRri+vhp6RuzQqTf9qYnsmenYWpCcp2Sj3qL81Yhb/MGSIqeRjK9Gl9wGF6jsOh uGnL/0SC2/DX7CdAChxF9rDNXpYQpMf1us99rvrzVXRs2HxLgiYm/omLLr4+g9dHKg9e 0ZI5nZGk5RIGHEMiDxw2EupXHWdMg2qXMRV48IZbgpVF6k1xe04aG+IrUQhNNp0aA7/N VJ1FjK5Et9r0uOkzd2oekk7IvZ11AyqYd3d43EQwneVluKg5o7YFbMjjWootOMIqc7wm rKxD0UdPnJGsJkm4+5YuSzHxHaj5t6Jq+1A1nHfAKEWIxqqAH30RRc1yBb3AMc0GlEAH u4Dg== X-Gm-Message-State: AOAM533xDGFUA+IBsP0bE/ZRCeE8wDYd5HwE5wJKs+q6wQ0vaXHO9mgu K8Ojczopbc3sutUaQlINYA== X-Google-Smtp-Source: ABdhPJyeE+8PoNxfgpaeDUMOndroR8zgVrS3CO8yHJhJksMrFthNe+c/3c7W0mmGZiR/nU8JNbm/hw== X-Received: by 2002:ad4:5f4e:0:b0:462:3d31:6faf with SMTP id p14-20020ad45f4e000000b004623d316fafmr10929569qvg.119.1653447194082; Tue, 24 May 2022 19:53:14 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id 74-20020a37064d000000b0069fc13ce204sm558352qkg.53.2022.05.24.19.53.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:53:13 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 5/7] ss: Fix coding style issues in user_ent_hash_build() Date: Tue, 24 May 2022 19:53:05 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye Make checkpatch.pl --strict happy about user_ent_hash_build(). Signed-off-by: Peilin Ye --- misc/ss.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index ec14d746c669..42d04bf432eb 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -599,7 +599,7 @@ static void user_ent_hash_build(void) strlcpy(name, root, sizeof(name)); - if (strlen(name) == 0 || name[strlen(name)-1] != '/') + if (strlen(name) == 0 || name[strlen(name) - 1] != '/') strcat(name, "/"); nameoff = strlen(name); @@ -622,7 +622,8 @@ static void user_ent_hash_build(void) snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); pos = strlen(name); - if ((dir1 = opendir(name)) == NULL) { + dir1 = opendir(name); + if (!dir1) { freecon(pid_context); continue; } @@ -640,9 +641,9 @@ static void user_ent_hash_build(void) if (sscanf(d1->d_name, "%d%*c", &fd) != 1) continue; - snprintf(name+pos, sizeof(name) - pos, "%d", fd); + snprintf(name + pos, sizeof(name) - pos, "%d", fd); - link_len = readlink(name, lnk, sizeof(lnk)-1); + link_len = readlink(name, lnk, sizeof(lnk) - 1); if (link_len == -1) continue; lnk[link_len] = '\0'; @@ -650,7 +651,8 @@ static void user_ent_hash_build(void) if (strncmp(lnk, pattern, strlen(pattern))) continue; - sscanf(lnk, "socket:[%u]", &ino); + if (sscanf(lnk, "socket:[%u]", &ino) != 1) + continue; if (getfilecon(name, &sock_context) <= 0) sock_context = strdup(no_ctx); @@ -658,16 +660,16 @@ static void user_ent_hash_build(void) if (process[0] == '\0') { FILE *fp; - snprintf(tmp, sizeof(tmp), "%s/%d/stat", - root, pid); - if ((fp = fopen(tmp, "r")) != NULL) { + snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid); + + fp = fopen(tmp, "r"); + if (fp) { if (fscanf(fp, "%*d (%[^)])", process) < 1) ; /* ignore */ fclose(fp); } } - user_ent_add(ino, process, pid, fd, - pid_context, sock_context); + user_ent_add(ino, process, pid, fd, pid_context, sock_context); freecon(sock_context); } freecon(pid_context); From patchwork Wed May 25 02:53:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860691 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B8A5C433EF for ; Wed, 25 May 2022 02:53:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242730AbiEYCxe (ORCPT ); Tue, 24 May 2022 22:53:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237327AbiEYCxd (ORCPT ); Tue, 24 May 2022 22:53:33 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03DD83E0FB for ; Tue, 24 May 2022 19:53:32 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id l82so11762752qke.3 for ; Tue, 24 May 2022 19:53:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QVrNttX2vQ/YNgTwkjoNxyvvw+b0QnDYuwI8Qdo2f8c=; b=IwGWi+bUsD96+US/EVzUTP37ly1J9hQRPGXUhSZp4lW9alJ9BD0hS/XV8EBWJscZEU uRSvwdj7HjZ3k7m4wuUmFUxcpI4+Rfpya35R2wplOCm8jBGx+VoudDwkmv5BJi99tGaD S25NaFXNuJsT292cgw/PL4zaosb0hy55DrgbT5ZZGi0t/LZIS1WNvKFH8n3n/aNzK/2a YAm9aUKt8nGnrGgm1fLca/MpOl2rndIySmSRgSwZFoAxDanT1SpTOlPmOKuQLeCISyLt 0+3z6zX8Kq1/AV7ReXsvEI2yJKC99yPXhIXUPhiFwktEQhLenaxlxv38qbUYPMEFJ3Zr j7cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QVrNttX2vQ/YNgTwkjoNxyvvw+b0QnDYuwI8Qdo2f8c=; b=2SRl2rUgi4UZn4aZDC37aXyYgE7L1He+ecKgFQgLBHRPoh2OxUmvin/yd3/8hn5Vxg 7IGjMwK0FpQM0HagEVxpjGHRtkI32h6/4UQk8vBQNaRuuKsvEak/oKc+vca73NPXqnZ3 petQ8/ay2MquS9xsvOpKmGUWCkUj8RPjMtvM8yrdEnFjuq1PlNHNnSrA+o1IS0gnlQcZ vpnr5ZapCc51KGU4inZ7ydE+png3BM8Nxos+ImeMfBTgvyXk3BwGMDWC38ZDdrQphKhf L1UcehXzZsRjTuz0GZNZ0SfbxOwa1suP4DVM4VCEed1xXWpGwyVzDKNKOQenm7dy0wkc w2CA== X-Gm-Message-State: AOAM530jMbjf0xjFzJ7TOwguId7vTMaENdskkDV/59HMaYcMavpZ6S/S oXiJX8qN3qZkdyQdyx288Q== X-Google-Smtp-Source: ABdhPJyzXwhPQp7IIa6MUuiIyyXnmVEZhlI90JInpmaHfUyHe231jIpzfk5ClJsoY19kGo2VqVOVFw== X-Received: by 2002:a05:620a:459f:b0:6a3:a9e1:407d with SMTP id bp31-20020a05620a459f00b006a3a9e1407dmr6650337qkb.724.1653447211158; Tue, 24 May 2022 19:53:31 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id d15-20020a05622a15cf00b002f77a8bc37fsm658328qty.51.2022.05.24.19.53.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:53:30 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 6/7] ss: Factor out fd iterating logic from user_ent_hash_build() Date: Tue, 24 May 2022 19:53:21 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye We are planning to add a thread version of the -p, --process option. Move the logic iterating $PROC_ROOT/$PID/fd/ into a new function, user_ent_hash_build_task(), to make it easier. Since we will use this function for both processes and threads, rename local variables as such (e.g. from "process" to "task"). Signed-off-by: Peilin Ye --- misc/ss.c | 144 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 65 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 42d04bf432eb..07d0057b22cc 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -567,6 +567,81 @@ static void user_ent_add(unsigned int ino, char *process, *pp = p; } +#define MAX_PATH_LEN 1024 + +static void user_ent_hash_build_task(char *path, int pid) +{ + const char *no_ctx = "unavailable"; + char task[16] = {'\0', }; + char stat[MAX_PATH_LEN]; + int pos_id, pos_fd; + char *task_context; + struct dirent *d; + DIR *dir; + + if (getpidcon(pid, &task_context) != 0) + task_context = strdup(no_ctx); + + pos_id = strlen(path); /* $PROC_ROOT/$ID/ */ + + snprintf(path + pos_id, MAX_PATH_LEN - pos_id, "fd/"); + dir = opendir(path); + if (!dir) { + freecon(task_context); + return; + } + + pos_fd = strlen(path); /* $PROC_ROOT/$ID/fd/ */ + + while ((d = readdir(dir)) != NULL) { + const char *pattern = "socket:["; + char *sock_context; + unsigned int ino; + ssize_t link_len; + char lnk[64]; + int fd; + + if (sscanf(d->d_name, "%d%*c", &fd) != 1) + continue; + + snprintf(path + pos_fd, MAX_PATH_LEN - pos_fd, "%d", fd); + + link_len = readlink(path, lnk, sizeof(lnk) - 1); + if (link_len == -1) + continue; + lnk[link_len] = '\0'; + + if (strncmp(lnk, pattern, strlen(pattern))) + continue; + + if (sscanf(lnk, "socket:[%u]", &ino) != 1) + continue; + + if (getfilecon(path, &sock_context) <= 0) + sock_context = strdup(no_ctx); + + if (task[0] == '\0') { + FILE *fp; + + strlcpy(stat, path, pos_id + 1); + snprintf(stat + pos_id, sizeof(stat) - pos_id, "stat"); + + fp = fopen(stat, "r"); + if (fp) { + if (fscanf(fp, "%*d (%[^)])", task) < 1) + ; /* ignore */ + fclose(fp); + } + } + + user_ent_add(ino, task, pid, fd, task_context, sock_context); + freecon(sock_context); + } + + freecon(task_context); + closedir(dir); +} + static void user_ent_destroy(void) { struct user_ent *p, *p_next; @@ -589,13 +664,10 @@ static void user_ent_destroy(void) static void user_ent_hash_build(void) { const char *root = getenv("PROC_ROOT") ? : "/proc/"; + char name[MAX_PATH_LEN]; struct dirent *d; - char name[1024]; int nameoff; DIR *dir; - char *pid_context; - char *sock_context; - const char *no_ctx = "unavailable"; strlcpy(name, root, sizeof(name)); @@ -609,71 +681,13 @@ static void user_ent_hash_build(void) return; while ((d = readdir(dir)) != NULL) { - struct dirent *d1; - char process[16]; - int pid, pos; - DIR *dir1; + int pid; if (sscanf(d->d_name, "%d%*c", &pid) != 1) continue; - if (getpidcon(pid, &pid_context) != 0) - pid_context = strdup(no_ctx); - - snprintf(name + nameoff, sizeof(name) - nameoff, "%d/fd/", pid); - pos = strlen(name); - dir1 = opendir(name); - if (!dir1) { - freecon(pid_context); - continue; - } - - process[0] = '\0'; - - while ((d1 = readdir(dir1)) != NULL) { - const char *pattern = "socket:["; - unsigned int ino; - char lnk[64]; - int fd; - ssize_t link_len; - char tmp[1024]; - - if (sscanf(d1->d_name, "%d%*c", &fd) != 1) - continue; - - snprintf(name + pos, sizeof(name) - pos, "%d", fd); - - link_len = readlink(name, lnk, sizeof(lnk) - 1); - if (link_len == -1) - continue; - lnk[link_len] = '\0'; - - if (strncmp(lnk, pattern, strlen(pattern))) - continue; - - if (sscanf(lnk, "socket:[%u]", &ino) != 1) - continue; - - if (getfilecon(name, &sock_context) <= 0) - sock_context = strdup(no_ctx); - - if (process[0] == '\0') { - FILE *fp; - - snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid); - - fp = fopen(tmp, "r"); - if (fp) { - if (fscanf(fp, "%*d (%[^)])", process) < 1) - ; /* ignore */ - fclose(fp); - } - } - user_ent_add(ino, process, pid, fd, pid_context, sock_context); - freecon(sock_context); - } - freecon(pid_context); - closedir(dir1); + snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", pid); + user_ent_hash_build_task(name, pid); } closedir(dir); } From patchwork Wed May 25 02:53:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peilin Ye X-Patchwork-Id: 12860692 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 577A6C433EF for ; Wed, 25 May 2022 02:53:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242767AbiEYCx5 (ORCPT ); Tue, 24 May 2022 22:53:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242338AbiEYCx4 (ORCPT ); Tue, 24 May 2022 22:53:56 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B01FE7090F for ; Tue, 24 May 2022 19:53:54 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id m13so10135468qtx.0 for ; Tue, 24 May 2022 19:53:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QL4XbVs19Flmpd07pTiThIq7YuypQ+9+bnrvDH5lVcQ=; b=dBYx3PW+9BIaKrlEbcZYpchEksmEFHj+PWu0M97nflGKcM94N8m50AOlIOh3z07QY8 DCiU7IzpSYoZcwyDuHs818cvE1cKdmXgQxuchHjWGD8wrZZHu80bjx0vEjcRST1dSSiI EveZS7U/UTkhGm4TNlbmvQGkefP0qkJCJfPnW2dOsmiAmls8oBnQHvPwyKaeStd7x9Dg K+sjUDWIuIqsSgAmD2T7k8UASmZgfVdW+O7GmErRDCmktH7HjgUgT/9Npp3mYYt4EtoE LIyN9TYsu6SKAE5xXtf6Bq7mqt9+u1z8Zax4vdoYDYoA7jXBEYC+IvRxhP5OoryFIIEi dF0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QL4XbVs19Flmpd07pTiThIq7YuypQ+9+bnrvDH5lVcQ=; b=CRKFdYY/yk5GfeXFYEppNlc1UQzPjgfYDowB53rXrMhad6prbGEhoOZkoXhKD6xPbo 7uiTRTywJQNW7PfWMsJNY6IiPnSjtbw5fCpCkouj2vnifzc4KS5/IcMPKRjoM9cuTym2 PZz2Szx6rCEEJ6TMzuPmCKxj3yoiq8twXIxPgrnsVNjb13HIflqRhNAEMAw6UwbZkxPf K8isvmORwoDTgVc0ANcHUvnfI8v0hRDi4D160zHWWN+eojZ+trxjl3DM4cDaGIkTT3+9 EzcwsKAcNWlZC29uHV5Gw69Eil7T7nCOmz9jEWpc+OCYcMxol3H659elTeP4XNQlYPM5 X7Ew== X-Gm-Message-State: AOAM533gJwGqHLjmIK0Ax7wPGzvvLtr+hj02tcKvWW+mcSlXbCwCDwgd 62vqkohCpTuLRq9vrj8o7g== X-Google-Smtp-Source: ABdhPJwQhk7FW6IO3C09jgy2EVyM/GRSmuQmEoAv59AUHZL4FL81muqBvEyJxsJ81Zp7lcEUIYtKqA== X-Received: by 2002:ac8:5a46:0:b0:2f3:eb45:e465 with SMTP id o6-20020ac85a46000000b002f3eb45e465mr23146098qta.126.1653447233810; Tue, 24 May 2022 19:53:53 -0700 (PDT) Received: from bytedance.attlocal.net (ec2-3-231-65-244.compute-1.amazonaws.com. [3.231.65.244]) by smtp.gmail.com with ESMTPSA id o9-20020a05622a044900b002f39b99f697sm734387qtx.49.2022.05.24.19.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 May 2022 19:53:53 -0700 (PDT) From: Peilin Ye To: Stephen Hemminger , David Ahern , netdev@vger.kernel.org Cc: Peilin Ye , Richard Haines , Cong Wang , Peilin Ye Subject: [PATCH iproute2-next 7/7] ss: Introduce -T, --threads option Date: Tue, 24 May 2022 19:53:41 -0700 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com From: Peilin Ye The -p, -Z and -z options only show process (thread group leader) information. For example, if the thread group leader has exited, but another thread in the group is still using a socket, ss -[pZz] does not show it. Add a new option, -T (--threads), to show thread information. It implies the -p option. For example, imagine process A and thread B (in the same group) using the same socket. ss -p only shows A: $ ss -ltp "sport = 1234" State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 100 0.0.0.0:1234 0.0.0.0:* users:(("test",pid=2932547,fd=3)) ss -T shows A and B: $ ss -ltT "sport = 1234" State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 100 0.0.0.0:1234 0.0.0.0:* users:(("test",pid=2932547,tid=2932548,fd=3),("test",pid=2932547,tid=2932547,fd=3)) If -T is used, -Z and -z also show SELinux contexts for threads. Rename some variables (from "process" to "task", for example) since we use them for both processes and threads. Signed-off-by: Peilin Ye --- man/man8/ss.8 | 8 ++++- misc/ss.c | 94 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/man/man8/ss.8 b/man/man8/ss.8 index 68f0a699e61b..026530ed77bd 100644 --- a/man/man8/ss.8 +++ b/man/man8/ss.8 @@ -155,6 +155,10 @@ the number of packets dropped before they are de-multiplexed into the socket .B \-p, \-\-processes Show process using socket. .TP +.B \-T, \-\-threads +Show thread using socket. Implies \-p. +.BR \-p . +.TP .B \-i, \-\-info Show internal TCP information. Below fields may appear: .RS @@ -311,7 +315,9 @@ Continually display sockets as they are destroyed .B \-Z, \-\-context As the .B \-p -option but also shows process security context. +option but also shows process security context. If the +.B \-T +option is used, also shows thread security context. .sp For .BR netlink (7) diff --git a/misc/ss.c b/misc/ss.c index 07d0057b22cc..344bb0f055d6 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -112,7 +112,8 @@ static void freecon(char *context) int preferred_family = AF_UNSPEC; static int show_options; int show_details; -static int show_users; +static int show_processes; +static int show_threads; static int show_mem; static int show_tcpinfo; static int show_bpf; @@ -526,9 +527,10 @@ struct user_ent { struct user_ent *next; unsigned int ino; int pid; + int tid; int fd; - char *process; - char *process_ctx; + char *task; + char *task_ctx; char *socket_ctx; }; @@ -542,9 +544,9 @@ static int user_ent_hashfn(unsigned int ino) return val & (USER_ENT_HASH_SIZE - 1); } -static void user_ent_add(unsigned int ino, char *process, - int pid, int fd, - char *proc_ctx, +static void user_ent_add(unsigned int ino, char *task, + int pid, int tid, int fd, + char *task_ctx, char *sock_ctx) { struct user_ent *p, **pp; @@ -557,9 +559,10 @@ static void user_ent_add(unsigned int ino, char *process, p->next = NULL; p->ino = ino; p->pid = pid; + p->tid = tid; p->fd = fd; - p->process = strdup(process); - p->process_ctx = strdup(proc_ctx); + p->task = strdup(task); + p->task_ctx = strdup(task_ctx); p->socket_ctx = strdup(sock_ctx); pp = &user_ent_hash[user_ent_hashfn(ino)]; @@ -569,7 +572,7 @@ static void user_ent_add(unsigned int ino, char *process, #define MAX_PATH_LEN 1024 -static void user_ent_hash_build_task(char *path, int pid) +static void user_ent_hash_build_task(char *path, int pid, int tid) { const char *no_ctx = "unavailable"; char task[16] = {'\0', }; @@ -579,7 +582,7 @@ static void user_ent_hash_build_task(char *path, int pid) struct dirent *d; DIR *dir; - if (getpidcon(pid, &task_context) != 0) + if (getpidcon(tid, &task_context) != 0) task_context = strdup(no_ctx); pos_id = strlen(path); /* $PROC_ROOT/$ID/ */ @@ -634,7 +637,7 @@ static void user_ent_hash_build_task(char *path, int pid) } } - user_ent_add(ino, task, pid, fd, task_context, sock_context); + user_ent_add(ino, task, pid, tid, fd, task_context, sock_context); freecon(sock_context); } @@ -650,8 +653,8 @@ static void user_ent_destroy(void) while (cnt != USER_ENT_HASH_SIZE) { p = user_ent_hash[cnt]; while (p) { - free(p->process); - free(p->process_ctx); + free(p->task); + free(p->task_ctx); free(p->socket_ctx); p_next = p->next; free(p); @@ -687,7 +690,30 @@ static void user_ent_hash_build(void) continue; snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", pid); - user_ent_hash_build_task(name, pid); + user_ent_hash_build_task(name, pid, pid); + + if (show_threads) { + struct dirent *task_d; + DIR *task_dir; + + snprintf(name + nameoff, sizeof(name) - nameoff, "%d/task/", pid); + + task_dir = opendir(name); + if (!task_dir) + continue; + + while ((task_d = readdir(task_dir)) != NULL) { + int tid; + + if (sscanf(task_d->d_name, "%d%*c", &tid) != 1) + continue; + if (tid == pid) + continue; + + snprintf(name + nameoff, sizeof(name) - nameoff, "%d/", tid); + user_ent_hash_build_task(name, pid, tid); + } + } } closedir(dir); } @@ -704,6 +730,7 @@ static int find_entry(unsigned int ino, char **buf, int type) struct user_ent *p; int cnt = 0; char *ptr; + char thread_info[16] = {'\0', }; char *new_buf; int len, new_buf_len; int buf_used = 0; @@ -720,23 +747,27 @@ static int find_entry(unsigned int ino, char **buf, int type) while (1) { ptr = *buf + buf_used; + + if (show_threads) + snprintf(thread_info, sizeof(thread_info), "tid=%d,", p->tid); + switch (type) { case USERS: len = snprintf(ptr, buf_len - buf_used, - "(\"%s\",pid=%d,fd=%d),", - p->process, p->pid, p->fd); + "(\"%s\",pid=%d,%sfd=%d),", + p->task, p->pid, thread_info, p->fd); break; case PROC_CTX: len = snprintf(ptr, buf_len - buf_used, - "(\"%s\",pid=%d,proc_ctx=%s,fd=%d),", - p->process, p->pid, - p->process_ctx, p->fd); + "(\"%s\",pid=%d,%sproc_ctx=%s,fd=%d),", + p->task, p->pid, thread_info, + p->task_ctx, p->fd); break; case PROC_SOCK_CTX: len = snprintf(ptr, buf_len - buf_used, - "(\"%s\",pid=%d,proc_ctx=%s,fd=%d,sock_ctx=%s),", - p->process, p->pid, - p->process_ctx, p->fd, + "(\"%s\",pid=%d,%sproc_ctx=%s,fd=%d,sock_ctx=%s),", + p->task, p->pid, thread_info, + p->task_ctx, p->fd, p->socket_ctx); break; default: @@ -2417,7 +2448,7 @@ static void proc_ctx_print(struct sockstat *s) out(" users:(%s)", buf); free(buf); } - } else if (show_users) { + } else if (show_processes || show_threads) { if (find_entry(s->ino, &buf, USERS) > 0) { out(" users:(%s)", buf); free(buf); @@ -5312,6 +5343,7 @@ static void _usage(FILE *dest) " -e, --extended show detailed socket information\n" " -m, --memory show socket memory usage\n" " -p, --processes show process using socket\n" +" -T, --threads show thread using socket\n" " -i, --info show internal TCP information\n" " --tipcinfo show internal tipc socket information\n" " -s, --summary show socket usage summary\n" @@ -5319,8 +5351,8 @@ static void _usage(FILE *dest) " --cgroup show cgroup information\n" " -b, --bpf show bpf filter socket information\n" " -E, --events continually display sockets as they are destroyed\n" -" -Z, --context display process SELinux security contexts\n" -" -z, --contexts display process and socket SELinux security contexts\n" +" -Z, --context display task SELinux security contexts\n" +" -z, --contexts display task and socket SELinux security contexts\n" " -N, --net switch to the specified network namespace name\n" "\n" " -4, --ipv4 display only IP version 4 sockets\n" @@ -5441,6 +5473,7 @@ static const struct option long_opts[] = { { "memory", 0, 0, 'm' }, { "info", 0, 0, 'i' }, { "processes", 0, 0, 'p' }, + { "threads", 0, 0, 'T' }, { "bpf", 0, 0, 'b' }, { "events", 0, 0, 'E' }, { "dccp", 0, 0, 'd' }, @@ -5491,7 +5524,7 @@ int main(int argc, char *argv[]) int state_filter = 0; while ((ch = getopt_long(argc, argv, - "dhaletuwxnro460spbEf:mMiA:D:F:vVzZN:KHSO", + "dhaletuwxnro460spTbEf:mMiA:D:F:vVzZN:KHSO", long_opts, NULL)) != EOF) { switch (ch) { case 'n': @@ -5514,7 +5547,10 @@ int main(int argc, char *argv[]) show_tcpinfo = 1; break; case 'p': - show_users++; + show_processes++; + break; + case 'T': + show_threads++; break; case 'b': show_options = 1; @@ -5683,7 +5719,7 @@ int main(int argc, char *argv[]) } } - if (show_users || show_proc_ctx || show_sock_ctx) + if (show_processes || show_threads || show_proc_ctx || show_sock_ctx) user_ent_hash_build(); argc -= optind; @@ -5803,7 +5839,7 @@ int main(int argc, char *argv[]) if (current_filter.dbs & (1<