swapctl.c revision 1.2 1 1.2 thorpej /* $NetBSD: swapctl.c,v 1.2 1997/06/15 03:47:53 thorpej Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1996, 1997 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. All advertising materials mentioning features or use of this software
16 1.1 mrg * must display the following acknowledgement:
17 1.1 mrg * This product includes software developed by Matthew R. Green for
18 1.1 mrg * The NetBSD Foundation.
19 1.1 mrg * 4. The name of the author may not be used to endorse or promote products
20 1.1 mrg * derived from this software without specific prior written permission.
21 1.1 mrg *
22 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 mrg * SUCH DAMAGE.
33 1.1 mrg */
34 1.1 mrg
35 1.1 mrg /*
36 1.1 mrg * swapctl command:
37 1.1 mrg * -A add all devices listed as `sw' in /etc/fstab
38 1.1 mrg * -a <dev> add this device
39 1.1 mrg * -d <dev> remove this swap device (not supported yet)
40 1.1 mrg * -l list swap devices
41 1.1 mrg * -s short listing of swap devices
42 1.1 mrg * -k use kilobytes
43 1.1 mrg * -p <pri> use this priority
44 1.1 mrg * -c change priority
45 1.2 thorpej *
46 1.2 thorpej * or, if invoked as "swapon" (compatibility mode):
47 1.2 thorpej *
48 1.2 thorpej * -a all devices listed as `sw' in /etc/fstab
49 1.2 thorpej * <dev> add this device
50 1.1 mrg */
51 1.1 mrg
52 1.1 mrg #include <sys/param.h>
53 1.1 mrg
54 1.1 mrg #include <vm/vm_swap.h>
55 1.1 mrg
56 1.1 mrg #include <unistd.h>
57 1.1 mrg #include <errno.h>
58 1.1 mrg #include <stdio.h>
59 1.1 mrg #include <stdlib.h>
60 1.1 mrg #include <string.h>
61 1.1 mrg #include <fstab.h>
62 1.1 mrg
63 1.1 mrg #include "swapctl.h"
64 1.1 mrg
65 1.2 thorpej int command;
66 1.2 thorpej
67 1.2 thorpej /*
68 1.2 thorpej * Commands for swapctl(8). These are mutually exclusive.
69 1.2 thorpej */
70 1.2 thorpej #define CMD_A 0x01 /* process /etc/fstab */
71 1.2 thorpej #define CMD_a 0x02 /* add a swap file/device */
72 1.2 thorpej #define CMD_c 0x04 /* change priority of a swap file/device */
73 1.2 thorpej #define CMD_d 0x08 /* delete a swap file/device */
74 1.2 thorpej #define CMD_l 0x10 /* list swap files/devices */
75 1.2 thorpej #define CMD_s 0x20 /* summary of swap files/devices */
76 1.2 thorpej
77 1.2 thorpej #define SET_COMMAND(cmd) \
78 1.2 thorpej do { \
79 1.2 thorpej if (command) \
80 1.2 thorpej usage(); \
81 1.2 thorpej command = (cmd); \
82 1.2 thorpej } while (0)
83 1.2 thorpej
84 1.2 thorpej /*
85 1.2 thorpej * Commands that require a "path" argument at the end of the command
86 1.2 thorpej * line, and the ones which require that none exist.
87 1.2 thorpej */
88 1.2 thorpej #define REQUIRE_PATH (CMD_a | CMD_c | CMD_d)
89 1.2 thorpej #define REQUIRE_NOPATH (CMD_A | CMD_l | CMD_s)
90 1.2 thorpej
91 1.2 thorpej /*
92 1.2 thorpej * Option flags, and the commands with which they are valid.
93 1.2 thorpej */
94 1.2 thorpej int kflag; /* display in 1K blocks */
95 1.2 thorpej #define KFLAG_CMDS (CMD_l | CMD_s)
96 1.2 thorpej
97 1.2 thorpej int pflag; /* priority was specified */
98 1.2 thorpej #define PFLAG_CMDS (CMD_A | CMD_a | CMD_c)
99 1.2 thorpej
100 1.1 mrg int pri; /* uses 0 as default pri */
101 1.1 mrg
102 1.1 mrg static void change_priority __P((char *));
103 1.1 mrg static void add_swap __P((char *));
104 1.1 mrg static void del_swap __P((char *));
105 1.1 mrg static void do_fstab __P((void));
106 1.1 mrg static void usage __P((void));
107 1.2 thorpej static void swapon_command __P((int, char **));
108 1.2 thorpej #if 0
109 1.2 thorpej static void swapoff_command __P((int, char **));
110 1.2 thorpej #endif
111 1.2 thorpej
112 1.2 thorpej extern char *__progname; /* from crt0.o */
113 1.1 mrg
114 1.1 mrg int
115 1.1 mrg main(argc, argv)
116 1.1 mrg int argc;
117 1.1 mrg char *argv[];
118 1.1 mrg {
119 1.1 mrg int c;
120 1.2 thorpej
121 1.2 thorpej if (strcmp(__progname, "swapon") == 0) {
122 1.2 thorpej swapon_command(argc, argv);
123 1.2 thorpej /* NOTREACHED */
124 1.2 thorpej }
125 1.2 thorpej
126 1.2 thorpej #if 0
127 1.2 thorpej if (strcmp(__progname, "swapoff") == 0) {
128 1.2 thorpej swapoff_command(argc, argv);
129 1.2 thorpej /* NOTREACHED */
130 1.2 thorpej }
131 1.2 thorpej #endif
132 1.2 thorpej
133 1.2 thorpej while ((c = getopt(argc, argv, "Aacdlkp:s")) != -1) {
134 1.2 thorpej switch (c) {
135 1.1 mrg case 'A':
136 1.2 thorpej SET_COMMAND(CMD_A);
137 1.1 mrg break;
138 1.2 thorpej
139 1.1 mrg case 'a':
140 1.2 thorpej SET_COMMAND(CMD_a);
141 1.1 mrg break;
142 1.2 thorpej
143 1.1 mrg case 'c':
144 1.2 thorpej SET_COMMAND(CMD_c);
145 1.1 mrg break;
146 1.2 thorpej
147 1.1 mrg case 'd':
148 1.2 thorpej SET_COMMAND(CMD_d);
149 1.1 mrg break;
150 1.2 thorpej
151 1.1 mrg case 'l':
152 1.2 thorpej SET_COMMAND(CMD_l);
153 1.1 mrg break;
154 1.2 thorpej
155 1.1 mrg case 'k':
156 1.1 mrg kflag = 1;
157 1.1 mrg break;
158 1.2 thorpej
159 1.1 mrg case 'p':
160 1.1 mrg pflag = 1;
161 1.2 thorpej /* XXX strtol() */
162 1.1 mrg pri = atoi(optarg);
163 1.1 mrg break;
164 1.2 thorpej
165 1.1 mrg case 's':
166 1.2 thorpej SET_COMMAND(CMD_s);
167 1.1 mrg break;
168 1.2 thorpej
169 1.2 thorpej default:
170 1.2 thorpej usage();
171 1.2 thorpej /* NOTREACHED */
172 1.1 mrg }
173 1.1 mrg }
174 1.2 thorpej
175 1.2 thorpej /* Did the user specify a command? */
176 1.2 thorpej if (command == 0)
177 1.1 mrg usage();
178 1.1 mrg
179 1.1 mrg argv += optind;
180 1.2 thorpej argc -= optind;
181 1.2 thorpej
182 1.2 thorpej switch (argc) {
183 1.2 thorpej case 0:
184 1.2 thorpej if (command & REQUIRE_PATH)
185 1.2 thorpej usage();
186 1.2 thorpej break;
187 1.2 thorpej
188 1.2 thorpej case 1:
189 1.2 thorpej if (command & REQUIRE_NOPATH)
190 1.2 thorpej usage();
191 1.2 thorpej break;
192 1.2 thorpej
193 1.2 thorpej default:
194 1.1 mrg usage();
195 1.2 thorpej }
196 1.2 thorpej
197 1.2 thorpej /* To change priority, you have to specify one. */
198 1.2 thorpej if ((command == CMD_c) && pflag == 0)
199 1.1 mrg usage();
200 1.1 mrg
201 1.2 thorpej /* Dispatch the command. */
202 1.2 thorpej switch (command) {
203 1.2 thorpej case CMD_l:
204 1.1 mrg list_swap(pri, kflag, pflag, 0, 1);
205 1.2 thorpej break;
206 1.2 thorpej
207 1.2 thorpej case CMD_s:
208 1.1 mrg list_swap(pri, kflag, pflag, 0, 0);
209 1.2 thorpej break;
210 1.2 thorpej
211 1.2 thorpej case CMD_c:
212 1.1 mrg change_priority(argv[0]);
213 1.2 thorpej break;
214 1.2 thorpej
215 1.2 thorpej case CMD_a:
216 1.1 mrg add_swap(argv[0]);
217 1.2 thorpej break;
218 1.2 thorpej
219 1.2 thorpej case CMD_d:
220 1.1 mrg del_swap(argv[0]);
221 1.2 thorpej break;
222 1.2 thorpej
223 1.2 thorpej case CMD_A:
224 1.2 thorpej do_fstab();
225 1.2 thorpej break;
226 1.2 thorpej }
227 1.2 thorpej
228 1.2 thorpej exit(0);
229 1.2 thorpej }
230 1.2 thorpej
231 1.2 thorpej /*
232 1.2 thorpej * swapon_command: emulate the old swapon(8) program.
233 1.2 thorpej */
234 1.2 thorpej void
235 1.2 thorpej swapon_command(argc, argv)
236 1.2 thorpej int argc;
237 1.2 thorpej char **argv;
238 1.2 thorpej {
239 1.2 thorpej int ch, fiztab = 0;
240 1.2 thorpej
241 1.2 thorpej while ((ch = getopt(argc, argv, "a")) != -1) {
242 1.2 thorpej switch (ch) {
243 1.2 thorpej case 'a':
244 1.2 thorpej fiztab = 1;
245 1.2 thorpej break;
246 1.2 thorpej default:
247 1.2 thorpej goto swapon_usage;
248 1.2 thorpej }
249 1.2 thorpej }
250 1.2 thorpej argc -= optind;
251 1.2 thorpej argv += optind;
252 1.2 thorpej
253 1.2 thorpej if (fiztab) {
254 1.2 thorpej if (argc)
255 1.2 thorpej goto swapon_usage;
256 1.1 mrg do_fstab();
257 1.2 thorpej exit(0);
258 1.2 thorpej } else if (argc == 0)
259 1.2 thorpej goto swapon_usage;
260 1.2 thorpej
261 1.2 thorpej while (argc) {
262 1.2 thorpej add_swap(argv[0]);
263 1.2 thorpej argc--;
264 1.2 thorpej argv++;
265 1.2 thorpej }
266 1.1 mrg exit(0);
267 1.2 thorpej /* NOTREACHED */
268 1.2 thorpej
269 1.2 thorpej swapon_usage:
270 1.2 thorpej fprintf(stderr, "usage: %s -a\n", __progname);
271 1.2 thorpej fprintf(stderr, " %s <path> ...\n", __progname);
272 1.2 thorpej exit(1);
273 1.1 mrg }
274 1.1 mrg
275 1.1 mrg /*
276 1.1 mrg * change_priority: change the priority of a swap device.
277 1.1 mrg */
278 1.1 mrg void
279 1.1 mrg change_priority(path)
280 1.1 mrg char *path;
281 1.1 mrg {
282 1.1 mrg
283 1.1 mrg if (swapctl(SWAP_CTL, path, pri) < 0)
284 1.1 mrg warn("%s", path);
285 1.1 mrg }
286 1.1 mrg
287 1.1 mrg /*
288 1.1 mrg * add_swap: add the pathname to the list of swap devices.
289 1.1 mrg */
290 1.1 mrg void
291 1.1 mrg add_swap(path)
292 1.1 mrg char *path;
293 1.1 mrg {
294 1.1 mrg
295 1.1 mrg if (swapctl(SWAP_ON, path, pri) < 0)
296 1.2 thorpej err(1, "%s", path);
297 1.1 mrg }
298 1.1 mrg
299 1.1 mrg /*
300 1.1 mrg * del_swap: remove the pathname to the list of swap devices.
301 1.1 mrg */
302 1.1 mrg void
303 1.1 mrg del_swap(path)
304 1.2 thorpej char *path;
305 1.1 mrg {
306 1.1 mrg
307 1.1 mrg if (swapctl(SWAP_OFF, path, pri) < 0)
308 1.2 thorpej err(1, "%s", path);
309 1.1 mrg }
310 1.1 mrg
311 1.1 mrg void
312 1.1 mrg do_fstab()
313 1.1 mrg {
314 1.1 mrg struct fstab *fp;
315 1.1 mrg char *s;
316 1.1 mrg long priority;
317 1.1 mrg
318 1.1 mrg #define PRIORITYEQ "priority="
319 1.1 mrg #define NFSMNTPT "nfsmntpt="
320 1.1 mrg #define PATH_MOUNT "/sbin/mount_nfs"
321 1.1 mrg while (fp = getfsent()) {
322 1.1 mrg char *spec;
323 1.1 mrg
324 1.1 mrg if (strcmp(fp->fs_type, "sw") != 0)
325 1.1 mrg continue;
326 1.1 mrg
327 1.1 mrg spec = fp->fs_spec;
328 1.1 mrg
329 1.1 mrg if (s = strstr(fp->fs_mntops, PRIORITYEQ)) {
330 1.1 mrg s += sizeof(PRIORITYEQ) - 1;
331 1.1 mrg priority = atol(s);
332 1.1 mrg } else
333 1.1 mrg priority = pri;
334 1.1 mrg
335 1.1 mrg if (s = strstr(fp->fs_mntops, NFSMNTPT)) {
336 1.1 mrg char *t, cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
337 1.1 mrg
338 1.1 mrg t = strpbrk(s, ",");
339 1.1 mrg if (t != 0)
340 1.1 mrg *t = '\0';
341 1.1 mrg spec = strdup(s + strlen(NFSMNTPT));
342 1.1 mrg if (t != 0)
343 1.1 mrg *t = ',';
344 1.1 mrg
345 1.1 mrg if (spec == NULL)
346 1.1 mrg errx(1, "Out of memory");
347 1.1 mrg
348 1.1 mrg if (strlen(spec) == 0) {
349 1.1 mrg warnx("empty mountpoint");
350 1.1 mrg free(spec);
351 1.1 mrg continue;
352 1.1 mrg }
353 1.1 mrg snprintf(cmd, sizeof(cmd), "%s %s %s",
354 1.1 mrg PATH_MOUNT, fp->fs_spec, spec);
355 1.1 mrg if (system(cmd) != 0) {
356 1.1 mrg warnx("%s: mount failed", fp->fs_spec);
357 1.1 mrg continue;
358 1.1 mrg }
359 1.1 mrg }
360 1.1 mrg
361 1.1 mrg if (swapctl(SWAP_ON, spec, (int)priority) < 0)
362 1.1 mrg warn("%s", spec);
363 1.1 mrg else
364 1.2 thorpej printf("%s: adding %s as swap device at priority %d\n",
365 1.2 thorpej __progname, fp->fs_spec, priority);
366 1.1 mrg
367 1.1 mrg if (spec != fp->fs_spec)
368 1.1 mrg free(spec);
369 1.1 mrg }
370 1.1 mrg }
371 1.1 mrg
372 1.1 mrg void
373 1.1 mrg usage()
374 1.1 mrg {
375 1.1 mrg
376 1.2 thorpej fprintf(stderr, "usage: %s -A [-p priority]\n", __progname);
377 1.2 thorpej fprintf(stderr, " %s -a [-p priority] path\n", __progname);
378 1.2 thorpej fprintf(stderr, " %s -c -p priority path\n", __progname);
379 1.2 thorpej fprintf(stderr, " %s -d path\n", __progname);
380 1.2 thorpej fprintf(stderr, " %s -l | -s [-k]\n", __progname);
381 1.1 mrg exit(1);
382 1.1 mrg }
383