From nobody Tue Feb 10 23:53:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1696376409; cv=none; d=zohomail.com; s=zohoarc; b=nzV1ugukSzo5px56BJsrUvIlrqfED4TMJ6z13YdmvGo3JRFv6C/jUAD9f6DUCtxd3qSCCPB4aSCSakJaUuBW/QoO6VdLGgL9h9xdOwCWydQuatC02I+0sM3wKBVfdKnYBPwd6q0qK+YzswSeuv+YG8952ajwaqoLGYIS2lbN4X4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1696376409; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vv+aIku3DmAqisUQxx9HbMj8tI0pAJwZVGFoGM6lqnw=; b=by+SHXN7qeiItNza82a1goCEvBYrDslg2Vk93qN/HJRTpMmiyVDYsHzmpjtrnVvOz0aqU/gfmEfXvttzvA09BozzH6OFcPDekdAm+LHIHvL6Ex6pvVUpPlYLlGy2IVtPqFn7NC5ASAvEOUMb38sypApotAFhGrTQ/h4Gx0JS/ZA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1696376409894552.1689422022971; Tue, 3 Oct 2023 16:40:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qnovL-00049W-5E; Tue, 03 Oct 2023 19:35:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qnovJ-00048T-UD for qemu-devel@nongnu.org; Tue, 03 Oct 2023 19:35:29 -0400 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qnovI-00071Q-4Z for qemu-devel@nongnu.org; Tue, 03 Oct 2023 19:35:29 -0400 Received: by mail-io1-xd41.google.com with SMTP id ca18e2360f4ac-79f915e5b47so59904339f.2 for ; Tue, 03 Oct 2023 16:35:27 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id w25-20020a6b4a19000000b0079fa1a7cd36sm593596iob.30.2023.10.03.16.35.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 16:35:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20230601.gappssmtp.com; s=20230601; t=1696376124; x=1696980924; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vv+aIku3DmAqisUQxx9HbMj8tI0pAJwZVGFoGM6lqnw=; b=kokjwctmR6dOZXx8mqR/7KOyarl6r7QTq5FaSo6N9kkW+CzVtjqd7LjVAsW11xLePW Mi+6X9kdkMLWt7FmLCsAJRaNEWf9bqgS6x5O3pEFuXt2tbAjYDIUhcKaScaZzyuVJ7yo Ms6XPwLl1/2g0n9GpBgihIL6wvodm4ore9L8QXln0SphV2EhACCBXbNK3yRbB3/JjH0H hGfRPRwI2UNODPd4yNMwPYjhRjdUG+l86zPqjz+rmtBDdlTBPmMIrEVxMNm3okq3BrRg J9eJ2sogXFEUlkL49Pp+ai21oUxk9aGduWI9Te8NppKLW/gbYFyaNLs1zt5BPnp0iBo3 ZkGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696376124; x=1696980924; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vv+aIku3DmAqisUQxx9HbMj8tI0pAJwZVGFoGM6lqnw=; b=C1zdKnyHieWmca/LwzSlJELQR3r/9g4q40UhbImWMV6cmGFHoB2mS+EKsKdUQxxmMu 1mbqhw+WgLdQVFt6Pf4WVWuJbqr2essTixXP5JPsBGEkSJLmTVAS2PJzMbyWgxV0GJAh b8aXTNAM3c1AY2wjFLm133oQyUsHnL1huUPr4WZ0ea+irx8fBe/pZQ49U/glnkjg/zQn AB8ttyZpMK0dyKRbrh9vkdNPnnSYDP/eWcSKMAiN9b7Zl+1ZUjt4jrlB6a0Rx7YOZtWr GUZQAJk0leoiFBnJ8GMCgaFQT5DE72qLdwgWxa6slLFBWG5+03pg7eKzeOO9K/3TsB99 kHWA== X-Gm-Message-State: AOJu0YwPGz4ky4bixA9Q4wpoZyOWI76Pc/vdmgWIIDrZY8Sj1aHpdKxn cUWuwc+omUSwVTiLh1DqvjQ0IJK3W/QsTH01wygelhpZ X-Google-Smtp-Source: AGHT+IFw4OoDwo4flJGVdNrfgTtXrScqPpMtqyLxdUBmxMbcaRVLolhNc8uhX597cfLvsI7THGuRbg== X-Received: by 2002:a5d:9943:0:b0:79f:9eb4:3ea1 with SMTP id v3-20020a5d9943000000b0079f9eb43ea1mr1019417ios.3.1696376124287; Tue, 03 Oct 2023 16:35:24 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Wainer dos Santos Moschetta , Beraldo Leal , Warner Losh , Kyle Evans , Karim Taha , Stacey Son , Richard Henderson Subject: [PULL 19/51] bsd-user: Implement get_filename_from_fd. Date: Tue, 3 Oct 2023 17:31:43 -0600 Message-ID: <20231003233215.95557-20-imp@bsdimp.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231003233215.95557-1-imp@bsdimp.com> References: <20231003233215.95557-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::d41; envelope-from=imp@bsdimp.com; helo=mail-io1-xd41.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bsdimp-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1696376411670100010 Content-Type: text/plain; charset="utf-8" From: Karim Taha Signed-off-by: Stacey Son Signed-off-by: Karim Taha Reviewed-by: Richard Henderson Reviewed-by: Warner Losh Message-Id: <20230925182425.3163-20-kariem.taha2.7@gmail.com> --- bsd-user/freebsd/meson.build | 1 + bsd-user/freebsd/os-proc.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 bsd-user/freebsd/os-proc.c diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build index f2f047cca31..8fd6c7cfb82 100644 --- a/bsd-user/freebsd/meson.build +++ b/bsd-user/freebsd/meson.build @@ -1,5 +1,6 @@ bsd_user_ss.add(files( 'os-stat.c', + 'os-proc.c', 'os-sys.c', 'os-syscall.c', )) diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c new file mode 100644 index 00000000000..2603c5c6538 --- /dev/null +++ b/bsd-user/freebsd/os-proc.c @@ -0,0 +1,82 @@ +/* + * FreeBSD process related emulation code + * + * Copyright (c) 2013-15 Stacey D. Son + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include "qemu/osdep.h" + +#include +#include +#include +struct kinfo_proc; +#include + +#include "qemu.h" + +/* + * Get the filename for the given file descriptor. + * Note that this may return NULL (fail) if no longer cached in the kernel. + */ +char * +get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len); +char * +get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len) +{ + char *ret =3D NULL; + unsigned int cnt; + struct procstat *procstat =3D NULL; + struct kinfo_proc *kp =3D NULL; + struct filestat_list *head =3D NULL; + struct filestat *fst; + + procstat =3D procstat_open_sysctl(); + if (procstat =3D=3D NULL) { + goto out; + } + + kp =3D procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt); + if (kp =3D=3D NULL) { + goto out; + } + + head =3D procstat_getfiles(procstat, kp, 0); + if (head =3D=3D NULL) { + goto out; + } + + STAILQ_FOREACH(fst, head, next) { + if (fd =3D=3D fst->fs_fd) { + if (fst->fs_path !=3D NULL) { + (void)strlcpy(filename, fst->fs_path, len); + ret =3D filename; + } + break; + } + } + +out: + if (head !=3D NULL) { + procstat_freefiles(procstat, head); + } + if (kp !=3D NULL) { + procstat_freeprocs(procstat, kp); + } + if (procstat !=3D NULL) { + procstat_close(procstat); + } + return ret; +} + --=20 2.41.0