swapon.c revision 38039
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[] =
4538039Scharnier	"$Id$";
461558Srgrimes#endif /* not lint */
471558Srgrimes
4838039Scharnier#include <err.h>
4938039Scharnier#include <errno.h>
501558Srgrimes#include <fstab.h>
511558Srgrimes#include <stdio.h>
5238039Scharnier#include <string.h>
536661Sphk#include <unistd.h>
541558Srgrimes
5526740Scharnierstatic void usage __P((void));
566661Sphkint	add __P((char *name, int ignoreebusy));
576661Sphk
586661Sphkint
596661Sphkmain(int argc, char **argv)
601558Srgrimes{
611558Srgrimes	register struct fstab *fsp;
621558Srgrimes	register int stat;
631558Srgrimes	int ch, doall;
641558Srgrimes
651558Srgrimes	doall = 0;
6624359Simp	while ((ch = getopt(argc, argv, "a")) != -1)
671558Srgrimes		switch((char)ch) {
681558Srgrimes		case 'a':
691558Srgrimes			doall = 1;
701558Srgrimes			break;
711558Srgrimes		case '?':
721558Srgrimes		default:
731558Srgrimes			usage();
741558Srgrimes		}
751558Srgrimes	argv += optind;
761558Srgrimes
771558Srgrimes	stat = 0;
781558Srgrimes	if (doall)
796661Sphk		while ((fsp = getfsent()) != NULL) {
801558Srgrimes			if (strcmp(fsp->fs_type, FSTAB_SW))
811558Srgrimes				continue;
8218073Sjkh			if (strstr(fsp->fs_mntops, "noauto"))
8318073Sjkh				continue;
841558Srgrimes			if (add(fsp->fs_spec, 1))
851558Srgrimes				stat = 1;
861558Srgrimes			else
871558Srgrimes				printf("swapon: adding %s as swap device\n",
881558Srgrimes				    fsp->fs_spec);
891558Srgrimes		}
901558Srgrimes	else if (!*argv)
911558Srgrimes		usage();
921558Srgrimes	for (; *argv; ++argv)
931558Srgrimes		stat |= add(*argv, 0);
941558Srgrimes	exit(stat);
951558Srgrimes}
961558Srgrimes
976661Sphkint
986661Sphkadd(char *name, int ignoreebusy)
991558Srgrimes{
1001558Srgrimes	extern int errno;
1011558Srgrimes
1021558Srgrimes	if (swapon(name) == -1) {
1031558Srgrimes		switch (errno) {
1041558Srgrimes		case EBUSY:
1051558Srgrimes			if (!ignoreebusy)
10626740Scharnier				warnx("%s: device already in use", name);
1071558Srgrimes			break;
1081558Srgrimes		default:
10926740Scharnier			warn("%s", name);
1101558Srgrimes			break;
1111558Srgrimes		}
1121558Srgrimes		return(1);
1131558Srgrimes	}
1141558Srgrimes	return(0);
1151558Srgrimes}
1161558Srgrimes
11726740Scharnierstatic void
1181558Srgrimesusage()
1191558Srgrimes{
1201558Srgrimes	fprintf(stderr, "usage: swapon [-a] [special_file ...]\n");
1211558Srgrimes	exit(1);
1221558Srgrimes}
123