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