swapctl.c revision 1.30 1 1.30 martin /* $NetBSD: swapctl.c,v 1.30 2006/08/22 14:08:36 martin Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.11 mrg * Copyright (c) 1996, 1997, 1999 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.7 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * swapctl command:
33 1.10 mrg * -A add all devices listed as `sw' in /etc/fstab (also
34 1.13 soren * (sets the dump device, if listed in fstab)
35 1.30 martin * -D [<dev>|none] set dumpdev to <dev> or disable dumps
36 1.16 mrg * -z show dumpdev
37 1.12 lukem * -U remove all devices listed as `sw' in /etc/fstab.
38 1.12 lukem * -t [blk|noblk] if -A or -U , add (remove) either all block device
39 1.12 lukem * or all non-block devices
40 1.1 mrg * -a <dev> add this device
41 1.17 lukem * -d <dev> remove this swap device
42 1.25 mrg * -g use gigabytes
43 1.24 mrg * -h use humanize_number(3) for listing
44 1.1 mrg * -l list swap devices
45 1.25 mrg * -m use megabytes
46 1.1 mrg * -s short listing of swap devices
47 1.1 mrg * -k use kilobytes
48 1.1 mrg * -p <pri> use this priority
49 1.1 mrg * -c change priority
50 1.2 thorpej *
51 1.2 thorpej * or, if invoked as "swapon" (compatibility mode):
52 1.2 thorpej *
53 1.2 thorpej * -a all devices listed as `sw' in /etc/fstab
54 1.4 thorpej * -t same as -t above (feature not present in old
55 1.4 thorpej * swapon(8) command)
56 1.2 thorpej * <dev> add this device
57 1.1 mrg */
58 1.22 agc #include <sys/cdefs.h>
59 1.22 agc
60 1.22 agc #ifndef lint
61 1.30 martin __RCSID("$NetBSD: swapctl.c,v 1.30 2006/08/22 14:08:36 martin Exp $");
62 1.22 agc #endif
63 1.22 agc
64 1.1 mrg
65 1.1 mrg #include <sys/param.h>
66 1.4 thorpej #include <sys/stat.h>
67 1.14 mrg #include <sys/swap.h>
68 1.1 mrg
69 1.1 mrg #include <unistd.h>
70 1.3 mikel #include <err.h>
71 1.1 mrg #include <errno.h>
72 1.1 mrg #include <stdio.h>
73 1.1 mrg #include <stdlib.h>
74 1.1 mrg #include <string.h>
75 1.1 mrg #include <fstab.h>
76 1.1 mrg
77 1.1 mrg #include "swapctl.h"
78 1.1 mrg
79 1.2 thorpej int command;
80 1.2 thorpej
81 1.2 thorpej /*
82 1.2 thorpej * Commands for swapctl(8). These are mutually exclusive.
83 1.2 thorpej */
84 1.12 lukem #define CMD_A 0x01 /* process /etc/fstab for adding */
85 1.10 mrg #define CMD_D 0x02 /* set dumpdev */
86 1.12 lukem #define CMD_U 0x04 /* process /etc/fstab for removing */
87 1.12 lukem #define CMD_a 0x08 /* add a swap file/device */
88 1.12 lukem #define CMD_c 0x10 /* change priority of a swap file/device */
89 1.12 lukem #define CMD_d 0x20 /* delete a swap file/device */
90 1.12 lukem #define CMD_l 0x40 /* list swap files/devices */
91 1.12 lukem #define CMD_s 0x80 /* summary of swap files/devices */
92 1.16 mrg #define CMD_z 0x100 /* show dump device */
93 1.2 thorpej
94 1.2 thorpej #define SET_COMMAND(cmd) \
95 1.2 thorpej do { \
96 1.2 thorpej if (command) \
97 1.2 thorpej usage(); \
98 1.2 thorpej command = (cmd); \
99 1.2 thorpej } while (0)
100 1.2 thorpej
101 1.2 thorpej /*
102 1.2 thorpej * Commands that require a "path" argument at the end of the command
103 1.2 thorpej * line, and the ones which require that none exist.
104 1.2 thorpej */
105 1.10 mrg #define REQUIRE_PATH (CMD_D | CMD_a | CMD_c | CMD_d)
106 1.16 mrg #define REQUIRE_NOPATH (CMD_A | CMD_U | CMD_l | CMD_s | CMD_z)
107 1.2 thorpej
108 1.2 thorpej /*
109 1.2 thorpej * Option flags, and the commands with which they are valid.
110 1.2 thorpej */
111 1.25 mrg int kflag; /* display in 1K^x blocks */
112 1.2 thorpej #define KFLAG_CMDS (CMD_l | CMD_s)
113 1.25 mrg #define MFLAG_CMDS (CMD_l | CMD_s)
114 1.25 mrg #define GFLAG_CMDS (CMD_l | CMD_s)
115 1.2 thorpej
116 1.24 mrg int hflag; /* display with humanize_number */
117 1.24 mrg #define HFLAG_CMDS (CMD_l | CMD_s)
118 1.24 mrg
119 1.2 thorpej int pflag; /* priority was specified */
120 1.2 thorpej #define PFLAG_CMDS (CMD_A | CMD_a | CMD_c)
121 1.2 thorpej
122 1.4 thorpej char *tflag; /* swap device type (blk or noblk) */
123 1.16 mrg #define TFLAG_CMDS (CMD_A | CMD_U)
124 1.4 thorpej
125 1.1 mrg int pri; /* uses 0 as default pri */
126 1.1 mrg
127 1.29 christos static void change_priority(char *);
128 1.29 christos static int add_swap(char *, int);
129 1.29 christos static int delete_swap(char *);
130 1.29 christos static void set_dumpdev(char *);
131 1.30 martin static int get_dumpdev(void);
132 1.28 xtraeme static void do_fstab(int);
133 1.28 xtraeme static void usage(void);
134 1.28 xtraeme static void swapon_command(int, char **);
135 1.2 thorpej #if 0
136 1.28 xtraeme static void swapoff_command(int, char **);
137 1.2 thorpej #endif
138 1.2 thorpej
139 1.1 mrg int
140 1.28 xtraeme main(int argc, char *argv[])
141 1.1 mrg {
142 1.1 mrg int c;
143 1.2 thorpej
144 1.18 cgd if (strcmp(getprogname(), "swapon") == 0) {
145 1.2 thorpej swapon_command(argc, argv);
146 1.2 thorpej /* NOTREACHED */
147 1.2 thorpej }
148 1.2 thorpej
149 1.2 thorpej #if 0
150 1.18 cgd if (strcmp(getprogname(), "swapoff") == 0) {
151 1.2 thorpej swapoff_command(argc, argv);
152 1.2 thorpej /* NOTREACHED */
153 1.2 thorpej }
154 1.2 thorpej #endif
155 1.2 thorpej
156 1.25 mrg while ((c = getopt(argc, argv, "ADUacdghklmp:st:z")) != -1) {
157 1.2 thorpej switch (c) {
158 1.1 mrg case 'A':
159 1.2 thorpej SET_COMMAND(CMD_A);
160 1.1 mrg break;
161 1.2 thorpej
162 1.10 mrg case 'D':
163 1.10 mrg SET_COMMAND(CMD_D);
164 1.10 mrg break;
165 1.10 mrg
166 1.12 lukem case 'U':
167 1.12 lukem SET_COMMAND(CMD_U);
168 1.12 lukem break;
169 1.12 lukem
170 1.1 mrg case 'a':
171 1.2 thorpej SET_COMMAND(CMD_a);
172 1.1 mrg break;
173 1.2 thorpej
174 1.1 mrg case 'c':
175 1.2 thorpej SET_COMMAND(CMD_c);
176 1.1 mrg break;
177 1.2 thorpej
178 1.1 mrg case 'd':
179 1.2 thorpej SET_COMMAND(CMD_d);
180 1.1 mrg break;
181 1.2 thorpej
182 1.25 mrg case 'g':
183 1.25 mrg kflag = 3; /* 1k ^ 3 */
184 1.25 mrg break;
185 1.25 mrg
186 1.24 mrg case 'h':
187 1.24 mrg hflag = 1;
188 1.24 mrg break;
189 1.24 mrg
190 1.25 mrg case 'k':
191 1.25 mrg kflag = 1;
192 1.25 mrg break;
193 1.25 mrg
194 1.1 mrg case 'l':
195 1.2 thorpej SET_COMMAND(CMD_l);
196 1.1 mrg break;
197 1.2 thorpej
198 1.25 mrg case 'm':
199 1.25 mrg kflag = 2; /* 1k ^ 2 */
200 1.1 mrg break;
201 1.2 thorpej
202 1.1 mrg case 'p':
203 1.1 mrg pflag = 1;
204 1.2 thorpej /* XXX strtol() */
205 1.1 mrg pri = atoi(optarg);
206 1.1 mrg break;
207 1.2 thorpej
208 1.1 mrg case 's':
209 1.2 thorpej SET_COMMAND(CMD_s);
210 1.1 mrg break;
211 1.2 thorpej
212 1.4 thorpej case 't':
213 1.4 thorpej if (tflag != NULL)
214 1.4 thorpej usage();
215 1.4 thorpej tflag = optarg;
216 1.4 thorpej break;
217 1.4 thorpej
218 1.16 mrg case 'z':
219 1.16 mrg SET_COMMAND(CMD_z);
220 1.16 mrg break;
221 1.16 mrg
222 1.2 thorpej default:
223 1.2 thorpej usage();
224 1.2 thorpej /* NOTREACHED */
225 1.1 mrg }
226 1.1 mrg }
227 1.2 thorpej
228 1.2 thorpej /* Did the user specify a command? */
229 1.2 thorpej if (command == 0)
230 1.1 mrg usage();
231 1.1 mrg
232 1.1 mrg argv += optind;
233 1.2 thorpej argc -= optind;
234 1.2 thorpej
235 1.2 thorpej switch (argc) {
236 1.2 thorpej case 0:
237 1.2 thorpej if (command & REQUIRE_PATH)
238 1.2 thorpej usage();
239 1.2 thorpej break;
240 1.2 thorpej
241 1.2 thorpej case 1:
242 1.2 thorpej if (command & REQUIRE_NOPATH)
243 1.2 thorpej usage();
244 1.2 thorpej break;
245 1.2 thorpej
246 1.2 thorpej default:
247 1.1 mrg usage();
248 1.2 thorpej }
249 1.2 thorpej
250 1.2 thorpej /* To change priority, you have to specify one. */
251 1.2 thorpej if ((command == CMD_c) && pflag == 0)
252 1.1 mrg usage();
253 1.1 mrg
254 1.4 thorpej /* Sanity-check -t */
255 1.4 thorpej if (tflag != NULL) {
256 1.12 lukem if (command != CMD_A && command != CMD_U)
257 1.4 thorpej usage();
258 1.4 thorpej if (strcmp(tflag, "blk") != 0 &&
259 1.4 thorpej strcmp(tflag, "noblk") != 0)
260 1.4 thorpej usage();
261 1.4 thorpej }
262 1.4 thorpej
263 1.2 thorpej /* Dispatch the command. */
264 1.2 thorpej switch (command) {
265 1.2 thorpej case CMD_l:
266 1.30 martin if (!list_swap(pri, kflag, pflag, 0, 1, hflag))
267 1.30 martin exit(1);
268 1.2 thorpej break;
269 1.2 thorpej
270 1.2 thorpej case CMD_s:
271 1.24 mrg list_swap(pri, kflag, pflag, 0, 0, hflag);
272 1.2 thorpej break;
273 1.2 thorpej
274 1.2 thorpej case CMD_c:
275 1.1 mrg change_priority(argv[0]);
276 1.2 thorpej break;
277 1.2 thorpej
278 1.2 thorpej case CMD_a:
279 1.12 lukem if (! add_swap(argv[0], pri))
280 1.12 lukem exit(1);
281 1.2 thorpej break;
282 1.2 thorpej
283 1.2 thorpej case CMD_d:
284 1.12 lukem if (! delete_swap(argv[0]))
285 1.12 lukem exit(1);
286 1.2 thorpej break;
287 1.2 thorpej
288 1.2 thorpej case CMD_A:
289 1.12 lukem do_fstab(1);
290 1.2 thorpej break;
291 1.10 mrg
292 1.10 mrg case CMD_D:
293 1.10 mrg set_dumpdev(argv[0]);
294 1.10 mrg break;
295 1.12 lukem
296 1.16 mrg case CMD_z:
297 1.30 martin if (!get_dumpdev())
298 1.30 martin exit(1);
299 1.16 mrg break;
300 1.16 mrg
301 1.12 lukem case CMD_U:
302 1.12 lukem do_fstab(0);
303 1.12 lukem break;
304 1.2 thorpej }
305 1.2 thorpej
306 1.2 thorpej exit(0);
307 1.2 thorpej }
308 1.2 thorpej
309 1.2 thorpej /*
310 1.2 thorpej * swapon_command: emulate the old swapon(8) program.
311 1.2 thorpej */
312 1.12 lukem static void
313 1.28 xtraeme swapon_command(int argc, char **argv)
314 1.2 thorpej {
315 1.2 thorpej int ch, fiztab = 0;
316 1.2 thorpej
317 1.4 thorpej while ((ch = getopt(argc, argv, "at:")) != -1) {
318 1.2 thorpej switch (ch) {
319 1.2 thorpej case 'a':
320 1.2 thorpej fiztab = 1;
321 1.2 thorpej break;
322 1.4 thorpej case 't':
323 1.4 thorpej if (tflag != NULL)
324 1.4 thorpej usage();
325 1.4 thorpej tflag = optarg;
326 1.4 thorpej break;
327 1.2 thorpej default:
328 1.2 thorpej goto swapon_usage;
329 1.2 thorpej }
330 1.2 thorpej }
331 1.2 thorpej argc -= optind;
332 1.2 thorpej argv += optind;
333 1.2 thorpej
334 1.2 thorpej if (fiztab) {
335 1.2 thorpej if (argc)
336 1.2 thorpej goto swapon_usage;
337 1.4 thorpej /* Sanity-check -t */
338 1.4 thorpej if (tflag != NULL) {
339 1.4 thorpej if (strcmp(tflag, "blk") != 0 &&
340 1.4 thorpej strcmp(tflag, "noblk") != 0)
341 1.4 thorpej usage();
342 1.4 thorpej }
343 1.12 lukem do_fstab(1);
344 1.2 thorpej exit(0);
345 1.4 thorpej } else if (argc == 0 || tflag != NULL)
346 1.2 thorpej goto swapon_usage;
347 1.2 thorpej
348 1.2 thorpej while (argc) {
349 1.12 lukem if (! add_swap(argv[0], pri))
350 1.12 lukem exit(1);
351 1.2 thorpej argc--;
352 1.2 thorpej argv++;
353 1.2 thorpej }
354 1.1 mrg exit(0);
355 1.2 thorpej /* NOTREACHED */
356 1.2 thorpej
357 1.2 thorpej swapon_usage:
358 1.18 cgd fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
359 1.18 cgd fprintf(stderr, " %s <path> ...\n", getprogname());
360 1.2 thorpej exit(1);
361 1.1 mrg }
362 1.1 mrg
363 1.1 mrg /*
364 1.1 mrg * change_priority: change the priority of a swap device.
365 1.1 mrg */
366 1.12 lukem static void
367 1.29 christos change_priority(char *path)
368 1.1 mrg {
369 1.1 mrg
370 1.1 mrg if (swapctl(SWAP_CTL, path, pri) < 0)
371 1.30 martin err(1, "%s", path);
372 1.1 mrg }
373 1.1 mrg
374 1.1 mrg /*
375 1.1 mrg * add_swap: add the pathname to the list of swap devices.
376 1.1 mrg */
377 1.12 lukem static int
378 1.29 christos add_swap(char *path, int priority)
379 1.1 mrg {
380 1.10 mrg struct stat sb;
381 1.10 mrg
382 1.10 mrg if (stat(path, &sb) < 0)
383 1.10 mrg goto oops;
384 1.10 mrg
385 1.10 mrg if (sb.st_mode & S_IROTH)
386 1.19 pooka warnx("WARNING: %s is readable by the world", path);
387 1.10 mrg if (sb.st_mode & S_IWOTH)
388 1.19 pooka warnx("WARNING: %s is writable by the world", path);
389 1.1 mrg
390 1.12 lukem if (swapctl(SWAP_ON, path, priority) < 0) {
391 1.10 mrg oops:
392 1.30 martin err(1, "%s", path);
393 1.12 lukem }
394 1.12 lukem return (1);
395 1.1 mrg }
396 1.1 mrg
397 1.1 mrg /*
398 1.11 mrg * delete_swap: remove the pathname to the list of swap devices.
399 1.1 mrg */
400 1.12 lukem static int
401 1.29 christos delete_swap(char *path)
402 1.1 mrg {
403 1.1 mrg
404 1.30 martin if (swapctl(SWAP_OFF, path, pri) < 0)
405 1.30 martin err(1, "%s", path);
406 1.12 lukem return (1);
407 1.1 mrg }
408 1.1 mrg
409 1.12 lukem static void
410 1.29 christos set_dumpdev(char *path)
411 1.10 mrg {
412 1.30 martin int rv;
413 1.10 mrg
414 1.30 martin if (strcmp(path, "none") == 0)
415 1.30 martin rv = swapctl(SWAP_DUMPOFF, NULL, 0);
416 1.30 martin else
417 1.30 martin rv = swapctl(SWAP_DUMPDEV, path, 0);
418 1.30 martin
419 1.30 martin if (rv == -1)
420 1.30 martin err(1, "could not set dump device to %s", path);
421 1.10 mrg else
422 1.18 cgd printf("%s: setting dump device to %s\n", getprogname(), path);
423 1.16 mrg }
424 1.16 mrg
425 1.30 martin static int
426 1.28 xtraeme get_dumpdev(void)
427 1.16 mrg {
428 1.16 mrg dev_t dev;
429 1.16 mrg char *name;
430 1.16 mrg
431 1.30 martin if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) {
432 1.16 mrg warn("could not get dump device");
433 1.30 martin return 0;
434 1.30 martin } else if (dev == NODEV) {
435 1.21 drochner printf("no dump device set\n");
436 1.30 martin return 0;
437 1.30 martin } else {
438 1.16 mrg name = devname(dev, S_IFBLK);
439 1.16 mrg printf("dump device is ");
440 1.16 mrg if (name)
441 1.16 mrg printf("%s\n", name);
442 1.16 mrg else
443 1.16 mrg printf("major %d minor %d\n", major(dev), minor(dev));
444 1.16 mrg }
445 1.30 martin return 1;
446 1.10 mrg }
447 1.10 mrg
448 1.12 lukem static void
449 1.28 xtraeme do_fstab(int add)
450 1.1 mrg {
451 1.1 mrg struct fstab *fp;
452 1.1 mrg char *s;
453 1.1 mrg long priority;
454 1.4 thorpej struct stat st;
455 1.4 thorpej int isblk;
456 1.8 mrg int gotone = 0;
457 1.27 lukem
458 1.27 lukem #ifdef RESCUEDIR
459 1.27 lukem #define PATH_MOUNT RESCUEDIR "/mount_nfs"
460 1.27 lukem #define PATH_UMOUNT RESCUEDIR "/umount"
461 1.27 lukem #else
462 1.12 lukem #define PATH_MOUNT "/sbin/mount_nfs"
463 1.12 lukem #define PATH_UMOUNT "/sbin/umount"
464 1.27 lukem #endif
465 1.27 lukem
466 1.12 lukem char cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
467 1.1 mrg
468 1.1 mrg #define PRIORITYEQ "priority="
469 1.1 mrg #define NFSMNTPT "nfsmntpt="
470 1.5 mikel while ((fp = getfsent()) != NULL) {
471 1.28 xtraeme char *spec;
472 1.1 mrg
473 1.10 mrg spec = fp->fs_spec;
474 1.12 lukem cmd[0] = '\0';
475 1.10 mrg
476 1.12 lukem if (strcmp(fp->fs_type, "dp") == 0 && add) {
477 1.10 mrg set_dumpdev(spec);
478 1.10 mrg continue;
479 1.10 mrg }
480 1.10 mrg
481 1.1 mrg if (strcmp(fp->fs_type, "sw") != 0)
482 1.1 mrg continue;
483 1.20 jdolecek
484 1.20 jdolecek /* handle dp as mnt option */
485 1.20 jdolecek if (strstr(fp->fs_mntops, "dp") && add)
486 1.20 jdolecek set_dumpdev(spec);
487 1.20 jdolecek
488 1.4 thorpej isblk = 0;
489 1.1 mrg
490 1.5 mikel if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
491 1.1 mrg s += sizeof(PRIORITYEQ) - 1;
492 1.1 mrg priority = atol(s);
493 1.1 mrg } else
494 1.1 mrg priority = pri;
495 1.1 mrg
496 1.5 mikel if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
497 1.12 lukem char *t;
498 1.1 mrg
499 1.4 thorpej /*
500 1.4 thorpej * Skip this song and dance if we're only
501 1.4 thorpej * doing block devices.
502 1.4 thorpej */
503 1.12 lukem if (tflag != NULL && strcmp(tflag, "blk") == 0)
504 1.4 thorpej continue;
505 1.4 thorpej
506 1.1 mrg t = strpbrk(s, ",");
507 1.1 mrg if (t != 0)
508 1.1 mrg *t = '\0';
509 1.1 mrg spec = strdup(s + strlen(NFSMNTPT));
510 1.1 mrg if (t != 0)
511 1.1 mrg *t = ',';
512 1.1 mrg
513 1.1 mrg if (spec == NULL)
514 1.1 mrg errx(1, "Out of memory");
515 1.1 mrg
516 1.1 mrg if (strlen(spec) == 0) {
517 1.1 mrg warnx("empty mountpoint");
518 1.28 xtraeme free(spec);
519 1.1 mrg continue;
520 1.1 mrg }
521 1.12 lukem if (add) {
522 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s %s",
523 1.12 lukem PATH_MOUNT, fp->fs_spec, spec);
524 1.12 lukem if (system(cmd) != 0) {
525 1.12 lukem warnx("%s: mount failed", fp->fs_spec);
526 1.12 lukem continue;
527 1.12 lukem }
528 1.12 lukem } else {
529 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s",
530 1.12 lukem PATH_UMOUNT, fp->fs_spec);
531 1.1 mrg }
532 1.4 thorpej } else {
533 1.4 thorpej /*
534 1.4 thorpej * Determine blk-ness.
535 1.4 thorpej */
536 1.4 thorpej if (stat(spec, &st) < 0) {
537 1.15 itojun warn("%s", spec);
538 1.4 thorpej continue;
539 1.4 thorpej }
540 1.4 thorpej if (S_ISBLK(st.st_mode))
541 1.4 thorpej isblk = 1;
542 1.4 thorpej }
543 1.4 thorpej
544 1.4 thorpej /*
545 1.4 thorpej * Skip this type if we're told to.
546 1.4 thorpej */
547 1.4 thorpej if (tflag != NULL) {
548 1.4 thorpej if (strcmp(tflag, "blk") == 0 && isblk == 0)
549 1.4 thorpej continue;
550 1.4 thorpej if (strcmp(tflag, "noblk") == 0 && isblk == 1)
551 1.4 thorpej continue;
552 1.1 mrg }
553 1.1 mrg
554 1.12 lukem if (add) {
555 1.12 lukem if (add_swap(spec, (int)priority)) {
556 1.12 lukem gotone = 1;
557 1.12 lukem printf(
558 1.12 lukem "%s: adding %s as swap device at priority %d\n",
559 1.18 cgd getprogname(), fp->fs_spec, (int)priority);
560 1.12 lukem }
561 1.12 lukem } else {
562 1.12 lukem if (delete_swap(spec)) {
563 1.12 lukem gotone = 1;
564 1.12 lukem printf(
565 1.12 lukem "%s: removing %s as swap device\n",
566 1.18 cgd getprogname(), fp->fs_spec);
567 1.12 lukem }
568 1.12 lukem if (cmd[0]) {
569 1.12 lukem if (system(cmd) != 0) {
570 1.12 lukem warnx("%s: umount failed", fp->fs_spec);
571 1.12 lukem continue;
572 1.12 lukem }
573 1.12 lukem }
574 1.8 mrg }
575 1.1 mrg
576 1.1 mrg if (spec != fp->fs_spec)
577 1.28 xtraeme free(spec);
578 1.1 mrg }
579 1.8 mrg if (gotone == 0)
580 1.8 mrg exit(1);
581 1.1 mrg }
582 1.1 mrg
583 1.12 lukem static void
584 1.28 xtraeme usage(void)
585 1.1 mrg {
586 1.18 cgd const char *progname = getprogname();
587 1.1 mrg
588 1.4 thorpej fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
589 1.18 cgd progname);
590 1.18 cgd fprintf(stderr, " %s -D dumppath\n", progname);
591 1.18 cgd fprintf(stderr, " %s -U [-t blk|noblk]\n", progname);
592 1.18 cgd fprintf(stderr, " %s -a [-p priority] path\n", progname);
593 1.18 cgd fprintf(stderr, " %s -c -p priority path\n", progname);
594 1.18 cgd fprintf(stderr, " %s -d path\n", progname);
595 1.25 mrg fprintf(stderr, " %s -l | -s [-k|-m|-g|-h]\n", progname);
596 1.26 cjep fprintf(stderr, " %s -z\n", progname);
597 1.1 mrg exit(1);
598 1.1 mrg }
599