[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 19/28] bsd-user: Implement get_filename_from_fd.
From: |
Karim Taha |
Subject: |
[PATCH v5 19/28] bsd-user: Implement get_filename_from_fd. |
Date: |
Mon, 25 Sep 2023 21:24:16 +0300 |
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/freebsd/meson.build | 1 +
bsd-user/freebsd/os-proc.c | 80 ++++++++++++++++++++++++++++++++++++
2 files changed, 81 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 f2f047cca3..8fd6c7cfb8 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 0000000000..cb35f29f10
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.c
@@ -0,0 +1,80 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/sysctl.h>
+struct kinfo_proc;
+#include <libprocstat.h>
+
+#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.
+ */
+static char *
+get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
+{
+ char *ret = NULL;
+ unsigned int cnt;
+ struct procstat *procstat = NULL;
+ struct kinfo_proc *kp = NULL;
+ struct filestat_list *head = NULL;
+ struct filestat *fst;
+
+ procstat = procstat_open_sysctl();
+ if (procstat == NULL) {
+ goto out;
+ }
+
+ kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
+ if (kp == NULL) {
+ goto out;
+ }
+
+ head = procstat_getfiles(procstat, kp, 0);
+ if (head == NULL) {
+ goto out;
+ }
+
+ STAILQ_FOREACH(fst, head, next) {
+ if (fd == fst->fs_fd) {
+ if (fst->fs_path != NULL) {
+ (void)strlcpy(filename, fst->fs_path, len);
+ ret = filename;
+ }
+ break;
+ }
+ }
+
+out:
+ if (head != NULL) {
+ procstat_freefiles(procstat, head);
+ }
+ if (kp != NULL) {
+ procstat_freeprocs(procstat, kp);
+ }
+ if (procstat != NULL) {
+ procstat_close(procstat);
+ }
+ return ret;
+}
+
--
2.42.0
- [PATCH v5 09/28] bsd-user: Implement host_to_target_waitstatus conversion., (continued)
- [PATCH v5 09/28] bsd-user: Implement host_to_target_waitstatus conversion., Karim Taha, 2023/09/25
- [PATCH v5 10/28] bsd-user: Get number of cpus., Karim Taha, 2023/09/25
- [PATCH v5 11/28] bsd-user: Implement getgroups(2) and setgroups(2) system calls., Karim Taha, 2023/09/25
- [PATCH v5 12/28] bsd-user: Implement umask(2), setlogin(2) and getlogin(2), Karim Taha, 2023/09/25
- [PATCH v5 14/28] bsd-user: Implement getrlimit(2) and setrlimit(2), Karim Taha, 2023/09/25
- [PATCH v5 15/28] bsd-user: Implement several get/set system calls:, Karim Taha, 2023/09/25
- [PATCH v5 13/28] bsd-user: Implement getrusage(2)., Karim Taha, 2023/09/25
- [PATCH v5 16/28] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid., Karim Taha, 2023/09/25
- [PATCH v5 17/28] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2)., Karim Taha, 2023/09/25
- [PATCH v5 18/28] bsd-user: Implement getpriority(2) and setpriority(2)., Karim Taha, 2023/09/25
- [PATCH v5 19/28] bsd-user: Implement get_filename_from_fd.,
Karim Taha <=
- [PATCH v5 20/28] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve., Karim Taha, 2023/09/25
- [PATCH v5 22/28] bsd-user: Implement execve(2) and fexecve(2) system calls., Karim Taha, 2023/09/25
- [PATCH v5 23/28] bsd-user: Implement wait4(2) and wait6(2) system calls., Karim Taha, 2023/09/25
- [PATCH v5 21/28] bsd-user: Implement procctl(2) along with necessary conversion functions., Karim Taha, 2023/09/25
- [PATCH v5 24/28] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls., Karim Taha, 2023/09/25
- [PATCH v5 25/28] bsd-user: Implement pdgetpid(2) and the undocumented setugid., Karim Taha, 2023/09/25
- [PATCH v5 28/28] bsd-user: Implement pdfork(2) system call., Karim Taha, 2023/09/25
- [PATCH v5 27/28] bsd-user: Implement rfork(2) system call., Karim Taha, 2023/09/25
- [PATCH v5 26/28] bsd-user: Implement fork(2) and vfork(2) system calls., Karim Taha, 2023/09/25
- Re: [PATCH v5 00/28] bsd-user: Implement freebsd process related system calls., Warner Losh, 2023/09/27