-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchinfo.c
284 lines (209 loc) · 6.87 KB
/
archinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <mach/machine.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <libgen.h>
#define VERSION "0.4.0"
#define noErr 0
#define DEFAULT_BUFFER_SIZE 4096
#define SYSCTL_ERROR 1
#define is_match(x) (x == 0)
enum fmt { JSON=0, COLUMNS };
enum show { ALL=0, ARM64, X86_64 };
static int max_arg_size = 0;
typedef struct ProcInfo {
bool ok;
char *name;
char arch[10];
} procinfo;
// arch_info() is due to the spelunking work of Patrick Wardle <https://www.patreon.com/posts/45121749>
static char *arch_info(pid_t pid) {
size_t size;
cpu_type_t type = -1;
int mib[CTL_MAXNAME] = {0};
size_t length = CTL_MAXNAME;
struct kinfo_proc procInfo = {0};
if (noErr != sysctlnametomib("sysctl.proc_cputype", mib, &length)) return("unknown");
mib[length] = pid;
length++;
size = sizeof(cpu_type_t);
if (noErr != sysctl(mib, (u_int)length, &type, &size, 0, 0)) return("unknown");
if (CPU_TYPE_X86_64 == type) return("x86_64");
if (CPU_TYPE_ARM64 == type) {
mib[0] = CTL_KERN; //(re)init mib
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = pid;
length = 4; //(re)set length
size = sizeof(procInfo); //(re)set size
if(noErr != sysctl(mib, (u_int)length, &procInfo, &size, NULL, 0)) return("arm64"); //get proc info
//'P_TRANSLATED' set? set architecture to 'x86_64'
return( (P_TRANSLATED == (P_TRANSLATED & procInfo.kp_proc.p_flag)) ? "x86_64" : "arm64");
}
return("unknown");
}
// retrieve process info
procinfo proc_info(pid_t pid) {
size_t size = max_arg_size;
procinfo p;
int mib[3] = { CTL_KERN, KERN_PROCARGS2, pid };
struct kinfo_proc *info;
size_t length;
int count;
// get size for buffer
(void)sysctl(mib, 3, NULL, &length, NULL, 0);
char* buffer = (char *)calloc(length+32, sizeof(char)); // need +32 b/c this is busted on Big Sur
// get the info
if (sysctl(mib, 3, buffer, &size, NULL, 0) == 0) {
// copy the info
p.ok = TRUE;
p.name = buffer;
strncpy(p.arch, arch_info(pid), 10);
} else {
free(buffer);
p.ok = FALSE;
}
return(p);
}
// output one line of process info with architecture info
void output_one(enum fmt output_type, pid_t pid, procinfo p, bool only_basename) {
if (output_type == COLUMNS) {
printf(
"%7d %6s %s\n",
pid,
p.arch,
(only_basename ? basename((char *)(p.name+sizeof(int))) : p.name+sizeof(int))
);
} else if (output_type == JSON) {
printf(
"{\"pid\":%d,\"arch\":\"%s\",\"name\":\"%s\"}\n",
pid,
p.arch,
(only_basename ? basename((char *)(p.name+sizeof(int))) : p.name+sizeof(int))
);
}
}
// walk through process list, get PID and name, then pass that on to output_one() to
// grab the architecture and do the actual output
int enumerate_processes(enum fmt output_type, enum show to_show, bool only_basename) {
int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
struct kinfo_proc *info;
size_t length;
int count;
// get the running process list
if (sysctl(mib, 3, NULL, &length, NULL, 0) < 0) return(SYSCTL_ERROR);
// make some room for results
if (!(info = calloc(length, sizeof(char)))) return(SYSCTL_ERROR);
// get the results
if (sysctl(mib, 3, info, &length, NULL, 0) < 0) {
free(info);
return(SYSCTL_ERROR);
}
// how many results?
count = (int)(length / sizeof(struct kinfo_proc));
// for each result
for (int i = 0; i < count; i++) {
pid_t pid = info[i].kp_proc.p_pid; // get PID
if (pid == 0) continue;
procinfo p = proc_info(pid); // get process info
if (p.ok) {
if (
(to_show == ALL) ||
((to_show == ARM64) && is_match(strncmp("arm", p.arch, 3))) ||
((to_show == X86_64) && is_match(strncmp("x86", p.arch, 3)))
) {
output_one(output_type, pid, p, only_basename);
}
free(p.name);
}
}
free(info);
return(0);
}
// call to display cmdline tool help
void help() {
printf("archinfo %s\n", VERSION);
printf("boB Rudis <[email protected]>\n");
printf("\n");
printf("archinfo outputs a list of running processes with architecture (x86_64/amd64) information\n");
printf("\n");
printf("USAGE:\n");
printf(" archinfo [--arm|--x86] [--basename] [--columns|--json] [--pid #]\n");
printf("\n");
printf("FLAGS/OPTIONS:\n");
printf(" --arm Only show arm64 processes (default is to show both)\n");
printf(" --x86 Only show x86_64 processes (default is to show both)\n");
printf(" --basename Only show basename of process executable\n");
printf(" --columns Output process list in columns (default)\n");
printf(" --json Output process list in ndjson\n");
printf(" --pid # Output process architecture info for the specified process\n");
printf(" --help Display this help text\n");
}
void init() {
if (max_arg_size == 0) {
size_t size = sizeof(max_arg_size);
if (sysctl((int[]) { CTL_KERN, KERN_ARGMAX }, 2, &max_arg_size, &size, NULL, 0) == -1) {
perror("sysctl argument size");
max_arg_size = DEFAULT_BUFFER_SIZE;
}
}
}
int main(int argc, char** argv) {
int c;
enum show to_show = ALL;
bool show_help = FALSE;
bool do_one = FALSE;
bool only_basename = FALSE;
pid_t pid = -1;
enum fmt output_type = COLUMNS;
while(true) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{ "arm", no_argument, 0, 'a' },
{ "x86", no_argument, 0, 'x' },
{ "basename", no_argument, 0, 'b' },
{ "json", no_argument, 0, 'j' },
{ "columns", no_argument, 0, 'c' },
{ "help", no_argument, 0, 'h' },
{ "pid", required_argument, 0, 'p' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "axbjchp:", long_options, &option_index);
if (c == -1) break;
switch(c) {
case 'a': to_show = ARM64; break;
case 'x': to_show = X86_64; break;
case 'b': only_basename = TRUE; break;
case 'p': do_one = TRUE; pid = atoi(optarg); break;
case 'h': show_help = TRUE; break;
case 'j': output_type = JSON; break;
case 'c': output_type = COLUMNS; break;
}
}
init();
// only show help if --help is in the arg list
if (show_help) {
help();
return(0);
}
// only do one process if --pid is in the arg list
if (do_one) {
if (pid > 0) {
procinfo p = proc_info(pid);
if (p.ok) {
output_one(output_type, pid, p, only_basename);
free(p.name);
return(0);
}
}
return(SYSCTL_ERROR);
}
// otherwise do them all
return(enumerate_processes(output_type, to_show, only_basename));
}