xref: /illumos-gate/usr/src/cmd/allocate/dminfo.c (revision 2a8bcb4e)
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
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  *
21*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <locale.h>
267c478bd9Sstevel@tonic-gate #include <stdio.h>
27*45916cd2Sjpk #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <malloc.h>
327c478bd9Sstevel@tonic-gate #include <memory.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/file.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <bsm/devices.h>
387c478bd9Sstevel@tonic-gate #define	DMPFILE	"/etc/security/device_maps"
397c478bd9Sstevel@tonic-gate #define	RETRY_SLEEP	6
407c478bd9Sstevel@tonic-gate #define	RETRY_COUNT	10
417c478bd9Sstevel@tonic-gate #define	EINVOKE	2
427c478bd9Sstevel@tonic-gate #define	EFAIL 1
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
45bdcaf822Sbasabi #define	TEXT_DOMAIN "SUNW_BSM_DMINFO"
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern off_t lseek();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate char	*getdmapfield();
517c478bd9Sstevel@tonic-gate char	*getdmapdfield();
527c478bd9Sstevel@tonic-gate static void	printdmapent();
537c478bd9Sstevel@tonic-gate static void	dmapi_err();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static char	*prog_name;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * printdmapent(dmapp) prints a devmap_t structure pointed to by dmapp.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static void
printdmapent(dmapp)617c478bd9Sstevel@tonic-gate printdmapent(dmapp)
627c478bd9Sstevel@tonic-gate 	devmap_t *dmapp;
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	(void) printf("%s:", dmapp->dmap_devname);
657c478bd9Sstevel@tonic-gate 	(void) printf("%s:", dmapp->dmap_devtype);
66*45916cd2Sjpk 	(void) printf("%s", dmapp->dmap_devlist);
677c478bd9Sstevel@tonic-gate 	(void) printf("\n");
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
72bdcaf822Sbasabi  * dmapi_err(exit_code,err_msg) prints message pointed to by err_msg to
73bdcaf822Sbasabi  * stderr. Then prints usage message to stderr. Then exits program with
74bdcaf822Sbasabi  * exit_code.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate static void
dmapi_err(int exit_code,char * err_msg)78*45916cd2Sjpk dmapi_err(int exit_code, char *err_msg)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	if (err_msg != NULL) {
817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dmapinfo:%s\n", err_msg);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	if (exit_code == EINVOKE) {
847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
857c478bd9Sstevel@tonic-gate 			"Usage: %s [-v] [-a] [-f filename] %s\n",
867c478bd9Sstevel@tonic-gate 			prog_name,
877c478bd9Sstevel@tonic-gate 			"[-d device ...]");
887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
897c478bd9Sstevel@tonic-gate 			"       %s [-v] [-a] [-f filename] %s\n",
907c478bd9Sstevel@tonic-gate 			prog_name,
917c478bd9Sstevel@tonic-gate 			"[-n name ...]");
927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
937c478bd9Sstevel@tonic-gate 			"       %s [-v] [-a] [-f filename] %s\n",
947c478bd9Sstevel@tonic-gate 			prog_name,
957c478bd9Sstevel@tonic-gate 			"[-t type ...]");
967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
977c478bd9Sstevel@tonic-gate 			"       %s [-v] [-a] [-f filename] %s\n",
987c478bd9Sstevel@tonic-gate 			prog_name,
997c478bd9Sstevel@tonic-gate 			"[-u Entry]");
1007c478bd9Sstevel@tonic-gate 	}
101*45916cd2Sjpk 
1027c478bd9Sstevel@tonic-gate 	exit(exit_code);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
105bdcaf822Sbasabi int
main(int argc,char ** argv)106*45916cd2Sjpk main(int argc, char **argv)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	devmap_t *dmapp;
1097c478bd9Sstevel@tonic-gate 	devmap_t dmap;
1107c478bd9Sstevel@tonic-gate 	char	*mptr;
1117c478bd9Sstevel@tonic-gate 	char	*tptr;
1127c478bd9Sstevel@tonic-gate 	char	*nptr;
1137c478bd9Sstevel@tonic-gate 	char	*filename = DMPFILE;
1147c478bd9Sstevel@tonic-gate 	int	name = 0;
1157c478bd9Sstevel@tonic-gate 	int	device = 0;
1167c478bd9Sstevel@tonic-gate 	int	file = 0;
1177c478bd9Sstevel@tonic-gate 	int	verbose = 0;
1187c478bd9Sstevel@tonic-gate 	int	cntr = 0;
1197c478bd9Sstevel@tonic-gate 	int	any = 0;
1207c478bd9Sstevel@tonic-gate 	int	update = 0;
1217c478bd9Sstevel@tonic-gate 	int	tp = 0;
1227c478bd9Sstevel@tonic-gate 	int	des;
1237c478bd9Sstevel@tonic-gate 	int	status;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/* Internationalization */
1267c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1277c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * point prog_name to invocation name
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if ((tptr = strrchr(*argv, '/')) != NULL)
1337c478bd9Sstevel@tonic-gate 		prog_name = ++tptr;
1347c478bd9Sstevel@tonic-gate 		else
1357c478bd9Sstevel@tonic-gate 		prog_name = *argv;
1367c478bd9Sstevel@tonic-gate 	argc--;
1377c478bd9Sstevel@tonic-gate 	argv++;
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * parse arguments
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	while ((argc >= 1) && (argv[0][0] == '-')) {
1427c478bd9Sstevel@tonic-gate 		switch (argv[0][1]) {
1437c478bd9Sstevel@tonic-gate 		case 'a':
1447c478bd9Sstevel@tonic-gate 			any++;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 'd':
1477c478bd9Sstevel@tonic-gate 			if ((name) || (device) || (update) || (tp)) {
1487c478bd9Sstevel@tonic-gate 				dmapi_err(EINVOKE,
1497c478bd9Sstevel@tonic-gate 					gettext("option conflict"));
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 			device++;
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		case 'f':
1547c478bd9Sstevel@tonic-gate 			argc--;
1557c478bd9Sstevel@tonic-gate 			argv++;
1567c478bd9Sstevel@tonic-gate 			if (argc <= 0)
1577c478bd9Sstevel@tonic-gate 				dmapi_err(EINVOKE,
1587c478bd9Sstevel@tonic-gate 					gettext("missing file name"));
1597c478bd9Sstevel@tonic-gate 			filename = *argv;
1607c478bd9Sstevel@tonic-gate 			file++;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case 'n':
1637c478bd9Sstevel@tonic-gate 			if ((name) || (device) || (update) || (tp)) {
1647c478bd9Sstevel@tonic-gate 				dmapi_err(EINVOKE,
1657c478bd9Sstevel@tonic-gate 					gettext("option conflict"));
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 			name++;
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		case 't':
1707c478bd9Sstevel@tonic-gate 			if ((name) || (device) || (update) || (tp)) {
1717c478bd9Sstevel@tonic-gate 				dmapi_err(EINVOKE,
1727c478bd9Sstevel@tonic-gate 					gettext("option conflict"));
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 			tp++;
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		case 'u':
1777c478bd9Sstevel@tonic-gate 			if ((name) || (device) || (update) || (tp)) {
1787c478bd9Sstevel@tonic-gate 				dmapi_err(EINVOKE,
1797c478bd9Sstevel@tonic-gate 					gettext("option conflict"));
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			update++;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		case 'v':
1847c478bd9Sstevel@tonic-gate 			verbose++;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		default:
1877c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
1887c478bd9Sstevel@tonic-gate 				gettext("bad option"));
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		argc--;
1927c478bd9Sstevel@tonic-gate 		argv++;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * -d(device) -n(name) and -u(update) switches require at least one
1967c478bd9Sstevel@tonic-gate 	 * argument.
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	if (file)
1997c478bd9Sstevel@tonic-gate 		setdmapfile(filename);
2007c478bd9Sstevel@tonic-gate 	if ((device) || (name) || (update) || (tp)) {
2017c478bd9Sstevel@tonic-gate 		if (argc < 1) {
2027c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
2037c478bd9Sstevel@tonic-gate 				gettext("insufficient args for this option"));
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	if (update) {
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * -u(update) switch requires only one argument
2097c478bd9Sstevel@tonic-gate 		 */
2107c478bd9Sstevel@tonic-gate 		if (argc != 1) {
2117c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
2127c478bd9Sstevel@tonic-gate 				gettext("too many args for this option"));
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		/*
2157c478bd9Sstevel@tonic-gate 		 * read entry argument from stdin into a devmap_t known as dmap
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		if ((dmap.dmap_devname = getdmapfield(*argv)) == NULL) {
2187c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
2197c478bd9Sstevel@tonic-gate 				gettext("Bad dmap_devname in entry argument"));
2207c478bd9Sstevel@tonic-gate 		}
221*45916cd2Sjpk 		if ((dmap.dmap_devtype = getdmapfield(NULL)) ==
2227c478bd9Sstevel@tonic-gate 			NULL) {
2237c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
2247c478bd9Sstevel@tonic-gate 				gettext("Bad dmap_devtype in entry Argument"));
2257c478bd9Sstevel@tonic-gate 		}
226*45916cd2Sjpk 		if ((dmap.dmap_devlist = getdmapfield(NULL)) ==
2277c478bd9Sstevel@tonic-gate 			NULL) {
2287c478bd9Sstevel@tonic-gate 			dmapi_err(EINVOKE,
2297c478bd9Sstevel@tonic-gate 				gettext("Bad dmap_devlist in entry argument"));
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * Find out how long device list is and create a buffer to
2337c478bd9Sstevel@tonic-gate 		 * hold it.  Then copy it there. This is done since we do not
2347c478bd9Sstevel@tonic-gate 		 * want to corrupt the existing string.
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		cntr = strlen(dmap.dmap_devlist) + 1;
237bdcaf822Sbasabi 		mptr = calloc((unsigned)cntr, sizeof (char));
2387c478bd9Sstevel@tonic-gate 		if (mptr == NULL) {
2397c478bd9Sstevel@tonic-gate 			if (verbose) {
2407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2417c478bd9Sstevel@tonic-gate 					gettext(
2427c478bd9Sstevel@tonic-gate 					"dmapinfo: Cannot calloc memory\n"));
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 			exit(1);
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 		(void) strcpy(mptr, dmap.dmap_devlist);
2477c478bd9Sstevel@tonic-gate 		/*
2487c478bd9Sstevel@tonic-gate 		 * open the device maps file for read/ write. We are not
2497c478bd9Sstevel@tonic-gate 		 * sure we want to write to it yet but we may and this is a
2507c478bd9Sstevel@tonic-gate 		 * easy way to get the file descriptor. We want the file
2517c478bd9Sstevel@tonic-gate 		 * descriptor so we can lock the file.
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 		if ((des = open(filename, O_RDWR)) < 0) {
2547c478bd9Sstevel@tonic-gate 			if (verbose) {
2557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
256bdcaf822Sbasabi 				gettext("dmapinfo: Cannot open %s\n"),
257bdcaf822Sbasabi 				    filename);
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 			exit(1);
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 		cntr = 0;
2627c478bd9Sstevel@tonic-gate #ifdef CMW
2637c478bd9Sstevel@tonic-gate 		while ((status = flock(des, LOCK_EX | LOCK_NB) == -1) &&
2647c478bd9Sstevel@tonic-gate 			(cntr++ < RETRY_COUNT)) {
2657c478bd9Sstevel@tonic-gate 			(void) sleep(RETRY_SLEEP);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate #else
2687c478bd9Sstevel@tonic-gate 		while (((status = lockf(des, F_TLOCK, 0)) == -1) &&
2697c478bd9Sstevel@tonic-gate 			(cntr++ < RETRY_COUNT)) {
2707c478bd9Sstevel@tonic-gate 			(void) sleep(RETRY_SLEEP);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate #endif
2737c478bd9Sstevel@tonic-gate 		if (status == -1) {
2747c478bd9Sstevel@tonic-gate 			if (verbose) {
2757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2767c478bd9Sstevel@tonic-gate 			gettext("dmapinfo: Cannot lock %s\n"), filename);
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			exit(1);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * Now that we have the device_maps file then lets check
2827c478bd9Sstevel@tonic-gate 		 * for previous entrys with the same name.  If it already
2837c478bd9Sstevel@tonic-gate 		 * exists then we will exit with status of 1.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if (verbose) {
2867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2877c478bd9Sstevel@tonic-gate 			gettext("dmapinfo: Checking %s for name (%s).\n"),
2887c478bd9Sstevel@tonic-gate 				filename, dmap.dmap_devname);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		if (getdmapnam(dmap.dmap_devname) != NULL) {
2917c478bd9Sstevel@tonic-gate 			if (verbose) {
2927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2937c478bd9Sstevel@tonic-gate 			gettext("dmapinfo: Device name (%s) found in %s.\n"),
2947c478bd9Sstevel@tonic-gate 					dmap.dmap_devname, filename);
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			exit(1);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		if (verbose) {
2997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3007c478bd9Sstevel@tonic-gate 		gettext("dmapinfo: Device name (%s) not found in %s.\n"),
3017c478bd9Sstevel@tonic-gate 				dmap.dmap_devname, filename);
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		/*
3047c478bd9Sstevel@tonic-gate 		 * We now Know name does not exist and now we need to check
3057c478bd9Sstevel@tonic-gate 		 * to see if any of the devices in the device list are in the
3067c478bd9Sstevel@tonic-gate 		 * device maps file. If the already exist then we will exit
3077c478bd9Sstevel@tonic-gate 		 * with a status of 1.
3087c478bd9Sstevel@tonic-gate 		 */
3097c478bd9Sstevel@tonic-gate 		nptr = mptr;
3107c478bd9Sstevel@tonic-gate 		nptr = getdmapdfield(nptr);
3117c478bd9Sstevel@tonic-gate 		while (nptr) {
3127c478bd9Sstevel@tonic-gate 			if (verbose) {
313*45916cd2Sjpk 				(void) fprintf(stderr,
314*45916cd2Sjpk 				    gettext("dmapinfo: "
315*45916cd2Sjpk 					"Check %s for device (%s).\n"),
316*45916cd2Sjpk 				    filename, nptr);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			if (getdmapdev(nptr) != NULL) {
3197c478bd9Sstevel@tonic-gate 				if (verbose) {
320*45916cd2Sjpk 					(void) fprintf(stderr,
321*45916cd2Sjpk 					    gettext("dmapinfo: "
322*45916cd2Sjpk 						"Device (%s) found in %s.\n"),
323*45916cd2Sjpk 					    nptr, filename);
3247c478bd9Sstevel@tonic-gate 				}
3257c478bd9Sstevel@tonic-gate 				exit(1);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 			if (verbose) {
328*45916cd2Sjpk 				(void) fprintf(stderr,
329*45916cd2Sjpk 				    gettext("dmapinfo: "
330*45916cd2Sjpk 					"Device (%s) not found in %s.\n"),
331*45916cd2Sjpk 				    nptr, filename);
3327c478bd9Sstevel@tonic-gate 			}
333*45916cd2Sjpk 			nptr = getdmapdfield(NULL);
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * Good the entry is uniq. So lets find out how long it is
3377c478bd9Sstevel@tonic-gate 		 * and add it to the end of device maps file in a pretty
3387c478bd9Sstevel@tonic-gate 		 * way.
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if (verbose) {
3417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dmapinfo: Adding entry to %s\n",
3427c478bd9Sstevel@tonic-gate 				filename);
3437c478bd9Sstevel@tonic-gate 			printdmapent(&dmap);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		cntr = strlen(dmap.dmap_devname);
3467c478bd9Sstevel@tonic-gate 		cntr += strlen(dmap.dmap_devtype);
3477c478bd9Sstevel@tonic-gate 		cntr += strlen(dmap.dmap_devlist);
3487c478bd9Sstevel@tonic-gate 		cntr += 15;
349bdcaf822Sbasabi 		tptr = calloc((unsigned)cntr, sizeof (char));
3507c478bd9Sstevel@tonic-gate 		if (tptr == NULL) {
3517c478bd9Sstevel@tonic-gate 			exit(1);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, dmap.dmap_devname);
3547c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, ":\\\n\t");
3557c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, dmap.dmap_devtype);
3567c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, ":\\\n\t");
3577c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, dmap.dmap_devlist);
3587c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, ":\\\n\t");
3597c478bd9Sstevel@tonic-gate 		(void) strcat(tptr, "\n");
3607c478bd9Sstevel@tonic-gate 		cntr = strlen(tptr);
3617c478bd9Sstevel@tonic-gate #ifdef CMW
3627c478bd9Sstevel@tonic-gate 		if (lseek(des, 0L, L_XTND) == -1L) {
3637c478bd9Sstevel@tonic-gate 			exit(1);
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate #else
3667c478bd9Sstevel@tonic-gate 		if (lseek(des, 0L, SEEK_END) == -1L) {
3677c478bd9Sstevel@tonic-gate 			exit(1);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate #endif
3707c478bd9Sstevel@tonic-gate 		if (write(des, tptr, cntr) == -1) {
3717c478bd9Sstevel@tonic-gate 			exit(1);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 		if (close(des) == -1) {
3747c478bd9Sstevel@tonic-gate 			exit(1);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		if (verbose) {
3777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dmapinfo: Entry added to %s\n",
3787c478bd9Sstevel@tonic-gate 				filename);
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		exit(0);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Look for devices in device_maps file. If verbose switch is set
3847c478bd9Sstevel@tonic-gate 	 * then print entry(s) found. If "any" switch  is set then, if any
3857c478bd9Sstevel@tonic-gate 	 * device is found will result in a exit status of 0. If "any" switch
3867c478bd9Sstevel@tonic-gate 	 * is not set then, if any device is not will result in a exit status
3877c478bd9Sstevel@tonic-gate 	 * of 1.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	if (device) {
390*45916cd2Sjpk 		setdmapent();
3917c478bd9Sstevel@tonic-gate 		while (argc >= 1) {
3927c478bd9Sstevel@tonic-gate 			if ((dmapp = getdmapdev(*argv)) != NULL) {
3937c478bd9Sstevel@tonic-gate 				if (verbose) {
3947c478bd9Sstevel@tonic-gate 					printdmapent(dmapp);
3957c478bd9Sstevel@tonic-gate 				}
3967c478bd9Sstevel@tonic-gate 				cntr++;
397*45916cd2Sjpk 			} else if (any == 0) {
398*45916cd2Sjpk 				enddmapent();
3997c478bd9Sstevel@tonic-gate 				exit(1);
400*45916cd2Sjpk 			}
4017c478bd9Sstevel@tonic-gate 			argc--;
4027c478bd9Sstevel@tonic-gate 			argv++;
4037c478bd9Sstevel@tonic-gate 		}
404*45916cd2Sjpk 		enddmapent();
4057c478bd9Sstevel@tonic-gate 		if (cntr != 0)
4067c478bd9Sstevel@tonic-gate 			exit(0);
4077c478bd9Sstevel@tonic-gate 		exit(1);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * Look for names in device_maps file. If verbose switch is set
4117c478bd9Sstevel@tonic-gate 	 * then print entry(s) found. If "any" switch  is set then, if any
4127c478bd9Sstevel@tonic-gate 	 * name is found will result in a exit status of 0. If "any" switch
4137c478bd9Sstevel@tonic-gate 	 * is not set then, if any name is not will result in a exit status
4147c478bd9Sstevel@tonic-gate 	 * of 1.
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	if (name) {
417*45916cd2Sjpk 		setdmapent();
4187c478bd9Sstevel@tonic-gate 		while (argc >= 1) {
4197c478bd9Sstevel@tonic-gate 			if ((dmapp = getdmapnam(*argv)) != NULL) {
4207c478bd9Sstevel@tonic-gate 				if (verbose) {
4217c478bd9Sstevel@tonic-gate 					printdmapent(dmapp);
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 				cntr++;
4247c478bd9Sstevel@tonic-gate 			} else if (any == 0)
4257c478bd9Sstevel@tonic-gate 				exit(1);
4267c478bd9Sstevel@tonic-gate 			argc--;
4277c478bd9Sstevel@tonic-gate 			argv++;
4287c478bd9Sstevel@tonic-gate 		}
429*45916cd2Sjpk 		enddmapent();
4307c478bd9Sstevel@tonic-gate 		if (cntr != 0)
4317c478bd9Sstevel@tonic-gate 			exit(0);
4327c478bd9Sstevel@tonic-gate 		exit(1);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Read all entrys from device maps file. If verbose flag is set
4367c478bd9Sstevel@tonic-gate 	 * then all the device maps files are printed.  This is useful for
4377c478bd9Sstevel@tonic-gate 	 * piping to grep. Also this option used without the verbose option
4387c478bd9Sstevel@tonic-gate 	 * is useful to check for device maps file and for at least one
4397c478bd9Sstevel@tonic-gate 	 * entry.  If the device maps file is found and there is one entry
4407c478bd9Sstevel@tonic-gate 	 * the return status is 0.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	if (tp) {
4437c478bd9Sstevel@tonic-gate 		cntr = 0;
444*45916cd2Sjpk 		setdmapent();
4457c478bd9Sstevel@tonic-gate 		while (argc >= 1) {
4467c478bd9Sstevel@tonic-gate 			while ((dmapp = getdmaptype(*argv)) != 0) {
4477c478bd9Sstevel@tonic-gate 				cntr++;
4487c478bd9Sstevel@tonic-gate 				if (verbose) {
4497c478bd9Sstevel@tonic-gate 					printdmapent(dmapp);
4507c478bd9Sstevel@tonic-gate 				}
4517c478bd9Sstevel@tonic-gate 			}
452*45916cd2Sjpk 			if ((any == 0) && (cntr == 0)) {
453*45916cd2Sjpk 				enddmapent();
4547c478bd9Sstevel@tonic-gate 				exit(1);
455*45916cd2Sjpk 			}
4567c478bd9Sstevel@tonic-gate 			argc--;
4577c478bd9Sstevel@tonic-gate 			argv++;
4587c478bd9Sstevel@tonic-gate 		}
459*45916cd2Sjpk 		enddmapent();
4607c478bd9Sstevel@tonic-gate 		if (cntr == 0)
4617c478bd9Sstevel@tonic-gate 			exit(1);
4627c478bd9Sstevel@tonic-gate 		exit(0);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * Read all entrys from device maps file. If verbose flag is set
4667c478bd9Sstevel@tonic-gate 	 * then all the device maps files are printed.  This is useful for
4677c478bd9Sstevel@tonic-gate 	 * piping to grep. Also this option used without the verbose option
4687c478bd9Sstevel@tonic-gate 	 * is useful to check for device maps file and for atleast one
4697c478bd9Sstevel@tonic-gate 	 * entry.  If the device maps file is found and there is one entry
4707c478bd9Sstevel@tonic-gate 	 * the return status is 0.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	cntr = 0;
4737c478bd9Sstevel@tonic-gate 	setdmapent();
4747c478bd9Sstevel@tonic-gate 	while ((dmapp = getdmapent()) != 0) {
4757c478bd9Sstevel@tonic-gate 		cntr++;
4767c478bd9Sstevel@tonic-gate 		if (verbose) {
4777c478bd9Sstevel@tonic-gate 			printdmapent(dmapp);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	enddmapent();
4817c478bd9Sstevel@tonic-gate 	if (cntr == 0)
4827c478bd9Sstevel@tonic-gate 		exit(1);
483bdcaf822Sbasabi 	return (0);
4847c478bd9Sstevel@tonic-gate }
485