]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - mount/swapon.c
2 * A swapon(8)/swapoff(8) for Linux 0.99.
3 * swapon.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
4 * Added '-s' (Summary option) <Vincent.Renardias@waw.com> 02/1997.
6 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
7 * - added Native Language Support
8 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
9 * - fixed strerr(errno) in gettext calls
20 #include "swap_constants.h"
24 #define streq(s, t) (strcmp ((s), (t)) == 0)
26 #define _PATH_FSTAB "/etc/fstab"
27 #define PROC_SWAPS "/proc/swaps"
29 #define SWAPON_NEEDS_TWO_ARGS
31 /* Nonzero for chatty (-v). This is a nonstandard flag (not in BSD). */
33 int priority
= -1; /* non-prioritized swap by default */
35 extern char version
[];
36 static char *program_name
;
37 static struct option longopts
[] =
40 { "help", 0, 0, 'h' },
41 { "priority", required_argument
, 0, 'p' },
42 { "summary", 0, 0, 's' },
43 { "verbose", 0, 0, 'v' },
44 { "version", 0, 0, 'V' },
49 usage (FILE *fp
, int n
)
51 fprintf (fp
, _("usage: %s [-hV]\n"
53 " %s [-v] [-p priority] special ...\n"
55 program_name
, program_name
, program_name
, program_name
);
59 #ifdef SWAPON_HAS_TWO_ARGS
60 #define SWAPON_NEEDS_TWO_ARGS
63 #ifdef SWAPON_NEEDS_TWO_ARGS
64 #ifdef SWAPON_HAS_TWO_ARGS
68 /* We want a swapon with two args, but have an old libc.
69 Build the kernel call by hand. */
70 #include <linux/unistd.h>
72 _syscall2(int, swapon
, const char *, path
, int, flags
);
74 _syscall1(int, swapoff
, const char *, path
);
77 /* just do as libc says */
82 swap (const char *special
, int prio
)
88 printf(_("%s on %s\n"), program_name
, special
);
90 if (streq (program_name
, "swapon")) {
91 if (stat(special
, &st
) < 0) {
93 fprintf (stderr
, _("swapon: cannot stat %s: %s\n"), special
, strerror (errsv
));
97 /* people generally dislike this warning - now it is printed
98 only when `verbose' is set */
99 if (verbose
&& (st
.st_mode
& 07077) != 0) {
100 fprintf(stderr
, _("swapon: warning: %s has insecure permissions %04o, "
101 "0600 suggested\n"), special
, st
.st_mode
& 07777);
104 /* test for holes by LBT */
105 if (S_ISREG(st
.st_mode
)) {
106 if (st
.st_blocks
* 512 < st
.st_size
) {
108 _("swapon: Skipping file %s - it appears to have holes.\n"),
114 #ifdef SWAPON_NEEDS_TWO_ARGS
118 #ifdef SWAP_FLAG_PREFER
120 if (prio
> SWAP_FLAG_PRIO_MASK
)
121 prio
= SWAP_FLAG_PRIO_MASK
;
122 flags
= SWAP_FLAG_PREFER
123 | ((prio
& SWAP_FLAG_PRIO_MASK
) << SWAP_FLAG_PRIO_SHIFT
);
126 status
= swapon (special
, flags
);
129 status
= swapon (special
);
132 status
= swapoff (special
);
136 fprintf (stderr
, "%s: %s: %s\n", program_name
, special
, strerror (errsv
));
143 display_summary(void)
148 if ((swaps
= fopen(PROC_SWAPS
, "r")) == NULL
) {
150 fprintf (stderr
, "%s: %s: %s\n", program_name
, PROC_SWAPS
,
154 while ( fgets(line
, sizeof(line
), swaps
))
161 main (int argc
, char *argv
[])
163 struct mntent
*fstab
;
168 setlocale(LC_ALL
, "");
169 bindtextdomain(PACKAGE
, LOCALEDIR
);
172 if (strrchr (argv
[0], '/') != NULL
)
173 program_name
= strrchr (argv
[0], '/') + 1;
175 program_name
= argv
[0];
177 while ((c
= getopt_long (argc
, argv
, "ahp:svV", longopts
, NULL
)) != EOF
)
186 case 'p': /* priority */
187 priority
= atoi(optarg
);
189 case 's': /* tell about current use of swap areas */
190 status
= display_summary();
192 case 'v': /* be chatty */
195 case 'V': /* version */
196 printf ("%s: %s\n", program_name
, version
);
210 FILE *fp
= setmntent(_PATH_FSTAB
, "r");
213 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
214 _PATH_FSTAB
, strerror(errsv
));
217 while ((fstab
= getmntent(fp
)) != NULL
) {
218 if (streq (fstab
->mnt_type
, MNTTYPE_SWAP
)) {
219 /* parse mount options; */
220 char *opt
, *opts
= strdup(fstab
->mnt_opts
);
222 for (opt
= strtok (opts
, ","); opt
!= NULL
;
223 opt
= strtok (NULL
, ","))
224 if (strncmp(opt
, "pri=", 4) == 0)
225 priority
= atoi(opt
+4);
226 status
|= swap (fstab
->mnt_fsname
, priority
);
229 } else if (*argv
== NULL
) {
232 while (*argv
!= NULL
)
233 status
|= swap (*argv
++,priority
);