]> git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/mslutil/mslutil.c
atrun: Install LaunchDaemon
[apple_cmds.git] / system_cmds / 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 | vmlite]\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_FULL;
72 } else if (strcmp(argv[3], "vmlite") == 0) {
73 flavor = MEMORYSTATUS_ENABLE_MSL_LITE_VM;
74 }
75
76 if (flavor == 0) {
77 print_usage();
78 exit(1);
79 }
80
81 ret = send_msl_command(pid, flavor);
82 } else if (strcmp(argv[2], "--disable") == 0) {
83 ret = send_msl_command(pid, MEMORYSTATUS_DISABLE_MSL);
84 } else {
85 print_usage();
86 exit(1);
87 }
88
89 if (ret != 0) {
90 exit(1);
91 } else {
92 exit(0);
93 }
94 }
95
96