Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp3193444ybh; Sat, 25 Jul 2020 14:29:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyLIHSaQEretqpaOkPqhxz2ZKrNnkv2Er6y6m6vxObuqvu7tdNLeYI3kkeKfsuh1kVNu+F X-Received: by 2002:aa7:d6cf:: with SMTP id x15mr14445625edr.164.1595712590437; Sat, 25 Jul 2020 14:29:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595712590; cv=none; d=google.com; s=arc-20160816; b=b7nAmZJI3F8vgKY6hUhVKA3fgNZ4NvatWo45C2tgndfFJoEqcmEoXP1/IR33FCAMua KrdugUqSho8V8lT7VaDvV8f/ilOUdq0V1oInDsc6WMREKeiOpRpzzbSiwxv84+6H5N7T y+LIPHRhZ2LAdgNFptSDJtThHo+lFPyR8Jv82mhCY47NyZkscSkmtAFyZHiAZaRugt8W paKNnFi+cW9QGPHEizIuflya924iC5VCF3UHPaY0N9CaXGOv2vdUKLLP0QJHfl/sZAw3 taI6qj2n26EIho2ilosEj8AtFJx3AM5Lk5BWVAWlpW2LQDusKKHfhnJ1XBEXU74mZphb tMvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=9vUjQUwq1PEERKf5qY60lLL26f/JpD/pXHJh6zM3IeM=; b=M9OqSdo+qtEHnrm+GasvyyqtAFd1xCHJoM5C/AqF4OU8Dh7Jl50wOu7GlC2G7Om3yK VUq3SABFc9Et9C5NQwg+Xf5kvOk4TNeJkchkmr3SQoYuxjSxbN5ZIo491gPT3jtYwTxK jYPZo8rnMnPyasmlzcFYQgc7sgNhJ2lh+iMn+JJcTWT7qaqaulbDzQptAt+Px76+iWZ2 4aIwH6mTdcH3k+OJnvi3Uzqprb7cklf26+OdhKgb7qWTsJeGxFZPSs/ht4nJgLMQvh4J iol/qwHSchSlp45l50zwnLJtyHGKPMkXH5UAzHRzBhsG9eNCCnfR0u1FsK8YBOo+V3Cj +CYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b="SiQ/TcpG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r7si2918002eji.524.2020.07.25.14.29.28; Sat, 25 Jul 2020 14:29:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b="SiQ/TcpG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727877AbgGYV3C (ORCPT + 99 others); Sat, 25 Jul 2020 17:29:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727897AbgGYV3B (ORCPT ); Sat, 25 Jul 2020 17:29:01 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5284FC08C5CE for ; Sat, 25 Jul 2020 14:29:01 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id a32so9552366qtb.5 for ; Sat, 25 Jul 2020 14:29:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tfz-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9vUjQUwq1PEERKf5qY60lLL26f/JpD/pXHJh6zM3IeM=; b=SiQ/TcpGmb4nIYR2tjY/C67/FDysFa43NfqWK5sh5Hx2lBSouWujK2NCbEsIJebtEd tojt5nScJQqxhwQo2UT4dTfanai5SFMOeWM3wbW2UGRg/GOgnXrFEu/+mFWQ3MnP1Yb7 iMIa5ei9q/lAJLiOifV8v6LOAScv3Ps1y6rYztN9UHKw8j7NHb25Lzv4hXbmkJ4ZZYg6 litzpjUq08/OFHnunvH80Kxv3oeAUNDmMr2UPu50xH7f+jQM2uBSoF1ZXUtvMz5kxJyC FVpx8B3J2ThVo5Hi2QwlBjTh8zv5peDOyKYjaeT0RI9ziaedfbYAhVswskDLXAA8ax/E xelg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9vUjQUwq1PEERKf5qY60lLL26f/JpD/pXHJh6zM3IeM=; b=foMYF711M+XWzfMKZbvbBkh1w93CNi3exmdrUkWYUXg2/Qq+ROlBCZjpLzgDAoO0KR Sv471IiFZEitXlQHVdWkzYN35VzbaPnRfOPKeV5m90PKY0akIHnVWxojZKM+eJ65hdcB Z11tZy1yzQ1o4ArRZAVeFP5nDKF5fscaLUS2xbi5V8U2BYe0IPSn8TMDK/H2W6DjlkBF xXpdOfx3NAvAq8fSAaM+pagE8kQGooqR2/GIExa8TtpSQ1x7I/JCuDCTDvyZbtNG0sdk O+b+4nAkLTBO6uvngNHQXtOp/eyuA2fqraSvPiSAUB2IBlkVXPA7F0pFZ6km2QC8isT4 VdSQ== X-Gm-Message-State: AOAM531MaTpR43Ep2/CaMOxkNaAZ6lNpJGgUQKGbjw3tKmVG7+F+4G7W DUAJDSfuIqtn08ktB368kjUKCbiF0xsEKw== X-Received: by 2002:ac8:4411:: with SMTP id j17mr15433311qtn.77.1595712540240; Sat, 25 Jul 2020 14:29:00 -0700 (PDT) Received: from foo.attlocal.net (108-232-117-128.lightspeed.sntcca.sbcglobal.net. [108.232.117.128]) by smtp.gmail.com with ESMTPSA id w1sm11597735qkj.90.2020.07.25.14.28.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jul 2020 14:28:59 -0700 (PDT) From: Pascal Bouchareine To: linux-kernel@vger.kernel.org Cc: Pascal Bouchareine , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, "Alexey Dobriyan" , "Jeff Layton" , "J. Bruce Fields" Subject: [PATCH] proc,fcntl: introduce F_SET_DESCRIPTION Date: Fri, 24 Jul 2020 17:40:43 -0700 Message-Id: <20200725004043.32326-1-kalou@tfz.net> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This command attaches a description to a file descriptor for troubleshooting purposes. The free string is displayed in the process fdinfo file for that fd /proc/pid/fdinfo/fd. One intended usage is to allow processes to self-document sockets for netstat and friends to report Signed-off-by: Pascal Bouchareine --- More context sent earlier this week: https://lore.kernel.org/linux-api/CAGbU3_nVvuzMn2wo4_ZKufWcGfmGsopVujzTWw-Bbeky=xS+GA@mail.gmail.com/T/#u Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 5 files changed, 35 insertions(+) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 996f3cfe7030..ae8045650836 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -1918,6 +1918,9 @@ A typical output is:: flags: 0100002 mnt_id: 19 +An optional 'desc' is set if the process documented its usage of +the file via the fcntl command F_SET_DESCRIPTION. + All locks associated with a file descriptor are shown in its fdinfo too:: lock: 1: FLOCK ADVISORY WRITE 359 00:13:11691 0 EOF diff --git a/fs/fcntl.c b/fs/fcntl.c index 2e4c0fa2074b..c1ef724a906e 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -319,6 +319,22 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description(struct file *file, char __user *desc) +{ + char *d; + + d = strndup_user(desc, MAX_FILE_DESC_SIZE); + if (IS_ERR(d)) + return PTR_ERR(d); + + spin_lock(&file->f_lock); + kfree(file->f_description); + file->f_description = d; + spin_unlock(&file->f_lock); + + return 0; +} + static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, struct file *filp) { @@ -426,6 +442,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, case F_SET_FILE_RW_HINT: err = fcntl_rw_hint(filp, cmd, arg); break; + case F_SET_DESCRIPTION: + err = fcntl_set_description(filp, argp); + break; default: break; } diff --git a/fs/proc/fd.c b/fs/proc/fd.c index 81882a13212d..60b3ff971b2b 100644 --- a/fs/proc/fd.c +++ b/fs/proc/fd.c @@ -57,6 +57,11 @@ static int seq_show(struct seq_file *m, void *v) (long long)file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); + spin_lock(&file->f_lock); + if (file->f_description) + seq_printf(m, "desc:\t%s\n", file->f_description); + spin_unlock(&file->f_lock); + show_fd_locks(m, file, files); if (seq_has_overflowed(m)) goto out; diff --git a/include/linux/fs.h b/include/linux/fs.h index f5abba86107d..09717bfa4e3b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -980,6 +980,9 @@ struct file { struct address_space *f_mapping; errseq_t f_wb_err; errseq_t f_sb_err; /* for syncfs */ + +#define MAX_FILE_DESC_SIZE 256 + char *f_description; } __randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */ diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 2f86b2ad6d7e..f86ff6dc45c7 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -55,6 +55,11 @@ #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* + * Set file description + */ +#define F_SET_DESCRIPTION (F_LINUX_SPECIFIC_BASE + 15) + /* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set. -- 2.25.1