xref: /onnv-gate/usr/src/cmd/svr4pkg/libinst/putparam.c (revision 12734:76969fc28795)
19781SMoriah.Waterland@Sun.COM /*
29781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
39781SMoriah.Waterland@Sun.COM  *
49781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
59781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
69781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
79781SMoriah.Waterland@Sun.COM  *
89781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
109781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
119781SMoriah.Waterland@Sun.COM  * and limitations under the License.
129781SMoriah.Waterland@Sun.COM  *
139781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
149781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
169781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
179781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
189781SMoriah.Waterland@Sun.COM  *
199781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
209781SMoriah.Waterland@Sun.COM  */
219781SMoriah.Waterland@Sun.COM 
229781SMoriah.Waterland@Sun.COM /*
23*12734Sgary.pennington@oracle.com  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
249781SMoriah.Waterland@Sun.COM  */
259781SMoriah.Waterland@Sun.COM 
269781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
279781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
289781SMoriah.Waterland@Sun.COM 
299781SMoriah.Waterland@Sun.COM 
309781SMoriah.Waterland@Sun.COM #include <stdio.h>
319781SMoriah.Waterland@Sun.COM #include <limits.h>
329781SMoriah.Waterland@Sun.COM #include <stdlib.h>
339781SMoriah.Waterland@Sun.COM #include <unistd.h>
349781SMoriah.Waterland@Sun.COM #include <string.h>
359781SMoriah.Waterland@Sun.COM #include <fcntl.h>
369781SMoriah.Waterland@Sun.COM #include <sys/types.h>
379781SMoriah.Waterland@Sun.COM #include <sys/stat.h>
389781SMoriah.Waterland@Sun.COM #include <signal.h>
399781SMoriah.Waterland@Sun.COM #include <errno.h>
409781SMoriah.Waterland@Sun.COM #include <assert.h>
419781SMoriah.Waterland@Sun.COM #include <pkgdev.h>
429781SMoriah.Waterland@Sun.COM #include <pkginfo.h>
439781SMoriah.Waterland@Sun.COM #include <pkglocs.h>
449781SMoriah.Waterland@Sun.COM #include <locale.h>
459781SMoriah.Waterland@Sun.COM #include <libintl.h>
469781SMoriah.Waterland@Sun.COM #include <instzones_api.h>
479781SMoriah.Waterland@Sun.COM #include <pkglib.h>
489781SMoriah.Waterland@Sun.COM #include <install.h>
499781SMoriah.Waterland@Sun.COM #include <libinst.h>
509781SMoriah.Waterland@Sun.COM #include <libadm.h>
519781SMoriah.Waterland@Sun.COM #include <messages.h>
529781SMoriah.Waterland@Sun.COM 
539781SMoriah.Waterland@Sun.COM static char *localeNames[] = {
549781SMoriah.Waterland@Sun.COM 	"LC_CTYPE",
559781SMoriah.Waterland@Sun.COM 	"LC_NUMERIC",
569781SMoriah.Waterland@Sun.COM 	"LC_TIME",
579781SMoriah.Waterland@Sun.COM 	"LC_COLLATE",
589781SMoriah.Waterland@Sun.COM 	"LC_MESSAGES",
599781SMoriah.Waterland@Sun.COM 	"LC_MONETARY",
609781SMoriah.Waterland@Sun.COM 	"LC_ALL",
619781SMoriah.Waterland@Sun.COM 	"LANG",
629781SMoriah.Waterland@Sun.COM 	"TZ",
639781SMoriah.Waterland@Sun.COM 	NULL
649781SMoriah.Waterland@Sun.COM };
659781SMoriah.Waterland@Sun.COM 
669781SMoriah.Waterland@Sun.COM #define	NUM_LOCALE_TYPES	100
679781SMoriah.Waterland@Sun.COM 
689781SMoriah.Waterland@Sun.COM static char	*envPtr[NUM_LOCALE_TYPES];
699781SMoriah.Waterland@Sun.COM 
709781SMoriah.Waterland@Sun.COM /*
719781SMoriah.Waterland@Sun.COM  * extern declarations
729781SMoriah.Waterland@Sun.COM  */
739781SMoriah.Waterland@Sun.COM 
749781SMoriah.Waterland@Sun.COM extern char	**environ;
759781SMoriah.Waterland@Sun.COM 
769781SMoriah.Waterland@Sun.COM /*
779781SMoriah.Waterland@Sun.COM  * this is the initial and incremental allocation used to
789781SMoriah.Waterland@Sun.COM  * populate the environment "environ"
799781SMoriah.Waterland@Sun.COM  */
809781SMoriah.Waterland@Sun.COM 
819781SMoriah.Waterland@Sun.COM #define	MALSIZ	64
829781SMoriah.Waterland@Sun.COM 
839781SMoriah.Waterland@Sun.COM void
putparam(char * param,char * value)849781SMoriah.Waterland@Sun.COM putparam(char *param, char *value)
859781SMoriah.Waterland@Sun.COM {
869781SMoriah.Waterland@Sun.COM 	char	*pt;
879781SMoriah.Waterland@Sun.COM 	int	ptlen;
889781SMoriah.Waterland@Sun.COM 	int	i, n;
899781SMoriah.Waterland@Sun.COM 
909781SMoriah.Waterland@Sun.COM 	/*
919781SMoriah.Waterland@Sun.COM 	 * If the environment is NULL, allocate space for the
929781SMoriah.Waterland@Sun.COM 	 * character pointers.
939781SMoriah.Waterland@Sun.COM 	 */
949781SMoriah.Waterland@Sun.COM 	if (environ == NULL) {
959781SMoriah.Waterland@Sun.COM 		environ = (char **)calloc(MALSIZ, sizeof (char *));
969781SMoriah.Waterland@Sun.COM 		if (environ == NULL) {
979781SMoriah.Waterland@Sun.COM 			progerr(gettext(ERR_MEMORY), errno);
989781SMoriah.Waterland@Sun.COM 			quit(99);
999781SMoriah.Waterland@Sun.COM 		}
1009781SMoriah.Waterland@Sun.COM 	}
1019781SMoriah.Waterland@Sun.COM 
1029781SMoriah.Waterland@Sun.COM 	/*
1039781SMoriah.Waterland@Sun.COM 	 * If this parameter is already in place and it has a different
1049781SMoriah.Waterland@Sun.COM 	 * value, clear the old value by freeing the memory previously
1059781SMoriah.Waterland@Sun.COM 	 * allocated. Otherwise, we leave well-enough alone.
1069781SMoriah.Waterland@Sun.COM 	 */
1079781SMoriah.Waterland@Sun.COM 	n = strlen(param);
1089781SMoriah.Waterland@Sun.COM 	for (i = 0; environ[i]; i++) {
1099781SMoriah.Waterland@Sun.COM 		if (strncmp(environ[i], param, n) == 0 &&
1109781SMoriah.Waterland@Sun.COM 		    (environ[i][n] == '=')) {
1119781SMoriah.Waterland@Sun.COM 			if (strcmp((environ[i]) + n + 1, value) == 0)
1129781SMoriah.Waterland@Sun.COM 				return;
1139781SMoriah.Waterland@Sun.COM 			else {
1149781SMoriah.Waterland@Sun.COM 				free(environ[i]);
1159781SMoriah.Waterland@Sun.COM 				break;
1169781SMoriah.Waterland@Sun.COM 			}
1179781SMoriah.Waterland@Sun.COM 		}
1189781SMoriah.Waterland@Sun.COM 	}
1199781SMoriah.Waterland@Sun.COM 
1209781SMoriah.Waterland@Sun.COM 	/* Allocate space for the new environment entry. */
1219781SMoriah.Waterland@Sun.COM 	ptlen = (strlen(param)+strlen(value)+2)*(sizeof (char));
1229781SMoriah.Waterland@Sun.COM 	pt = (char *)calloc(strlen(param)+strlen(value)+2, sizeof (char));
1239781SMoriah.Waterland@Sun.COM 	if (pt == NULL) {
1249781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_MEMORY), errno);
1259781SMoriah.Waterland@Sun.COM 		quit(99);
1269781SMoriah.Waterland@Sun.COM 	}
1279781SMoriah.Waterland@Sun.COM 
1289781SMoriah.Waterland@Sun.COM 	/*
1299781SMoriah.Waterland@Sun.COM 	 * Put the statement into the allocated space and point the
1309781SMoriah.Waterland@Sun.COM 	 * environment entry at it.
1319781SMoriah.Waterland@Sun.COM 	 */
1329781SMoriah.Waterland@Sun.COM 	(void) snprintf(pt, ptlen, "%s=%s", param, value);
1339781SMoriah.Waterland@Sun.COM 	if (environ[i]) {
1349781SMoriah.Waterland@Sun.COM 		environ[i] = pt;
1359781SMoriah.Waterland@Sun.COM 		return;
1369781SMoriah.Waterland@Sun.COM 	}
1379781SMoriah.Waterland@Sun.COM 
1389781SMoriah.Waterland@Sun.COM 	/*
1399781SMoriah.Waterland@Sun.COM 	 * With this parameter in place, if we're at the end of the
1409781SMoriah.Waterland@Sun.COM 	 * allocated environment then allocate more space.
1419781SMoriah.Waterland@Sun.COM 	 */
1429781SMoriah.Waterland@Sun.COM 	environ[i++] = pt;
1439781SMoriah.Waterland@Sun.COM 	if ((i % MALSIZ) == 0) {
1449781SMoriah.Waterland@Sun.COM 		environ = (char **)realloc((void *)environ,
1459781SMoriah.Waterland@Sun.COM 			(i+MALSIZ)*sizeof (char *));
1469781SMoriah.Waterland@Sun.COM 		if (environ == NULL) {
1479781SMoriah.Waterland@Sun.COM 			progerr(gettext(ERR_MEMORY), errno);
1489781SMoriah.Waterland@Sun.COM 			quit(1);
1499781SMoriah.Waterland@Sun.COM 		}
1509781SMoriah.Waterland@Sun.COM 	}
1519781SMoriah.Waterland@Sun.COM 
1529781SMoriah.Waterland@Sun.COM 	/* Terminate the environment properly. */
1539781SMoriah.Waterland@Sun.COM 	environ[i] = (char *)NULL;
1549781SMoriah.Waterland@Sun.COM }
1559781SMoriah.Waterland@Sun.COM 
1569781SMoriah.Waterland@Sun.COM /* bugid 4279039 */
1579781SMoriah.Waterland@Sun.COM void
getuserlocale(void)1589781SMoriah.Waterland@Sun.COM getuserlocale(void)
1599781SMoriah.Waterland@Sun.COM {
1609781SMoriah.Waterland@Sun.COM 	int i;
1619781SMoriah.Waterland@Sun.COM 
1629781SMoriah.Waterland@Sun.COM 	for (i = 0; (localeNames[i] != NULL) && (i < NUM_LOCALE_TYPES); i++) {
1639781SMoriah.Waterland@Sun.COM 		envPtr[i] = getenv(localeNames[i]);
1649781SMoriah.Waterland@Sun.COM 		if (envPtr[i]) {
1659781SMoriah.Waterland@Sun.COM 			putparam(localeNames[i], envPtr[i]);
1669781SMoriah.Waterland@Sun.COM 		}
1679781SMoriah.Waterland@Sun.COM 	}
1689781SMoriah.Waterland@Sun.COM }
1699781SMoriah.Waterland@Sun.COM 
1709781SMoriah.Waterland@Sun.COM /* bugid 4279039 */
1719781SMoriah.Waterland@Sun.COM void
putuserlocale(void)1729781SMoriah.Waterland@Sun.COM putuserlocale(void)
1739781SMoriah.Waterland@Sun.COM {
1749781SMoriah.Waterland@Sun.COM 	int i;
1759781SMoriah.Waterland@Sun.COM 
1769781SMoriah.Waterland@Sun.COM 	for (i = 0; (localeNames[i] != NULL) && (i < NUM_LOCALE_TYPES); i++) {
1779781SMoriah.Waterland@Sun.COM 		if (envPtr[i]) {
1789781SMoriah.Waterland@Sun.COM 			putparam(localeNames[i], envPtr[i]);
1799781SMoriah.Waterland@Sun.COM 		}
1809781SMoriah.Waterland@Sun.COM 	}
1819781SMoriah.Waterland@Sun.COM }
1829781SMoriah.Waterland@Sun.COM 
1839781SMoriah.Waterland@Sun.COM /*
1849781SMoriah.Waterland@Sun.COM  * Name:	putConditionInfo
1859781SMoriah.Waterland@Sun.COM  * Description:	put parent "condition" information to environment
1869781SMoriah.Waterland@Sun.COM  * Arguments:	a_parentZoneName - name of the parent zone
1879781SMoriah.Waterland@Sun.COM  *			== NULL - no name
1889781SMoriah.Waterland@Sun.COM  *		a_parentZoneType - parent zone "type"
1899781SMoriah.Waterland@Sun.COM  *			== NULL - no type
1909781SMoriah.Waterland@Sun.COM  * Returns:	void
1919781SMoriah.Waterland@Sun.COM  */
1929781SMoriah.Waterland@Sun.COM 
1939781SMoriah.Waterland@Sun.COM void
putConditionInfo(char * a_parentZoneName,char * a_parentZoneType)1949781SMoriah.Waterland@Sun.COM putConditionInfo(char *a_parentZoneName, char *a_parentZoneType)
1959781SMoriah.Waterland@Sun.COM {
1969781SMoriah.Waterland@Sun.COM 	char	*p;
1979781SMoriah.Waterland@Sun.COM 	char	*pa;
1989781SMoriah.Waterland@Sun.COM 	SML_TAG	*tag = SML_TAG__NULL;
1999781SMoriah.Waterland@Sun.COM 	SML_TAG	*ntag;
2009781SMoriah.Waterland@Sun.COM 
2019781SMoriah.Waterland@Sun.COM 	/* entry debugging info */
2029781SMoriah.Waterland@Sun.COM 
2039781SMoriah.Waterland@Sun.COM 	echoDebug(DBG_PUTPARAM_PUTCONDINFO_ENTRY);
2049781SMoriah.Waterland@Sun.COM 
2059781SMoriah.Waterland@Sun.COM 	/*
2069781SMoriah.Waterland@Sun.COM 	 * create tag to hold condition information:
2079781SMoriah.Waterland@Sun.COM 	 * <environmentConditionInformation>
2089781SMoriah.Waterland@Sun.COM 	 * <parentZone zoneName=<?> zoneType=<?>/>
2099781SMoriah.Waterland@Sun.COM 	 * <currentZone zoneName=<?> zoneType=<?>/>
2109781SMoriah.Waterland@Sun.COM 	 * </environmentConditionInformation>
2119781SMoriah.Waterland@Sun.COM 	 */
2129781SMoriah.Waterland@Sun.COM 
2139781SMoriah.Waterland@Sun.COM 	tag = smlNewTag(TAG_COND_TOPLEVEL);
2149781SMoriah.Waterland@Sun.COM 
2159781SMoriah.Waterland@Sun.COM 	/*
2169781SMoriah.Waterland@Sun.COM 	 * information about pkgadd or pkgrm environment
2179781SMoriah.Waterland@Sun.COM 	 * <parentZone zoneName=<?> zoneType=<?>/>
2189781SMoriah.Waterland@Sun.COM 	 */
2199781SMoriah.Waterland@Sun.COM 
2209781SMoriah.Waterland@Sun.COM 	/* allocate tag for parent info */
2219781SMoriah.Waterland@Sun.COM 
2229781SMoriah.Waterland@Sun.COM 	ntag = smlNewTag(TAG_COND_PARENT_ZONE);
2239781SMoriah.Waterland@Sun.COM 
2249781SMoriah.Waterland@Sun.COM 	/* parent zone name */
2259781SMoriah.Waterland@Sun.COM 
2269781SMoriah.Waterland@Sun.COM 	smlSetParam(ntag, TAG_COND_ZONE_NAME,
2279781SMoriah.Waterland@Sun.COM 		a_parentZoneName ? a_parentZoneName : "");
2289781SMoriah.Waterland@Sun.COM 
2299781SMoriah.Waterland@Sun.COM 	/* parent zone info */
2309781SMoriah.Waterland@Sun.COM 
2319781SMoriah.Waterland@Sun.COM 	smlSetParam(ntag, TAG_COND_ZONE_TYPE,
2329781SMoriah.Waterland@Sun.COM 		a_parentZoneType ? a_parentZoneType : "");
2339781SMoriah.Waterland@Sun.COM 
2349781SMoriah.Waterland@Sun.COM 	/* add to top level tag */
2359781SMoriah.Waterland@Sun.COM 
2369781SMoriah.Waterland@Sun.COM 	(void) smlAddTag(&tag, -1, ntag);
2379781SMoriah.Waterland@Sun.COM 	free(ntag);
2389781SMoriah.Waterland@Sun.COM 
2399781SMoriah.Waterland@Sun.COM 	/*
2409781SMoriah.Waterland@Sun.COM 	 * information about pkginstall or pkgremove environment
2419781SMoriah.Waterland@Sun.COM 	 * <currentZone zoneName=<?> zoneType=<?>/>
2429781SMoriah.Waterland@Sun.COM 	 */
2439781SMoriah.Waterland@Sun.COM 
2449781SMoriah.Waterland@Sun.COM 	/* allocate tag for parent info */
2459781SMoriah.Waterland@Sun.COM 
2469781SMoriah.Waterland@Sun.COM 	ntag = smlNewTag(TAG_COND_CURRENT_ZONE);
2479781SMoriah.Waterland@Sun.COM 
2489781SMoriah.Waterland@Sun.COM 	/* current zone name */
2499781SMoriah.Waterland@Sun.COM 
2509781SMoriah.Waterland@Sun.COM 	p = z_get_zonename();
2519781SMoriah.Waterland@Sun.COM 	if ((p != NULL) && (*p != '\0')) {
2529781SMoriah.Waterland@Sun.COM 		smlSetParam(ntag, TAG_COND_ZONE_NAME, p);
2539781SMoriah.Waterland@Sun.COM 		free(p);
2549781SMoriah.Waterland@Sun.COM 	}
2559781SMoriah.Waterland@Sun.COM 
2569781SMoriah.Waterland@Sun.COM 	/* current zone type */
2579781SMoriah.Waterland@Sun.COM 
2589781SMoriah.Waterland@Sun.COM 	smlSetParam(ntag, TAG_COND_ZONE_TYPE,
2599781SMoriah.Waterland@Sun.COM 		z_running_in_global_zone() == B_TRUE ?
2609781SMoriah.Waterland@Sun.COM 			TAG_VALUE_GLOBAL_ZONE : TAG_VALUE_NONGLOBAL_ZONE);
2619781SMoriah.Waterland@Sun.COM 
2629781SMoriah.Waterland@Sun.COM 	/* add to top level tag */
2639781SMoriah.Waterland@Sun.COM 
2649781SMoriah.Waterland@Sun.COM 	(void) smlAddTag(&tag, -1, ntag);
2659781SMoriah.Waterland@Sun.COM 	free(ntag);
2669781SMoriah.Waterland@Sun.COM 
2679781SMoriah.Waterland@Sun.COM 	/*
2689781SMoriah.Waterland@Sun.COM 	 * done filling in tag - convert to string and place in environment
2699781SMoriah.Waterland@Sun.COM 	 */
2709781SMoriah.Waterland@Sun.COM 
2719781SMoriah.Waterland@Sun.COM 	p = smlConvertTagToString(tag);
2729781SMoriah.Waterland@Sun.COM 
2739781SMoriah.Waterland@Sun.COM 	/* convert all new-line characters to space */
2749781SMoriah.Waterland@Sun.COM 
2759781SMoriah.Waterland@Sun.COM 	for (pa = p; *pa != '\0'; pa++) {
2769781SMoriah.Waterland@Sun.COM 		if (*pa == '\n') {
2779781SMoriah.Waterland@Sun.COM 			*pa = ' ';
2789781SMoriah.Waterland@Sun.COM 		}
2799781SMoriah.Waterland@Sun.COM 	}
2809781SMoriah.Waterland@Sun.COM 
2819781SMoriah.Waterland@Sun.COM 	echoDebug(DBG_PUTPARAM_PUTCONDINFO_EXIT, p);
2829781SMoriah.Waterland@Sun.COM 
2839781SMoriah.Waterland@Sun.COM 	putparam(PKGCOND_GLOBAL_VARIABLE, p);
2849781SMoriah.Waterland@Sun.COM }
285