xref: /titanic_51/usr/src/cmd/swap/swap.c (revision 4bc0a2ef2b7ba50a7a717e7ddbf31472ad28e358)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
233e1bd7a2Ssjelinek  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * 	Swap administrative interface
447c478bd9Sstevel@tonic-gate  *	Used to add/delete/list swap devices.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include	<sys/types.h>
487c478bd9Sstevel@tonic-gate #include	<sys/dumpadm.h>
497c478bd9Sstevel@tonic-gate #include	<string.h>
507c478bd9Sstevel@tonic-gate #include	<stdio.h>
517c478bd9Sstevel@tonic-gate #include	<stdlib.h>
527c478bd9Sstevel@tonic-gate #include	<unistd.h>
537c478bd9Sstevel@tonic-gate #include	<errno.h>
547c478bd9Sstevel@tonic-gate #include	<sys/param.h>
557c478bd9Sstevel@tonic-gate #include	<dirent.h>
567c478bd9Sstevel@tonic-gate #include	<sys/swap.h>
577c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
587c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
597c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
607c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
617c478bd9Sstevel@tonic-gate #include	<sys/uadmin.h>
627c478bd9Sstevel@tonic-gate #include	<vm/anon.h>
637c478bd9Sstevel@tonic-gate #include	<fcntl.h>
647c478bd9Sstevel@tonic-gate #include	<locale.h>
657c478bd9Sstevel@tonic-gate #include	<libintl.h>
663e1bd7a2Ssjelinek #include	<libdiskmgt.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	LFLAG	0x01	/* swap -l (list swap devices) */
697c478bd9Sstevel@tonic-gate #define	DFLAG	0x02	/* swap -d (delete swap device) */
707c478bd9Sstevel@tonic-gate #define	AFLAG	0x04	/* swap -a (add swap device) */
717c478bd9Sstevel@tonic-gate #define	SFLAG	0x08	/* swap -s (swap info summary) */
727c478bd9Sstevel@tonic-gate #define	P1FLAG	0x10	/* swap -1 (swapadd pass1; do not modify dump device) */
737c478bd9Sstevel@tonic-gate #define	P2FLAG	0x20	/* swap -2 (swapadd pass2; do not modify dump device) */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static char *prognamep;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int add(char *, off_t, off_t, int);
787c478bd9Sstevel@tonic-gate static int delete(char *, off_t);
797c478bd9Sstevel@tonic-gate static void usage(void);
807c478bd9Sstevel@tonic-gate static int doswap(void);
817c478bd9Sstevel@tonic-gate static int valid(char *, off_t, off_t);
827c478bd9Sstevel@tonic-gate static int list(void);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate int
857c478bd9Sstevel@tonic-gate main(int argc, char **argv)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	int c, flag = 0;
887c478bd9Sstevel@tonic-gate 	int ret;
893e1bd7a2Ssjelinek 	int error = 0;
907c478bd9Sstevel@tonic-gate 	off_t s_offset = 0;
917c478bd9Sstevel@tonic-gate 	off_t length = 0;
927c478bd9Sstevel@tonic-gate 	char *pathname;
933e1bd7a2Ssjelinek 	char *msg;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
987c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	prognamep = argv[0];
1037c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1047c478bd9Sstevel@tonic-gate 		usage();
1057c478bd9Sstevel@tonic-gate 		exit(1);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "lsd:a:12")) != EOF) {
1097c478bd9Sstevel@tonic-gate 		char *char_p;
1107c478bd9Sstevel@tonic-gate 		switch (c) {
1117c478bd9Sstevel@tonic-gate 		case 'l': 	/* list all the swap devices */
1127c478bd9Sstevel@tonic-gate 			if (argc != 2 || flag) {
1137c478bd9Sstevel@tonic-gate 				usage();
1147c478bd9Sstevel@tonic-gate 				exit(1);
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 			flag |= LFLAG;
1177c478bd9Sstevel@tonic-gate 			ret = list();
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case 's':
1207c478bd9Sstevel@tonic-gate 			if (argc != 2 || flag) {
1217c478bd9Sstevel@tonic-gate 				usage();
1227c478bd9Sstevel@tonic-gate 				exit(1);
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate 			flag |= SFLAG;
1257c478bd9Sstevel@tonic-gate 			ret = doswap();
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		case 'd':
1287c478bd9Sstevel@tonic-gate 			/*
1297c478bd9Sstevel@tonic-gate 			 * The argument for starting offset is optional.
1307c478bd9Sstevel@tonic-gate 			 * If no argument is specified, the entire swap file
1317c478bd9Sstevel@tonic-gate 			 * is added although this will fail if a non-zero
1327c478bd9Sstevel@tonic-gate 			 * starting offset was specified when added.
1337c478bd9Sstevel@tonic-gate 			 */
1347c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 1 || flag != 0) {
1357c478bd9Sstevel@tonic-gate 				usage();
1367c478bd9Sstevel@tonic-gate 				exit(1);
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 			flag |= DFLAG;
1397c478bd9Sstevel@tonic-gate 			pathname = optarg;
1407c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1417c478bd9Sstevel@tonic-gate 				errno = 0;
1427c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1437c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1457c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1467c478bd9Sstevel@tonic-gate 					exit(1);
1477c478bd9Sstevel@tonic-gate 				}
1487c478bd9Sstevel@tonic-gate 			}
1497c478bd9Sstevel@tonic-gate 			ret = delete(pathname, s_offset);
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		case 'a':
1537c478bd9Sstevel@tonic-gate 			/*
1547c478bd9Sstevel@tonic-gate 			 * The arguments for starting offset and number of
1557c478bd9Sstevel@tonic-gate 			 * blocks are optional.  If only the starting offset
1567c478bd9Sstevel@tonic-gate 			 * is specified, all the blocks to the end of the swap
1577c478bd9Sstevel@tonic-gate 			 * file will be added.  If no starting offset is
1587c478bd9Sstevel@tonic-gate 			 * specified, the entire swap file is assumed.
1597c478bd9Sstevel@tonic-gate 			 */
1607c478bd9Sstevel@tonic-gate 			if ((argc - optind) > 2 ||
1617c478bd9Sstevel@tonic-gate 			    (flag & ~(P1FLAG | P2FLAG)) != 0) {
1627c478bd9Sstevel@tonic-gate 				usage();
1637c478bd9Sstevel@tonic-gate 				exit(1);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			if (*optarg != '/') {
1667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1677c478bd9Sstevel@tonic-gate 				    gettext("%s: path must be absolute\n"),
1687c478bd9Sstevel@tonic-gate 				    prognamep);
1697c478bd9Sstevel@tonic-gate 				exit(1);
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			flag |= AFLAG;
1727c478bd9Sstevel@tonic-gate 			pathname = optarg;
1737c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1747c478bd9Sstevel@tonic-gate 				errno = 0;
1757c478bd9Sstevel@tonic-gate 				s_offset = strtol(argv[optind++], &char_p, 10);
1767c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1777c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1787c478bd9Sstevel@tonic-gate 					    gettext("error in [low block]\n"));
1797c478bd9Sstevel@tonic-gate 					exit(1);
1807c478bd9Sstevel@tonic-gate 				}
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			if (optind < argc) {
1837c478bd9Sstevel@tonic-gate 				errno = 0;
1847c478bd9Sstevel@tonic-gate 				length = strtol(argv[optind++], &char_p, 10);
1857c478bd9Sstevel@tonic-gate 				if (errno != 0 || *char_p != '\0') {
1867c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1877c478bd9Sstevel@tonic-gate 					gettext("error in [nbr of blocks]\n"));
1887c478bd9Sstevel@tonic-gate 					exit(1);
1897c478bd9Sstevel@tonic-gate 				}
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case '1':
1947c478bd9Sstevel@tonic-gate 			flag |= P1FLAG;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case '2':
1987c478bd9Sstevel@tonic-gate 			flag |= P2FLAG;
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		case '?':
2027c478bd9Sstevel@tonic-gate 			usage();
2037c478bd9Sstevel@tonic-gate 			exit(1);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2063e1bd7a2Ssjelinek 	/*
2073e1bd7a2Ssjelinek 	 * do the add here. Check for in use prior to add.
2083e1bd7a2Ssjelinek 	 * The values for length and offset are set above.
2093e1bd7a2Ssjelinek 	 */
2103e1bd7a2Ssjelinek 	if (flag & AFLAG) {
2113e1bd7a2Ssjelinek 		/*
2123e1bd7a2Ssjelinek 		 * If device is in use for a swap device, print message
2133e1bd7a2Ssjelinek 		 * and exit.
2143e1bd7a2Ssjelinek 		 */
2153e1bd7a2Ssjelinek 		if (dm_inuse(pathname, &msg, DM_WHO_SWAP, &error) ||
2163e1bd7a2Ssjelinek 		    error) {
2173e1bd7a2Ssjelinek 			if (error != 0) {
2183e1bd7a2Ssjelinek 				(void) fprintf(stderr, gettext("Error occurred"
2193e1bd7a2Ssjelinek 				    " with device in use checking: %s\n"),
2203e1bd7a2Ssjelinek 				    strerror(error));
2213e1bd7a2Ssjelinek 			} else {
2223e1bd7a2Ssjelinek 				(void) fprintf(stderr, "%s", msg);
2233e1bd7a2Ssjelinek 				free(msg);
2243e1bd7a2Ssjelinek 				exit(1);
2253e1bd7a2Ssjelinek 			}
2263e1bd7a2Ssjelinek 		}
2273e1bd7a2Ssjelinek 		if ((ret = valid(pathname,
2283e1bd7a2Ssjelinek 		    s_offset * 512, length * 512)) == 0) {
2293e1bd7a2Ssjelinek 		    ret = add(pathname, s_offset, length, flag);
2303e1bd7a2Ssjelinek 		}
2313e1bd7a2Ssjelinek 	}
2327c478bd9Sstevel@tonic-gate 	if (!flag) {
2337c478bd9Sstevel@tonic-gate 		usage();
2347c478bd9Sstevel@tonic-gate 		exit(1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return (ret);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static void
2417c478bd9Sstevel@tonic-gate usage(void)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep);
2447c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s -s\n", prognamep);
2457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"),
2467c478bd9Sstevel@tonic-gate 			prognamep);
2477c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -a <file name> [low block]"
2487c478bd9Sstevel@tonic-gate 	    " [nbr of blocks]\n"), prognamep);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Implement:
2537c478bd9Sstevel@tonic-gate  *	#define ctok(x) ((ctob(x))>>10)
2547c478bd9Sstevel@tonic-gate  * in a machine independent way. (Both assume a click > 1k)
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate static size_t
2577c478bd9Sstevel@tonic-gate ctok(pgcnt_t clicks)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	static int factor = -1;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (factor == -1)
2627c478bd9Sstevel@tonic-gate 		factor = (int)(sysconf(_SC_PAGESIZE) >> 10);
2637c478bd9Sstevel@tonic-gate 	return ((size_t)(clicks * factor));
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static int
2687c478bd9Sstevel@tonic-gate doswap(void)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	struct anoninfo ai;
2717c478bd9Sstevel@tonic-gate 	pgcnt_t allocated, reserved, available;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * max = total amount of swap space including physical memory
2757c478bd9Sstevel@tonic-gate 	 * ai.ani_max = MAX(anoninfo.ani_resv, anoninfo.ani_max) +
2767c478bd9Sstevel@tonic-gate 	 *	availrmem - swapfs_minfree;
2777c478bd9Sstevel@tonic-gate 	 * ai.ani_free = amount of unallocated anonymous memory
2787c478bd9Sstevel@tonic-gate 	 *	(ie. = resverved_unallocated + unreserved)
2797c478bd9Sstevel@tonic-gate 	 * ai.ani_free = anoninfo.ani_free + (availrmem - swapfs_minfree);
2807c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = total amount of reserved anonymous memory
2817c478bd9Sstevel@tonic-gate 	 * ai.ani_resv = anoninfo.ani_resv;
2827c478bd9Sstevel@tonic-gate 	 *
2837c478bd9Sstevel@tonic-gate 	 * allocated = anon memory not free
2847c478bd9Sstevel@tonic-gate 	 * reserved = anon memory reserved but not allocated
2857c478bd9Sstevel@tonic-gate 	 * available = anon memory not reserved
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if (swapctl(SC_AINFO, &ai) == -1) {
2887c478bd9Sstevel@tonic-gate 		perror(prognamep);
2897c478bd9Sstevel@tonic-gate 		return (2);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	allocated = ai.ani_max - ai.ani_free;
2937c478bd9Sstevel@tonic-gate 	reserved = ai.ani_resv - allocated;
2947c478bd9Sstevel@tonic-gate 	available = ai.ani_max - ai.ani_resv;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
2987c478bd9Sstevel@tonic-gate 	 * Translations (if any) of these keywords should match with
2997c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
3007c478bd9Sstevel@tonic-gate 	 * -s option:  "allocated", "reserved", "used", "available"
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	(void) printf(gettext("total: %luk bytes allocated + %luk reserved = \
3037c478bd9Sstevel@tonic-gate %luk used, %luk available\n"),
3047c478bd9Sstevel@tonic-gate 	    ctok(allocated), ctok(reserved), ctok(reserved) + ctok(allocated),
3057c478bd9Sstevel@tonic-gate 	    ctok(available));
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	return (0);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static int
3117c478bd9Sstevel@tonic-gate list(void)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	struct swaptable 	*st;
3147c478bd9Sstevel@tonic-gate 	struct swapent	*swapent;
3157c478bd9Sstevel@tonic-gate 	int	i;
3167c478bd9Sstevel@tonic-gate 	struct stat64 statbuf;
3177c478bd9Sstevel@tonic-gate 	char		*path;
3187c478bd9Sstevel@tonic-gate 	char		fullpath[MAXPATHLEN+1];
3197c478bd9Sstevel@tonic-gate 	int		num;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_GETNSWP, NULL)) == -1) {
3227c478bd9Sstevel@tonic-gate 		perror(prognamep);
3237c478bd9Sstevel@tonic-gate 		return (2);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	if (num == 0) {
3267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No swap devices configured\n"));
3277c478bd9Sstevel@tonic-gate 		return (1);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if ((st = malloc(num * sizeof (swapent_t) + sizeof (int)))
3317c478bd9Sstevel@tonic-gate 	    == NULL) {
3327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3337c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
3347c478bd9Sstevel@tonic-gate 		perror(prognamep);
3357c478bd9Sstevel@tonic-gate 		return (2);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 	if ((path = malloc(num * MAXPATHLEN)) == NULL) {
3387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3397c478bd9Sstevel@tonic-gate 			gettext("Malloc failed. Please try later.\n"));
3407c478bd9Sstevel@tonic-gate 		perror(prognamep);
3417c478bd9Sstevel@tonic-gate 		return (2);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
3447c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
3457c478bd9Sstevel@tonic-gate 		swapent->ste_path = path;
3467c478bd9Sstevel@tonic-gate 		path += MAXPATHLEN;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	st->swt_n = num;
3507c478bd9Sstevel@tonic-gate 	if ((num = swapctl(SC_LIST, st)) == -1) {
3517c478bd9Sstevel@tonic-gate 		perror(prognamep);
3527c478bd9Sstevel@tonic-gate 		return (2);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
3577c478bd9Sstevel@tonic-gate 	 * Following translations for "swap -l" should account for for
3587c478bd9Sstevel@tonic-gate 	 * alignment of header and output.
3597c478bd9Sstevel@tonic-gate 	 * The first translation is for the header.  If the alignment
3607c478bd9Sstevel@tonic-gate 	 *	of the header changes, change the next 5 formats as needed
3617c478bd9Sstevel@tonic-gate 	 *	to make alignment of output agree with alignment of the header.
3627c478bd9Sstevel@tonic-gate 	 * The next four translations are four cases for printing the
3637c478bd9Sstevel@tonic-gate 	 * 	1st & 2nd fields.
3647c478bd9Sstevel@tonic-gate 	 * The next translation is for printing the 3rd, 4th & 5th fields.
3657c478bd9Sstevel@tonic-gate 	 *
3667c478bd9Sstevel@tonic-gate 	 * Translations (if any) of the following keywords should match the
3677c478bd9Sstevel@tonic-gate 	 * translations (if any) of the swap.1M man page keywords for
3687c478bd9Sstevel@tonic-gate 	 * -l option:  "swapfile", "dev", "swaplo", "blocks", "free"
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	(void) printf(
3717c478bd9Sstevel@tonic-gate 		gettext("swapfile             dev  swaplo blocks   free\n"));
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	swapent = st->swt_ent;
3747c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, swapent++) {
3757c478bd9Sstevel@tonic-gate 		if (*swapent->ste_path != '/')
3767c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
3777c478bd9Sstevel@tonic-gate 				"/dev/%s", swapent->ste_path);
3787c478bd9Sstevel@tonic-gate 		else
3797c478bd9Sstevel@tonic-gate 			(void) snprintf(fullpath, sizeof (fullpath),
3807c478bd9Sstevel@tonic-gate 				"%s", swapent->ste_path);
3817c478bd9Sstevel@tonic-gate 		if (stat64(fullpath, &statbuf) < 0)
3827c478bd9Sstevel@tonic-gate 			if (*swapent->ste_path != '/')
3837c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "),
3847c478bd9Sstevel@tonic-gate 					swapent->ste_path);
3857c478bd9Sstevel@tonic-gate 			else
3867c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s ?,? "),
3877c478bd9Sstevel@tonic-gate 					fullpath);
3887c478bd9Sstevel@tonic-gate 		else {
389*4bc0a2efScasper 			if (S_ISBLK(statbuf.st_mode) ||
390*4bc0a2efScasper 			    S_ISCHR(statbuf.st_mode)) {
3917c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-19s %2lu,%-2lu"),
3927c478bd9Sstevel@tonic-gate 				    fullpath,
3937c478bd9Sstevel@tonic-gate 				    major(statbuf.st_rdev),
3947c478bd9Sstevel@tonic-gate 				    minor(statbuf.st_rdev));
395*4bc0a2efScasper 			} else {
3967c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%-20s  -  "), fullpath);
3977c478bd9Sstevel@tonic-gate 			}
398*4bc0a2efScasper 		}
3997c478bd9Sstevel@tonic-gate 		{
4007c478bd9Sstevel@tonic-gate 		int diskblks_per_page =
4017c478bd9Sstevel@tonic-gate 			(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT);
4027c478bd9Sstevel@tonic-gate 		(void) printf(gettext(" %6lu %6lu %6lu"), swapent->ste_start,
4037c478bd9Sstevel@tonic-gate 		    swapent->ste_pages * diskblks_per_page,
4047c478bd9Sstevel@tonic-gate 		    swapent->ste_free * diskblks_per_page);
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 		if (swapent->ste_flags & ST_INDEL)
4077c478bd9Sstevel@tonic-gate 			(void) printf(" INDEL\n");
4087c478bd9Sstevel@tonic-gate 		else
4097c478bd9Sstevel@tonic-gate 			(void) printf("\n");
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	return (0);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static void
4157c478bd9Sstevel@tonic-gate dumpadm_err(const char *warning)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s):\n", warning, strerror(errno));
4187c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
4197c478bd9Sstevel@tonic-gate 	    "run dumpadm(1M) to verify dump configuration\n"));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static int
4237c478bd9Sstevel@tonic-gate delete(char *path, off_t offset)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	swapres_t swr;
4267c478bd9Sstevel@tonic-gate 	int fd;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
4297c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (swapctl(SC_REMOVE, &swr) < 0) {
4327c478bd9Sstevel@tonic-gate 		switch (errno) {
4337c478bd9Sstevel@tonic-gate 		case (ENOSYS):
4347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4357c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
4367c478bd9Sstevel@tonic-gate 			    path);
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 		default:
4397c478bd9Sstevel@tonic-gate 			perror(path);
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		return (2);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * If our swap -d succeeded, open up /dev/dump and ask what the dump
4477c478bd9Sstevel@tonic-gate 	 * device is set to.  If this returns ENODEV, we just deleted the
4487c478bd9Sstevel@tonic-gate 	 * dump device, so try to change the dump device to another swap
4497c478bd9Sstevel@tonic-gate 	 * device.  We do this by firing up /usr/sbin/dumpadm -ud swap.
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
4527c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
4557c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
4567c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s was dump device --\n"
4577c478bd9Sstevel@tonic-gate 				    "invoking dumpadm(1M) -d swap to "
4587c478bd9Sstevel@tonic-gate 				    "select new dump device\n"), path);
4597c478bd9Sstevel@tonic-gate 				/*
4607c478bd9Sstevel@tonic-gate 				 * Close /dev/dump prior to executing dumpadm
4617c478bd9Sstevel@tonic-gate 				 * since /dev/dump mandates exclusive open.
4627c478bd9Sstevel@tonic-gate 				 */
4637c478bd9Sstevel@tonic-gate 				(void) close(fd);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 				if (system("/usr/sbin/dumpadm -ud swap") == -1)
4667c478bd9Sstevel@tonic-gate 					dumpadm_err(gettext(
4677c478bd9Sstevel@tonic-gate 				"Warning: failed to execute dumpadm -d swap"));
4687c478bd9Sstevel@tonic-gate 			} else
4697c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
4707c478bd9Sstevel@tonic-gate 				"Warning: failed to check dump device"));
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		(void) close(fd);
4737c478bd9Sstevel@tonic-gate 	} else
4747c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (0);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * swapres_t structure units are in 512-blocks
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate static int
4837c478bd9Sstevel@tonic-gate add(char *path, off_t offset, off_t cnt, int flags)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	swapres_t swr;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	int fd, have_dumpdev = 1;
4887c478bd9Sstevel@tonic-gate 	struct statvfs fsb;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 * Before adding swap, we first check to see if we have a dump
4927c478bd9Sstevel@tonic-gate 	 * device configured.  If we don't (errno == ENODEV), and if
4937c478bd9Sstevel@tonic-gate 	 * our SC_ADD is successful, then run /usr/sbin/dumpadm -ud swap
4947c478bd9Sstevel@tonic-gate 	 * to attempt to reconfigure the dump device to the new swap.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/dump", O_RDONLY)) >= 0) {
4977c478bd9Sstevel@tonic-gate 		char dumpdev[MAXPATHLEN];
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DIOCGETDEV, dumpdev) == -1) {
5007c478bd9Sstevel@tonic-gate 			if (errno == ENODEV)
5017c478bd9Sstevel@tonic-gate 				have_dumpdev = 0;
5027c478bd9Sstevel@tonic-gate 			else
5037c478bd9Sstevel@tonic-gate 				dumpadm_err(gettext(
5047c478bd9Sstevel@tonic-gate 				    "Warning: failed to check dump device"));
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		(void) close(fd);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	} else if (!(flags & P1FLAG))
5107c478bd9Sstevel@tonic-gate 		dumpadm_err(gettext("Warning: failed to open /dev/dump"));
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	swr.sr_name = path;
5137c478bd9Sstevel@tonic-gate 	swr.sr_start = offset;
5147c478bd9Sstevel@tonic-gate 	swr.sr_length = cnt;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if (swapctl(SC_ADD, &swr) < 0) {
5177c478bd9Sstevel@tonic-gate 		switch (errno) {
5187c478bd9Sstevel@tonic-gate 		case (ENOSYS):
5197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5207c478bd9Sstevel@tonic-gate 			    "%s: Invalid operation for this filesystem type\n"),
5217c478bd9Sstevel@tonic-gate 			    path);
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		case (EEXIST):
5247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
5257c478bd9Sstevel@tonic-gate 			    "%s: Overlapping swap files are not allowed\n"),
5267c478bd9Sstevel@tonic-gate 			    path);
5277c478bd9Sstevel@tonic-gate 			break;
5287c478bd9Sstevel@tonic-gate 		default:
5297c478bd9Sstevel@tonic-gate 			perror(path);
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 		return (2);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * If the swapctl worked and we don't have a dump device, and /etc
5377c478bd9Sstevel@tonic-gate 	 * is part of a writeable filesystem, then run dumpadm -ud swap.
5387c478bd9Sstevel@tonic-gate 	 * If /etc (presumably part of /) is still mounted read-only, then
5397c478bd9Sstevel@tonic-gate 	 * dumpadm will fail to write its config file, so there's no point
5407c478bd9Sstevel@tonic-gate 	 * running it now.  This also avoids spurious messages during boot
5417c478bd9Sstevel@tonic-gate 	 * when the first swapadd takes place, at which point / is still ro.
5427c478bd9Sstevel@tonic-gate 	 * Similarly, if swapadd invoked us with -1 or -2 (but root is
5437c478bd9Sstevel@tonic-gate 	 * writeable), we don't want to modify the dump device because
5447c478bd9Sstevel@tonic-gate 	 * /etc/init.d/savecore has yet to execute; if we run dumpadm now
5457c478bd9Sstevel@tonic-gate 	 * we would lose the user's previous setting.
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	if (!have_dumpdev && !(flags & (P1FLAG | P2FLAG)) &&
5487c478bd9Sstevel@tonic-gate 	    statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) {
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		(void) printf(
5517c478bd9Sstevel@tonic-gate 			gettext("operating system crash dump was previously "
5527c478bd9Sstevel@tonic-gate 		    "disabled --\ninvoking dumpadm(1M) -d swap to select "
5537c478bd9Sstevel@tonic-gate 		    "new dump device\n"));
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (system("/usr/sbin/dumpadm -ud swap") == -1)
5567c478bd9Sstevel@tonic-gate 			dumpadm_err(gettext(
5577c478bd9Sstevel@tonic-gate 			    "Warning: failed to execute dumpadm -d swap"));
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	return (0);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static int
5647c478bd9Sstevel@tonic-gate valid(char *pathname, off_t offset, off_t length)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	struct stat64		f;
5677c478bd9Sstevel@tonic-gate 	struct statvfs64	fs;
5687c478bd9Sstevel@tonic-gate 	off_t		need;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (stat64(pathname, &f) < 0 || statvfs64(pathname,  &fs) < 0) {
5717c478bd9Sstevel@tonic-gate 		(void) perror(pathname);
5727c478bd9Sstevel@tonic-gate 		return (errno);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) ||
5767c478bd9Sstevel@tonic-gate 		S_ISBLK(f.st_mode))) {
5777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5787c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not valid for swapping.\n"
5797c478bd9Sstevel@tonic-gate 		    "It must be a block device or a regular file with the\n"
5807c478bd9Sstevel@tonic-gate 		    "\"save user text on execution\" bit set.\n"),
5817c478bd9Sstevel@tonic-gate 		    pathname);
5827c478bd9Sstevel@tonic-gate 		return (EINVAL);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (S_ISREG(f.st_mode)) {
5867c478bd9Sstevel@tonic-gate 		if (length == 0)
5877c478bd9Sstevel@tonic-gate 			length = (off_t)f.st_size;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * "f.st_blocks < 8" because the first eight
5917c478bd9Sstevel@tonic-gate 		 * 512-byte sectors are always skipped
5927c478bd9Sstevel@tonic-gate 		 */
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if (f.st_size < (length - offset) || f.st_size == 0 ||
5957c478bd9Sstevel@tonic-gate 		    f.st_size > MAXOFF_T || f.st_blocks < 8 || length < 0) {
5967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: size is invalid\n"),
5977c478bd9Sstevel@tonic-gate 			    pathname);
5987c478bd9Sstevel@tonic-gate 			return (EINVAL);
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		if (offset < 0) {
6027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6037c478bd9Sstevel@tonic-gate 				gettext("%s: low block is invalid\n"),
6047c478bd9Sstevel@tonic-gate 				pathname);
6057c478bd9Sstevel@tonic-gate 			return (EINVAL);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		need = roundup(length, fs.f_bsize) / DEV_BSIZE;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * "need > f.st_blocks" to account for indirect blocks
6127c478bd9Sstevel@tonic-gate 		 * Note:
6137c478bd9Sstevel@tonic-gate 		 *  This can be fooled by a file large enough to
6147c478bd9Sstevel@tonic-gate 		 *  contain indirect blocks that also contains holes.
6157c478bd9Sstevel@tonic-gate 		 *  However, we don't know (and don't want to know)
6167c478bd9Sstevel@tonic-gate 		 *  about the underlying storage implementation.
6177c478bd9Sstevel@tonic-gate 		 *  But, if it doesn't have at least this many blocks,
6187c478bd9Sstevel@tonic-gate 		 *  there must be a hole.
6197c478bd9Sstevel@tonic-gate 		 */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		if (need > f.st_blocks) {
6227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6237c478bd9Sstevel@tonic-gate 			    "\"%s\" may contain holes - can't swap on it.\n"),
6247c478bd9Sstevel@tonic-gate 			    pathname);
6257c478bd9Sstevel@tonic-gate 			return (EINVAL);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * else, we cannot get st_size for S_ISBLK device and
6307c478bd9Sstevel@tonic-gate 	 * no meaningful checking can be done.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	return (0);
6347c478bd9Sstevel@tonic-gate }
635