Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.38
      1  1.38   mlelstv /*	$NetBSD: swapctl.c,v 1.38 2012/12/26 10:15:28 mlelstv 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.38   mlelstv __RCSID("$NetBSD: swapctl.c,v 1.38 2012/12/26 10:15:28 mlelstv 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.36     joerg static 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.36     joerg static 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.36     joerg static int	hflag;		/* display with humanize_number */
    130  1.24       mrg #define HFLAG_CMDS	(CMD_l | CMD_s)
    131  1.24       mrg 
    132  1.36     joerg static int	pflag;		/* priority was specified */
    133   1.2   thorpej #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
    134   1.2   thorpej 
    135  1.36     joerg static char	*tflag;		/* swap device type (blk, noblk, auto) */
    136  1.36     joerg static int	autoflag;	/* 1, if tflag is "auto" */
    137  1.16       mrg #define	TFLAG_CMDS	(CMD_A | CMD_U)
    138   1.4   thorpej 
    139  1.36     joerg static int	fflag;		/* first swap becomes dump */
    140  1.32    martin #define	FFLAG_CMDS	(CMD_A)
    141  1.32    martin 
    142  1.36     joerg static int	oflag;		/* only autoset dump device */
    143  1.32    martin #define	OFLAG_CMDS	(CMD_A)
    144  1.32    martin 
    145  1.36     joerg static int	nflag;		/* no execute, just print actions */
    146  1.32    martin #define	NFLAG_CMDS	(CMD_A | CMD_U)
    147  1.32    martin 
    148  1.36     joerg static 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.38   mlelstv static	void set_dumpdev1(char *);
    154  1.29  christos static	void set_dumpdev(char *);
    155  1.30    martin static	int get_dumpdev(void);
    156  1.36     joerg __dead static	void do_fstab(int);
    157  1.32    martin static	int check_fstab(void);
    158  1.32    martin static	void do_localdevs(int);
    159  1.32    martin static	void do_localdisk(const char *, int);
    160  1.32    martin static	int do_wedgesofdisk(int fd, int);
    161  1.32    martin static	int do_partitionsofdisk(const char *, int fd, int);
    162  1.36     joerg __dead static	void usage(void);
    163  1.36     joerg __dead static	void swapon_command(int, char **);
    164   1.2   thorpej #if 0
    165  1.28   xtraeme static	void swapoff_command(int, char **);
    166   1.2   thorpej #endif
    167   1.2   thorpej 
    168   1.1       mrg int
    169  1.28   xtraeme main(int argc, char *argv[])
    170   1.1       mrg {
    171   1.1       mrg 	int	c;
    172   1.2   thorpej 
    173  1.18       cgd 	if (strcmp(getprogname(), "swapon") == 0) {
    174   1.2   thorpej 		swapon_command(argc, argv);
    175   1.2   thorpej 		/* NOTREACHED */
    176   1.2   thorpej 	}
    177   1.2   thorpej 
    178   1.2   thorpej #if 0
    179  1.18       cgd 	if (strcmp(getprogname(), "swapoff") == 0) {
    180   1.2   thorpej 		swapoff_command(argc, argv);
    181   1.2   thorpej 		/* NOTREACHED */
    182   1.2   thorpej 	}
    183   1.2   thorpej #endif
    184   1.2   thorpej 
    185  1.32    martin 	while ((c = getopt(argc, argv, "ADUacdfghklmnop:qst:z")) != -1) {
    186   1.2   thorpej 		switch (c) {
    187   1.1       mrg 		case 'A':
    188   1.2   thorpej 			SET_COMMAND(CMD_A);
    189   1.1       mrg 			break;
    190   1.2   thorpej 
    191  1.10       mrg 		case 'D':
    192  1.10       mrg 			SET_COMMAND(CMD_D);
    193  1.10       mrg 			break;
    194  1.10       mrg 
    195  1.12     lukem 		case 'U':
    196  1.12     lukem 			SET_COMMAND(CMD_U);
    197  1.12     lukem 			break;
    198  1.12     lukem 
    199   1.1       mrg 		case 'a':
    200   1.2   thorpej 			SET_COMMAND(CMD_a);
    201   1.1       mrg 			break;
    202   1.2   thorpej 
    203   1.1       mrg 		case 'c':
    204   1.2   thorpej 			SET_COMMAND(CMD_c);
    205   1.1       mrg 			break;
    206   1.2   thorpej 
    207   1.1       mrg 		case 'd':
    208   1.2   thorpej 			SET_COMMAND(CMD_d);
    209   1.1       mrg 			break;
    210   1.2   thorpej 
    211  1.32    martin 		case 'f':
    212  1.32    martin 			fflag = 1;
    213  1.32    martin 			break;
    214  1.32    martin 
    215  1.25       mrg 		case 'g':
    216  1.25       mrg 			kflag = 3; /* 1k ^ 3 */
    217  1.25       mrg 			break;
    218  1.25       mrg 
    219  1.24       mrg 		case 'h':
    220  1.24       mrg 			hflag = 1;
    221  1.24       mrg 			break;
    222  1.24       mrg 
    223  1.25       mrg 		case 'k':
    224  1.25       mrg 			kflag = 1;
    225  1.25       mrg 			break;
    226  1.25       mrg 
    227   1.1       mrg 		case 'l':
    228   1.2   thorpej 			SET_COMMAND(CMD_l);
    229   1.1       mrg 			break;
    230   1.2   thorpej 
    231  1.25       mrg 		case 'm':
    232  1.25       mrg 			kflag = 2; /* 1k ^ 2 */
    233   1.1       mrg 			break;
    234   1.2   thorpej 
    235  1.32    martin 		case 'n':
    236  1.32    martin 			nflag = 1;
    237  1.32    martin 			break;
    238  1.32    martin 
    239  1.32    martin 		case 'o':
    240  1.32    martin 			oflag = 1;
    241  1.32    martin 			break;
    242  1.32    martin 
    243   1.1       mrg 		case 'p':
    244   1.1       mrg 			pflag = 1;
    245   1.2   thorpej 			/* XXX strtol() */
    246   1.1       mrg 			pri = atoi(optarg);
    247   1.1       mrg 			break;
    248   1.2   thorpej 
    249  1.32    martin 		case 'q':
    250  1.32    martin 			SET_COMMAND(CMD_q);
    251  1.32    martin 			break;
    252  1.32    martin 
    253   1.1       mrg 		case 's':
    254   1.2   thorpej 			SET_COMMAND(CMD_s);
    255   1.1       mrg 			break;
    256   1.2   thorpej 
    257   1.4   thorpej 		case 't':
    258   1.4   thorpej 			if (tflag != NULL)
    259   1.4   thorpej 				usage();
    260   1.4   thorpej 			tflag = optarg;
    261  1.32    martin 			if (strcmp(tflag, "auto") == 0)
    262  1.32    martin 				autoflag = 1;
    263   1.4   thorpej 			break;
    264   1.4   thorpej 
    265  1.16       mrg 		case 'z':
    266  1.16       mrg 			SET_COMMAND(CMD_z);
    267  1.16       mrg 			break;
    268  1.16       mrg 
    269   1.2   thorpej 		default:
    270   1.2   thorpej 			usage();
    271   1.2   thorpej 			/* NOTREACHED */
    272   1.1       mrg 		}
    273   1.1       mrg 	}
    274   1.2   thorpej 
    275   1.2   thorpej 	/* Did the user specify a command? */
    276   1.2   thorpej 	if (command == 0)
    277   1.1       mrg 		usage();
    278   1.1       mrg 
    279   1.1       mrg 	argv += optind;
    280   1.2   thorpej 	argc -= optind;
    281   1.2   thorpej 
    282   1.2   thorpej 	switch (argc) {
    283   1.2   thorpej 	case 0:
    284   1.2   thorpej 		if (command & REQUIRE_PATH)
    285   1.2   thorpej 			usage();
    286   1.2   thorpej 		break;
    287   1.2   thorpej 
    288   1.2   thorpej 	case 1:
    289   1.2   thorpej 		if (command & REQUIRE_NOPATH)
    290   1.2   thorpej 			usage();
    291   1.2   thorpej 		break;
    292   1.2   thorpej 
    293   1.2   thorpej 	default:
    294   1.1       mrg 		usage();
    295   1.2   thorpej 	}
    296   1.2   thorpej 
    297   1.2   thorpej 	/* To change priority, you have to specify one. */
    298   1.2   thorpej 	if ((command == CMD_c) && pflag == 0)
    299   1.1       mrg 		usage();
    300   1.1       mrg 
    301  1.32    martin 	/* -f and -o are mutualy exclusive */
    302  1.32    martin 	if (fflag && oflag)
    303  1.32    martin 		usage();
    304  1.32    martin 
    305   1.4   thorpej 	/* Sanity-check -t */
    306   1.4   thorpej 	if (tflag != NULL) {
    307  1.12     lukem 		if (command != CMD_A && command != CMD_U)
    308   1.4   thorpej 			usage();
    309   1.4   thorpej 		if (strcmp(tflag, "blk") != 0 &&
    310  1.32    martin 		    strcmp(tflag, "noblk") != 0 &&
    311  1.32    martin 		    strcmp(tflag, "auto") != 0)
    312   1.4   thorpej 			usage();
    313   1.4   thorpej 	}
    314   1.4   thorpej 
    315   1.2   thorpej 	/* Dispatch the command. */
    316   1.2   thorpej 	switch (command) {
    317   1.2   thorpej 	case CMD_l:
    318  1.30    martin 		if (!list_swap(pri, kflag, pflag, 0, 1, hflag))
    319  1.30    martin 			exit(1);
    320   1.2   thorpej 		break;
    321   1.2   thorpej 
    322   1.2   thorpej 	case CMD_s:
    323  1.24       mrg 		list_swap(pri, kflag, pflag, 0, 0, hflag);
    324   1.2   thorpej 		break;
    325   1.2   thorpej 
    326   1.2   thorpej 	case CMD_c:
    327   1.1       mrg 		change_priority(argv[0]);
    328   1.2   thorpej 		break;
    329   1.2   thorpej 
    330   1.2   thorpej 	case CMD_a:
    331  1.12     lukem 		if (! add_swap(argv[0], pri))
    332  1.12     lukem 			exit(1);
    333   1.2   thorpej 		break;
    334   1.2   thorpej 
    335   1.2   thorpej 	case CMD_d:
    336  1.12     lukem 		if (! delete_swap(argv[0]))
    337  1.12     lukem 			exit(1);
    338   1.2   thorpej 		break;
    339   1.2   thorpej 
    340   1.2   thorpej 	case CMD_A:
    341  1.32    martin 		if (autoflag)
    342  1.32    martin 			do_localdevs(1);
    343  1.32    martin 		else
    344  1.32    martin 			do_fstab(1);
    345   1.2   thorpej 		break;
    346  1.10       mrg 
    347  1.10       mrg 	case CMD_D:
    348  1.10       mrg 		set_dumpdev(argv[0]);
    349  1.10       mrg 		break;
    350  1.12     lukem 
    351  1.16       mrg 	case CMD_z:
    352  1.30    martin 		if (!get_dumpdev())
    353  1.30    martin 			exit(1);
    354  1.16       mrg 		break;
    355  1.16       mrg 
    356  1.12     lukem 	case CMD_U:
    357  1.32    martin 		if (autoflag)
    358  1.32    martin 			do_localdevs(0);
    359  1.32    martin 		else
    360  1.32    martin 			do_fstab(0);
    361  1.12     lukem 		break;
    362  1.32    martin 	case CMD_q:
    363  1.32    martin 		if (check_fstab()) {
    364  1.32    martin 			printf("%s: there are swap or dump devices defined in "
    365  1.32    martin 			    _PATH_FSTAB "\n", getprogname());
    366  1.32    martin 			exit(0);
    367  1.32    martin 		} else {
    368  1.32    martin 			printf("%s: no swap or dump devices in "
    369  1.32    martin 			    _PATH_FSTAB "\n", getprogname());
    370  1.32    martin 			exit(1);
    371  1.32    martin 		}
    372   1.2   thorpej 	}
    373   1.2   thorpej 
    374   1.2   thorpej 	exit(0);
    375   1.2   thorpej }
    376   1.2   thorpej 
    377   1.2   thorpej /*
    378   1.2   thorpej  * swapon_command: emulate the old swapon(8) program.
    379   1.2   thorpej  */
    380  1.12     lukem static void
    381  1.28   xtraeme swapon_command(int argc, char **argv)
    382   1.2   thorpej {
    383   1.2   thorpej 	int ch, fiztab = 0;
    384   1.2   thorpej 
    385   1.4   thorpej 	while ((ch = getopt(argc, argv, "at:")) != -1) {
    386   1.2   thorpej 		switch (ch) {
    387   1.2   thorpej 		case 'a':
    388   1.2   thorpej 			fiztab = 1;
    389   1.2   thorpej 			break;
    390   1.4   thorpej 		case 't':
    391   1.4   thorpej 			if (tflag != NULL)
    392   1.4   thorpej 				usage();
    393   1.4   thorpej 			tflag = optarg;
    394   1.4   thorpej 			break;
    395   1.2   thorpej 		default:
    396   1.2   thorpej 			goto swapon_usage;
    397   1.2   thorpej 		}
    398   1.2   thorpej 	}
    399   1.2   thorpej 	argc -= optind;
    400   1.2   thorpej 	argv += optind;
    401   1.2   thorpej 
    402   1.2   thorpej 	if (fiztab) {
    403   1.2   thorpej 		if (argc)
    404   1.2   thorpej 			goto swapon_usage;
    405   1.4   thorpej 		/* Sanity-check -t */
    406   1.4   thorpej 		if (tflag != NULL) {
    407   1.4   thorpej 			if (strcmp(tflag, "blk") != 0 &&
    408   1.4   thorpej 			    strcmp(tflag, "noblk") != 0)
    409   1.4   thorpej 				usage();
    410   1.4   thorpej 		}
    411  1.12     lukem 		do_fstab(1);
    412   1.2   thorpej 		exit(0);
    413   1.4   thorpej 	} else if (argc == 0 || tflag != NULL)
    414   1.2   thorpej 		goto swapon_usage;
    415   1.2   thorpej 
    416   1.2   thorpej 	while (argc) {
    417  1.12     lukem 		if (! add_swap(argv[0], pri))
    418  1.12     lukem 			exit(1);
    419   1.2   thorpej 		argc--;
    420   1.2   thorpej 		argv++;
    421   1.2   thorpej 	}
    422   1.1       mrg 	exit(0);
    423   1.2   thorpej 	/* NOTREACHED */
    424   1.2   thorpej 
    425   1.2   thorpej  swapon_usage:
    426  1.18       cgd 	fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
    427  1.18       cgd 	fprintf(stderr, "       %s <path> ...\n", getprogname());
    428   1.2   thorpej 	exit(1);
    429   1.1       mrg }
    430   1.1       mrg 
    431   1.1       mrg /*
    432   1.1       mrg  * change_priority:  change the priority of a swap device.
    433   1.1       mrg  */
    434  1.12     lukem static void
    435  1.29  christos change_priority(char *path)
    436   1.1       mrg {
    437   1.1       mrg 
    438   1.1       mrg 	if (swapctl(SWAP_CTL, path, pri) < 0)
    439  1.30    martin 		err(1, "%s", path);
    440   1.1       mrg }
    441   1.1       mrg 
    442   1.1       mrg /*
    443   1.1       mrg  * add_swap:  add the pathname to the list of swap devices.
    444   1.1       mrg  */
    445  1.12     lukem static int
    446  1.29  christos add_swap(char *path, int priority)
    447   1.1       mrg {
    448  1.10       mrg 	struct stat sb;
    449  1.38   mlelstv 	char buf[MAXPATHLEN];
    450  1.38   mlelstv 	char *spec;
    451  1.10       mrg 
    452  1.38   mlelstv 	if (getfsspecname(buf, sizeof(buf), path) == NULL)
    453  1.38   mlelstv 		goto oops;
    454  1.38   mlelstv 	spec = buf;
    455  1.38   mlelstv 
    456  1.38   mlelstv 	if (stat(spec, &sb) < 0)
    457  1.10       mrg 		goto oops;
    458  1.10       mrg 
    459  1.10       mrg 	if (sb.st_mode & S_IROTH)
    460  1.19     pooka 		warnx("WARNING: %s is readable by the world", path);
    461  1.10       mrg 	if (sb.st_mode & S_IWOTH)
    462  1.19     pooka 		warnx("WARNING: %s is writable by the world", path);
    463   1.1       mrg 
    464  1.32    martin 	if (fflag || oflag) {
    465  1.38   mlelstv 		set_dumpdev1(spec);
    466  1.32    martin 		if (oflag)
    467  1.32    martin 			exit(0);
    468  1.32    martin 		else
    469  1.32    martin 			fflag = 0;
    470  1.32    martin 	}
    471  1.32    martin 
    472  1.32    martin 	if (nflag)
    473  1.32    martin 		return 1;
    474  1.32    martin 
    475  1.38   mlelstv 	if (swapctl(SWAP_ON, spec, priority) < 0) {
    476  1.10       mrg oops:
    477  1.30    martin 		err(1, "%s", path);
    478  1.12     lukem 	}
    479  1.12     lukem 	return (1);
    480   1.1       mrg }
    481   1.1       mrg 
    482   1.1       mrg /*
    483  1.11       mrg  * delete_swap:  remove the pathname to the list of swap devices.
    484   1.1       mrg  */
    485  1.12     lukem static int
    486  1.29  christos delete_swap(char *path)
    487   1.1       mrg {
    488  1.38   mlelstv 	char buf[MAXPATHLEN];
    489  1.38   mlelstv 	char *spec;
    490  1.38   mlelstv 
    491  1.38   mlelstv 	if (getfsspecname(buf, sizeof(buf), path) == NULL)
    492  1.38   mlelstv 		err(1, "%s", path);
    493  1.38   mlelstv 	spec = buf;
    494   1.1       mrg 
    495  1.32    martin 	if (nflag)
    496  1.32    martin 		return 1;
    497  1.32    martin 
    498  1.38   mlelstv 	if (swapctl(SWAP_OFF, spec, pri) < 0)
    499  1.30    martin 		err(1, "%s", path);
    500  1.12     lukem 	return (1);
    501   1.1       mrg }
    502   1.1       mrg 
    503  1.12     lukem static void
    504  1.38   mlelstv set_dumpdev1(char *spec)
    505  1.10       mrg {
    506  1.32    martin 	int rv = 0;
    507  1.10       mrg 
    508  1.32    martin 	if (!nflag) {
    509  1.38   mlelstv 		if (strcmp(spec, "none") == 0)
    510  1.32    martin 			rv = swapctl(SWAP_DUMPOFF, NULL, 0);
    511  1.32    martin 		else
    512  1.38   mlelstv 			rv = swapctl(SWAP_DUMPDEV, spec, 0);
    513  1.32    martin 	}
    514  1.30    martin 
    515  1.30    martin 	if (rv == -1)
    516  1.38   mlelstv 		err(1, "could not set dump device to %s", spec);
    517  1.10       mrg 	else
    518  1.38   mlelstv 		printf("%s: setting dump device to %s\n", getprogname(), spec);
    519  1.38   mlelstv }
    520  1.38   mlelstv 
    521  1.38   mlelstv static void
    522  1.38   mlelstv set_dumpdev(char *path)
    523  1.38   mlelstv {
    524  1.38   mlelstv 	char buf[MAXPATHLEN];
    525  1.38   mlelstv 	char *spec;
    526  1.38   mlelstv 
    527  1.38   mlelstv 	if (getfsspecname(buf, sizeof(buf), path) == NULL)
    528  1.38   mlelstv 		err(1, "%s", path);
    529  1.38   mlelstv 	spec = buf;
    530  1.38   mlelstv 
    531  1.38   mlelstv 	return set_dumpdev1(spec);
    532  1.16       mrg }
    533  1.16       mrg 
    534  1.30    martin static int
    535  1.28   xtraeme get_dumpdev(void)
    536  1.16       mrg {
    537  1.16       mrg 	dev_t	dev;
    538  1.16       mrg 	char 	*name;
    539  1.16       mrg 
    540  1.30    martin 	if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) {
    541  1.16       mrg 		warn("could not get dump device");
    542  1.30    martin 		return 0;
    543  1.30    martin 	} else if (dev == NODEV) {
    544  1.21  drochner 		printf("no dump device set\n");
    545  1.30    martin 		return 0;
    546  1.30    martin 	} else {
    547  1.16       mrg 		name = devname(dev, S_IFBLK);
    548  1.16       mrg 		printf("dump device is ");
    549  1.16       mrg 		if (name)
    550  1.16       mrg 			printf("%s\n", name);
    551  1.16       mrg 		else
    552  1.34  christos 			printf("major %llu minor %llu\n",
    553  1.34  christos 			    (unsigned long long)major(dev),
    554  1.34  christos 			    (unsigned long long)minor(dev));
    555  1.16       mrg 	}
    556  1.30    martin 	return 1;
    557  1.10       mrg }
    558  1.10       mrg 
    559  1.12     lukem static void
    560  1.32    martin do_localdevs(int add)
    561  1.32    martin {
    562  1.32    martin 	size_t ressize;
    563  1.32    martin 	char *disknames, *disk;
    564  1.32    martin 	static const char mibname[] = "hw.disknames";
    565  1.32    martin 
    566  1.32    martin 	ressize = 0;
    567  1.32    martin 	if (sysctlbyname(mibname, NULL, &ressize, NULL, 0))
    568  1.32    martin 		return;
    569  1.32    martin 	ressize += 200;	/* add some arbitrary slope */
    570  1.32    martin 	disknames = malloc(ressize);
    571  1.32    martin 	if (sysctlbyname(mibname, disknames, &ressize, NULL, 0) == 0) {
    572  1.32    martin 		for (disk = strtok(disknames, " "); disk;
    573  1.32    martin 		    disk = strtok(NULL, " "))
    574  1.32    martin 			do_localdisk(disk, add);
    575  1.32    martin 	}
    576  1.32    martin 	free(disknames);
    577  1.32    martin }
    578  1.32    martin 
    579  1.32    martin static void
    580  1.32    martin do_localdisk(const char *disk, int add)
    581  1.32    martin {
    582  1.32    martin 	int fd;
    583  1.32    martin 	char dvname[MAXPATHLEN];
    584  1.32    martin 
    585  1.32    martin 	if ((fd = opendisk(disk, O_RDONLY, dvname, sizeof(dvname), 0)) == -1)
    586  1.32    martin 		return;
    587  1.32    martin 
    588  1.32    martin 	if (!do_wedgesofdisk(fd, add))
    589  1.32    martin 		do_partitionsofdisk(disk, fd, add);
    590  1.32    martin 
    591  1.32    martin 	close(fd);
    592  1.32    martin }
    593  1.32    martin 
    594  1.32    martin static int
    595  1.32    martin do_wedgesofdisk(int fd, int add)
    596  1.32    martin {
    597  1.32    martin 	char devicename[MAXPATHLEN];
    598  1.32    martin 	struct dkwedge_info *dkw;
    599  1.32    martin 	struct dkwedge_list dkwl;
    600  1.32    martin 	size_t bufsize;
    601  1.32    martin 	u_int i;
    602  1.32    martin 
    603  1.32    martin 	dkw = NULL;
    604  1.32    martin 	dkwl.dkwl_buf = dkw;
    605  1.32    martin 	dkwl.dkwl_bufsize = 0;
    606  1.32    martin 
    607  1.32    martin 	for (;;) {
    608  1.32    martin 		if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
    609  1.32    martin 			return 0;
    610  1.32    martin 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
    611  1.32    martin 			break;
    612  1.32    martin 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
    613  1.32    martin 		if (dkwl.dkwl_bufsize < bufsize) {
    614  1.32    martin 			dkw = realloc(dkwl.dkwl_buf, bufsize);
    615  1.32    martin 			if (dkw == NULL)
    616  1.32    martin 				return 0;
    617  1.32    martin 			dkwl.dkwl_buf = dkw;
    618  1.32    martin 			dkwl.dkwl_bufsize = bufsize;
    619  1.32    martin 		}
    620  1.32    martin 	}
    621  1.32    martin 
    622  1.32    martin 	for (i = 0; i < dkwl.dkwl_ncopied; i++) {
    623  1.32    martin 		if (strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) != 0)
    624  1.32    martin 			continue;
    625  1.32    martin 		snprintf(devicename, sizeof(devicename), "%s%s", _PATH_DEV,
    626  1.32    martin 		    dkw[i].dkw_devname);
    627  1.32    martin 		devicename[sizeof(devicename)-1] = '\0';
    628  1.32    martin 
    629  1.32    martin 		if (add) {
    630  1.32    martin 			if (add_swap(devicename, pri)) {
    631  1.32    martin 				printf(
    632  1.32    martin 			    	"%s: adding %s as swap device at priority 0\n",
    633  1.32    martin 				    getprogname(), devicename);
    634  1.32    martin 			}
    635  1.32    martin 		} else {
    636  1.32    martin 			if (delete_swap(devicename)) {
    637  1.32    martin 				printf(
    638  1.32    martin 				    "%s: removing %s as swap device\n",
    639  1.32    martin 				    getprogname(), devicename);
    640  1.32    martin 			}
    641  1.32    martin 		}
    642  1.32    martin 
    643  1.32    martin 	}
    644  1.32    martin 
    645  1.32    martin 	free(dkw);
    646  1.32    martin 	return dkwl.dkwl_nwedges != 0;
    647  1.32    martin }
    648  1.32    martin 
    649  1.32    martin static int
    650  1.32    martin do_partitionsofdisk(const char *prefix, int fd, int add)
    651  1.32    martin {
    652  1.32    martin 	char devicename[MAXPATHLEN];
    653  1.32    martin 	struct disklabel lab;
    654  1.32    martin 	uint i;
    655  1.32    martin 
    656  1.32    martin 	if (ioctl(fd, DIOCGDINFO, &lab) != 0)
    657  1.32    martin 		return 0;
    658  1.32    martin 
    659  1.32    martin 	for (i = 0; i < lab.d_npartitions; i++) {
    660  1.32    martin 		if (lab.d_partitions[i].p_fstype != FS_SWAP)
    661  1.32    martin 			continue;
    662  1.32    martin 		snprintf(devicename, sizeof(devicename), "%s%s%c", _PATH_DEV,
    663  1.32    martin 		    prefix, 'a'+i);
    664  1.32    martin 		devicename[sizeof(devicename)-1] = '\0';
    665  1.32    martin 
    666  1.32    martin 		if (add) {
    667  1.32    martin 			if (add_swap(devicename, pri)) {
    668  1.32    martin 				printf(
    669  1.32    martin 			    	"%s: adding %s as swap device at priority 0\n",
    670  1.32    martin 				    getprogname(), devicename);
    671  1.32    martin 			}
    672  1.32    martin 		} else {
    673  1.32    martin 			if (delete_swap(devicename)) {
    674  1.32    martin 				printf(
    675  1.32    martin 				    "%s: removing %s as swap device\n",
    676  1.32    martin 				    getprogname(), devicename);
    677  1.32    martin 			}
    678  1.32    martin 		}
    679  1.32    martin 	}
    680  1.32    martin 
    681  1.32    martin 	return 1;
    682  1.32    martin }
    683  1.32    martin 
    684  1.32    martin static int
    685  1.32    martin check_fstab(void)
    686  1.32    martin {
    687  1.32    martin 	struct	fstab *fp;
    688  1.32    martin 
    689  1.32    martin 	while ((fp = getfsent()) != NULL) {
    690  1.32    martin 		if (strcmp(fp->fs_type, "dp") == 0)
    691  1.32    martin 			return 1;
    692  1.32    martin 
    693  1.32    martin 		if (strcmp(fp->fs_type, "sw") == 0)
    694  1.32    martin 			return 1;
    695  1.32    martin 	}
    696  1.32    martin 
    697  1.32    martin 	return 0;
    698  1.32    martin }
    699  1.32    martin 
    700  1.32    martin static void
    701  1.28   xtraeme do_fstab(int add)
    702   1.1       mrg {
    703   1.1       mrg 	struct	fstab *fp;
    704   1.1       mrg 	char	*s;
    705   1.1       mrg 	long	priority;
    706   1.4   thorpej 	struct	stat st;
    707   1.4   thorpej 	int	isblk;
    708  1.35       apb 	int	success = 0;	/* set to 1 after a successful operation */
    709  1.35       apb 	int	error = 0;	/* set to 1 after an error */
    710  1.27     lukem 
    711  1.27     lukem #ifdef RESCUEDIR
    712  1.27     lukem #define PATH_MOUNT	RESCUEDIR "/mount_nfs"
    713  1.27     lukem #define PATH_UMOUNT	RESCUEDIR "/umount"
    714  1.27     lukem #else
    715  1.12     lukem #define PATH_MOUNT	"/sbin/mount_nfs"
    716  1.12     lukem #define PATH_UMOUNT	"/sbin/umount"
    717  1.27     lukem #endif
    718  1.27     lukem 
    719  1.12     lukem 	char	cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    720   1.1       mrg 
    721   1.1       mrg #define PRIORITYEQ	"priority="
    722   1.1       mrg #define NFSMNTPT	"nfsmntpt="
    723   1.5     mikel 	while ((fp = getfsent()) != NULL) {
    724  1.37  christos 		char buf[MAXPATHLEN];
    725  1.37  christos 		char *spec, *fsspec;
    726   1.1       mrg 
    727  1.37  christos 		if (getfsspecname(buf, sizeof(buf), fp->fs_spec) == NULL) {
    728  1.37  christos 			warn("%s", buf);
    729  1.37  christos 			continue;
    730  1.37  christos 		}
    731  1.37  christos 		fsspec = spec = buf;
    732  1.12     lukem 		cmd[0] = '\0';
    733  1.10       mrg 
    734  1.12     lukem 		if (strcmp(fp->fs_type, "dp") == 0 && add) {
    735  1.38   mlelstv 			set_dumpdev1(spec);
    736  1.10       mrg 			continue;
    737  1.10       mrg 		}
    738  1.10       mrg 
    739   1.1       mrg 		if (strcmp(fp->fs_type, "sw") != 0)
    740   1.1       mrg 			continue;
    741  1.20  jdolecek 
    742  1.20  jdolecek 		/* handle dp as mnt option */
    743  1.20  jdolecek 		if (strstr(fp->fs_mntops, "dp") && add)
    744  1.38   mlelstv 			set_dumpdev1(spec);
    745  1.20  jdolecek 
    746   1.4   thorpej 		isblk = 0;
    747   1.1       mrg 
    748   1.5     mikel 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
    749   1.1       mrg 			s += sizeof(PRIORITYEQ) - 1;
    750   1.1       mrg 			priority = atol(s);
    751   1.1       mrg 		} else
    752   1.1       mrg 			priority = pri;
    753   1.1       mrg 
    754   1.5     mikel 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
    755  1.12     lukem 			char *t;
    756   1.1       mrg 
    757   1.4   thorpej 			/*
    758   1.4   thorpej 			 * Skip this song and dance if we're only
    759   1.4   thorpej 			 * doing block devices.
    760   1.4   thorpej 			 */
    761  1.12     lukem 			if (tflag != NULL && strcmp(tflag, "blk") == 0)
    762   1.4   thorpej 				continue;
    763   1.4   thorpej 
    764   1.1       mrg 			t = strpbrk(s, ",");
    765   1.1       mrg 			if (t != 0)
    766   1.1       mrg 				*t = '\0';
    767   1.1       mrg 			spec = strdup(s + strlen(NFSMNTPT));
    768   1.1       mrg 			if (t != 0)
    769   1.1       mrg 				*t = ',';
    770   1.1       mrg 
    771   1.1       mrg 			if (spec == NULL)
    772   1.1       mrg 				errx(1, "Out of memory");
    773   1.1       mrg 
    774   1.1       mrg 			if (strlen(spec) == 0) {
    775   1.1       mrg 				warnx("empty mountpoint");
    776  1.28   xtraeme 				free(spec);
    777   1.1       mrg 				continue;
    778   1.1       mrg 			}
    779  1.12     lukem 			if (add) {
    780  1.12     lukem 				snprintf(cmd, sizeof(cmd), "%s %s %s",
    781  1.37  christos 					PATH_MOUNT, fsspec, spec);
    782  1.12     lukem 				if (system(cmd) != 0) {
    783  1.37  christos 					warnx("%s: mount failed", fsspec);
    784  1.12     lukem 					continue;
    785  1.12     lukem 				}
    786  1.12     lukem 			} else {
    787  1.12     lukem 				snprintf(cmd, sizeof(cmd), "%s %s",
    788  1.37  christos 					PATH_UMOUNT, fsspec);
    789   1.1       mrg 			}
    790   1.4   thorpej 		} else {
    791   1.4   thorpej 			/*
    792   1.4   thorpej 			 * Determine blk-ness.
    793   1.4   thorpej 			 */
    794   1.4   thorpej 			if (stat(spec, &st) < 0) {
    795  1.15    itojun 				warn("%s", spec);
    796   1.4   thorpej 				continue;
    797   1.4   thorpej 			}
    798   1.4   thorpej 			if (S_ISBLK(st.st_mode))
    799   1.4   thorpej 				isblk = 1;
    800   1.4   thorpej 		}
    801   1.4   thorpej 
    802   1.4   thorpej 		/*
    803   1.4   thorpej 		 * Skip this type if we're told to.
    804   1.4   thorpej 		 */
    805   1.4   thorpej 		if (tflag != NULL) {
    806   1.4   thorpej 			if (strcmp(tflag, "blk") == 0 && isblk == 0)
    807   1.4   thorpej 				continue;
    808   1.4   thorpej 			if (strcmp(tflag, "noblk") == 0 && isblk == 1)
    809   1.4   thorpej 				continue;
    810   1.1       mrg 		}
    811   1.1       mrg 
    812  1.12     lukem 		if (add) {
    813  1.12     lukem 			if (add_swap(spec, (int)priority)) {
    814  1.35       apb 				success = 1;
    815  1.12     lukem 				printf(
    816  1.12     lukem 			    	"%s: adding %s as swap device at priority %d\n",
    817  1.37  christos 				    getprogname(), fsspec, (int)priority);
    818  1.35       apb 			} else {
    819  1.35       apb 				error = 1;
    820  1.35       apb 				fprintf(stderr,
    821  1.35       apb 				    "%s: failed to add %s as swap device\n",
    822  1.37  christos 				    getprogname(), fsspec);
    823  1.12     lukem 			}
    824  1.12     lukem 		} else {
    825  1.12     lukem 			if (delete_swap(spec)) {
    826  1.35       apb 				success = 1;
    827  1.12     lukem 				printf(
    828  1.12     lukem 				    "%s: removing %s as swap device\n",
    829  1.37  christos 				    getprogname(), fsspec);
    830  1.35       apb 			} else {
    831  1.35       apb 				error = 1;
    832  1.35       apb 				fprintf(stderr,
    833  1.35       apb 				    "%s: failed to remove %s as swap device\n",
    834  1.37  christos 				    getprogname(), fsspec);
    835  1.12     lukem 			}
    836  1.12     lukem 			if (cmd[0]) {
    837  1.12     lukem 				if (system(cmd) != 0) {
    838  1.37  christos 					warnx("%s: umount failed", fsspec);
    839  1.35       apb 					error = 1;
    840  1.12     lukem 					continue;
    841  1.12     lukem 				}
    842  1.12     lukem 			}
    843   1.8       mrg 		}
    844   1.1       mrg 
    845  1.37  christos 		if (spec != fsspec)
    846  1.28   xtraeme 			free(spec);
    847   1.1       mrg 	}
    848  1.35       apb 	if (error)
    849   1.8       mrg 		exit(1);
    850  1.35       apb 	else if (success)
    851  1.35       apb 		exit(0);
    852  1.35       apb 	else
    853  1.35       apb 		exit(2); /* not really an error, but no swap devices found */
    854   1.1       mrg }
    855   1.1       mrg 
    856  1.12     lukem static void
    857  1.28   xtraeme usage(void)
    858   1.1       mrg {
    859  1.18       cgd 	const char *progname = getprogname();
    860   1.1       mrg 
    861  1.32    martin 	fprintf(stderr, "usage: %s -A [-f|-o] [-n] [-p priority] "
    862  1.32    martin 	    "[-t blk|noblk|auto]\n", progname);
    863  1.18       cgd 	fprintf(stderr, "       %s -a [-p priority] path\n", progname);
    864  1.32    martin 	fprintf(stderr, "       %s -q\n", progname);
    865  1.18       cgd 	fprintf(stderr, "       %s -c -p priority path\n", progname);
    866  1.31       wiz 	fprintf(stderr, "       %s -D dumpdev|none\n", progname);
    867  1.18       cgd 	fprintf(stderr, "       %s -d path\n", progname);
    868  1.25       mrg 	fprintf(stderr, "       %s -l | -s [-k|-m|-g|-h]\n", progname);
    869  1.32    martin 	fprintf(stderr, "       %s -U [-n] [-t blk|noblk|auto]\n", progname);
    870  1.26      cjep 	fprintf(stderr, "       %s -z\n", progname);
    871   1.1       mrg 	exit(1);
    872   1.1       mrg }
    873