summaryrefslogtreecommitdiffstats
path: root/system_cmds/mslutil/mslutil.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2021-05-09 14:20:58 -0400
committerCameron Katri <me@cameronkatri.com>2021-05-09 14:20:58 -0400
commit5fd83771641d15c418f747bd343ba6738d3875f7 (patch)
tree5abf0f78f680d9837dbd93d4d4c3933bb7509599 /system_cmds/mslutil/mslutil.c
downloadapple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.gz
apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.tar.zst
apple_cmds-5fd83771641d15c418f747bd343ba6738d3875f7.zip
Import macOS userland
adv_cmds-176 basic_cmds-55 bootstrap_cmds-116.100.1 developer_cmds-66 diskdev_cmds-667.40.1 doc_cmds-53.60.1 file_cmds-321.40.3 mail_cmds-35 misc_cmds-34 network_cmds-606.40.1 patch_cmds-17 remote_cmds-63 shell_cmds-216.60.1 system_cmds-880.60.2 text_cmds-106
Diffstat (limited to 'system_cmds/mslutil/mslutil.c')
-rw-r--r--system_cmds/mslutil/mslutil.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/system_cmds/mslutil/mslutil.c b/system_cmds/mslutil/mslutil.c
new file mode 100644
index 0000000..5738d4a
--- /dev/null
+++ b/system_cmds/mslutil/mslutil.c
@@ -0,0 +1,96 @@
+//
+// mslutil.c
+// mslutil
+//
+// Created by Christopher Deppe on 3/31/17.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/sysctl.h>
+#include <stack_logging.h>
+
+#define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc_internal.h. */
+
+static void print_usage()
+{
+ printf("usage: mslutil pid [--disable] | [--enable malloc | vm | full | lite | vmlite]\n");
+}
+
+static int send_msl_command(uint64_t pid, uint64_t flavor)
+{
+ uint64_t flags = flavor;
+ flags <<= 32;
+
+ flags |= (pid & 0xFFFFFFFF);
+
+ int ret = sysctlbyname("kern.memorystatus_vm_pressure_send", 0, 0, &flags, sizeof(flags));
+
+ if (ret) {
+ printf("send_msl_command - sysctl: kern.memorystatus_vm_pressure_send failed %s\n", strerror(errno));
+ } else {
+ printf("send_msl_command - success!\n");
+ }
+
+ return ret;
+}
+
+int main(int argc, const char * argv[])
+{
+ if (argc < 3) {
+ print_usage();
+ exit(1);
+ }
+
+ int ret = -1;
+
+ pid_t pid = atoi(argv[1]);
+
+ if (pid <= 0 || pid > BSD_PID_MAX) {
+ printf("Invalid pid\n");
+ exit(1);
+ }
+
+ if (strcmp(argv[2], "--enable") == 0) {
+ if (argc < 4) {
+ print_usage();
+ exit(1);
+ }
+
+ uint64_t flavor = 0;
+
+ if (strcmp(argv[3], "full") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC | MEMORYSTATUS_ENABLE_MSL_VM;
+ } else if (strcmp(argv[3], "malloc") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC;
+ } else if (strcmp(argv[3], "vm") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_VM;
+ } else if (strcmp(argv[3], "lite") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_LITE_FULL;
+ } else if (strcmp(argv[3], "vmlite") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_LITE_VM;
+ }
+
+ if (flavor == 0) {
+ print_usage();
+ exit(1);
+ }
+
+ ret = send_msl_command(pid, flavor);
+ } else if (strcmp(argv[2], "--disable") == 0) {
+ ret = send_msl_command(pid, MEMORYSTATUS_DISABLE_MSL);
+ } else {
+ print_usage();
+ exit(1);
+ }
+
+ if (ret != 0) {
+ exit(1);
+ } else {
+ exit(0);
+ }
+}
+
+