diff options
author | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
---|---|---|
committer | Cameron Katri <me@cameronkatri.com> | 2021-05-09 14:20:58 -0400 |
commit | 5fd83771641d15c418f747bd343ba6738d3875f7 (patch) | |
tree | 5abf0f78f680d9837dbd93d4d4c3933bb7509599 /system_cmds/dynamic_pager.tproj | |
download | apple_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/dynamic_pager.tproj')
-rw-r--r-- | system_cmds/dynamic_pager.tproj/com.apple.dynamic_pager.plist | 21 | ||||
-rw-r--r-- | system_cmds/dynamic_pager.tproj/dynamic_pager.8 | 32 | ||||
-rw-r--r-- | system_cmds/dynamic_pager.tproj/dynamic_pager.c | 114 | ||||
-rw-r--r-- | system_cmds/dynamic_pager.tproj/entitlements.plist | 8 | ||||
-rw-r--r-- | system_cmds/dynamic_pager.tproj/generate_plist.sh | 16 |
5 files changed, 191 insertions, 0 deletions
diff --git a/system_cmds/dynamic_pager.tproj/com.apple.dynamic_pager.plist b/system_cmds/dynamic_pager.tproj/com.apple.dynamic_pager.plist new file mode 100644 index 0000000..4214037 --- /dev/null +++ b/system_cmds/dynamic_pager.tproj/com.apple.dynamic_pager.plist @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>EnableTransactions</key> + <true/> + <key>Label</key> + <string>com.apple.dynamic_pager</string> + <key>KeepAlive</key> + <dict> + <key>SuccessfulExit</key> + <false/> + </dict> + <key>POSIXSpawnType</key> + <string>Interactive</string> + <key>ProgramArguments</key> + <array> + <string>/sbin/dynamic_pager</string> + </array> +</dict> +</plist> diff --git a/system_cmds/dynamic_pager.tproj/dynamic_pager.8 b/system_cmds/dynamic_pager.tproj/dynamic_pager.8 new file mode 100644 index 0000000..7d5bfc2 --- /dev/null +++ b/system_cmds/dynamic_pager.tproj/dynamic_pager.8 @@ -0,0 +1,32 @@ +.\" Copyright (c) 2003 Apple Computer, Inc. All rights reserved. +.\" +.Dd July 8, 2003 +.Dt dynamic_pager 8 +.Os "Mac OS X" +.Sh NAME +.Nm dynamic_pager +.Nd swap configuration daemon +.Sh SYNOPSIS +.Nm dynamic_pager +.Op Fl F Ar filename +.Sh DESCRIPTION +The +.Nm dynamic_pager +daemon can be used to specify a base name for swapfile names using the "-F" command line option or by modifying the configuration plist file. +.Sh OPTIONS +.Bl -tag -width Ds +.\" ========== +.It Fl F +The base name of the +.Ar filename +to use for the swapfiles. By default this is +.Pa /private/var/vm/swapfile . +.\" ========== +.Sh FILES +.Bl -tag -width /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist -compact +.It Pa /private/var/vm/swapfile* +Swapfiles. + +.It Pa /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist +Configuration file. +.El diff --git a/system_cmds/dynamic_pager.tproj/dynamic_pager.c b/system_cmds/dynamic_pager.tproj/dynamic_pager.c new file mode 100644 index 0000000..deb9379 --- /dev/null +++ b/system_cmds/dynamic_pager.tproj/dynamic_pager.c @@ -0,0 +1,114 @@ +#define mig_external + +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/sysctl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <dirent.h> + +/* + * We don't exit with a non-zero status anywhere here for 2 reasons: + * - the kernel can continue to create swapfiles in "/private/var/vm/swapfile<index>" + * - we want this job to run only once at boot and exit regardless of whether: + * -- it could clean up the swap directory + * -- it could set the prefix for the swapfile name. + */ + +static void +clean_swap_directory(const char *path) +{ + DIR *dir; + struct dirent *entry; + char buf[1024]; + + dir = opendir(path); + if (dir == NULL) { + fprintf(stderr,"dynamic_pager: cannot open swap directory %s\n", path); + exit(0); + } + + while ((entry = readdir(dir)) != NULL) { + if (entry->d_namlen>= 4 && strncmp(entry->d_name, "swap", 4) == 0) { + snprintf(buf, sizeof buf, "%s/%s", path, entry->d_name); + unlink(buf); + } + } + + closedir(dir); +} + +int +main(int argc, char **argv) +{ + int ch; + static char tmp[1024]; + struct statfs sfs; + char *q; + char fileroot[512]; + + seteuid(getuid()); + fileroot[0] = '\0'; + + while ((ch = getopt(argc, argv, "F:")) != EOF) { + switch((char)ch) { + + case 'F': + strncpy(fileroot, optarg, 500); + break; + + default: + (void)fprintf(stderr, + "usage: dynamic_pager [-F filename]\n"); + exit(0); + } + } + + /* + * set vm.swapfileprefix if a fileroot was passed from the command + * line, otherwise get the value from the kernel + */ + if (fileroot[0] != '\0') { + if (sysctlbyname("vm.swapfileprefix", NULL, 0, fileroot, sizeof(fileroot)) == -1) { + perror("Failed to set swapfile name prefix"); + } + } else { + size_t fileroot_len = sizeof(fileroot); + if (sysctlbyname("vm.swapfileprefix", fileroot, &fileroot_len, NULL, 0) == -1) { + perror("Failed to get swapfile name prefix"); + /* + * can't continue without a fileroot + */ + return (0); + } + } + + /* + * get rid of the filename at the end of the swap file specification + * we only want the portion of the pathname that should already exist + */ + strcpy(tmp, fileroot); + if ((q = strrchr(tmp, '/'))) + *q = 0; + + /* + * Remove all files in the swap directory. + */ + clean_swap_directory(tmp); + + if (statfs(tmp, &sfs) == -1) { + /* + * Setup the swap directory. + */ + + if (mkdir(tmp, 0755) == -1) { + (void)fprintf(stderr, "dynamic_pager: cannot create swap directory %s\n", tmp); + } + } + + chown(tmp, 0, 0); + + return (0); +} diff --git a/system_cmds/dynamic_pager.tproj/entitlements.plist b/system_cmds/dynamic_pager.tproj/entitlements.plist new file mode 100644 index 0000000..2e0c44d --- /dev/null +++ b/system_cmds/dynamic_pager.tproj/entitlements.plist @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>com.apple.rootless.volume.VM</key> + <true/> +</dict> +</plist> diff --git a/system_cmds/dynamic_pager.tproj/generate_plist.sh b/system_cmds/dynamic_pager.tproj/generate_plist.sh new file mode 100644 index 0000000..290a550 --- /dev/null +++ b/system_cmds/dynamic_pager.tproj/generate_plist.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -e +set -x + +cp "${SCRIPT_INPUT_FILE_0}" "${SCRIPT_OUTPUT_FILE_0}" +case "$PLATFORM_NAME" in +iphone*|appletv*|watch*|bridge*) + /usr/libexec/PlistBuddy -c "Add :LaunchOnlyOnce bool true" "${SCRIPT_OUTPUT_FILE_0}" + ;; +macosx) + ;; +*) + echo "Unsupported platform: $PLATFORM_NAME" + exit 1 + ;; +esac |