Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp43511ybg; Sat, 25 Jul 2020 19:11:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwl9K+nsEIB/Iw8CMDwj70Ly7lTMDxkBSu/6kJTvavJ1l06wDXjdEnOL7W3XcvbzfY88ZL9 X-Received: by 2002:a05:6402:c0c:: with SMTP id co12mr2989847edb.384.1595729476007; Sat, 25 Jul 2020 19:11:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595729476; cv=none; d=google.com; s=arc-20160816; b=ViX0tXjVRcd0EONI+pQvA0oYRCyyA1cf1y2VyLMyx26N6/vfdA7xox10rKtULhPOmi e/ySe0szgVRUNO1fxde9lU8M7amsfhvDdz5cm0mJEDW3aK5ffp1gnIZIrXnxBpfmcQE2 Vmf+XxSDo5Z3aBarKvgrU5EZA9I8KHkYmpM6UAq+CLlekYQTw1hwygRJalDlrbhE7wqm TfiUG5sdeUBNRLpk6MGuvYT0NtacPmKt759L9CwXvqKYEUDNOLcpxGEtDBaOQZ30WBIf ndpt+aoiEP3zl5JY2qDkrGqkyB3+Z10L654GhBzhHFbKPJnz2JS2C3hW743RwprJajzr WLig== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9jZxFouS/A32loCaaEjirlHFKl4WQoThnzuj6Eg5GJw=; b=zuwvwpnhGB+8aeVSAi00JENIzykseU23griEavyzyM+q/I3SksaNnmOtvJCPuZrDos B+B0t3cKRlNOHSo0zY0Y7XmYtJQ29co/CO4aqIRATTqBZBABWUWeBaEhgfCLbYQv71Wr dsW2BKha6T/++JmVpcrulohIv++Yv9JEu6xq4HVdIpiUGYrVTsZVrefzJmS9lIwd1Fl7 pFD3BmtnvWtxgO52/+DT5UQVj0ByxF5+Q3r1ghMlIgZg2JT0sc/yeFbO/gBKggHBKWmC I/x3yKy4rUjtRptfvTa7QpX00o5PcHJFciSQ4Kg0qPVWI0V0ZFcf5Fgf/EhoMBbb2XgJ yjlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b=rEBHahRR; 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 ga5si3382486ejb.567.2020.07.25.19.10.53; Sat, 25 Jul 2020 19:11:15 -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=rEBHahRR; 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 S1728121AbgGZCKn (ORCPT + 99 others); Sat, 25 Jul 2020 22:10:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727034AbgGZCKn (ORCPT ); Sat, 25 Jul 2020 22:10:43 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B514AC08C5C0 for ; Sat, 25 Jul 2020 19:10:42 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id a32so9793736qtb.5 for ; Sat, 25 Jul 2020 19:10:42 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=9jZxFouS/A32loCaaEjirlHFKl4WQoThnzuj6Eg5GJw=; b=rEBHahRRSczuTZCBkAc6vQkis/Z+kNxhajeIGNL62ZWHUD5aaezLBP7rnw7giyM44Q 2IyTPNwPtpqez+O+cKlMnW1NaisiRzkD0Qcz+7TWqxpHjX0WTyKXTigKAyP1DQWc3Mi3 ySQq8mfb9I9Oxm/ylPH2zvXFPQBckbJbhf3xALiDl6bYOM7slqK7KnrwquA/q4zaJIbh Ujldw9QKud0XKnO+trewrpJtle4cDlk+uhZAJBBm8QARzoCpBSw57AHy9JZGq4e2MXf9 AxcycjBnLSTkF4TbXBUxOeWhkSlF8XUD0/dZS/y04T23vnbcr2nbyeus7D5PsHNwhRHl ClWA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=9jZxFouS/A32loCaaEjirlHFKl4WQoThnzuj6Eg5GJw=; b=jrQdhn2kdMyBCLtz0l0UKmCKdYQRKhewSUbgHRwa9XpX61+nK5K6cLpOtOg76OAf4w d5y1SwMPVoLCeJEeAOoSPrbnN9b3OQ3oi9Cm7Fg1lJURFaxQm20WTXMA8G2Uu/07Ltn4 SvL0yEwG80Y/SSRFuQpWYyP4bR35g7vQp7i2cW+T8mOloztVkas8a9cvTyAQdO68pD/S pasl7424k4HmK5mXMwNckwNpUvwqowJjGtnlLlIzBrLi2Md/xrxnUyQMIoLHb2kD9LNb VpqNnIePsr3Yby64Wh+fOcYf6nNpEfqmvgiiakc+tx9GBwShkHsrA6dp5gf/qB60le6k MuqQ== X-Gm-Message-State: AOAM532iee3t6L2FWlqRl4WTNAeigMMmmD1U/DB25Aa2PTu3Pk3jMdqo O/+oKcM1gvQSH+qlb0Qzq6aaMZ0x9MeQsw== X-Received: by 2002:ac8:6b04:: with SMTP id w4mr17059602qts.364.1595729441653; Sat, 25 Jul 2020 19:10:41 -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 i18sm1209310qtv.39.2020.07.25.19.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jul 2020 19:10:40 -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" , "Al Viro" , "Jeff Layton" , "J. Bruce Fields" Subject: [PATCH v3] proc,fcntl: introduce F_SET_DESCRIPTION Date: Fri, 24 Jul 2020 22:22:36 -0700 Message-Id: <20200725052236.4062-1-kalou@tfz.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200725045921.2723-1-kalou@tfz.net> References: <20200725045921.2723-1-kalou@tfz.net> 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 --- Documentation/filesystems/proc.rst | 3 +++ fs/fcntl.c | 19 +++++++++++++++++++ fs/file_table.c | 2 ++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 6 files changed, 37 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/file_table.c b/fs/file_table.c index 656647f9575a..6673a48d2ea1 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -272,6 +272,8 @@ static void __fput(struct file *file) eventpoll_release(file); locks_remove_file(file); + kfree(file->f_description); + ima_file_free(file); if (unlikely(file->f_flags & FASYNC)) { if (file->f_op->fasync) 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