xref: /openbsd/sbin/swapctl/swapctl.c (revision b922e4d5)
1*b922e4d5Sjca /*	$OpenBSD: swapctl.c,v 1.26 2020/02/11 18:16:38 jca Exp $	*/
2694f38efSweingart /*	$NetBSD: swapctl.c,v 1.9 1998/07/26 20:23:15 mycroft Exp $	*/
3694f38efSweingart 
4694f38efSweingart /*
5694f38efSweingart  * Copyright (c) 1996, 1997 Matthew R. Green
6694f38efSweingart  * All rights reserved.
7694f38efSweingart  *
8694f38efSweingart  * Redistribution and use in source and binary forms, with or without
9694f38efSweingart  * modification, are permitted provided that the following conditions
10694f38efSweingart  * are met:
11694f38efSweingart  * 1. Redistributions of source code must retain the above copyright
12694f38efSweingart  *    notice, this list of conditions and the following disclaimer.
13694f38efSweingart  * 2. Redistributions in binary form must reproduce the above copyright
14694f38efSweingart  *    notice, this list of conditions and the following disclaimer in the
15694f38efSweingart  *    documentation and/or other materials provided with the distribution.
16694f38efSweingart  *
17694f38efSweingart  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18694f38efSweingart  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19694f38efSweingart  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20694f38efSweingart  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21694f38efSweingart  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22694f38efSweingart  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23694f38efSweingart  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24694f38efSweingart  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25694f38efSweingart  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26694f38efSweingart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27694f38efSweingart  * SUCH DAMAGE.
28694f38efSweingart  */
29694f38efSweingart 
30694f38efSweingart /*
31694f38efSweingart  * swapctl command:
32694f38efSweingart  *	-A		add all devices listed as `sw' in /etc/fstab
33694f38efSweingart  *	-t [blk|noblk]	if -A, add either all block device or all non-block
34694f38efSweingart  *			devices
356bef6715Smiod  *	-a <path>	add this device
366bef6715Smiod  *	-d <path>	remove this swap device
37694f38efSweingart  *	-l		list swap devices
38694f38efSweingart  *	-s		short listing of swap devices
39694f38efSweingart  *	-k		use kilobytes
40694f38efSweingart  *	-p <pri>	use this priority
416bef6715Smiod  *	-c <path>	change priority
42694f38efSweingart  *
43694f38efSweingart  * or, if invoked as "swapon" (compatibility mode):
44694f38efSweingart  *
45694f38efSweingart  *	-a		all devices listed as `sw' in /etc/fstab
46694f38efSweingart  *	-t		same as -t above (feature not present in old
47694f38efSweingart  *			swapon(8) command)
48694f38efSweingart  *	<dev>		add this device
49694f38efSweingart  */
50694f38efSweingart 
51694f38efSweingart #include <sys/stat.h>
52694f38efSweingart #include <sys/swap.h>
53821ff4bfSsemarie #include <sys/wait.h>
54694f38efSweingart 
55694f38efSweingart #include <unistd.h>
56694f38efSweingart #include <err.h>
57694f38efSweingart #include <errno.h>
58694f38efSweingart #include <stdio.h>
59694f38efSweingart #include <stdlib.h>
60a47b6461Sderaadt #include <limits.h>
61694f38efSweingart #include <string.h>
62694f38efSweingart #include <fstab.h>
6300453684Sstsp #include <util.h>
64694f38efSweingart 
65694f38efSweingart #include "swapctl.h"
66694f38efSweingart 
67694f38efSweingart int	command;
68694f38efSweingart 
69694f38efSweingart /*
70694f38efSweingart  * Commands for swapctl(8).  These are mutually exclusive.
71694f38efSweingart  */
72694f38efSweingart #define	CMD_A		0x01	/* process /etc/fstab */
73694f38efSweingart #define	CMD_a		0x02	/* add a swap file/device */
74694f38efSweingart #define	CMD_c		0x04	/* change priority of a swap file/device */
75694f38efSweingart #define	CMD_d		0x08	/* delete a swap file/device */
76694f38efSweingart #define	CMD_l		0x10	/* list swap files/devices */
77694f38efSweingart #define	CMD_s		0x20	/* summary of swap files/devices */
78694f38efSweingart 
79694f38efSweingart #define	SET_COMMAND(cmd) \
80694f38efSweingart do { \
81694f38efSweingart 	if (command) \
82694f38efSweingart 		usage(); \
83694f38efSweingart 	command = (cmd); \
84694f38efSweingart } while (0)
85694f38efSweingart 
86694f38efSweingart /*
87694f38efSweingart  * Commands that require a "path" argument at the end of the command
88694f38efSweingart  * line, and the ones which require that none exist.
89694f38efSweingart  */
90694f38efSweingart #define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d)
91694f38efSweingart #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
92694f38efSweingart 
93694f38efSweingart /*
94694f38efSweingart  * Option flags, and the commands with which they are valid.
95694f38efSweingart  */
96694f38efSweingart int	kflag;		/* display in 1K blocks */
97694f38efSweingart #define	KFLAG_CMDS	(CMD_l | CMD_s)
98694f38efSweingart 
99694f38efSweingart int	pflag;		/* priority was specified */
100694f38efSweingart #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
101694f38efSweingart 
102694f38efSweingart char	*tflag;		/* swap device type (blk or noblk) */
103694f38efSweingart #define	TFLAG_CMDS	(CMD_A)
104694f38efSweingart 
105694f38efSweingart int	pri;		/* uses 0 as default pri */
106694f38efSweingart 
107c72b5b24Smillert static	void change_priority(char *);
108c72b5b24Smillert static	void add_swap(char *);
109c72b5b24Smillert static	void del_swap(char *);
110c72b5b24Smillert static	void do_fstab(void);
111c72b5b24Smillert static	void usage(void);
112c72b5b24Smillert static	int  swapon_command(int, char **);
113694f38efSweingart 
114694f38efSweingart extern	char *__progname;	/* from crt0.o */
115694f38efSweingart 
116694f38efSweingart int
main(int argc,char * argv[])117bc52e260Sderaadt main(int argc, char *argv[])
118694f38efSweingart {
119a47b6461Sderaadt 	const char *errstr;
120694f38efSweingart 	int	c;
121694f38efSweingart 
12266420897Smickey 	if (strcmp(__progname, "swapon") == 0)
12366420897Smickey 		return swapon_command(argc, argv);
124694f38efSweingart 
125694f38efSweingart 	while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) {
126694f38efSweingart 		switch (c) {
127694f38efSweingart 		case 'A':
128694f38efSweingart 			SET_COMMAND(CMD_A);
129694f38efSweingart 			break;
130694f38efSweingart 
131694f38efSweingart 		case 'a':
132694f38efSweingart 			SET_COMMAND(CMD_a);
133694f38efSweingart 			break;
134694f38efSweingart 
135694f38efSweingart 		case 'c':
136694f38efSweingart 			SET_COMMAND(CMD_c);
137694f38efSweingart 			break;
138694f38efSweingart 
139694f38efSweingart 		case 'd':
140694f38efSweingart 			SET_COMMAND(CMD_d);
141694f38efSweingart 			break;
142694f38efSweingart 
143694f38efSweingart 		case 'l':
144694f38efSweingart 			SET_COMMAND(CMD_l);
145694f38efSweingart 			break;
146694f38efSweingart 
147694f38efSweingart 		case 'k':
148694f38efSweingart 			kflag = 1;
149694f38efSweingart 			break;
150694f38efSweingart 
151694f38efSweingart 		case 'p':
152694f38efSweingart 			pflag = 1;
153a47b6461Sderaadt 			pri = strtonum(optarg, 0, INT_MAX, &errstr);
154a47b6461Sderaadt 			if (errstr)
155a47b6461Sderaadt 				errx(1, "-p %s: %s", errstr, optarg);
156694f38efSweingart 			break;
157694f38efSweingart 
158694f38efSweingart 		case 's':
159694f38efSweingart 			SET_COMMAND(CMD_s);
160694f38efSweingart 			break;
161694f38efSweingart 
162694f38efSweingart 		case 't':
163694f38efSweingart 			if (tflag != NULL)
164694f38efSweingart 				usage();
165694f38efSweingart 			tflag = optarg;
166694f38efSweingart 			break;
167694f38efSweingart 
168694f38efSweingart 		default:
169694f38efSweingart 			usage();
170694f38efSweingart 			/* NOTREACHED */
171694f38efSweingart 		}
172694f38efSweingart 	}
173694f38efSweingart 
174694f38efSweingart 	argv += optind;
175694f38efSweingart 	argc -= optind;
176694f38efSweingart 
177fdae1d23Sderaadt 	/* Did the user specify a command? */
178fdae1d23Sderaadt 	if (command == 0) {
179fdae1d23Sderaadt 		if (argc == 0)
180fdae1d23Sderaadt 			SET_COMMAND(CMD_l);
181fdae1d23Sderaadt 		else
182fdae1d23Sderaadt 			usage();
183fdae1d23Sderaadt 	}
184fdae1d23Sderaadt 
185694f38efSweingart 	switch (argc) {
186694f38efSweingart 	case 0:
187694f38efSweingart 		if (command & REQUIRE_PATH)
188694f38efSweingart 			usage();
189694f38efSweingart 		break;
190694f38efSweingart 
191694f38efSweingart 	case 1:
192694f38efSweingart 		if (command & REQUIRE_NOPATH)
193694f38efSweingart 			usage();
194694f38efSweingart 		break;
195694f38efSweingart 
196694f38efSweingart 	default:
197694f38efSweingart 		usage();
198694f38efSweingart 	}
199694f38efSweingart 
200694f38efSweingart 	/* To change priority, you have to specify one. */
201694f38efSweingart 	if ((command == CMD_c) && pflag == 0)
202694f38efSweingart 		usage();
203694f38efSweingart 
204694f38efSweingart 	/* Sanity-check -t */
205694f38efSweingart 	if (tflag != NULL) {
206694f38efSweingart 		if (command != CMD_A)
207694f38efSweingart 			usage();
208694f38efSweingart 		if (strcmp(tflag, "blk") != 0 &&
209694f38efSweingart 		    strcmp(tflag, "noblk") != 0)
210694f38efSweingart 			usage();
211694f38efSweingart 	}
212694f38efSweingart 
213694f38efSweingart 	/* Dispatch the command. */
214694f38efSweingart 	switch (command) {
215694f38efSweingart 	case CMD_l:
21603b50a27Smillert 		list_swap(pri, kflag, pflag, 1);
217694f38efSweingart 		break;
218694f38efSweingart 
219694f38efSweingart 	case CMD_s:
22003b50a27Smillert 		list_swap(pri, kflag, pflag, 0);
221694f38efSweingart 		break;
222694f38efSweingart 
223694f38efSweingart 	case CMD_c:
224694f38efSweingart 		change_priority(argv[0]);
225694f38efSweingart 		break;
226694f38efSweingart 
227694f38efSweingart 	case CMD_a:
228694f38efSweingart 		add_swap(argv[0]);
229694f38efSweingart 		break;
230694f38efSweingart 
231694f38efSweingart 	case CMD_d:
232694f38efSweingart 		del_swap(argv[0]);
233694f38efSweingart 		break;
234694f38efSweingart 
235694f38efSweingart 	case CMD_A:
236694f38efSweingart 		do_fstab();
237694f38efSweingart 		break;
238694f38efSweingart 	}
239694f38efSweingart 
24066420897Smickey 	return (0);
241694f38efSweingart }
242694f38efSweingart 
243694f38efSweingart /*
244694f38efSweingart  * swapon_command: emulate the old swapon(8) program.
245694f38efSweingart  */
24666420897Smickey int
swapon_command(int argc,char ** argv)247bc52e260Sderaadt swapon_command(int argc, char **argv)
248694f38efSweingart {
249694f38efSweingart 	int ch, fiztab = 0;
250694f38efSweingart 
251694f38efSweingart 	while ((ch = getopt(argc, argv, "at:")) != -1) {
252694f38efSweingart 		switch (ch) {
253694f38efSweingart 		case 'a':
254694f38efSweingart 			fiztab = 1;
255694f38efSweingart 			break;
256694f38efSweingart 		case 't':
257694f38efSweingart 			if (tflag != NULL)
258694f38efSweingart 				usage();
259694f38efSweingart 			tflag = optarg;
260694f38efSweingart 			break;
261694f38efSweingart 		default:
262694f38efSweingart 			goto swapon_usage;
263694f38efSweingart 		}
264694f38efSweingart 	}
265694f38efSweingart 	argc -= optind;
266694f38efSweingart 	argv += optind;
267694f38efSweingart 
268694f38efSweingart 	if (fiztab) {
269694f38efSweingart 		if (argc)
270694f38efSweingart 			goto swapon_usage;
271694f38efSweingart 		/* Sanity-check -t */
272694f38efSweingart 		if (tflag != NULL) {
273694f38efSweingart 			if (strcmp(tflag, "blk") != 0 &&
274694f38efSweingart 			    strcmp(tflag, "noblk") != 0)
275694f38efSweingart 				usage();
276694f38efSweingart 		}
277694f38efSweingart 		do_fstab();
27866420897Smickey 		return (0);
279694f38efSweingart 	} else if (argc == 0 || tflag != NULL)
280694f38efSweingart 		goto swapon_usage;
281694f38efSweingart 
282694f38efSweingart 	while (argc) {
283694f38efSweingart 		add_swap(argv[0]);
284694f38efSweingart 		argc--;
285694f38efSweingart 		argv++;
286694f38efSweingart 	}
28766420897Smickey 	return (0);
288694f38efSweingart 
289694f38efSweingart  swapon_usage:
290a35de285Sjmc 	fprintf(stderr, "usage: %s -a | path\n", __progname);
29166420897Smickey 	return (1);
292694f38efSweingart }
293694f38efSweingart 
294694f38efSweingart /*
295694f38efSweingart  * change_priority:  change the priority of a swap device.
296694f38efSweingart  */
297694f38efSweingart void
change_priority(char * path)298bc52e260Sderaadt change_priority(char *path)
299694f38efSweingart {
300694f38efSweingart 
301df69c215Sderaadt 	if (swapctl(SWAP_CTL, path, pri) == -1)
302694f38efSweingart 		warn("%s", path);
303694f38efSweingart }
304694f38efSweingart 
305694f38efSweingart /*
306694f38efSweingart  * add_swap:  add the pathname to the list of swap devices.
307694f38efSweingart  */
308694f38efSweingart void
add_swap(char * path)309bc52e260Sderaadt add_swap(char *path)
310694f38efSweingart {
311694f38efSweingart 
312df69c215Sderaadt 	if (swapctl(SWAP_ON, path, pri) == -1)
313590f2c1aSmiod 		if (errno != EBUSY)
314694f38efSweingart 			err(1, "%s", path);
315694f38efSweingart }
316694f38efSweingart 
317694f38efSweingart /*
3180bbbe695Sart  * del_swap:  remove the pathname from the list of swap devices.
319694f38efSweingart  */
320694f38efSweingart void
del_swap(char * path)321bc52e260Sderaadt del_swap(char *path)
322694f38efSweingart {
323694f38efSweingart 
324df69c215Sderaadt 	if (swapctl(SWAP_OFF, path, pri) == -1)
325694f38efSweingart 		err(1, "%s", path);
326694f38efSweingart }
327694f38efSweingart 
328694f38efSweingart void
do_fstab(void)329bc52e260Sderaadt do_fstab(void)
330694f38efSweingart {
331694f38efSweingart 	struct	fstab *fp;
332694f38efSweingart 	char	*s;
333694f38efSweingart 	long	priority;
334694f38efSweingart 	struct	stat st;
335*b922e4d5Sjca 	mode_t	rejecttype = 0;
336694f38efSweingart 	int	gotone = 0;
337694f38efSweingart 
338590f2c1aSmiod 	/*
339590f2c1aSmiod 	 * Select which mount point types to reject, depending on the
340590f2c1aSmiod 	 * value of the -t parameter.
341590f2c1aSmiod 	 */
342590f2c1aSmiod 	if (tflag != NULL) {
343590f2c1aSmiod 		if (strcmp(tflag, "blk") == 0)
344590f2c1aSmiod 			rejecttype = S_IFREG;
345590f2c1aSmiod 		else if (strcmp(tflag, "noblk") == 0)
346590f2c1aSmiod 			rejecttype = S_IFBLK;
347*b922e4d5Sjca 	}
348590f2c1aSmiod 
349694f38efSweingart #define PRIORITYEQ	"priority="
350694f38efSweingart #define NFSMNTPT	"nfsmntpt="
351694f38efSweingart #define PATH_MOUNT	"/sbin/mount_nfs"
352694f38efSweingart 	while ((fp = getfsent()) != NULL) {
353694f38efSweingart 		const char *spec;
354694f38efSweingart 
355694f38efSweingart 		if (strcmp(fp->fs_type, "sw") != 0)
356694f38efSweingart 			continue;
357694f38efSweingart 
358694f38efSweingart 		spec = fp->fs_spec;
359694f38efSweingart 
360694f38efSweingart 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
361694f38efSweingart 			s += sizeof(PRIORITYEQ) - 1;
362694f38efSweingart 			priority = atol(s);
363694f38efSweingart 		} else
364694f38efSweingart 			priority = pri;
365694f38efSweingart 
366694f38efSweingart 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
367821ff4bfSsemarie 			char *t;
368821ff4bfSsemarie 			pid_t pid;
369821ff4bfSsemarie 			int status;
370694f38efSweingart 
371694f38efSweingart 			/*
372694f38efSweingart 			 * Skip this song and dance if we're only
373694f38efSweingart 			 * doing block devices.
374694f38efSweingart 			 */
375590f2c1aSmiod 			if (rejecttype == S_IFREG)
376694f38efSweingart 				continue;
377694f38efSweingart 
378694f38efSweingart 			t = strpbrk(s, ",");
379694f38efSweingart 			if (t != 0)
380694f38efSweingart 				*t = '\0';
381694f38efSweingart 			spec = strdup(s + strlen(NFSMNTPT));
382694f38efSweingart 			if (spec == NULL)
3831a6152b9Savsm 				err(1, "strdup");
384694f38efSweingart 
385ca198d9fSderaadt 			if (t != 0)
386ca198d9fSderaadt 				*t = ',';
387ca198d9fSderaadt 
388694f38efSweingart 			if (strlen(spec) == 0) {
389694f38efSweingart 				warnx("empty mountpoint");
390694f38efSweingart 				free((char *)spec);
391694f38efSweingart 				continue;
392694f38efSweingart 			}
393821ff4bfSsemarie 
394821ff4bfSsemarie 			switch (pid = vfork()) {
395821ff4bfSsemarie 			case -1:	/* error */
396821ff4bfSsemarie 				err(1, "vfork");
397821ff4bfSsemarie 			case 0:
398821ff4bfSsemarie 				execl(PATH_MOUNT, PATH_MOUNT, fp->fs_spec, spec,
39953408464Skrw 				    (char *)NULL);
400821ff4bfSsemarie 				err(1, "execl");
401821ff4bfSsemarie 			}
402df69c215Sderaadt 			while (waitpid(pid, &status, 0) == -1)
403821ff4bfSsemarie 				if (errno != EINTR)
404821ff4bfSsemarie 					err(1, "waitpid");
405821ff4bfSsemarie 			if (status != 0) {
406694f38efSweingart 				warnx("%s: mount failed", fp->fs_spec);
40700453684Sstsp 				free((char *)spec);
408694f38efSweingart 				continue;
409694f38efSweingart 			}
41000453684Sstsp 		} else if (isduid(spec, 0)) {
41100453684Sstsp 			if (rejecttype == S_IFBLK)
41200453684Sstsp 				continue;
413694f38efSweingart 		} else {
414694f38efSweingart 			/*
415590f2c1aSmiod 			 * Determine blk-ness.  Don't even consider a
416590f2c1aSmiod 			 * mountpoint outside /dev as a block device.
417694f38efSweingart 			 */
418590f2c1aSmiod 			if (rejecttype == S_IFREG) {
419590f2c1aSmiod 				if (strncmp("/dev/", spec, 5) != 0)
420590f2c1aSmiod 					continue;
421590f2c1aSmiod 			}
422df69c215Sderaadt 			if (stat(spec, &st) == -1) {
423b638aa94Smillert 				warn("%s", spec);
424694f38efSweingart 				continue;
425694f38efSweingart 			}
426590f2c1aSmiod 			if ((st.st_mode & S_IFMT) == rejecttype)
427590f2c1aSmiod 				continue;
428694f38efSweingart 
429694f38efSweingart 			/*
430590f2c1aSmiod 			 * Do not allow fancy objects to be swap areas.
431694f38efSweingart 			 */
432590f2c1aSmiod 			if (!S_ISREG(st.st_mode) &&
433590f2c1aSmiod 			    !S_ISBLK(st.st_mode))
434694f38efSweingart 				continue;
435694f38efSweingart 		}
436694f38efSweingart 
437df69c215Sderaadt 		if (swapctl(SWAP_ON, spec, (int)priority) == -1) {
438590f2c1aSmiod 			if (errno != EBUSY)
439694f38efSweingart 				warn("%s", spec);
440590f2c1aSmiod 		} else {
441694f38efSweingart 			gotone = 1;
442694f38efSweingart 			printf("%s: adding %s as swap device at priority %d\n",
443694f38efSweingart 			    __progname, fp->fs_spec, (int)priority);
444694f38efSweingart 		}
445694f38efSweingart 
446694f38efSweingart 		if (spec != fp->fs_spec)
447694f38efSweingart 			free((char *)spec);
448694f38efSweingart 	}
449694f38efSweingart 	if (gotone == 0)
450694f38efSweingart 		exit(1);
451694f38efSweingart }
452694f38efSweingart 
453694f38efSweingart void
usage(void)454bc52e260Sderaadt usage(void)
455694f38efSweingart {
456694f38efSweingart 
457694f38efSweingart 	fprintf(stderr, "usage: %s -A [-p priority] [-t blk | noblk]\n",
458694f38efSweingart 	    __progname);
459694f38efSweingart 	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
460694f38efSweingart 	fprintf(stderr, "       %s -c -p priority path\n", __progname);
461694f38efSweingart 	fprintf(stderr, "       %s -d path\n", __progname);
462fdae1d23Sderaadt 	fprintf(stderr, "       %s [[-l] | -s] [-k]\n", __progname);
463694f38efSweingart 	exit(1);
464694f38efSweingart }
465