xref: /openbsd/sbin/swapctl/swapctl.c (revision 898184e3)
1 /*	$OpenBSD: swapctl.c,v 1.18 2010/12/29 12:14:41 stsp Exp $	*/
2 /*	$NetBSD: swapctl.c,v 1.9 1998/07/26 20:23:15 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1996, 1997 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * swapctl command:
34  *	-A		add all devices listed as `sw' in /etc/fstab
35  *	-t [blk|noblk]	if -A, add either all block device or all non-block
36  *			devices
37  *	-a <path>	add this device
38  *	-d <path>	remove this swap device
39  *	-l		list swap devices
40  *	-s		short listing of swap devices
41  *	-k		use kilobytes
42  *	-p <pri>	use this priority
43  *	-c <path>	change priority
44  *
45  * or, if invoked as "swapon" (compatibility mode):
46  *
47  *	-a		all devices listed as `sw' in /etc/fstab
48  *	-t		same as -t above (feature not present in old
49  *			swapon(8) command)
50  *	<dev>		add this device
51  */
52 
53 #include <sys/param.h>
54 #include <sys/stat.h>
55 
56 #include <sys/swap.h>
57 
58 #include <unistd.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <fstab.h>
65 #include <util.h>
66 
67 #include "swapctl.h"
68 
69 int	command;
70 
71 /*
72  * Commands for swapctl(8).  These are mutually exclusive.
73  */
74 #define	CMD_A		0x01	/* process /etc/fstab */
75 #define	CMD_a		0x02	/* add a swap file/device */
76 #define	CMD_c		0x04	/* change priority of a swap file/device */
77 #define	CMD_d		0x08	/* delete a swap file/device */
78 #define	CMD_l		0x10	/* list swap files/devices */
79 #define	CMD_s		0x20	/* summary of swap files/devices */
80 
81 #define	SET_COMMAND(cmd) \
82 do { \
83 	if (command) \
84 		usage(); \
85 	command = (cmd); \
86 } while (0)
87 
88 /*
89  * Commands that require a "path" argument at the end of the command
90  * line, and the ones which require that none exist.
91  */
92 #define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d)
93 #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
94 
95 /*
96  * Option flags, and the commands with which they are valid.
97  */
98 int	kflag;		/* display in 1K blocks */
99 #define	KFLAG_CMDS	(CMD_l | CMD_s)
100 
101 int	pflag;		/* priority was specified */
102 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
103 
104 char	*tflag;		/* swap device type (blk or noblk) */
105 #define	TFLAG_CMDS	(CMD_A)
106 
107 int	pri;		/* uses 0 as default pri */
108 
109 static	void change_priority(char *);
110 static	void add_swap(char *);
111 static	void del_swap(char *);
112 static	void do_fstab(void);
113 static	void usage(void);
114 static	int  swapon_command(int, char **);
115 
116 extern	char *__progname;	/* from crt0.o */
117 
118 int
119 main(int argc, char *argv[])
120 {
121 	int	c;
122 
123 	if (strcmp(__progname, "swapon") == 0)
124 		return swapon_command(argc, argv);
125 
126 	while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) {
127 		switch (c) {
128 		case 'A':
129 			SET_COMMAND(CMD_A);
130 			break;
131 
132 		case 'a':
133 			SET_COMMAND(CMD_a);
134 			break;
135 
136 		case 'c':
137 			SET_COMMAND(CMD_c);
138 			break;
139 
140 		case 'd':
141 			SET_COMMAND(CMD_d);
142 			break;
143 
144 		case 'l':
145 			SET_COMMAND(CMD_l);
146 			break;
147 
148 		case 'k':
149 			kflag = 1;
150 			break;
151 
152 		case 'p':
153 			pflag = 1;
154 			/* XXX strtol() */
155 			pri = atoi(optarg);
156 			break;
157 
158 		case 's':
159 			SET_COMMAND(CMD_s);
160 			break;
161 
162 		case 't':
163 			if (tflag != NULL)
164 				usage();
165 			tflag = optarg;
166 			break;
167 
168 		default:
169 			usage();
170 			/* NOTREACHED */
171 		}
172 	}
173 
174 	argv += optind;
175 	argc -= optind;
176 
177 	/* Did the user specify a command? */
178 	if (command == 0) {
179 		if (argc == 0)
180 			SET_COMMAND(CMD_l);
181 		else
182 			usage();
183 	}
184 
185 	switch (argc) {
186 	case 0:
187 		if (command & REQUIRE_PATH)
188 			usage();
189 		break;
190 
191 	case 1:
192 		if (command & REQUIRE_NOPATH)
193 			usage();
194 		break;
195 
196 	default:
197 		usage();
198 	}
199 
200 	/* To change priority, you have to specify one. */
201 	if ((command == CMD_c) && pflag == 0)
202 		usage();
203 
204 	/* Sanity-check -t */
205 	if (tflag != NULL) {
206 		if (command != CMD_A)
207 			usage();
208 		if (strcmp(tflag, "blk") != 0 &&
209 		    strcmp(tflag, "noblk") != 0)
210 			usage();
211 	}
212 
213 	/* Dispatch the command. */
214 	switch (command) {
215 	case CMD_l:
216 		list_swap(pri, kflag, pflag, 1);
217 		break;
218 
219 	case CMD_s:
220 		list_swap(pri, kflag, pflag, 0);
221 		break;
222 
223 	case CMD_c:
224 		change_priority(argv[0]);
225 		break;
226 
227 	case CMD_a:
228 		add_swap(argv[0]);
229 		break;
230 
231 	case CMD_d:
232 		del_swap(argv[0]);
233 		break;
234 
235 	case CMD_A:
236 		do_fstab();
237 		break;
238 	}
239 
240 	return (0);
241 }
242 
243 /*
244  * swapon_command: emulate the old swapon(8) program.
245  */
246 int
247 swapon_command(int argc, char **argv)
248 {
249 	int ch, fiztab = 0;
250 
251 	while ((ch = getopt(argc, argv, "at:")) != -1) {
252 		switch (ch) {
253 		case 'a':
254 			fiztab = 1;
255 			break;
256 		case 't':
257 			if (tflag != NULL)
258 				usage();
259 			tflag = optarg;
260 			break;
261 		default:
262 			goto swapon_usage;
263 		}
264 	}
265 	argc -= optind;
266 	argv += optind;
267 
268 	if (fiztab) {
269 		if (argc)
270 			goto swapon_usage;
271 		/* Sanity-check -t */
272 		if (tflag != NULL) {
273 			if (strcmp(tflag, "blk") != 0 &&
274 			    strcmp(tflag, "noblk") != 0)
275 				usage();
276 		}
277 		do_fstab();
278 		return (0);
279 	} else if (argc == 0 || tflag != NULL)
280 		goto swapon_usage;
281 
282 	while (argc) {
283 		add_swap(argv[0]);
284 		argc--;
285 		argv++;
286 	}
287 	return (0);
288 
289  swapon_usage:
290 	fprintf(stderr, "usage: %s -a | path\n", __progname);
291 	return (1);
292 }
293 
294 /*
295  * change_priority:  change the priority of a swap device.
296  */
297 void
298 change_priority(char *path)
299 {
300 
301 	if (swapctl(SWAP_CTL, path, pri) < 0)
302 		warn("%s", path);
303 }
304 
305 /*
306  * add_swap:  add the pathname to the list of swap devices.
307  */
308 void
309 add_swap(char *path)
310 {
311 
312 	if (swapctl(SWAP_ON, path, pri) < 0)
313 		if (errno != EBUSY)
314 			err(1, "%s", path);
315 }
316 
317 /*
318  * del_swap:  remove the pathname from the list of swap devices.
319  */
320 void
321 del_swap(char *path)
322 {
323 
324 	if (swapctl(SWAP_OFF, path, pri) < 0)
325 		err(1, "%s", path);
326 }
327 
328 void
329 do_fstab(void)
330 {
331 	struct	fstab *fp;
332 	char	*s;
333 	long	priority;
334 	struct	stat st;
335 	mode_t	rejecttype;
336 	int	gotone = 0;
337 
338 	/*
339 	 * Select which mount point types to reject, depending on the
340 	 * value of the -t parameter.
341 	 */
342 	if (tflag != NULL) {
343 		if (strcmp(tflag, "blk") == 0)
344 			rejecttype = S_IFREG;
345 		else if (strcmp(tflag, "noblk") == 0)
346 			rejecttype = S_IFBLK;
347 	} else
348 		rejecttype = 0;
349 
350 #define PRIORITYEQ	"priority="
351 #define NFSMNTPT	"nfsmntpt="
352 #define PATH_MOUNT	"/sbin/mount_nfs"
353 	while ((fp = getfsent()) != NULL) {
354 		const char *spec;
355 
356 		if (strcmp(fp->fs_type, "sw") != 0)
357 			continue;
358 
359 		spec = fp->fs_spec;
360 
361 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
362 			s += sizeof(PRIORITYEQ) - 1;
363 			priority = atol(s);
364 		} else
365 			priority = pri;
366 
367 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
368 			char *t, cmd[sizeof(PATH_MOUNT)+PATH_MAX+1+PATH_MAX+1];
369 			int l;
370 
371 			/*
372 			 * Skip this song and dance if we're only
373 			 * doing block devices.
374 			 */
375 			if (rejecttype == S_IFREG)
376 				continue;
377 
378 			t = strpbrk(s, ",");
379 			if (t != 0)
380 				*t = '\0';
381 			spec = strdup(s + strlen(NFSMNTPT));
382 			if (spec == NULL)
383 				err(1, "strdup");
384 
385 			if (t != 0)
386 				*t = ',';
387 
388 			if (strlen(spec) == 0) {
389 				warnx("empty mountpoint");
390 				free((char *)spec);
391 				continue;
392 			}
393 			l = snprintf(cmd, sizeof(cmd), "%s %s %s",
394 			    PATH_MOUNT, fp->fs_spec, spec);
395 			if (l == -1 || l >= sizeof(cmd))
396 				errx(1, "path too long");
397 			if (system(cmd) != 0) {
398 				warnx("%s: mount failed", fp->fs_spec);
399 				free((char *)spec);
400 				continue;
401 			}
402 		} else if (isduid(spec, 0)) {
403 			if (rejecttype == S_IFBLK)
404 				continue;
405 		} else {
406 			/*
407 			 * Determine blk-ness.  Don't even consider a
408 			 * mountpoint outside /dev as a block device.
409 			 */
410 			if (rejecttype == S_IFREG) {
411 				if (strncmp("/dev/", spec, 5) != 0)
412 					continue;
413 			}
414 			if (stat(spec, &st) < 0) {
415 				warn("%s", spec);
416 				continue;
417 			}
418 			if ((st.st_mode & S_IFMT) == rejecttype)
419 				continue;
420 
421 			/*
422 			 * Do not allow fancy objects to be swap areas.
423 			 */
424 			if (!S_ISREG(st.st_mode) &&
425 			    !S_ISBLK(st.st_mode))
426 				continue;
427 		}
428 
429 		if (swapctl(SWAP_ON, spec, (int)priority) < 0) {
430 			if (errno != EBUSY)
431 				warn("%s", spec);
432 		} else {
433 			gotone = 1;
434 			printf("%s: adding %s as swap device at priority %d\n",
435 			    __progname, fp->fs_spec, (int)priority);
436 		}
437 
438 		if (spec != fp->fs_spec)
439 			free((char *)spec);
440 	}
441 	if (gotone == 0)
442 		exit(1);
443 }
444 
445 void
446 usage(void)
447 {
448 
449 	fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
450 	    __progname);
451 	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
452 	fprintf(stderr, "       %s -c -p priority path\n", __progname);
453 	fprintf(stderr, "       %s -d path\n", __progname);
454 	fprintf(stderr, "       %s [[-l] | -s] [-k]\n", __progname);
455 	exit(1);
456 }
457