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