summaryrefslogtreecommitdiffstats
path: root/system_cmds/dynamic_pager.tproj/dynamic_pager.c
blob: deb9379c191e80c31b9b4738e0d600b5664b7621 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
}