]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/freebsd/os-proc.c
bsd-user: Implement get_filename_from_fd.
[mirror_qemu.git] / bsd-user / freebsd / os-proc.c
1 /*
2 * FreeBSD process related emulation code
3 *
4 * Copyright (c) 2013-15 Stacey D. Son
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "qemu/osdep.h"
20
21 #include <sys/param.h>
22 #include <sys/queue.h>
23 #include <sys/sysctl.h>
24 struct kinfo_proc;
25 #include <libprocstat.h>
26
27 #include "qemu.h"
28
29 /*
30 * Get the filename for the given file descriptor.
31 * Note that this may return NULL (fail) if no longer cached in the kernel.
32 */
33 char *
34 get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len);
35 char *
36 get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
37 {
38 char *ret = NULL;
39 unsigned int cnt;
40 struct procstat *procstat = NULL;
41 struct kinfo_proc *kp = NULL;
42 struct filestat_list *head = NULL;
43 struct filestat *fst;
44
45 procstat = procstat_open_sysctl();
46 if (procstat == NULL) {
47 goto out;
48 }
49
50 kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
51 if (kp == NULL) {
52 goto out;
53 }
54
55 head = procstat_getfiles(procstat, kp, 0);
56 if (head == NULL) {
57 goto out;
58 }
59
60 STAILQ_FOREACH(fst, head, next) {
61 if (fd == fst->fs_fd) {
62 if (fst->fs_path != NULL) {
63 (void)strlcpy(filename, fst->fs_path, len);
64 ret = filename;
65 }
66 break;
67 }
68 }
69
70 out:
71 if (head != NULL) {
72 procstat_freefiles(procstat, head);
73 }
74 if (kp != NULL) {
75 procstat_freeprocs(procstat, kp);
76 }
77 if (procstat != NULL) {
78 procstat_close(procstat);
79 }
80 return ret;
81 }
82