blob: 55124d0b8e4453681d87e53103fac46a434b253e [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001#include <sys/swap.h>
2#include <string.h>
3#include <errno.h>
4#include <mntent.h>
5#include "internal.h"
6
7const char swapoff_usage[] = "swapoff block-device\n"
8"\n"
9"\tStop swapping virtual memory pages on the given device.\n";
10
11extern int
12swapoff_fn(const struct FileInfo * i)
13{
14 struct mntent entries[100];
15 int count = 0;
16 FILE * swapsTable = setmntent("/etc/swaps", "r");
17 struct mntent * m;
18
19 if (!(swapoff(i->source))) {
20 if ( swapsTable == 0 ) {
21 fprintf(stderr, "/etc/swaps: %s\n", strerror(errno));
22 return 1;
23 }
24 while ( (m = getmntent(swapsTable)) != 0 ) {
25 entries[count].mnt_fsname = strdup(m->mnt_fsname);
26 entries[count].mnt_dir = strdup(m->mnt_dir);
27 entries[count].mnt_type = strdup(m->mnt_type);
28 entries[count].mnt_opts = strdup(m->mnt_opts);
29 entries[count].mnt_freq = m->mnt_freq;
30 entries[count].mnt_passno = m->mnt_passno;
31 count++;
32 }
33 endmntent(swapsTable);
34 if ( (swapsTable = setmntent("/etc/swaps", "w")) ) {
35 int id;
36 for ( id = 0; id < count; id++ ) {
37 int result =
38 (strcmp(entries[id].mnt_fsname, i->source)==0
39 ||strcmp(entries[id].mnt_dir, i->source)==0);
40 if ( result )
41 continue;
42 else
43 addmntent(swapsTable, &entries[id]);
44 }
45 endmntent(swapsTable);
46 }
47 else if ( errno != EROFS )
48 fprintf(stderr, "/etc/swaps: %s\n", strerror(errno));
49 return (0);
50 }
51 return (-1);
52}