Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.35
      1  1.35       apb /*	$NetBSD: swapctl.c,v 1.35 2009/09/24 16:15:20 apb 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.1       mrg  *
     16   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1       mrg  * SUCH DAMAGE.
     27   1.1       mrg  */
     28   1.1       mrg 
     29   1.1       mrg /*
     30   1.1       mrg  * swapctl command:
     31  1.10       mrg  *	-A		add all devices listed as `sw' in /etc/fstab (also
     32  1.13     soren  *			(sets the dump device, if listed in fstab)
     33  1.30    martin  *	-D [<dev>|none]	set dumpdev to <dev> or disable dumps
     34  1.16       mrg  *	-z		show dumpdev
     35  1.12     lukem  *	-U		remove all devices listed as `sw' in /etc/fstab.
     36  1.32    martin  *	-t [blk|noblk|auto]
     37  1.32    martin  *			if -A or -U , add (remove) either all block device
     38  1.32    martin  *			or all non-block devices, or all swap partitions
     39  1.32    martin  *	-q		check if any swap or dump devices are defined in
     40  1.32    martin  *			/etc/fstab
     41   1.1       mrg  *	-a <dev>	add this device
     42  1.17     lukem  *	-d <dev>	remove this swap device
     43  1.32    martin  *	-f		with -A -t auto, use the first swap as dump device
     44  1.25       mrg  *	-g		use gigabytes
     45  1.24       mrg  *	-h		use humanize_number(3) for listing
     46   1.1       mrg  *	-l		list swap devices
     47  1.25       mrg  *	-m		use megabytes
     48  1.32    martin  *	-n		print actions, but do not add/remove swap or
     49  1.32    martin  *			with -A/-U
     50  1.32    martin  *	-o		with -A -t auto only configure the first swap as dump,
     51  1.32    martin  *			(similar to -f), but do not add any further swap devs
     52   1.1       mrg  *	-s		short listing of swap devices
     53   1.1       mrg  *	-k		use kilobytes
     54   1.1       mrg  *	-p <pri>	use this priority
     55   1.1       mrg  *	-c		change priority
     56   1.2   thorpej  *
     57   1.2   thorpej  * or, if invoked as "swapon" (compatibility mode):
     58   1.2   thorpej  *
     59   1.2   thorpej  *	-a		all devices listed as `sw' in /etc/fstab
     60   1.4   thorpej  *	-t		same as -t above (feature not present in old
     61   1.4   thorpej  *			swapon(8) command)
     62   1.2   thorpej  *	<dev>		add this device
     63   1.1       mrg  */
     64  1.22       agc #include <sys/cdefs.h>
     65  1.22       agc 
     66  1.22       agc #ifndef lint
     67  1.35       apb __RCSID("$NetBSD: swapctl.c,v 1.35 2009/09/24 16:15:20 apb Exp $");
     68  1.22       agc #endif
     69  1.22       agc 
     70   1.1       mrg 
     71   1.1       mrg #include <sys/param.h>
     72   1.4   thorpej #include <sys/stat.h>
     73  1.14       mrg #include <sys/swap.h>
     74  1.32    martin #include <sys/sysctl.h>
     75  1.32    martin #include <sys/disk.h>
     76  1.32    martin #include <sys/disklabel.h>
     77   1.1       mrg 
     78   1.1       mrg #include <unistd.h>
     79   1.3     mikel #include <err.h>
     80   1.1       mrg #include <errno.h>
     81   1.1       mrg #include <stdio.h>
     82   1.1       mrg #include <stdlib.h>
     83   1.1       mrg #include <string.h>
     84   1.1       mrg #include <fstab.h>
     85  1.32    martin #include <fcntl.h>
     86  1.32    martin #include <util.h>
     87  1.32    martin #include <paths.h>
     88   1.1       mrg 
     89   1.1       mrg #include "swapctl.h"
     90   1.1       mrg 
     91   1.2   thorpej int	command;
     92   1.2   thorpej 
     93   1.2   thorpej /*
     94   1.2   thorpej  * Commands for swapctl(8).  These are mutually exclusive.
     95   1.2   thorpej  */
     96  1.12     lukem #define	CMD_A		0x01	/* process /etc/fstab for adding */
     97  1.10       mrg #define	CMD_D		0x02	/* set dumpdev */
     98  1.12     lukem #define	CMD_U		0x04	/* process /etc/fstab for removing */
     99  1.12     lukem #define	CMD_a		0x08	/* add a swap file/device */
    100  1.12     lukem #define	CMD_c		0x10	/* change priority of a swap file/device */
    101  1.12     lukem #define	CMD_d		0x20	/* delete a swap file/device */
    102  1.12     lukem #define	CMD_l		0x40	/* list swap files/devices */
    103  1.12     lukem #define	CMD_s		0x80	/* summary of swap files/devices */
    104  1.16       mrg #define	CMD_z		0x100	/* show dump device */
    105  1.32    martin #define	CMD_q		0x200	/* check for dump/swap in /etc/fstab */
    106   1.2   thorpej 
    107   1.2   thorpej #define	SET_COMMAND(cmd) \
    108   1.2   thorpej do { \
    109   1.2   thorpej 	if (command) \
    110   1.2   thorpej 		usage(); \
    111   1.2   thorpej 	command = (cmd); \
    112   1.2   thorpej } while (0)
    113   1.2   thorpej 
    114   1.2   thorpej /*
    115   1.2   thorpej  * Commands that require a "path" argument at the end of the command
    116   1.2   thorpej  * line, and the ones which require that none exist.
    117   1.2   thorpej  */
    118  1.10       mrg #define	REQUIRE_PATH	(CMD_D | CMD_a | CMD_c | CMD_d)
    119  1.32    martin #define	REQUIRE_NOPATH	(CMD_A | CMD_U | CMD_l | CMD_s | CMD_z | CMD_q)
    120   1.2   thorpej 
    121   1.2   thorpej /*
    122   1.2   thorpej  * Option flags, and the commands with which they are valid.
    123   1.2   thorpej  */
    124  1.25       mrg int	kflag;		/* display in 1K^x blocks */
    125   1.2   thorpej #define	KFLAG_CMDS	(CMD_l | CMD_s)
    126  1.25       mrg #define MFLAG_CMDS	(CMD_l | CMD_s)
    127  1.25       mrg #define GFLAG_CMDS	(CMD_l | CMD_s)
    128   1.2   thorpej 
    129  1.24       mrg int	hflag;		/* display with humanize_number */
    130  1.24       mrg #define HFLAG_CMDS	(CMD_l | CMD_s)
    131  1.24       mrg 
    132   1.2   thorpej int	pflag;		/* priority was specified */
    133   1.2   thorpej #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
    134   1.2   thorpej 
    135  1.32    martin char	*tflag;		/* swap device type (blk, noblk, auto) */
    136  1.32    martin int	autoflag;	/* 1, if tflag is "auto" */
    137  1.16       mrg #define	TFLAG_CMDS	(CMD_A | CMD_U)
    138   1.4   thorpej 
    139  1.32    martin int	fflag;		/* first swap becomes dump */
    140  1.32    martin #define	FFLAG_CMDS	(CMD_A)
    141  1.32    martin 
    142  1.32    martin int	oflag;		/* only autoset dump device */
    143  1.32    martin #define	OFLAG_CMDS	(CMD_A)
    144  1.32    martin 
    145  1.32    martin int	nflag;		/* no execute, just print actions */
    146  1.32    martin #define	NFLAG_CMDS	(CMD_A | CMD_U)
    147  1.32    martin 
    148   1.1       mrg int	pri;		/* uses 0 as default pri */
    149   1.1       mrg 
    150  1.29  christos static	void change_priority(char *);
    151  1.29  christos static	int  add_swap(char *, int);
    152  1.29  christos static	int  delete_swap(char *);
    153  1.29  christos static	void set_dumpdev(char *);
    154  1.30    martin static	int get_dumpdev(void);
    155  1.28   xtraeme static	void do_fstab(int);
    156  1.32    martin static	int check_fstab(void);
    157  1.32    martin static	void do_localdevs(int);
    158  1.32    martin static	void do_localdisk(const char *, int);
    159  1.32    martin static	int do_wedgesofdisk(int fd, int);
    160  1.32    martin static	int do_partitionsofdisk(const char *, int fd, int);
    161  1.28   xtraeme static	void usage(void);
    162  1.28   xtraeme static	void swapon_command(int, char **);
    163   1.2   thorpej #if 0
    164  1.28   xtraeme static	void swapoff_command(int, char **);
    165   1.2   thorpej #endif
    166   1.2   thorpej 
    167   1.1       mrg int
    168  1.28   xtraeme main(int argc, char *argv[])
    169   1.1       mrg {
    170   1.1       mrg 	int	c;
    171   1.2   thorpej 
    172  1.18       cgd 	if (strcmp(getprogname(), "swapon") == 0) {
    173   1.2   thorpej 		swapon_command(argc, argv);
    174   1.2   thorpej 		/* NOTREACHED */
    175   1.2   thorpej 	}
    176   1.2   thorpej 
    177   1.2   thorpej #if 0
    178  1.18       cgd 	if (strcmp(getprogname(), "swapoff") == 0) {
    179   1.2   thorpej 		swapoff_command(argc, argv);
    180   1.2   thorpej 		/* NOTREACHED */
    181   1.2   thorpej 	}
    182   1.2   thorpej #endif
    183   1.2   thorpej 
    184  1.32    martin 	while ((c = getopt(argc, argv, "ADUacdfghklmnop:qst:z")) != -1) {
    185   1.2   thorpej 		switch (c) {
    186   1.1       mrg 		case 'A':
    187   1.2   thorpej 			SET_COMMAND(CMD_A);
    188   1.1       mrg 			break;
    189   1.2   thorpej 
    190  1.10       mrg 		case 'D':
    191  1.10       mrg 			SET_COMMAND(CMD_D);
    192  1.10       mrg 			break;
    193  1.10       mrg 
    194  1.12     lukem 		case 'U':
    195  1.12     lukem 			SET_COMMAND(CMD_U);
    196  1.12     lukem 			break;
    197  1.12     lukem 
    198   1.1       mrg 		case 'a':
    199   1.2   thorpej 			SET_COMMAND(CMD_a);
    200   1.1       mrg 			break;
    201   1.2   thorpej 
    202   1.1       mrg 		case 'c':
    203   1.2   thorpej 			SET_COMMAND(CMD_c);
    204   1.1       mrg 			break;
    205   1.2   thorpej 
    206   1.1       mrg 		case 'd':
    207   1.2   thorpej 			SET_COMMAND(CMD_d);
    208   1.1       mrg 			break;
    209   1.2   thorpej 
    210  1.32    martin 		case 'f':
    211  1.32    martin 			fflag = 1;
    212  1.32    martin 			break;
    213  1.32    martin 
    214  1.25       mrg 		case 'g':
    215  1.25       mrg 			kflag = 3; /* 1k ^ 3 */
    216  1.25       mrg 			break;
    217  1.25       mrg 
    218  1.24       mrg 		case 'h':
    219  1.24       mrg 			hflag = 1;
    220  1.24       mrg 			break;
    221  1.24       mrg 
    222  1.25       mrg 		case 'k':
    223  1.25       mrg 			kflag = 1;
    224  1.25       mrg 			break;
    225  1.25       mrg 
    226   1.1       mrg 		case 'l':
    227   1.2   thorpej 			SET_COMMAND(CMD_l);
    228   1.1       mrg 			break;
    229   1.2   thorpej 
    230  1.25       mrg 		case 'm':
    231  1.25       mrg 			kflag = 2; /* 1k ^ 2 */
    232   1.1       mrg 			break;
    233   1.2   thorpej 
    234  1.32    martin 		case 'n':
    235  1.32    martin 			nflag = 1;
    236  1.32    martin 			break;
    237  1.32    martin 
    238  1.32    martin 		case 'o':
    239  1.32    martin 			oflag = 1;
    240  1.32    martin 			break;
    241  1.32    martin 
    242   1.1       mrg 		case 'p':
    243   1.1       mrg 			pflag = 1;
    244   1.2   thorpej 			/* XXX strtol() */
    245   1.1       mrg 			pri = atoi(optarg);
    246   1.1       mrg 			break;
    247   1.2   thorpej 
    248  1.32    martin 		case 'q':
    249  1.32    martin 			SET_COMMAND(CMD_q);
    250  1.32    martin 			break;
    251  1.32    martin 
    252   1.1       mrg 		case 's':
    253   1.2   thorpej 			SET_COMMAND(CMD_s);
    254   1.1       mrg 			break;
    255   1.2   thorpej 
    256   1.4   thorpej 		case 't':
    257   1.4   thorpej 			if (tflag != NULL)
    258   1.4   thorpej 				usage();
    259   1.4   thorpej 			tflag = optarg;
    260  1.32    martin 			if (strcmp(tflag, "auto") == 0)
    261  1.32    martin 				autoflag = 1;
    262   1.4   thorpej 			break;
    263   1.4   thorpej 
    264  1.16       mrg 		case 'z':
    265  1.16       mrg 			SET_COMMAND(CMD_z);
    266  1.16       mrg 			break;
    267  1.16       mrg 
    268   1.2   thorpej 		default:
    269   1.2   thorpej 			usage();
    270   1.2   thorpej 			/* NOTREACHED */
    271   1.1       mrg 		}
    272   1.1       mrg 	}
    273   1.2   thorpej 
    274   1.2   thorpej 	/* Did the user specify a command? */
    275   1.2   thorpej 	if (command == 0)
    276   1.1       mrg 		usage();
    277   1.1       mrg 
    278   1.1       mrg 	argv += optind;
    279   1.2   thorpej 	argc -= optind;
    280   1.2   thorpej 
    281   1.2   thorpej 	switch (argc) {
    282   1.2   thorpej 	case 0:
    283   1.2   thorpej 		if (command & REQUIRE_PATH)
    284   1.2   thorpej 			usage();
    285   1.2   thorpej 		break;
    286   1.2   thorpej 
    287   1.2   thorpej 	case 1:
    288   1.2   thorpej 		if (command & REQUIRE_NOPATH)
    289   1.2   thorpej 			usage();
    290   1.2   thorpej 		break;
    291   1.2   thorpej 
    292   1.2   thorpej 	default:
    293   1.1       mrg 		usage();
    294   1.2   thorpej 	}
    295   1.2   thorpej 
    296   1.2   thorpej 	/* To change priority, you have to specify one. */
    297   1.2   thorpej 	if ((command == CMD_c) && pflag == 0)
    298   1.1       mrg 		usage();
    299   1.1       mrg 
    300  1.32    martin 	/* -f and -o are mutualy exclusive */
    301  1.32    martin 	if (fflag && oflag)
    302  1.32    martin 		usage();
    303  1.32    martin 
    304   1.4   thorpej 	/* Sanity-check -t */
    305   1.4   thorpej 	if (tflag != NULL) {
    306  1.12     lukem 		if (command != CMD_A && command != CMD_U)
    307   1.4   thorpej 			usage();
    308   1.4   thorpej 		if (strcmp(tflag, "blk") != 0 &&
    309  1.32    martin 		    strcmp(tflag, "noblk") != 0 &&
    310  1.32    martin 		    strcmp(tflag, "auto") != 0)
    311   1.4   thorpej 			usage();
    312   1.4   thorpej 	}
    313   1.4   thorpej 
    314   1.2   thorpej 	/* Dispatch the command. */
    315   1.2   thorpej 	switch (command) {
    316   1.2   thorpej 	case CMD_l:
    317  1.30    martin 		if (!list_swap(pri, kflag, pflag, 0, 1, hflag))
    318  1.30    martin 			exit(1);
    319   1.2   thorpej 		break;
    320   1.2   thorpej 
    321   1.2   thorpej 	case CMD_s:
    322  1.24       mrg 		list_swap(pri, kflag, pflag, 0, 0, hflag);
    323   1.2   thorpej 		break;
    324   1.2   thorpej 
    325   1.2   thorpej 	case CMD_c:
    326   1.1       mrg 		change_priority(argv[0]);
    327   1.2   thorpej 		break;
    328   1.2   thorpej 
    329   1.2   thorpej 	case CMD_a:
    330  1.12     lukem 		if (! add_swap(argv[0], pri))
    331  1.12     lukem 			exit(1);
    332   1.2   thorpej 		break;
    333   1.2   thorpej 
    334   1.2   thorpej 	case CMD_d:
    335  1.12     lukem 		if (! delete_swap(argv[0]))
    336  1.12     lukem 			exit(1);
    337   1.2   thorpej 		break;
    338   1.2   thorpej 
    339   1.2   thorpej 	case CMD_A:
    340  1.32    martin 		if (autoflag)
    341  1.32    martin 			do_localdevs(1);
    342  1.32    martin 		else
    343  1.32    martin 			do_fstab(1);
    344   1.2   thorpej 		break;
    345  1.10       mrg 
    346  1.10       mrg 	case CMD_D:
    347  1.10       mrg 		set_dumpdev(argv[0]);
    348  1.10       mrg 		break;
    349  1.12     lukem 
    350  1.16       mrg 	case CMD_z:
    351  1.30    martin 		if (!get_dumpdev())
    352  1.30    martin 			exit(1);
    353  1.16       mrg 		break;
    354  1.16       mrg 
    355  1.12     lukem 	case CMD_U:
    356  1.32    martin 		if (autoflag)
    357  1.32    martin 			do_localdevs(0);
    358  1.32    martin 		else
    359  1.32    martin 			do_fstab(0);
    360  1.12     lukem 		break;
    361  1.32    martin 	case CMD_q:
    362  1.32    martin 		if (check_fstab()) {
    363  1.32    martin 			printf("%s: there are swap or dump devices defined in "
    364  1.32    martin 			    _PATH_FSTAB "\n", getprogname());
    365  1.32    martin 			exit(0);
    366  1.32    martin 		} else {
    367  1.32    martin 			printf("%s: no swap or dump devices in "
    368  1.32    martin 			    _PATH_FSTAB "\n", getprogname());
    369  1.32    martin 			exit(1);
    370  1.32    martin 		}
    371   1.2   thorpej 	}
    372   1.2   thorpej 
    373   1.2   thorpej 	exit(0);
    374   1.2   thorpej }
    375   1.2   thorpej 
    376   1.2   thorpej /*
    377   1.2   thorpej  * swapon_command: emulate the old swapon(8) program.
    378   1.2   thorpej  */
    379  1.12     lukem static void
    380  1.28   xtraeme swapon_command(int argc, char **argv)
    381   1.2   thorpej {
    382   1.2   thorpej 	int ch, fiztab = 0;
    383   1.2   thorpej 
    384   1.4   thorpej 	while ((ch = getopt(argc, argv, "at:")) != -1) {
    385   1.2   thorpej 		switch (ch) {
    386   1.2   thorpej 		case 'a':
    387   1.2   thorpej 			fiztab = 1;
    388   1.2   thorpej 			break;
    389   1.4   thorpej 		case 't':
    390   1.4   thorpej 			if (tflag != NULL)
    391   1.4   thorpej 				usage();
    392   1.4   thorpej 			tflag = optarg;
    393   1.4   thorpej 			break;
    394   1.2   thorpej 		default:
    395   1.2   thorpej 			goto swapon_usage;
    396   1.2   thorpej 		}
    397   1.2   thorpej 	}
    398   1.2   thorpej 	argc -= optind;
    399   1.2   thorpej 	argv += optind;
    400   1.2   thorpej 
    401   1.2   thorpej 	if (fiztab) {
    402   1.2   thorpej 		if (argc)
    403   1.2   thorpej 			goto swapon_usage;
    404   1.4   thorpej 		/* Sanity-check -t */
    405   1.4   thorpej 		if (tflag != NULL) {
    406   1.4   thorpej 			if (strcmp(tflag, "blk") != 0 &&
    407   1.4   thorpej 			    strcmp(tflag, "noblk") != 0)
    408   1.4   thorpej 				usage();
    409   1.4   thorpej 		}
    410  1.12     lukem 		do_fstab(1);
    411   1.2   thorpej 		exit(0);
    412   1.4   thorpej 	} else if (argc == 0 || tflag != NULL)
    413   1.2   thorpej 		goto swapon_usage;
    414   1.2   thorpej 
    415   1.2   thorpej 	while (argc) {
    416  1.12     lukem 		if (! add_swap(argv[0], pri))
    417  1.12     lukem 			exit(1);
    418   1.2   thorpej 		argc--;
    419   1.2   thorpej 		argv++;
    420   1.2   thorpej 	}
    421   1.1       mrg 	exit(0);
    422   1.2   thorpej 	/* NOTREACHED */
    423   1.2   thorpej 
    424   1.2   thorpej  swapon_usage:
    425  1.18       cgd 	fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
    426  1.18       cgd 	fprintf(stderr, "       %s <path> ...\n", getprogname());
    427   1.2   thorpej 	exit(1);
    428   1.1       mrg }
    429   1.1       mrg 
    430   1.1       mrg /*
    431   1.1       mrg  * change_priority:  change the priority of a swap device.
    432   1.1       mrg  */
    433  1.12     lukem static void
    434  1.29  christos change_priority(char *path)
    435   1.1       mrg {
    436   1.1       mrg 
    437   1.1       mrg 	if (swapctl(SWAP_CTL, path, pri) < 0)
    438  1.30    martin 		err(1, "%s", path);
    439   1.1       mrg }
    440   1.1       mrg 
    441   1.1       mrg /*
    442   1.1       mrg  * add_swap:  add the pathname to the list of swap devices.
    443   1.1       mrg  */
    444  1.12     lukem static int
    445  1.29  christos add_swap(char *path, int priority)
    446   1.1       mrg {
    447  1.10       mrg 	struct stat sb;
    448  1.10       mrg 
    449  1.10       mrg 	if (stat(path, &sb) < 0)
    450  1.10       mrg 		goto oops;
    451  1.10       mrg 
    452  1.10       mrg 	if (sb.st_mode & S_IROTH)
    453  1.19     pooka 		warnx("WARNING: %s is readable by the world", path);
    454  1.10       mrg 	if (sb.st_mode & S_IWOTH)
    455  1.19     pooka 		warnx("WARNING: %s is writable by the world", path);
    456   1.1       mrg 
    457  1.32    martin 	if (fflag || oflag) {
    458  1.32    martin 		set_dumpdev(path);
    459  1.32    martin 		if (oflag)
    460  1.32    martin 			exit(0);
    461  1.32    martin 		else
    462  1.32    martin 			fflag = 0;
    463  1.32    martin 	}
    464  1.32    martin 
    465  1.32    martin 	if (nflag)
    466  1.32    martin 		return 1;
    467  1.32    martin 
    468  1.12     lukem 	if (swapctl(SWAP_ON, path, priority) < 0) {
    469  1.10       mrg oops:
    470  1.30    martin 		err(1, "%s", path);
    471  1.12     lukem 	}
    472  1.12     lukem 	return (1);
    473   1.1       mrg }
    474   1.1       mrg 
    475   1.1       mrg /*
    476  1.11       mrg  * delete_swap:  remove the pathname to the list of swap devices.
    477   1.1       mrg  */
    478  1.12     lukem static int
    479  1.29  christos delete_swap(char *path)
    480   1.1       mrg {
    481   1.1       mrg 
    482  1.32    martin 	if (nflag)
    483  1.32    martin 		return 1;
    484  1.32    martin 
    485  1.30    martin 	if (swapctl(SWAP_OFF, path, pri) < 0)
    486  1.30    martin 		err(1, "%s", path);
    487  1.12     lukem 	return (1);
    488   1.1       mrg }
    489   1.1       mrg 
    490  1.12     lukem static void
    491  1.29  christos set_dumpdev(char *path)
    492  1.10       mrg {
    493  1.32    martin 	int rv = 0;
    494  1.10       mrg 
    495  1.32    martin 	if (!nflag) {
    496  1.32    martin 		if (strcmp(path, "none") == 0)
    497  1.32    martin 			rv = swapctl(SWAP_DUMPOFF, NULL, 0);
    498  1.32    martin 		else
    499  1.32    martin 			rv = swapctl(SWAP_DUMPDEV, path, 0);
    500  1.32    martin 	}
    501  1.30    martin 
    502  1.30    martin 	if (rv == -1)
    503  1.30    martin 		err(1, "could not set dump device to %s", path);
    504  1.10       mrg 	else
    505  1.18       cgd 		printf("%s: setting dump device to %s\n", getprogname(), path);
    506  1.16       mrg }
    507  1.16       mrg 
    508  1.30    martin static int
    509  1.28   xtraeme get_dumpdev(void)
    510  1.16       mrg {
    511  1.16       mrg 	dev_t	dev;
    512  1.16       mrg 	char 	*name;
    513  1.16       mrg 
    514  1.30    martin 	if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) {
    515  1.16       mrg 		warn("could not get dump device");
    516  1.30    martin 		return 0;
    517  1.30    martin 	} else if (dev == NODEV) {
    518  1.21  drochner 		printf("no dump device set\n");
    519  1.30    martin 		return 0;
    520  1.30    martin 	} else {
    521  1.16       mrg 		name = devname(dev, S_IFBLK);
    522  1.16       mrg 		printf("dump device is ");
    523  1.16       mrg 		if (name)
    524  1.16       mrg 			printf("%s\n", name);
    525  1.16       mrg 		else
    526  1.34  christos 			printf("major %llu minor %llu\n",
    527  1.34  christos 			    (unsigned long long)major(dev),
    528  1.34  christos 			    (unsigned long long)minor(dev));
    529  1.16       mrg 	}
    530  1.30    martin 	return 1;
    531  1.10       mrg }
    532  1.10       mrg 
    533  1.12     lukem static void
    534  1.32    martin do_localdevs(int add)
    535  1.32    martin {
    536  1.32    martin 	size_t ressize;
    537  1.32    martin 	char *disknames, *disk;
    538  1.32    martin 	static const char mibname[] = "hw.disknames";
    539  1.32    martin 
    540  1.32    martin 	ressize = 0;
    541  1.32    martin 	if (sysctlbyname(mibname, NULL, &ressize, NULL, 0))
    542  1.32    martin 		return;
    543  1.32    martin 	ressize += 200;	/* add some arbitrary slope */
    544  1.32    martin 	disknames = malloc(ressize);
    545  1.32    martin 	if (sysctlbyname(mibname, disknames, &ressize, NULL, 0) == 0) {
    546  1.32    martin 		for (disk = strtok(disknames, " "); disk;
    547  1.32    martin 		    disk = strtok(NULL, " "))
    548  1.32    martin 			do_localdisk(disk, add);
    549  1.32    martin 	}
    550  1.32    martin 	free(disknames);
    551  1.32    martin }
    552  1.32    martin 
    553  1.32    martin static void
    554  1.32    martin do_localdisk(const char *disk, int add)
    555  1.32    martin {
    556  1.32    martin 	int fd;
    557  1.32    martin 	char dvname[MAXPATHLEN];
    558  1.32    martin 
    559  1.32    martin 	if ((fd = opendisk(disk, O_RDONLY, dvname, sizeof(dvname), 0)) == -1)
    560  1.32    martin 		return;
    561  1.32    martin 
    562  1.32    martin 	if (!do_wedgesofdisk(fd, add))
    563  1.32    martin 		do_partitionsofdisk(disk, fd, add);
    564  1.32    martin 
    565  1.32    martin 	close(fd);
    566  1.32    martin }
    567  1.32    martin 
    568  1.32    martin static int
    569  1.32    martin do_wedgesofdisk(int fd, int add)
    570  1.32    martin {
    571  1.32    martin 	char devicename[MAXPATHLEN];
    572  1.32    martin 	struct dkwedge_info *dkw;
    573  1.32    martin 	struct dkwedge_list dkwl;
    574  1.32    martin 	size_t bufsize;
    575  1.32    martin 	u_int i;
    576  1.32    martin 
    577  1.32    martin 	dkw = NULL;
    578  1.32    martin 	dkwl.dkwl_buf = dkw;
    579  1.32    martin 	dkwl.dkwl_bufsize = 0;
    580  1.32    martin 
    581  1.32    martin 	for (;;) {
    582  1.32    martin 		if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
    583  1.32    martin 			return 0;
    584  1.32    martin 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
    585  1.32    martin 			break;
    586  1.32    martin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
    587  1.32    martin 		if (dkwl.dkwl_bufsize < bufsize) {
    588  1.32    martin 			dkw = realloc(dkwl.dkwl_buf, bufsize);
    589  1.32    martin 			if (dkw == NULL)
    590  1.32    martin 				return 0;
    591  1.32    martin 			dkwl.dkwl_buf = dkw;
    592  1.32    martin 			dkwl.dkwl_bufsize = bufsize;
    593  1.32    martin 		}
    594  1.32    martin 	}
    595  1.32    martin 
    596  1.32    martin 	for (i = 0; i < dkwl.dkwl_ncopied; i++) {
    597  1.32    martin 		if (strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) != 0)
    598  1.32    martin 			continue;
    599  1.32    martin 		snprintf(devicename, sizeof(devicename), "%s%s", _PATH_DEV,
    600  1.32    martin 		    dkw[i].dkw_devname);
    601  1.32    martin 		devicename[sizeof(devicename)-1] = '\0';
    602  1.32    martin 
    603  1.32    martin 		if (add) {
    604  1.32    martin 			if (add_swap(devicename, pri)) {
    605  1.32    martin 				printf(
    606  1.32    martin 			    	"%s: adding %s as swap device at priority 0\n",
    607  1.32    martin 				    getprogname(), devicename);
    608  1.32    martin 			}
    609  1.32    martin 		} else {
    610  1.32    martin 			if (delete_swap(devicename)) {
    611  1.32    martin 				printf(
    612  1.32    martin 				    "%s: removing %s as swap device\n",
    613  1.32    martin 				    getprogname(), devicename);
    614  1.32    martin 			}
    615  1.32    martin 		}
    616  1.32    martin 
    617  1.32    martin 	}
    618  1.32    martin 
    619  1.32    martin 	free(dkw);
    620  1.32    martin 	return dkwl.dkwl_nwedges != 0;
    621  1.32    martin }
    622  1.32    martin 
    623  1.32    martin static int
    624  1.32    martin do_partitionsofdisk(const char *prefix, int fd, int add)
    625  1.32    martin {
    626  1.32    martin 	char devicename[MAXPATHLEN];
    627  1.32    martin 	struct disklabel lab;
    628  1.32    martin 	uint i;
    629  1.32    martin 
    630  1.32    martin 	if (ioctl(fd, DIOCGDINFO, &lab) != 0)
    631  1.32    martin 		return 0;
    632  1.32    martin 
    633  1.32    martin 	for (i = 0; i < lab.d_npartitions; i++) {
    634  1.32    martin 		if (lab.d_partitions[i].p_fstype != FS_SWAP)
    635  1.32    martin 			continue;
    636  1.32    martin 		snprintf(devicename, sizeof(devicename), "%s%s%c", _PATH_DEV,
    637  1.32    martin 		    prefix, 'a'+i);
    638  1.32    martin 		devicename[sizeof(devicename)-1] = '\0';
    639  1.32    martin 
    640  1.32    martin 		if (add) {
    641  1.32    martin 			if (add_swap(devicename, pri)) {
    642  1.32    martin 				printf(
    643  1.32    martin 			    	"%s: adding %s as swap device at priority 0\n",
    644  1.32    martin 				    getprogname(), devicename);
    645  1.32    martin 			}
    646  1.32    martin 		} else {
    647  1.32    martin 			if (delete_swap(devicename)) {
    648  1.32    martin 				printf(
    649  1.32    martin 				    "%s: removing %s as swap device\n",
    650  1.32    martin 				    getprogname(), devicename);
    651  1.32    martin 			}
    652  1.32    martin 		}
    653  1.32    martin 	}
    654  1.32    martin 
    655  1.32    martin 	return 1;
    656  1.32    martin }
    657  1.32    martin 
    658  1.32    martin static int
    659  1.32    martin check_fstab(void)
    660  1.32    martin {
    661  1.32    martin 	struct	fstab *fp;
    662  1.32    martin 
    663  1.32    martin 	while ((fp = getfsent()) != NULL) {
    664  1.32    martin 		if (strcmp(fp->fs_type, "dp") == 0)
    665  1.32    martin 			return 1;
    666  1.32    martin 
    667  1.32    martin 		if (strcmp(fp->fs_type, "sw") == 0)
    668  1.32    martin 			return 1;
    669  1.32    martin 	}
    670  1.32    martin 
    671  1.32    martin 	return 0;
    672  1.32    martin }
    673  1.32    martin 
    674  1.32    martin static void
    675  1.28   xtraeme do_fstab(int add)
    676   1.1       mrg {
    677   1.1       mrg 	struct	fstab *fp;
    678   1.1       mrg 	char	*s;
    679   1.1       mrg 	long	priority;
    680   1.4   thorpej 	struct	stat st;
    681   1.4   thorpej 	int	isblk;
    682  1.35       apb 	int	success = 0;	/* set to 1 after a successful operation */
    683  1.35       apb 	int	error = 0;	/* set to 1 after an error */
    684  1.27     lukem 
    685  1.27     lukem #ifdef RESCUEDIR
    686  1.27     lukem #define PATH_MOUNT	RESCUEDIR "/mount_nfs"
    687  1.27     lukem #define PATH_UMOUNT	RESCUEDIR "/umount"
    688  1.27     lukem #else
    689  1.12     lukem #define PATH_MOUNT	"/sbin/mount_nfs"
    690  1.12     lukem #define PATH_UMOUNT	"/sbin/umount"
    691  1.27     lukem #endif
    692  1.27     lukem 
    693  1.12     lukem 	char	cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    694   1.1       mrg 
    695   1.1       mrg #define PRIORITYEQ	"priority="
    696   1.1       mrg #define NFSMNTPT	"nfsmntpt="
    697   1.5     mikel 	while ((fp = getfsent()) != NULL) {
    698  1.28   xtraeme 		char *spec;
    699   1.1       mrg 
    700  1.10       mrg 		spec = fp->fs_spec;
    701  1.12     lukem 		cmd[0] = '\0';
    702  1.10       mrg 
    703  1.12     lukem 		if (strcmp(fp->fs_type, "dp") == 0 && add) {
    704  1.10       mrg 			set_dumpdev(spec);
    705  1.10       mrg 			continue;
    706  1.10       mrg 		}
    707  1.10       mrg 
    708   1.1       mrg 		if (strcmp(fp->fs_type, "sw") != 0)
    709   1.1       mrg 			continue;
    710  1.20  jdolecek 
    711  1.20  jdolecek 		/* handle dp as mnt option */
    712  1.20  jdolecek 		if (strstr(fp->fs_mntops, "dp") && add)
    713  1.20  jdolecek 			set_dumpdev(spec);
    714  1.20  jdolecek 
    715   1.4   thorpej 		isblk = 0;
    716   1.1       mrg 
    717   1.5     mikel 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
    718   1.1       mrg 			s += sizeof(PRIORITYEQ) - 1;
    719   1.1       mrg 			priority = atol(s);
    720   1.1       mrg 		} else
    721   1.1       mrg 			priority = pri;
    722   1.1       mrg 
    723   1.5     mikel 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
    724  1.12     lukem 			char *t;
    725   1.1       mrg 
    726   1.4   thorpej 			/*
    727   1.4   thorpej 			 * Skip this song and dance if we're only
    728   1.4   thorpej 			 * doing block devices.
    729   1.4   thorpej 			 */
    730  1.12     lukem 			if (tflag != NULL && strcmp(tflag, "blk") == 0)
    731   1.4   thorpej 				continue;
    732   1.4   thorpej 
    733   1.1       mrg 			t = strpbrk(s, ",");
    734   1.1       mrg 			if (t != 0)
    735   1.1       mrg 				*t = '\0';
    736   1.1       mrg 			spec = strdup(s + strlen(NFSMNTPT));
    737   1.1       mrg 			if (t != 0)
    738   1.1       mrg 				*t = ',';
    739   1.1       mrg 
    740   1.1       mrg 			if (spec == NULL)
    741   1.1       mrg 				errx(1, "Out of memory");
    742   1.1       mrg 
    743   1.1       mrg 			if (strlen(spec) == 0) {
    744   1.1       mrg 				warnx("empty mountpoint");
    745  1.28   xtraeme 				free(spec);
    746   1.1       mrg 				continue;
    747   1.1       mrg 			}
    748  1.12     lukem 			if (add) {
    749  1.12     lukem 				snprintf(cmd, sizeof(cmd), "%s %s %s",
    750  1.12     lukem 					PATH_MOUNT, fp->fs_spec, spec);
    751  1.12     lukem 				if (system(cmd) != 0) {
    752  1.12     lukem 					warnx("%s: mount failed", fp->fs_spec);
    753  1.12     lukem 					continue;
    754  1.12     lukem 				}
    755  1.12     lukem 			} else {
    756  1.12     lukem 				snprintf(cmd, sizeof(cmd), "%s %s",
    757  1.12     lukem 					PATH_UMOUNT, fp->fs_spec);
    758   1.1       mrg 			}
    759   1.4   thorpej 		} else {
    760   1.4   thorpej 			/*
    761   1.4   thorpej 			 * Determine blk-ness.
    762   1.4   thorpej 			 */
    763   1.4   thorpej 			if (stat(spec, &st) < 0) {
    764  1.15    itojun 				warn("%s", spec);
    765   1.4   thorpej 				continue;
    766   1.4   thorpej 			}
    767   1.4   thorpej 			if (S_ISBLK(st.st_mode))
    768   1.4   thorpej 				isblk = 1;
    769   1.4   thorpej 		}
    770   1.4   thorpej 
    771   1.4   thorpej 		/*
    772   1.4   thorpej 		 * Skip this type if we're told to.
    773   1.4   thorpej 		 */
    774   1.4   thorpej 		if (tflag != NULL) {
    775   1.4   thorpej 			if (strcmp(tflag, "blk") == 0 && isblk == 0)
    776   1.4   thorpej 				continue;
    777   1.4   thorpej 			if (strcmp(tflag, "noblk") == 0 && isblk == 1)
    778   1.4   thorpej 				continue;
    779   1.1       mrg 		}
    780   1.1       mrg 
    781  1.12     lukem 		if (add) {
    782  1.12     lukem 			if (add_swap(spec, (int)priority)) {
    783  1.35       apb 				success = 1;
    784  1.12     lukem 				printf(
    785  1.12     lukem 			    	"%s: adding %s as swap device at priority %d\n",
    786  1.18       cgd 				    getprogname(), fp->fs_spec, (int)priority);
    787  1.35       apb 			} else {
    788  1.35       apb 				error = 1;
    789  1.35       apb 				fprintf(stderr,
    790  1.35       apb 				    "%s: failed to add %s as swap device\n",
    791  1.35       apb 				    getprogname(), fp->fs_spec);
    792  1.12     lukem 			}
    793  1.12     lukem 		} else {
    794  1.12     lukem 			if (delete_swap(spec)) {
    795  1.35       apb 				success = 1;
    796  1.12     lukem 				printf(
    797  1.12     lukem 				    "%s: removing %s as swap device\n",
    798  1.18       cgd 				    getprogname(), fp->fs_spec);
    799  1.35       apb 			} else {
    800  1.35       apb 				error = 1;
    801  1.35       apb 				fprintf(stderr,
    802  1.35       apb 				    "%s: failed to remove %s as swap device\n",
    803  1.35       apb 				    getprogname(), fp->fs_spec);
    804  1.12     lukem 			}
    805  1.12     lukem 			if (cmd[0]) {
    806  1.12     lukem 				if (system(cmd) != 0) {
    807  1.12     lukem 					warnx("%s: umount failed", fp->fs_spec);
    808  1.35       apb 					error = 1;
    809  1.12     lukem 					continue;
    810  1.12     lukem 				}
    811  1.12     lukem 			}
    812   1.8       mrg 		}
    813   1.1       mrg 
    814   1.1       mrg 		if (spec != fp->fs_spec)
    815  1.28   xtraeme 			free(spec);
    816   1.1       mrg 	}
    817  1.35       apb 	if (error)
    818   1.8       mrg 		exit(1);
    819  1.35       apb 	else if (success)
    820  1.35       apb 		exit(0);
    821  1.35       apb 	else
    822  1.35       apb 		exit(2); /* not really an error, but no swap devices found */
    823   1.1       mrg }
    824   1.1       mrg 
    825  1.12     lukem static void
    826  1.28   xtraeme usage(void)
    827   1.1       mrg {
    828  1.18       cgd 	const char *progname = getprogname();
    829   1.1       mrg 
    830  1.32    martin 	fprintf(stderr, "usage: %s -A [-f|-o] [-n] [-p priority] "
    831  1.32    martin 	    "[-t blk|noblk|auto]\n", progname);
    832  1.18       cgd 	fprintf(stderr, "       %s -a [-p priority] path\n", progname);
    833  1.32    martin 	fprintf(stderr, "       %s -q\n", progname);
    834  1.18       cgd 	fprintf(stderr, "       %s -c -p priority path\n", progname);
    835  1.31       wiz 	fprintf(stderr, "       %s -D dumpdev|none\n", progname);
    836  1.18       cgd 	fprintf(stderr, "       %s -d path\n", progname);
    837  1.25       mrg 	fprintf(stderr, "       %s -l | -s [-k|-m|-g|-h]\n", progname);
    838  1.32    martin 	fprintf(stderr, "       %s -U [-n] [-t blk|noblk|auto]\n", progname);
    839  1.26      cjep 	fprintf(stderr, "       %s -z\n", progname);
    840   1.1       mrg 	exit(1);
    841   1.1       mrg }
    842