swapctl.c revision 1.14 1 /* $NetBSD: swapctl.c,v 1.14 2000/04/13 08:29:56 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997, 1999 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 (also
34 * (sets the dump device, if listed in fstab)
35 * -D <dev> set dumpdev to <dev>
36 * -U remove all devices listed as `sw' in /etc/fstab.
37 * -t [blk|noblk] if -A or -U , add (remove) either all block device
38 * or all non-block devices
39 * -a <dev> add this device
40 * -d <dev> remove this swap device (not supported yet)
41 * -l list swap devices
42 * -s short listing of swap devices
43 * -k use kilobytes
44 * -p <pri> use this priority
45 * -c change priority
46 *
47 * or, if invoked as "swapon" (compatibility mode):
48 *
49 * -a all devices listed as `sw' in /etc/fstab
50 * -t same as -t above (feature not present in old
51 * swapon(8) command)
52 * <dev> add this device
53 */
54
55 #include <sys/param.h>
56 #include <sys/stat.h>
57 #include <sys/swap.h>
58
59 #include <unistd.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <fstab.h>
66
67 #include "swapctl.h"
68
69 int command;
70
71 /*
72 * Commands for swapctl(8). These are mutually exclusive.
73 */
74 #define CMD_A 0x01 /* process /etc/fstab for adding */
75 #define CMD_D 0x02 /* set dumpdev */
76 #define CMD_U 0x04 /* process /etc/fstab for removing */
77 #define CMD_a 0x08 /* add a swap file/device */
78 #define CMD_c 0x10 /* change priority of a swap file/device */
79 #define CMD_d 0x20 /* delete a swap file/device */
80 #define CMD_l 0x40 /* list swap files/devices */
81 #define CMD_s 0x80 /* 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_D | CMD_a | CMD_c | CMD_d)
95 #define REQUIRE_NOPATH (CMD_A | CMD_U | 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 | CMD_U )
108
109 int pri; /* uses 0 as default pri */
110
111 static void change_priority __P((const char *));
112 static int add_swap __P((const char *, int));
113 static int delete_swap __P((const char *));
114 static void set_dumpdev __P((const char *));
115 int main __P((int, char *[]));
116 static void do_fstab __P((int));
117 static void usage __P((void));
118 static void swapon_command __P((int, char **));
119 #if 0
120 static void swapoff_command __P((int, char **));
121 #endif
122
123 extern char *__progname; /* from crt0.o */
124
125 int
126 main(argc, argv)
127 int argc;
128 char *argv[];
129 {
130 int c;
131
132 if (strcmp(__progname, "swapon") == 0) {
133 swapon_command(argc, argv);
134 /* NOTREACHED */
135 }
136
137 #if 0
138 if (strcmp(__progname, "swapoff") == 0) {
139 swapoff_command(argc, argv);
140 /* NOTREACHED */
141 }
142 #endif
143
144 while ((c = getopt(argc, argv, "ADUacdlkp:st:")) != -1) {
145 switch (c) {
146 case 'A':
147 SET_COMMAND(CMD_A);
148 break;
149
150 case 'D':
151 SET_COMMAND(CMD_D);
152 break;
153
154 case 'U':
155 SET_COMMAND(CMD_U);
156 break;
157
158 case 'a':
159 SET_COMMAND(CMD_a);
160 break;
161
162 case 'c':
163 SET_COMMAND(CMD_c);
164 break;
165
166 case 'd':
167 SET_COMMAND(CMD_d);
168 break;
169
170 case 'l':
171 SET_COMMAND(CMD_l);
172 break;
173
174 case 'k':
175 kflag = 1;
176 break;
177
178 case 'p':
179 pflag = 1;
180 /* XXX strtol() */
181 pri = atoi(optarg);
182 break;
183
184 case 's':
185 SET_COMMAND(CMD_s);
186 break;
187
188 case 't':
189 if (tflag != NULL)
190 usage();
191 tflag = optarg;
192 break;
193
194 default:
195 usage();
196 /* NOTREACHED */
197 }
198 }
199
200 /* Did the user specify a command? */
201 if (command == 0)
202 usage();
203
204 argv += optind;
205 argc -= optind;
206
207 switch (argc) {
208 case 0:
209 if (command & REQUIRE_PATH)
210 usage();
211 break;
212
213 case 1:
214 if (command & REQUIRE_NOPATH)
215 usage();
216 break;
217
218 default:
219 usage();
220 }
221
222 /* To change priority, you have to specify one. */
223 if ((command == CMD_c) && pflag == 0)
224 usage();
225
226 /* Sanity-check -t */
227 if (tflag != NULL) {
228 if (command != CMD_A && command != CMD_U)
229 usage();
230 if (strcmp(tflag, "blk") != 0 &&
231 strcmp(tflag, "noblk") != 0)
232 usage();
233 }
234
235 /* Dispatch the command. */
236 switch (command) {
237 case CMD_l:
238 list_swap(pri, kflag, pflag, 0, 1);
239 break;
240
241 case CMD_s:
242 list_swap(pri, kflag, pflag, 0, 0);
243 break;
244
245 case CMD_c:
246 change_priority(argv[0]);
247 break;
248
249 case CMD_a:
250 if (! add_swap(argv[0], pri))
251 exit(1);
252 break;
253
254 case CMD_d:
255 if (! delete_swap(argv[0]))
256 exit(1);
257 break;
258
259 case CMD_A:
260 do_fstab(1);
261 break;
262
263 case CMD_D:
264 set_dumpdev(argv[0]);
265 break;
266
267 case CMD_U:
268 do_fstab(0);
269 break;
270 }
271
272 exit(0);
273 }
274
275 /*
276 * swapon_command: emulate the old swapon(8) program.
277 */
278 static void
279 swapon_command(argc, argv)
280 int argc;
281 char **argv;
282 {
283 int ch, fiztab = 0;
284
285 while ((ch = getopt(argc, argv, "at:")) != -1) {
286 switch (ch) {
287 case 'a':
288 fiztab = 1;
289 break;
290 case 't':
291 if (tflag != NULL)
292 usage();
293 tflag = optarg;
294 break;
295 default:
296 goto swapon_usage;
297 }
298 }
299 argc -= optind;
300 argv += optind;
301
302 if (fiztab) {
303 if (argc)
304 goto swapon_usage;
305 /* Sanity-check -t */
306 if (tflag != NULL) {
307 if (strcmp(tflag, "blk") != 0 &&
308 strcmp(tflag, "noblk") != 0)
309 usage();
310 }
311 do_fstab(1);
312 exit(0);
313 } else if (argc == 0 || tflag != NULL)
314 goto swapon_usage;
315
316 while (argc) {
317 if (! add_swap(argv[0], pri))
318 exit(1);
319 argc--;
320 argv++;
321 }
322 exit(0);
323 /* NOTREACHED */
324
325 swapon_usage:
326 fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", __progname);
327 fprintf(stderr, " %s <path> ...\n", __progname);
328 exit(1);
329 }
330
331 /*
332 * change_priority: change the priority of a swap device.
333 */
334 static void
335 change_priority(path)
336 const char *path;
337 {
338
339 if (swapctl(SWAP_CTL, path, pri) < 0)
340 warn("%s", path);
341 }
342
343 /*
344 * add_swap: add the pathname to the list of swap devices.
345 */
346 static int
347 add_swap(path, priority)
348 const char *path;
349 int priority;
350 {
351 struct stat sb;
352
353 if (stat(path, &sb) < 0)
354 goto oops;
355
356 if (sb.st_mode & S_IROTH)
357 warnx("%s is readable by the world", path);
358 if (sb.st_mode & S_IWOTH)
359 warnx("%s is writable by the world", path);
360
361 if (swapctl(SWAP_ON, path, priority) < 0) {
362 oops:
363 warn("%s", path);
364 return (0);
365 }
366 return (1);
367 }
368
369 /*
370 * delete_swap: remove the pathname to the list of swap devices.
371 */
372 static int
373 delete_swap(path)
374 const char *path;
375 {
376
377 if (swapctl(SWAP_OFF, path, pri) < 0) {
378 warn("%s", path);
379 return (0);
380 }
381 return (1);
382 }
383
384 static void
385 set_dumpdev(path)
386 const char *path;
387 {
388
389 if (swapctl(SWAP_DUMPDEV, path, NULL) == -1)
390 warn("could not set dump device to %s", path);
391 else
392 printf("%s: setting dump device to %s\n", __progname, path);
393 }
394
395 static void
396 do_fstab(add)
397 int add;
398 {
399 struct fstab *fp;
400 char *s;
401 long priority;
402 struct stat st;
403 int isblk;
404 int gotone = 0;
405 #define PATH_MOUNT "/sbin/mount_nfs"
406 #define PATH_UMOUNT "/sbin/umount"
407 char cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
408
409 #define PRIORITYEQ "priority="
410 #define NFSMNTPT "nfsmntpt="
411 while ((fp = getfsent()) != NULL) {
412 const char *spec;
413
414 spec = fp->fs_spec;
415 cmd[0] = '\0';
416
417 if (strcmp(fp->fs_type, "dp") == 0 && add) {
418 set_dumpdev(spec);
419 continue;
420 }
421
422 if (strcmp(fp->fs_type, "sw") != 0)
423 continue;
424 isblk = 0;
425
426 if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
427 s += sizeof(PRIORITYEQ) - 1;
428 priority = atol(s);
429 } else
430 priority = pri;
431
432 if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
433 char *t;
434
435 /*
436 * Skip this song and dance if we're only
437 * doing block devices.
438 */
439 if (tflag != NULL && strcmp(tflag, "blk") == 0)
440 continue;
441
442 t = strpbrk(s, ",");
443 if (t != 0)
444 *t = '\0';
445 spec = strdup(s + strlen(NFSMNTPT));
446 if (t != 0)
447 *t = ',';
448
449 if (spec == NULL)
450 errx(1, "Out of memory");
451
452 if (strlen(spec) == 0) {
453 warnx("empty mountpoint");
454 free((char *)spec);
455 continue;
456 }
457 if (add) {
458 snprintf(cmd, sizeof(cmd), "%s %s %s",
459 PATH_MOUNT, fp->fs_spec, spec);
460 if (system(cmd) != 0) {
461 warnx("%s: mount failed", fp->fs_spec);
462 continue;
463 }
464 } else {
465 snprintf(cmd, sizeof(cmd), "%s %s",
466 PATH_UMOUNT, fp->fs_spec);
467 }
468 } else {
469 /*
470 * Determine blk-ness.
471 */
472 if (stat(spec, &st) < 0) {
473 warn(spec);
474 continue;
475 }
476 if (S_ISBLK(st.st_mode))
477 isblk = 1;
478 }
479
480 /*
481 * Skip this type if we're told to.
482 */
483 if (tflag != NULL) {
484 if (strcmp(tflag, "blk") == 0 && isblk == 0)
485 continue;
486 if (strcmp(tflag, "noblk") == 0 && isblk == 1)
487 continue;
488 }
489
490 if (add) {
491 if (add_swap(spec, (int)priority)) {
492 gotone = 1;
493 printf(
494 "%s: adding %s as swap device at priority %d\n",
495 __progname, fp->fs_spec, (int)priority);
496 }
497 } else {
498 if (delete_swap(spec)) {
499 gotone = 1;
500 printf(
501 "%s: removing %s as swap device\n",
502 __progname, fp->fs_spec);
503 }
504 if (cmd[0]) {
505 if (system(cmd) != 0) {
506 warnx("%s: umount failed", fp->fs_spec);
507 continue;
508 }
509 }
510 }
511
512 if (spec != fp->fs_spec)
513 free((char *)spec);
514 }
515 if (gotone == 0)
516 exit(1);
517 }
518
519 static void
520 usage()
521 {
522
523 fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
524 __progname);
525 fprintf(stderr, " %s -D dumppath\n", __progname);
526 fprintf(stderr, " %s -U [-t blk|noblk]\n", __progname);
527 fprintf(stderr, " %s -a [-p priority] path\n", __progname);
528 fprintf(stderr, " %s -c -p priority path\n", __progname);
529 fprintf(stderr, " %s -d path\n", __progname);
530 fprintf(stderr, " %s -l | -s [-k]\n", __progname);
531 exit(1);
532 }
533