swapon.c revision 92806
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3538039Scharnierstatic const char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
4138039Scharnier#if 0
421558Srgrimesstatic char sccsid[] = "@(#)swapon.c	8.1 (Berkeley) 6/5/93";
4338039Scharnier#endif
4438039Scharnierstatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/sbin/swapon/swapon.c 92806 2002-03-20 17:55:10Z obrien $";
461558Srgrimes#endif /* not lint */
471558Srgrimes
4838039Scharnier#include <err.h>
4938039Scharnier#include <errno.h>
501558Srgrimes#include <fstab.h>
511558Srgrimes#include <stdio.h>
5278732Sdd#include <stdlib.h>
5338039Scharnier#include <string.h>
546661Sphk#include <unistd.h>
551558Srgrimes
5626740Scharnierstatic void usage __P((void));
576661Sphkint	add __P((char *name, int ignoreebusy));
586661Sphk
596661Sphkint
606661Sphkmain(int argc, char **argv)
611558Srgrimes{
6292806Sobrien	struct fstab *fsp;
6392806Sobrien	int stat;
641558Srgrimes	int ch, doall;
651558Srgrimes
661558Srgrimes	doall = 0;
6724359Simp	while ((ch = getopt(argc, argv, "a")) != -1)
681558Srgrimes		switch((char)ch) {
691558Srgrimes		case 'a':
701558Srgrimes			doall = 1;
711558Srgrimes			break;
721558Srgrimes		case '?':
731558Srgrimes		default:
741558Srgrimes			usage();
751558Srgrimes		}
761558Srgrimes	argv += optind;
771558Srgrimes
781558Srgrimes	stat = 0;
791558Srgrimes	if (doall)
806661Sphk		while ((fsp = getfsent()) != NULL) {
811558Srgrimes			if (strcmp(fsp->fs_type, FSTAB_SW))
821558Srgrimes				continue;
8318073Sjkh			if (strstr(fsp->fs_mntops, "noauto"))
8418073Sjkh				continue;
851558Srgrimes			if (add(fsp->fs_spec, 1))
861558Srgrimes				stat = 1;
871558Srgrimes			else
881558Srgrimes				printf("swapon: adding %s as swap device\n",
891558Srgrimes				    fsp->fs_spec);
901558Srgrimes		}
911558Srgrimes	else if (!*argv)
921558Srgrimes		usage();
931558Srgrimes	for (; *argv; ++argv)
941558Srgrimes		stat |= add(*argv, 0);
951558Srgrimes	exit(stat);
961558Srgrimes}
971558Srgrimes
986661Sphkint
996661Sphkadd(char *name, int ignoreebusy)
1001558Srgrimes{
1011558Srgrimes	if (swapon(name) == -1) {
1021558Srgrimes		switch (errno) {
1031558Srgrimes		case EBUSY:
1041558Srgrimes			if (!ignoreebusy)
10526740Scharnier				warnx("%s: device already in use", name);
1061558Srgrimes			break;
1071558Srgrimes		default:
10826740Scharnier			warn("%s", name);
1091558Srgrimes			break;
1101558Srgrimes		}
1111558Srgrimes		return(1);
1121558Srgrimes	}
1131558Srgrimes	return(0);
1141558Srgrimes}
1151558Srgrimes
11626740Scharnierstatic void
1171558Srgrimesusage()
1181558Srgrimes{
1191558Srgrimes	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
1201558Srgrimes	exit(1);
1211558Srgrimes}
122