swapon.c revision 107913
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 107913 2002-12-15 19:17:57Z dillon $"; 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 56107913Sdillonstatic void usage(const char *); 57107913Sdillonstatic int is_swapoff(const char *); 58107913Sdillonint swap_on_off(char *name, int ignoreebusy, int do_swapoff); 596661Sphk 606661Sphkint 616661Sphkmain(int argc, char **argv) 621558Srgrimes{ 6392806Sobrien struct fstab *fsp; 6492806Sobrien int stat; 651558Srgrimes int ch, doall; 66107913Sdillon int do_swapoff; 67107913Sdillon char *pname = argv[0]; 681558Srgrimes 69107913Sdillon do_swapoff = is_swapoff(pname); 70107913Sdillon 711558Srgrimes doall = 0; 7224359Simp while ((ch = getopt(argc, argv, "a")) != -1) 731558Srgrimes switch((char)ch) { 741558Srgrimes case 'a': 751558Srgrimes doall = 1; 761558Srgrimes break; 771558Srgrimes case '?': 781558Srgrimes default: 79107913Sdillon usage(pname); 801558Srgrimes } 811558Srgrimes argv += optind; 821558Srgrimes 831558Srgrimes stat = 0; 841558Srgrimes if (doall) 856661Sphk while ((fsp = getfsent()) != NULL) { 861558Srgrimes if (strcmp(fsp->fs_type, FSTAB_SW)) 871558Srgrimes continue; 8818073Sjkh if (strstr(fsp->fs_mntops, "noauto")) 8918073Sjkh continue; 90107913Sdillon if (swap_on_off(fsp->fs_spec, 1, do_swapoff)) 911558Srgrimes stat = 1; 921558Srgrimes else 93107913Sdillon printf("%s: %sing %s as swap device\n", 94107913Sdillon pname, do_swapoff ? "remov" : "add", 951558Srgrimes fsp->fs_spec); 961558Srgrimes } 971558Srgrimes else if (!*argv) 98107913Sdillon usage(pname); 991558Srgrimes for (; *argv; ++argv) 100107913Sdillon stat |= swap_on_off(*argv, 0, do_swapoff); 1011558Srgrimes exit(stat); 1021558Srgrimes} 1031558Srgrimes 1046661Sphkint 105107913Sdillonswap_on_off(char *name, int ignoreebusy, int do_swapoff) 1061558Srgrimes{ 107107913Sdillon if ((do_swapoff ? swapoff(name) : swapon(name)) == -1) { 1081558Srgrimes switch (errno) { 1091558Srgrimes case EBUSY: 1101558Srgrimes if (!ignoreebusy) 11126740Scharnier warnx("%s: device already in use", name); 1121558Srgrimes break; 1131558Srgrimes default: 11426740Scharnier warn("%s", name); 1151558Srgrimes break; 1161558Srgrimes } 1171558Srgrimes return(1); 1181558Srgrimes } 1191558Srgrimes return(0); 1201558Srgrimes} 1211558Srgrimes 12226740Scharnierstatic void 123107913Sdillonusage(const char *pname) 1241558Srgrimes{ 125107913Sdillon fprintf(stderr, "usage: %s [-a] [special_file ...]\n", pname); 1261558Srgrimes exit(1); 1271558Srgrimes} 128107913Sdillon 129107913Sdillonstatic int 130107913Sdillonis_swapoff(const char *s) 131107913Sdillon{ 132107913Sdillon const char *u; 133107913Sdillon 134107913Sdillon if ((u = strrchr(s, '/')) != NULL) 135107913Sdillon ++u; 136107913Sdillon else 137107913Sdillon u = s; 138107913Sdillon if (strcmp(u, "swapoff") == 0) 139107913Sdillon return 1; 140107913Sdillon else 141107913Sdillon return 0; 142107913Sdillon} 143