]> git.saurik.com Git - apple/system_cmds.git/blame - dynamic_pager.tproj/dynamic_pager.c
system_cmds-735.tar.gz
[apple/system_cmds.git] / dynamic_pager.tproj / dynamic_pager.c
CommitLineData
1815bff5
A
1#define mig_external
2
c3a08f59 3#include <sys/mount.h>
1815bff5
A
4#include <sys/stat.h>
5#include <sys/sysctl.h>
1815bff5
A
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
1815bff5 9#include <unistd.h>
34d340d7
A
10#include <dirent.h>
11
c3a08f59 12/*
1a7e3f61
A
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.
c3a08f59 18 */
c3a08f59 19
34d340d7
A
20static void
21clean_swap_directory(const char *path)
22{
23 DIR *dir;
24 struct dirent *entry;
25 char buf[1024];
26
27 dir = opendir(path);
28 if (dir == NULL) {
29 fprintf(stderr,"dynamic_pager: cannot open swap directory %s\n", path);
1a7e3f61 30 exit(0);
34d340d7
A
31 }
32
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);
cf37c299 36 unlink(buf);
34d340d7
A
37 }
38 }
39
40 closedir(dir);
41}
42
1815bff5
A
43int
44main(int argc, char **argv)
45{
1815bff5
A
46 char default_filename[] = "/private/var/vm/swapfile";
47 int ch;
1a7e3f61 48 int err=0;
8459d725
A
49 static char tmp[1024];
50 struct statfs sfs;
51 char *q;
1a7e3f61 52 char fileroot[512];
1815bff5
A
53
54 seteuid(getuid());
55 strcpy(fileroot, default_filename);
56
1a7e3f61 57 while ((ch = getopt(argc, argv, "F:")) != EOF) {
1815bff5
A
58 switch((char)ch) {
59
60 case 'F':
61 strncpy(fileroot, optarg, 500);
62 break;
63
1815bff5
A
64 default:
65 (void)fprintf(stderr,
1a7e3f61
A
66 "usage: dynamic_pager [-F filename]\n");
67 exit(0);
1815bff5
A
68 }
69 }
c3a08f59 70
8459d725
A
71 /*
72 * get rid of the filename at the end of the swap file specification
73 * we only want the portion of the pathname that should already exist
74 */
75 strcpy(tmp, fileroot);
76 if ((q = strrchr(tmp, '/')))
77 *q = 0;
78
79 /*
80 * Remove all files in the swap directory.
81 */
82 clean_swap_directory(tmp);
83
8459d725 84 if (statfs(tmp, &sfs) == -1) {
c3a08f59 85 /*
cf37c299
A
86 * Setup the swap directory.
87 */
c3a08f59 88
cf37c299
A
89 if (mkdir(tmp, 0755) == -1) {
90 (void)fprintf(stderr, "dynamic_pager: cannot create swap directory %s\n", tmp);
8459d725 91 }
c3a08f59 92 }
c3a08f59 93
1a7e3f61 94 chown(tmp, 0, 0);
c3a08f59 95
1a7e3f61
A
96 err = sysctlbyname("vm.swapfileprefix", NULL, 0, fileroot, sizeof(fileroot));
97 if (err) {
98 (void)fprintf(stderr, "Failed to set swapfile name prefix with error: %d\n", err);
99 }
c3a08f59 100
1815bff5
A
101 return (0);
102}