swapctl.c revision 1.8
1/*	$OpenBSD: swapctl.c,v 1.8 2002/05/15 23:24:43 art 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 <dev>	add this device
38 *	-d <dev>	remove this swap device (not supported yet)
39 *	-l		list swap devices
40 *	-s		short listing of swap devices
41 *	-k		use kilobytes
42 *	-p <pri>	use this priority
43 *	-c		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
66#include "swapctl.h"
67
68int	command;
69
70/*
71 * Commands for swapctl(8).  These are mutually exclusive.
72 */
73#define	CMD_A		0x01	/* process /etc/fstab */
74#define	CMD_a		0x02	/* add a swap file/device */
75#define	CMD_c		0x04	/* change priority of a swap file/device */
76#define	CMD_d		0x08	/* delete a swap file/device */
77#define	CMD_l		0x10	/* list swap files/devices */
78#define	CMD_s		0x20	/* summary of swap files/devices */
79
80#define	SET_COMMAND(cmd) \
81do { \
82	if (command) \
83		usage(); \
84	command = (cmd); \
85} while (0)
86
87/*
88 * Commands that require a "path" argument at the end of the command
89 * line, and the ones which require that none exist.
90 */
91#define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d)
92#define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
93
94/*
95 * Option flags, and the commands with which they are valid.
96 */
97int	kflag;		/* display in 1K blocks */
98#define	KFLAG_CMDS	(CMD_l | CMD_s)
99
100int	pflag;		/* priority was specified */
101#define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
102
103char	*tflag;		/* swap device type (blk or noblk) */
104#define	TFLAG_CMDS	(CMD_A)
105
106int	pri;		/* uses 0 as default pri */
107
108static	void change_priority(char *);
109static	void add_swap(char *);
110static	void del_swap(char *);
111	int  main(int, char *[]);
112static	void do_fstab(void);
113static	void usage(void);
114static	int  swapon_command(int, char **);
115
116extern	char *__progname;	/* from crt0.o */
117
118int
119main(argc, argv)
120	int	argc;
121	char	*argv[];
122{
123	int	c;
124
125	if (strcmp(__progname, "swapon") == 0)
126		return swapon_command(argc, argv);
127
128	while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) {
129		switch (c) {
130		case 'A':
131			SET_COMMAND(CMD_A);
132			break;
133
134		case 'a':
135			SET_COMMAND(CMD_a);
136			break;
137
138		case 'c':
139			SET_COMMAND(CMD_c);
140			break;
141
142		case 'd':
143			SET_COMMAND(CMD_d);
144			break;
145
146		case 'l':
147			SET_COMMAND(CMD_l);
148			break;
149
150		case 'k':
151			kflag = 1;
152			break;
153
154		case 'p':
155			pflag = 1;
156			/* XXX strtol() */
157			pri = atoi(optarg);
158			break;
159
160		case 's':
161			SET_COMMAND(CMD_s);
162			break;
163
164		case 't':
165			if (tflag != NULL)
166				usage();
167			tflag = optarg;
168			break;
169
170		default:
171			usage();
172			/* NOTREACHED */
173		}
174	}
175
176	/* Did the user specify a command? */
177	if (command == 0)
178		usage();
179
180	argv += optind;
181	argc -= optind;
182
183	switch (argc) {
184	case 0:
185		if (command & REQUIRE_PATH)
186			usage();
187		break;
188
189	case 1:
190		if (command & REQUIRE_NOPATH)
191			usage();
192		break;
193
194	default:
195		usage();
196	}
197
198	/* To change priority, you have to specify one. */
199	if ((command == CMD_c) && pflag == 0)
200		usage();
201
202	/* Sanity-check -t */
203	if (tflag != NULL) {
204		if (command != CMD_A)
205			usage();
206		if (strcmp(tflag, "blk") != 0 &&
207		    strcmp(tflag, "noblk") != 0)
208			usage();
209	}
210
211	/* Dispatch the command. */
212	switch (command) {
213	case CMD_l:
214		list_swap(pri, kflag, pflag, 0, 1);
215		break;
216
217	case CMD_s:
218		list_swap(pri, kflag, pflag, 0, 0);
219		break;
220
221	case CMD_c:
222		change_priority(argv[0]);
223		break;
224
225	case CMD_a:
226		add_swap(argv[0]);
227		break;
228
229	case CMD_d:
230		del_swap(argv[0]);
231		break;
232
233	case CMD_A:
234		do_fstab();
235		break;
236	}
237
238	return (0);
239}
240
241/*
242 * swapon_command: emulate the old swapon(8) program.
243 */
244int
245swapon_command(argc, argv)
246	int argc;
247	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 [-t blk|noblk]\n"
291			"       %s <path> ...\n",
292	    __progname, __progname);
293	return (1);
294}
295
296/*
297 * change_priority:  change the priority of a swap device.
298 */
299void
300change_priority(path)
301	char	*path;
302{
303
304	if (swapctl(SWAP_CTL, path, pri) < 0)
305		warn("%s", path);
306}
307
308/*
309 * add_swap:  add the pathname to the list of swap devices.
310 */
311void
312add_swap(path)
313	char *path;
314{
315
316	if (swapctl(SWAP_ON, path, pri) < 0)
317		if (errno != EBUSY)
318			err(1, "%s", path);
319}
320
321/*
322 * del_swap:  remove the pathname to the list of swap devices.
323 */
324void
325del_swap(path)
326	char *path;
327{
328
329	if (swapctl(SWAP_OFF, path, pri) < 0)
330		err(1, "%s", path);
331}
332
333void
334do_fstab()
335{
336	struct	fstab *fp;
337	char	*s;
338	long	priority;
339	struct	stat st;
340	mode_t	rejecttype;
341	int	gotone = 0;
342
343	/*
344	 * Select which mount point types to reject, depending on the
345	 * value of the -t parameter.
346	 */
347	if (tflag != NULL) {
348		if (strcmp(tflag, "blk") == 0)
349			rejecttype = S_IFREG;
350		else if (strcmp(tflag, "noblk") == 0)
351			rejecttype = S_IFBLK;
352	} else
353		rejecttype = 0;
354
355#define PRIORITYEQ	"priority="
356#define NFSMNTPT	"nfsmntpt="
357#define PATH_MOUNT	"/sbin/mount_nfs"
358	while ((fp = getfsent()) != NULL) {
359		const char *spec;
360
361		if (strcmp(fp->fs_type, "sw") != 0)
362			continue;
363
364		spec = fp->fs_spec;
365
366		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
367			s += sizeof(PRIORITYEQ) - 1;
368			priority = atol(s);
369		} else
370			priority = pri;
371
372		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
373			char *t, cmd[strlen(PATH_MOUNT)+1+PATH_MAX+1+PATH_MAX+1];
374
375			/*
376			 * Skip this song and dance if we're only
377			 * doing block devices.
378			 */
379			if (rejecttype == S_IFREG)
380				continue;
381
382			t = strpbrk(s, ",");
383			if (t != 0)
384				*t = '\0';
385			spec = strdup(s + strlen(NFSMNTPT));
386			if (t != 0)
387				*t = ',';
388
389			if (spec == NULL)
390				errx(1, "Out of memory");
391
392			if (strlen(spec) == 0) {
393				warnx("empty mountpoint");
394				free((char *)spec);
395				continue;
396			}
397			snprintf(cmd, sizeof(cmd), "%s %s %s",
398			    PATH_MOUNT, fp->fs_spec, spec);
399			if (system(cmd) != 0) {
400				warnx("%s: mount failed", fp->fs_spec);
401				continue;
402			}
403		} else {
404			/*
405			 * Determine blk-ness.  Don't even consider a
406			 * mountpoint outside /dev as a block device.
407			 */
408			if (rejecttype == S_IFREG) {
409				if (strncmp("/dev/", spec, 5) != 0)
410					continue;
411			}
412			if (stat(spec, &st) < 0) {
413				warn("%s", spec);
414				continue;
415			}
416			if ((st.st_mode & S_IFMT) == rejecttype)
417				continue;
418
419			/*
420			 * Do not allow fancy objects to be swap areas.
421			 */
422			if (!S_ISREG(st.st_mode) &&
423			    !S_ISBLK(st.st_mode))
424				continue;
425		}
426
427		if (swapctl(SWAP_ON, spec, (int)priority) < 0) {
428			if (errno != EBUSY)
429				warn("%s", spec);
430		} else {
431			gotone = 1;
432			printf("%s: adding %s as swap device at priority %d\n",
433			    __progname, fp->fs_spec, (int)priority);
434		}
435
436		if (spec != fp->fs_spec)
437			free((char *)spec);
438	}
439	if (gotone == 0)
440		exit(1);
441}
442
443void
444usage()
445{
446
447	fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
448	    __progname);
449	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
450	fprintf(stderr, "       %s -c -p priority path\n", __progname);
451	fprintf(stderr, "       %s -d path\n", __progname);
452	fprintf(stderr, "       %s -l | -s [-k]\n", __progname);
453	exit(1);
454}
455