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