]>
git.saurik.com Git - apple/system_cmds.git/blob - dynamic_pager.tproj/dynamic_pager.c
5 #include <sys/sysctl.h>
13 * We don't exit with a non-zero status anywhere here for 2 reasons:
14 * - the kernel can continue to create swapfiles in "/private/var/vm/swapfile<index>"
15 * - we want this job to run only once at boot and exit regardless of whether:
16 * -- it could clean up the swap directory
17 * -- it could set the prefix for the swapfile name.
21 clean_swap_directory(const char *path
)
29 fprintf(stderr
,"dynamic_pager: cannot open swap directory %s\n", path
);
33 while ((entry
= readdir(dir
)) != NULL
) {
34 if (entry
->d_namlen
>= 4 && strncmp(entry
->d_name
, "swap", 4) == 0) {
35 snprintf(buf
, sizeof buf
, "%s/%s", path
, entry
->d_name
);
44 main(int argc
, char **argv
)
47 static char tmp
[1024];
55 while ((ch
= getopt(argc
, argv
, "F:")) != EOF
) {
59 strncpy(fileroot
, optarg
, 500);
64 "usage: dynamic_pager [-F filename]\n");
70 * set vm.swapfileprefix if a fileroot was passed from the command
71 * line, otherwise get the value from the kernel
73 if (fileroot
[0] != '\0') {
74 if (sysctlbyname("vm.swapfileprefix", NULL
, 0, fileroot
, sizeof(fileroot
)) == -1) {
75 perror("Failed to set swapfile name prefix");
78 size_t fileroot_len
= sizeof(fileroot
);
79 if (sysctlbyname("vm.swapfileprefix", fileroot
, &fileroot_len
, NULL
, 0) == -1) {
80 perror("Failed to get swapfile name prefix");
82 * can't continue without a fileroot
89 * get rid of the filename at the end of the swap file specification
90 * we only want the portion of the pathname that should already exist
92 strcpy(tmp
, fileroot
);
93 if ((q
= strrchr(tmp
, '/')))
97 * Remove all files in the swap directory.
99 clean_swap_directory(tmp
);
101 if (statfs(tmp
, &sfs
) == -1) {
103 * Setup the swap directory.
106 if (mkdir(tmp
, 0755) == -1) {
107 (void)fprintf(stderr
, "dynamic_pager: cannot create swap directory %s\n", tmp
);