xref: /titanic_53/usr/src/cmd/swap/swap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * 	Swap administrative interface
44*7c478bd9Sstevel@tonic-gate  *	Used to add/delete/list swap devices.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
48*7c478bd9Sstevel@tonic-gate #include	<sys/dumpadm.h>
49*7c478bd9Sstevel@tonic-gate #include	<string.h>
50*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
51*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
52*7c478bd9Sstevel@tonic-gate #include	<unistd.h>
53*7c478bd9Sstevel@tonic-gate #include	<errno.h>
54*7c478bd9Sstevel@tonic-gate #include	<sys/param.h>
55*7c478bd9Sstevel@tonic-gate #include	<dirent.h>
56*7c478bd9Sstevel@tonic-gate #include	<sys/swap.h>
57*7c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
58*7c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
59*7c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
60*7c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
61*7c478bd9Sstevel@tonic-gate #include	<sys/uadmin.h>
62*7c478bd9Sstevel@tonic-gate #include	<vm/anon.h>
63*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
64*7c478bd9Sstevel@tonic-gate #include	<locale.h>
65*7c478bd9Sstevel@tonic-gate #include	<libintl.h>
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #define	LFLAG	0x01	/* swap -l (list swap devices) */
68*7c478bd9Sstevel@tonic-gate #define	DFLAG	0x02	/* swap -d (delete swap device) */
69*7c478bd9Sstevel@tonic-gate #define	AFLAG	0x04	/* swap -a (add swap device) */
70*7c478bd9Sstevel@tonic-gate #define	SFLAG	0x08	/* swap -s (swap info summary) */
71*7c478bd9Sstevel@tonic-gate #define	P1FLAG	0x10	/* swap -1 (swapadd pass1; do not modify dump device) */
72*7c478bd9Sstevel@tonic-gate #define	P2FLAG	0x20	/* swap -2 (swapadd pass2; do not modify dump device) */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate static char *prognamep;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static int add(char *, off_t, off_t, int);
77*7c478bd9Sstevel@tonic-gate static int delete(char *, off_t);
78*7c478bd9Sstevel@tonic-gate static void usage(void);
79*7c478bd9Sstevel@tonic-gate static int doswap(void);
80*7c478bd9Sstevel@tonic-gate static int valid(char *, off_t, off_t);
81*7c478bd9Sstevel@tonic-gate static int list(void);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate int
84*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	int c, flag = 0;
87*7c478bd9Sstevel@tonic-gate 	int ret;
88*7c478bd9Sstevel@tonic-gate 	off_t s_offset = 0;
89*7c478bd9Sstevel@tonic-gate 	off_t length = 0;
90*7c478bd9Sstevel@tonic-gate 	char *pathname;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
95*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
96*7c478bd9Sstevel@tonic-gate #endif
97*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	prognamep = argv[0];
100*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
101*7c478bd9Sstevel@tonic-gate 		usage();
102*7c478bd9Sstevel@tonic-gate 		exit(1);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "lsd:a:12")) != EOF) {
106*7c478bd9Sstevel@tonic-gate 		char *char_p;
107*7c478bd9Sstevel@tonic-gate 		switch (c) {
108*7c478bd9Sstevel@tonic-gate 		case 'l': 	/* list all the swap devices */
109*7c478bd9Sstevel@tonic-gate 			if (argc != 2 || flag) {
110*7c478bd9Sstevel@tonic-gate 				usage();
111*7c478bd9Sstevel@tonic-gate 				exit(1);
112*7c478bd9Sstevel@tonic-gate 			}
113*7c478bd9Sstevel@tonic-gate 			flag |= LFLAG;
114*7c478bd9Sstevel@tonic-gate 			ret = list();
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 		case 's':
117*7c478bd9Sstevel@tonic-gate 			if (argc != 2 || flag) {
118*7c478bd9Sstevel@tonic-gate 				usage();
119*7c478bd9Sstevel@tonic-gate 				exit(1);
120*7c478bd9Sstevel@tonic-gate 			}
121*7c478bd9Sstevel@tonic-gate 			flag |= SFLAG;
122*7c478bd9Sstevel@tonic-gate 			ret = doswap();
123*7c478bd9Sstevel@tonic-gate 			break;
124*7c478bd9Sstevel@tonic-gate 		case 'd':
125*7c478bd9Sstevel@tonic-gate 			/*
126*7c478bd9Sstevel@tonic-gate 			 * The argument for starting offset is optional.
127*7c478bd9Sstevel@tonic-gate 			 * If no argument is specified, the entire swap file
128*7c478bd9Sstevel@tonic-gate 			 * is added although this will fail if a non-zero
129*7c478bd9Sstevel@tonic-gate 			 * starting offset was specified when added.
130*7c478bd9Sstevel@tonic-gate 			 */
131*7c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 1 || flag != 0) {
132*7c478bd9Sstevel@tonic-gate 				usage();
133*7c478bd9Sstevel@tonic-gate 				exit(1);
134*7c478bd9Sstevel@tonic-gate 			}
135*7c478bd9Sstevel@tonic-gate 			flag |= DFLAG;
136*7c478bd9Sstevel@tonic-gate 			pathname = optarg;
137*7c478bd9Sstevel@tonic-gate 			if (optind < argc) {
138*7c478bd9Sstevel@tonic-gate 				errno = 0;
139*7c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
140*7c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
141*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
142*7c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
143*7c478bd9Sstevel@tonic-gate 					exit(1);
144*7c478bd9Sstevel@tonic-gate 				}
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 			ret = delete(pathname, s_offset);
147*7c478bd9Sstevel@tonic-gate 			break;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		case 'a':
150*7c478bd9Sstevel@tonic-gate 			/*
151*7c478bd9Sstevel@tonic-gate 			 * The arguments for starting offset and number of
152*7c478bd9Sstevel@tonic-gate 			 * blocks are optional.  If only the starting offset
153*7c478bd9Sstevel@tonic-gate 			 * is specified, all the blocks to the end of the swap
154*7c478bd9Sstevel@tonic-gate 			 * file will be added.  If no starting offset is
155*7c478bd9Sstevel@tonic-gate 			 * specified, the entire swap file is assumed.
156*7c478bd9Sstevel@tonic-gate 			 */
157*7c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 2 ||
158*7c478bd9Sstevel@tonic-gate 			    (flag & ~(P1FLAG | P2FLAG)) != 0) {
159*7c478bd9Sstevel@tonic-gate 				usage();
160*7c478bd9Sstevel@tonic-gate 				exit(1);
161*7c478bd9Sstevel@tonic-gate 			}
162*7c478bd9Sstevel@tonic-gate 			if (*optarg != '/') {
163*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
164*7c478bd9Sstevel@tonic-gate 				    gettext("%s: path must be absolute\n"),
165*7c478bd9Sstevel@tonic-gate 				    prognamep);
166*7c478bd9Sstevel@tonic-gate 				exit(1);
167*7c478bd9Sstevel@tonic-gate 			}
168*7c478bd9Sstevel@tonic-gate 			flag |= AFLAG;
169*7c478bd9Sstevel@tonic-gate 			pathname = optarg;
170*7c478bd9Sstevel@tonic-gate 			if (optind < argc) {
171*7c478bd9Sstevel@tonic-gate 				errno = 0;
172*7c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
173*7c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
174*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
175*7c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
176*7c478bd9Sstevel@tonic-gate 					exit(1);
177*7c478bd9Sstevel@tonic-gate 				}
178*7c478bd9Sstevel@tonic-gate 			}
179*7c478bd9Sstevel@tonic-gate 			if (optind < argc) {
180*7c478bd9Sstevel@tonic-gate 				errno = 0;
181*7c478bd9Sstevel@tonic-gate 				length = strtol(argv[optind++], &char_p, 10);
182*7c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
183*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
184*7c478bd9Sstevel@tonic-gate 					gettext("error in [nbr of blocks]\n"));
185*7c478bd9Sstevel@tonic-gate 					exit(1);
186*7c478bd9Sstevel@tonic-gate 				}
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 			if ((ret = valid(pathname,
189*7c478bd9Sstevel@tonic-gate 			    s_offset * 512, length * 512)) == 0)
190*7c478bd9Sstevel@tonic-gate 				ret = add(pathname, s_offset, length, flag);
191*7c478bd9Sstevel@tonic-gate 			break;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		case '1':
194*7c478bd9Sstevel@tonic-gate 			flag |= P1FLAG;
195*7c478bd9Sstevel@tonic-gate 			break;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		case '2':
198*7c478bd9Sstevel@tonic-gate 			flag |= P2FLAG;
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		case '?':
202*7c478bd9Sstevel@tonic-gate 			usage();
203*7c478bd9Sstevel@tonic-gate 			exit(1);
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	if (!flag) {
207*7c478bd9Sstevel@tonic-gate 		usage();
208*7c478bd9Sstevel@tonic-gate 		exit(1);
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	return (ret);
211*7c478bd9Sstevel@tonic-gate }
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate static void
215*7c478bd9Sstevel@tonic-gate usage(void)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep);
218*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s -s\n", prognamep);
219*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
220*7c478bd9Sstevel@tonic-gate 			prognamep);
221*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
222*7c478bd9Sstevel@tonic-gate 	    " [nbr of blocks]\n"), prognamep);
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /*
226*7c478bd9Sstevel@tonic-gate  * Implement:
227*7c478bd9Sstevel@tonic-gate  *	#define ctok(x) ((ctob(x))>>10)
228*7c478bd9Sstevel@tonic-gate  * in a machine independent way. (Both assume a click > 1k)
229*7c478bd9Sstevel@tonic-gate  */
230*7c478bd9Sstevel@tonic-gate static size_t
231*7c478bd9Sstevel@tonic-gate ctok(pgcnt_t clicks)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	static int factor = -1;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	if (factor == -1)
236*7c478bd9Sstevel@tonic-gate 		factor = (int)(sysconf(_SC_PAGESIZE) >> 10);
237*7c478bd9Sstevel@tonic-gate 	return ((size_t)(clicks * factor));
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate static int
242*7c478bd9Sstevel@tonic-gate doswap(void)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	struct anoninfo ai;
245*7c478bd9Sstevel@tonic-gate 	pgcnt_t allocated, reserved, available;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	/*
248*7c478bd9Sstevel@tonic-gate 	 * max = total amount of swap space including physical memory
249*7c478bd9Sstevel@tonic-gate 	 * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) +
250*7c478bd9Sstevel@tonic-gate 	 *	availrmem - swapfs_minfree;
251*7c478bd9Sstevel@tonic-gate 	 * ai.ani_free = amount of unallocated anonymous memory
252*7c478bd9Sstevel@tonic-gate 	 *	(ie. = resverved_unallocated + unreserved)
253*7c478bd9Sstevel@tonic-gate 	 * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree);
254*7c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = total amount of reserved anonymous memory
255*7c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = anoninfo.ani_resv;
256*7c478bd9Sstevel@tonic-gate 	 *
257*7c478bd9Sstevel@tonic-gate 	 * allocated = anon memory not free
258*7c478bd9Sstevel@tonic-gate 	 * reserved = anon memory reserved but not allocated
259*7c478bd9Sstevel@tonic-gate 	 * available = anon memory not reserved
260*7c478bd9Sstevel@tonic-gate 	 */
261*7c478bd9Sstevel@tonic-gate 	if (swapctl(SC_AINFO, &ai) == -1) {
262*7c478bd9Sstevel@tonic-gate 		perror(prognamep);
263*7c478bd9Sstevel@tonic-gate 		return (2);
264*7c478bd9Sstevel@tonic-gate 	}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	allocated = ai.ani_max - ai.ani_free;
267*7c478bd9Sstevel@tonic-gate 	reserved = ai.ani_resv - allocated;
268*7c478bd9Sstevel@tonic-gate 	available = ai.ani_max - ai.ani_resv;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	/*
271*7c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
272*7c478bd9Sstevel@tonic-gate 	 * Translations (if any) of these keywords should match with
273*7c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
274*7c478bd9Sstevel@tonic-gate 	 * -s option:  "allocated", "reserved", "used", "available"
275*7c478bd9Sstevel@tonic-gate 	 */
276*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext("total: %luk bytes allocated + %luk reserved = \
277*7c478bd9Sstevel@tonic-gate %luk used, %luk available\n"),
278*7c478bd9Sstevel@tonic-gate 	    ctok(allocated), ctok(reserved), ctok(reserved) + ctok(allocated),
279*7c478bd9Sstevel@tonic-gate 	    ctok(available));
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	return (0);
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate static int
285*7c478bd9Sstevel@tonic-gate list(void)
286*7c478bd9Sstevel@tonic-gate {
287*7c478bd9Sstevel@tonic-gate 	struct swaptable 	*st;
288*7c478bd9Sstevel@tonic-gate 	struct swapent	*swapent;
289*7c478bd9Sstevel@tonic-gate 	int	i;
290*7c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
291*7c478bd9Sstevel@tonic-gate 	char		*path;
292*7c478bd9Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN+1];
293*7c478bd9Sstevel@tonic-gate 	int		num;
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
296*7c478bd9Sstevel@tonic-gate 		perror(prognamep);
297*7c478bd9Sstevel@tonic-gate 		return (2);
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 	if (num == 0) {
300*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No swap devices configured\n"));
301*7c478bd9Sstevel@tonic-gate 		return (1);
302*7c478bd9Sstevel@tonic-gate 	}
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	if ((st = malloc(num * sizeof (swapent_t) + sizeof (int)))
305*7c478bd9Sstevel@tonic-gate 	    == NULL) {
306*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
307*7c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
308*7c478bd9Sstevel@tonic-gate 		perror(prognamep);
309*7c478bd9Sstevel@tonic-gate 		return (2);
310*7c478bd9Sstevel@tonic-gate 	}
311*7c478bd9Sstevel@tonic-gate 	if ((path = malloc(num * MAXPATHLEN)) == NULL) {
312*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
313*7c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
314*7c478bd9Sstevel@tonic-gate 		perror(prognamep);
315*7c478bd9Sstevel@tonic-gate 		return (2);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
318*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
319*7c478bd9Sstevel@tonic-gate 		swapent->ste_path = path;
320*7c478bd9Sstevel@tonic-gate 		path += MAXPATHLEN;
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	st->swt_n = num;
324*7c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
325*7c478bd9Sstevel@tonic-gate 		perror(prognamep);
326*7c478bd9Sstevel@tonic-gate 		return (2);
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	/*
330*7c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
331*7c478bd9Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
332*7c478bd9Sstevel@tonic-gate 	 * alignment of header and output.
333*7c478bd9Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
334*7c478bd9Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
335*7c478bd9Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
336*7c478bd9Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
337*7c478bd9Sstevel@tonic-gate 	 * 	1st & 2nd fields.
338*7c478bd9Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
339*7c478bd9Sstevel@tonic-gate 	 *
340*7c478bd9Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
341*7c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
342*7c478bd9Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
343*7c478bd9Sstevel@tonic-gate 	 */
344*7c478bd9Sstevel@tonic-gate 	(void) printf(
345*7c478bd9Sstevel@tonic-gate 		gettext("swapfile             dev  swaplo blocks   free\n"));
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
348*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
349*7c478bd9Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
350*7c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
351*7c478bd9Sstevel@tonic-gate 				"/dev/%s", swapent->ste_path);
352*7c478bd9Sstevel@tonic-gate 		else
353*7c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
354*7c478bd9Sstevel@tonic-gate 				"%s", swapent->ste_path);
355*7c478bd9Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
356*7c478bd9Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
357*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
358*7c478bd9Sstevel@tonic-gate 					swapent->ste_path);
359*7c478bd9Sstevel@tonic-gate 			else
360*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
361*7c478bd9Sstevel@tonic-gate 					fullpath);
362*7c478bd9Sstevel@tonic-gate 		else {
363*7c478bd9Sstevel@tonic-gate 			if (statbuf.st_mode & (S_IFBLK | S_IFCHR))
364*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
365*7c478bd9Sstevel@tonic-gate 				    fullpath,
366*7c478bd9Sstevel@tonic-gate 				    major(statbuf.st_rdev),
367*7c478bd9Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
368*7c478bd9Sstevel@tonic-gate 			else
369*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
370*7c478bd9Sstevel@tonic-gate 		}
371*7c478bd9Sstevel@tonic-gate 		{
372*7c478bd9Sstevel@tonic-gate 		int diskblks_per_page =
373*7c478bd9Sstevel@tonic-gate 			(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
374*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext(" %6lu %6lu %6lu"), swapent->ste_start,
375*7c478bd9Sstevel@tonic-gate 		    swapent->ste_pages * diskblks_per_page,
376*7c478bd9Sstevel@tonic-gate 		    swapent->ste_free * diskblks_per_page);
377*7c478bd9Sstevel@tonic-gate 		}
378*7c478bd9Sstevel@tonic-gate 		if (swapent->ste_flags & ST_INDEL)
379*7c478bd9Sstevel@tonic-gate 			(void) printf(" INDEL\n");
380*7c478bd9Sstevel@tonic-gate 		else
381*7c478bd9Sstevel@tonic-gate 			(void) printf("\n");
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 	return (0);
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate static void
387*7c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
390*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
391*7c478bd9Sstevel@tonic-gate 	    "run dumpadm(1M) to verify dump configuration\n"));
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate static int
395*7c478bd9Sstevel@tonic-gate delete(char *path, off_t offset)
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	swapres_t swr;
398*7c478bd9Sstevel@tonic-gate 	int fd;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
401*7c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
404*7c478bd9Sstevel@tonic-gate 		switch (errno) {
405*7c478bd9Sstevel@tonic-gate 		case (ENOSYS):
406*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
407*7c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
408*7c478bd9Sstevel@tonic-gate 			    path);
409*7c478bd9Sstevel@tonic-gate 			break;
410*7c478bd9Sstevel@tonic-gate 		default:
411*7c478bd9Sstevel@tonic-gate 			perror(path);
412*7c478bd9Sstevel@tonic-gate 			break;
413*7c478bd9Sstevel@tonic-gate 		}
414*7c478bd9Sstevel@tonic-gate 		return (2);
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/*
418*7c478bd9Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
419*7c478bd9Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
420*7c478bd9Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
421*7c478bd9Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
422*7c478bd9Sstevel@tonic-gate 	 */
423*7c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
424*7c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
427*7c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
428*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
429*7c478bd9Sstevel@tonic-gate 				    "invoking dumpadm(1M) -d swap to "
430*7c478bd9Sstevel@tonic-gate 				    "select new dump device\n"), path);
431*7c478bd9Sstevel@tonic-gate 				/*
432*7c478bd9Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
433*7c478bd9Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
434*7c478bd9Sstevel@tonic-gate 				 */
435*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
438*7c478bd9Sstevel@tonic-gate 					dumpadm_err(gettext(
439*7c478bd9Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
440*7c478bd9Sstevel@tonic-gate 			} else
441*7c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
442*7c478bd9Sstevel@tonic-gate 				"Warning: failed to check dump device"));
443*7c478bd9Sstevel@tonic-gate 		}
444*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
445*7c478bd9Sstevel@tonic-gate 	} else
446*7c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	return (0);
449*7c478bd9Sstevel@tonic-gate }
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate /*
452*7c478bd9Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
453*7c478bd9Sstevel@tonic-gate  */
454*7c478bd9Sstevel@tonic-gate static int
455*7c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	swapres_t swr;
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
460*7c478bd9Sstevel@tonic-gate 	struct statvfs fsb;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	/*
463*7c478bd9Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
464*7c478bd9Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
465*7c478bd9Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
466*7c478bd9Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
467*7c478bd9Sstevel@tonic-gate 	 */
468*7c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
469*7c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
472*7c478bd9Sstevel@tonic-gate 			if (errno == ENODEV)
473*7c478bd9Sstevel@tonic-gate 				have_dumpdev = 0;
474*7c478bd9Sstevel@tonic-gate 			else
475*7c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
476*7c478bd9Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
482*7c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
485*7c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
486*7c478bd9Sstevel@tonic-gate 	swr.sr_length = cnt;
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
489*7c478bd9Sstevel@tonic-gate 		switch (errno) {
490*7c478bd9Sstevel@tonic-gate 		case (ENOSYS):
491*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
492*7c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
493*7c478bd9Sstevel@tonic-gate 			    path);
494*7c478bd9Sstevel@tonic-gate 			break;
495*7c478bd9Sstevel@tonic-gate 		case (EEXIST):
496*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
497*7c478bd9Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
498*7c478bd9Sstevel@tonic-gate 			    path);
499*7c478bd9Sstevel@tonic-gate 			break;
500*7c478bd9Sstevel@tonic-gate 		default:
501*7c478bd9Sstevel@tonic-gate 			perror(path);
502*7c478bd9Sstevel@tonic-gate 			break;
503*7c478bd9Sstevel@tonic-gate 		}
504*7c478bd9Sstevel@tonic-gate 		return (2);
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	/*
508*7c478bd9Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
509*7c478bd9Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
510*7c478bd9Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
511*7c478bd9Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
512*7c478bd9Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
513*7c478bd9Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
514*7c478bd9Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
515*7c478bd9Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
516*7c478bd9Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
517*7c478bd9Sstevel@tonic-gate 	 * we would lose the user's previous setting.
518*7c478bd9Sstevel@tonic-gate 	 */
519*7c478bd9Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
520*7c478bd9Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 		(void) printf(
523*7c478bd9Sstevel@tonic-gate 			gettext("operating system crash dump was previously "
524*7c478bd9Sstevel@tonic-gate 		    "disabled --\ninvoking dumpadm(1M) -d swap to select "
525*7c478bd9Sstevel@tonic-gate 		    "new dump device\n"));
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
528*7c478bd9Sstevel@tonic-gate 			dumpadm_err(gettext(
529*7c478bd9Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
530*7c478bd9Sstevel@tonic-gate 	}
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	return (0);
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate static int
536*7c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
537*7c478bd9Sstevel@tonic-gate {
538*7c478bd9Sstevel@tonic-gate 	struct stat64		f;
539*7c478bd9Sstevel@tonic-gate 	struct statvfs64	fs;
540*7c478bd9Sstevel@tonic-gate 	off_t		need;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
543*7c478bd9Sstevel@tonic-gate 		(void) perror(pathname);
544*7c478bd9Sstevel@tonic-gate 		return (errno);
545*7c478bd9Sstevel@tonic-gate 	}
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
548*7c478bd9Sstevel@tonic-gate 		S_ISBLK(f.st_mode))) {
549*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
550*7c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
551*7c478bd9Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
552*7c478bd9Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
553*7c478bd9Sstevel@tonic-gate 		    pathname);
554*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
555*7c478bd9Sstevel@tonic-gate 	}
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
558*7c478bd9Sstevel@tonic-gate 		if (length == 0)
559*7c478bd9Sstevel@tonic-gate 			length = (off_t)f.st_size;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 		/*
562*7c478bd9Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
563*7c478bd9Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
564*7c478bd9Sstevel@tonic-gate 		 */
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
567*7c478bd9Sstevel@tonic-gate 		    f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) {
568*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
569*7c478bd9Sstevel@tonic-gate 			    pathname);
570*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
571*7c478bd9Sstevel@tonic-gate 		}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 		if (offset < 0) {
574*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
575*7c478bd9Sstevel@tonic-gate 				gettext("%s: low block is invalid\n"),
576*7c478bd9Sstevel@tonic-gate 				pathname);
577*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
578*7c478bd9Sstevel@tonic-gate 		}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 		/*
583*7c478bd9Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
584*7c478bd9Sstevel@tonic-gate 		 * Note:
585*7c478bd9Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
586*7c478bd9Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
587*7c478bd9Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
588*7c478bd9Sstevel@tonic-gate 		 *  about the underlying storage implementation.
589*7c478bd9Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
590*7c478bd9Sstevel@tonic-gate 		 *  there must be a hole.
591*7c478bd9Sstevel@tonic-gate 		 */
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 		if (need > f.st_blocks) {
594*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
595*7c478bd9Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
596*7c478bd9Sstevel@tonic-gate 			    pathname);
597*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
598*7c478bd9Sstevel@tonic-gate 		}
599*7c478bd9Sstevel@tonic-gate 	}
600*7c478bd9Sstevel@tonic-gate 	/*
601*7c478bd9Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
602*7c478bd9Sstevel@tonic-gate 	 * no meaningful checking can be done.
603*7c478bd9Sstevel@tonic-gate 	 */
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	return (0);
606*7c478bd9Sstevel@tonic-gate }
607