]> git.saurik.com Git - apple/system_cmds.git/blob - mslutil/mslutil.c
system_cmds-790.tar.gz
[apple/system_cmds.git] / mslutil / mslutil.c
1 //
2 // mslutil.c
3 // mslutil
4 //
5 // Created by Christopher Deppe on 3/31/17.
6 //
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <sys/sysctl.h>
13 #include <stack_logging.h>
14
15 #define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc_internal.h. */
16
17 static void print_usage()
18 {
19 printf("usage: mslutil pid [--disable] | [--enable malloc | vm | full | lite]\n");
20 }
21
22 static int send_msl_command(uint64_t pid, uint64_t flavor)
23 {
24 uint64_t flags = flavor;
25 flags <<= 32;
26
27 flags |= (pid & 0xFFFFFFFF);
28
29 int ret = sysctlbyname("kern.memorystatus_vm_pressure_send", 0, 0, &flags, sizeof(flags));
30
31 if (ret) {
32 printf("send_msl_command - sysctl: kern.memorystatus_vm_pressure_send failed %s\n", strerror(errno));
33 } else {
34 printf("send_msl_command - success!\n");
35 }
36
37 return ret;
38 }
39
40 int main(int argc, const char * argv[])
41 {
42 if (argc < 3) {
43 print_usage();
44 exit(1);
45 }
46
47 int ret = -1;
48
49 pid_t pid = atoi(argv[1]);
50
51 if (pid <= 0 || pid > BSD_PID_MAX) {
52 printf("Invalid pid\n");
53 exit(1);
54 }
55
56 if (strcmp(argv[2], "--enable") == 0) {
57 if (argc < 4) {
58 print_usage();
59 exit(1);
60 }
61
62 uint64_t flavor = 0;
63
64 if (strcmp(argv[3], "full") == 0) {
65 flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC | MEMORYSTATUS_ENABLE_MSL_VM;
66 } else if (strcmp(argv[3], "malloc") == 0) {
67 flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC;
68 } else if (strcmp(argv[3], "vm") == 0) {
69 flavor = MEMORYSTATUS_ENABLE_MSL_VM;
70 } else if (strcmp(argv[3], "lite") == 0) {
71 flavor = MEMORYSTATUS_ENABLE_MSL_LITE;
72 }
73
74 if (flavor == 0) {
75 print_usage();
76 exit(1);
77 }
78
79 ret = send_msl_command(pid, flavor);
80 } else if (strcmp(argv[2], "--disable") == 0) {
81 ret = send_msl_command(pid, MEMORYSTATUS_DISABLE_MSL);
82 } else {
83 print_usage();
84 exit(1);
85 }
86
87 if (ret != 0) {
88 exit(1);
89 } else {
90 exit(0);
91 }
92 }
93
94