Mercurial > minori
annotate dep/animia/src/bsd.cpp @ 89:e6fab256ddc4
dialog/info: make some stuff more sane
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 31 Oct 2023 23:06:33 -0400 |
parents | c912128af0eb |
children | 18979b066284 |
rev | line source |
---|---|
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 /** |
62 | 2 * bsd.cpp |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 * - provides support for most* versions of BSD |
62 | 4 * - this also works for OS X :) |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 * more technical details: this is essentially a wrapper |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
6 * around the very C-like BSD system functions that are... |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
7 * kind of unnatural to use in modern C++. |
62 | 8 **/ |
86 | 9 #include <assert.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
10 #include <fcntl.h> |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
11 #include <iostream> |
62 | 12 #include <string> |
13 #include <sys/sysctl.h> | |
14 #include <sys/types.h> | |
15 #include <sys/user.h> | |
16 #include <unordered_map> | |
17 #include <vector> | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
18 #ifdef __FreeBSD__ |
62 | 19 # include <libutil.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
20 #elif defined(__APPLE__) |
62 | 21 # include <libproc.h> |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
22 #endif |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
23 |
84
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
24 namespace Animia { namespace Unix { |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
25 |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
26 /* this is a cleaned up version of a function from... Apple? |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
27 ...anyway, what it essentially does is gets the size and stuff from |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
28 sysctl() and reserves the space in a vector to store the PIDs */ |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
29 std::vector<int> get_all_pids() { |
62 | 30 std::vector<int> ret; |
31 struct kinfo_proc* result = NULL; | |
32 size_t length = 0; | |
33 static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
34 |
62 | 35 /* get appropriate length from sysctl() |
36 note: the reason this isn't checked is actually because this will | |
37 *always* return an error on OS X (or... maybe I'm doing it wrong :) ) */ | |
38 sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
39 |
62 | 40 result = (struct kinfo_proc*)malloc(length); |
41 if (result == NULL) | |
42 return std::vector<int>(); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
43 |
62 | 44 /* actually get our results */ |
45 if (sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0) == ENOMEM) { | |
46 assert(result != NULL); | |
47 free(result); | |
48 throw std::bad_alloc(); | |
49 } | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
50 |
62 | 51 /* add pids to our vector */ |
52 ret.reserve(length / sizeof(*result)); | |
53 for (int i = 0; i < length / sizeof(*result); i++) | |
54 ret.push_back(result[i].kp_proc.p_pid); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
55 |
62 | 56 return ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
57 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
58 |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
59 std::string get_process_name(int pid) { |
62 | 60 std::string ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
61 #ifdef __FreeBSD__ |
62 | 62 struct kinfo_proc* proc = kinfo_getproc(pid); |
63 if (!proc) | |
64 return ""; | |
65 ret = proc->ki_comm; | |
66 free(proc); | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
67 #elif defined(__APPLE__) |
62 | 68 struct proc_bsdinfo proc; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
69 |
62 | 70 int st = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &proc, PROC_PIDTBSDINFO_SIZE); |
71 if (st != PROC_PIDTBSDINFO_SIZE) | |
72 return ""; | |
73 ret = proc.pbi_comm; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
74 #endif |
62 | 75 return ret; |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
76 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
77 |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
78 std::vector<std::string> get_open_files(int pid) { |
62 | 79 /* note: this is OS X only right now. eventually, I'll find a way |
80 to do this in FreeBSD, OpenBSD and the like */ | |
81 std::vector<std::string> ret; | |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
82 |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
83 if (pid == 0) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
84 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
85 |
62 | 86 int bufsz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
87 if (bufsz == -1) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
88 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
89 |
62 | 90 struct proc_fdinfo* info = (struct proc_fdinfo*)malloc(bufsz); |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
91 if (!info) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
92 return ret; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
93 |
62 | 94 proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info, bufsz); |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
95 |
62 | 96 // iterate over stuff |
97 ret.reserve(bufsz / sizeof(info[0])); | |
98 for (int i = 0; i < bufsz / sizeof(info[0]); i++) { | |
99 if (info[i].proc_fdtype == PROX_FDTYPE_VNODE) { | |
100 struct vnode_fdinfowithpath vnodeInfo; | |
78
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
101 |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
102 int sz = proc_pidfdinfo(pid, info[i].proc_fd, PROC_PIDFDVNODEPATHINFO, &vnodeInfo, PROC_PIDFDVNODEPATHINFO_SIZE); |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
103 if (sz != PROC_PIDFDVNODEPATHINFO_SIZE) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
104 continue; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
105 |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
106 if (vnodeInfo.pfi.fi_openflags & O_WRONLY || vnodeInfo.pfi.fi_openflags & O_RDWR) |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
107 continue; |
1ce00c1c8ddc
dep/animia: update to upstream
Paper <mrpapersonic@gmail.com>
parents:
62
diff
changeset
|
108 |
62 | 109 ret.push_back(vnodeInfo.pvip.vip_path); |
110 } | |
111 } | |
112 return ret; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
113 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
114 |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
115 std::unordered_map<int, std::vector<std::string>> get_all_open_files() { |
62 | 116 std::unordered_map<int, std::vector<std::string>> map; |
117 std::vector<int> pids = get_all_pids(); | |
118 for (int i : pids) { | |
119 map[i] = get_open_files(i); | |
120 } | |
121 return map; | |
56
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 } |
6ff7aabeb9d7
deps: add animia for open files detection
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
123 |
84
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
124 } // namespace Unix |
eab9e623eb84
dep/animia: update from upstream
Paper <mrpapersonic@gmail.com>
parents:
78
diff
changeset
|
125 } // namespace Animia |