1/* vi: set sw=4 ts=4: */ 2/* 3 * Mini swapon/swapoff implementation for busybox 4 * 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 * 7 * Licensed under the GPL version 2, see the file LICENSE in this tarball. 8 */ 9 10#include "libbb.h" 11#include <mntent.h> 12#include <sys/swap.h> 13 14static int swap_enable_disable(char *device) 15{ 16 int status; 17 struct stat st; 18 19 xstat(device, &st); 20 21#if ENABLE_DESKTOP 22 /* test for holes */ 23 if (S_ISREG(st.st_mode)) 24 if (st.st_blocks * 512 < st.st_size) 25 bb_error_msg("warning: swap file has holes"); 26#endif 27 28 if (applet_name[5] == 'n') 29 status = swapon(device, 0); 30 else 31 status = swapoff(device); 32 33 if (status != 0) { 34 bb_perror_msg("%s", device); 35 return 1; 36 } 37 38 return 0; 39} 40 41static int do_em_all(void) 42{ 43 struct mntent *m; 44 FILE *f; 45 int err; 46 47 f = setmntent("/etc/fstab", "r"); 48 if (f == NULL) 49 bb_perror_msg_and_die("/etc/fstab"); 50 51 err = 0; 52 while ((m = getmntent(f)) != NULL) 53 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) 54 err += swap_enable_disable(m->mnt_fsname); 55 56 endmntent(f); 57 58 return err; 59} 60 61int swap_on_off_main(int argc, char **argv); 62int swap_on_off_main(int argc, char **argv) 63{ 64 int ret; 65 66 if (argc == 1) 67 bb_show_usage(); 68 69 ret = getopt32(argv, "a"); 70 if (ret) 71 return do_em_all(); 72 73 /* ret = 0; redundant */ 74 while (*++argv) 75 ret += swap_enable_disable(*argv); 76 return ret; 77} 78