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