Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp95858pxa; Thu, 13 Aug 2020 21:00:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgHJkI7pTNXH2yLDcQ2RHQ9bzE+pkX4ytUF7OLs0u26No1aHeUbR+G2pvOVj0AzObqrWO1 X-Received: by 2002:a17:907:20db:: with SMTP id qq27mr659315ejb.550.1597377628026; Thu, 13 Aug 2020 21:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597377628; cv=none; d=google.com; s=arc-20160816; b=01LRQXGvx5WaWC9Ae+BriPoZnfzi2m7SAYbQ67bS6l35tguXbnkief8PBBvV/0pQyc 2NvQevIQZwxJzcWDletC7Gr2+7xY8VpDrBQUg2ZYZ/RQIf24FXuftyWJDkEzm3FGaCfU EcIYsJ6jb9X2+zkpjAnxVgC75bTqwu4cWBHDKGE9ADd3+dDN4inLrA4+Szvw7k95lEV/ r/x/gV7VmZNFvWDsg8a1cWZ6jZf0Ct2wDS4G1r3uod2E3fniHl6Y7OhDdSd20e8c4Oco 6jjbLa/DdJ0c+V285v3VYHaYViLpGbcQFlrji9pUdDK21rgiVM1taqGgyfCPgYJoBT4h DM3Q== 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=cqQTcE8OeHpaauD8pedadIVKN4wJE4wCvdlEy6whEus=; b=cZyBIvDGH9YGo5v1XVka42MIyX1tXrVBegVhyqGsqs8ZC/zxjzd/EQJoCM9d8kRo27 l3H0ZIQlFyknHXM2T0+ty3HB96JyHGv3EKyTHkpzlJgJL4t0lX+Nbop+9HRSxFsXk8Qf Dn+6iJA+f67nQRwIqI+9LfBRUnEWam4ntj/aJHJq2Z52PaaJbBMvRBiMX6P4MHOkNLnU DfR0gbt1JCN5NpkQl4YZUk4Qor0roOUwDxgAgEWA/vvJUTm/v1bcPFG5zx1MvkYZ6H9j 1Qvx9W4hLwDuskALZvgqEzg3e5fezDU/QdRWkBvX2QKNJ6quVXQR6L9FUAuULnYyc48v ONWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tfz-net.20150623.gappssmtp.com header.s=20150623 header.b=ZKYsHZrN; 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 d20si4702539ejz.445.2020.08.13.21.00.05; Thu, 13 Aug 2020 21:00:28 -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=ZKYsHZrN; 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 S1726676AbgHND5W (ORCPT + 99 others); Thu, 13 Aug 2020 23:57:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726679AbgHND5U (ORCPT ); Thu, 13 Aug 2020 23:57:20 -0400 Received: from mail-qk1-x743.google.com (mail-qk1-x743.google.com [IPv6:2607:f8b0:4864:20::743]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1964CC061384 for ; Thu, 13 Aug 2020 20:57:19 -0700 (PDT) Received: by mail-qk1-x743.google.com with SMTP id b79so7278569qkg.9 for ; Thu, 13 Aug 2020 20:57:19 -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=cqQTcE8OeHpaauD8pedadIVKN4wJE4wCvdlEy6whEus=; b=ZKYsHZrNG6TPK2onXgFXhs8i86GF2T+1GmWoXgb8w0ubJG5cm80B7iPXmbIHm+elBT arwFQ8LvJNxI5TCW7WxokIejMqEK8GbVMlo9dvRzFAJe7Wr8zSTPbqpYZrhm5x0hEjkj fwQwpWTfhaLoOJw0QJyT7CPhOfG8cTKK88ot9+eFf9IImU6qn6kpkLz0w5fLcWBk++On uRJMsOgGr5zPwICP/dtUis8xhGrqgBzXBQWjvrlMyu6LhVj29pBQIuiZIDZ6v2nWISBk 6nn2uSLOZo4C3M2SBiRiiD6hg39fY7r408UD2ZjInSdcPcJgexW2237YChFhM+T/bfgR 2qyQ== 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=cqQTcE8OeHpaauD8pedadIVKN4wJE4wCvdlEy6whEus=; b=gcFBSRhmYwrDnJ5+JADPMSqUYK7CXaTgQHu0aVxMENI7/7Y7gOPv0PnyP3/IfskADi UL5O3CwaA6qqxLcc5ZPllJuYM6SFPcQihZiL+aAP4ioMJ8h1XpRjzlnETcFZTC6PtB1n rZiger5KXFUwL/Rne/8Oxq02Ah5xLsHEC7eQHbObqJdYaPl4jopov76vQIWC5jUJil8G wYB4p95fqBQsmooZjzgJZuYlAfRps9ia5ZWZ/9DP3i42vhz+u5+s8JNE+5+l7uqojcie ibBKWfCFNCW882xk5g/g4kT8wRK+RvF7TE1rdI+8r2Aki7f8g/Q9Zo8gaNqCjPbS1JKE hwqQ== X-Gm-Message-State: AOAM530KCWulFFRyHYxarUnJ2c1wqvPojfhV5ucuHvuBZwXsmuukNX1D IT0OWwAoBf8ycppu9PxKjs8HCjLwEOvIhg== X-Received: by 2002:a05:620a:c08:: with SMTP id l8mr381427qki.57.1597377438042; Thu, 13 Aug 2020 20:57:18 -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 k11sm7229460qkk.93.2020.08.13.20.57.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2020 20:57:17 -0700 (PDT) From: Pascal Bouchareine To: linux-kernel@vger.kernel.org Cc: Pascal Bouchareine , linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, "Andrew Morton" , "Alexey Dobriyan" , "Al Viro" , "Jeff Layton" , "J. Bruce Fields" Subject: [PATCH v4 2/2] fcntl: introduce F_SET_DESCRIPTION Date: Thu, 13 Aug 2020 20:54:53 -0700 Message-Id: <20200814035453.210716-3-kalou@tfz.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200814035453.210716-1-kalou@tfz.net> References: <20200814035453.210716-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 --- fs/fcntl.c | 21 +++++++++++++++++++++ fs/file_table.c | 2 ++ fs/proc/fd.c | 5 +++++ include/linux/fs.h | 3 +++ include/uapi/linux/fcntl.h | 5 +++++ 5 files changed, 36 insertions(+) diff --git a/fs/fcntl.c b/fs/fcntl.c index 2e4c0fa2074b..9fbeaaf02802 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -319,6 +319,24 @@ static long fcntl_rw_hint(struct file *file, unsigned int cmd, } } +static long fcntl_set_description(struct file *file, char __user *desc) +{ + char *d, *old; + + d = strndup_user(desc, MAX_FILE_DESC_SIZE, GFP_KERNEL_ACCOUNT); + if (IS_ERR(d)) + return PTR_ERR(d); + + spin_lock(&file->f_lock); + old = file->f_description; + file->f_description = d; + spin_unlock(&file->f_lock); + + kfree(old); + + return 0; +} + static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, struct file *filp) { @@ -426,6 +444,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..a2a683d603b6 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..465385e52f49 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