xref: /titanic_52/usr/src/cmd/svr4pkg/pkginstall/pkgenv.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 #include <stdio.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <pkgstrct.h>
37 #include <string.h>
38 #include <locale.h>
39 #include <libintl.h>
40 #include <pkglib.h>
41 #include "install.h"
42 #include "libadm.h"
43 #include "libinst.h"
44 
45 #define	ERR_PKGINFO	"unable to access pkginfo file <%s>"
46 #define	ERR_PKGMAP	"unable to access pkgmap file <%s>"
47 #define	ERR_NOPARAM	"%s parameter is not defined in <%s>"
48 #define	ERR_PKGBAD	"PKG parameter is invalid <%s>"
49 #define	ERR_PKGMTCH	"PKG parameter <%s> does not match instance <%s>"
50 
51 char	*pkgarch;
52 char	*pkgvers;
53 char	*pkgabrv;
54 char	*pkgname;
55 char	pkgwild[PKGSIZ+1];
56 
57 /*
58  * This function confirms the presence of pkgmap and pkginfo and verifies
59  * that the mandatory parameters are available in the environment.
60  */
61 int
62 pkgenv(char *pkginst, char *p_pkginfo, char *p_pkgmap)
63 {
64 	FILE	*fp;
65 	char 	*value,
66 		path[PATH_MAX],
67 		param[MAX_PKG_PARAM_LENGTH];
68 	int	errflg;
69 
70 	errflg = 0;
71 	if (access(p_pkgmap, 0)) {
72 		progerr(gettext(ERR_PKGMAP), p_pkgmap);
73 		return (1);
74 	}
75 	if ((fp = fopen(p_pkginfo, "r")) == NULL) {
76 		progerr(gettext(ERR_PKGINFO), p_pkginfo);
77 		return (1);
78 	}
79 	param[0] = '\0';
80 	while (value = fpkgparam(fp, param)) {
81 		if (strcmp("PATH", param))
82 			putparam(param, value);
83 		free(value);
84 		param[0] = '\0';
85 	}
86 	(void) fclose(fp);
87 	/*
88 	 * verify that required parameters are now present in
89 	 * the environment
90 	 */
91 	if ((pkgabrv = getenv("PKG")) == NULL) {
92 		progerr(gettext(ERR_NOPARAM), "PKG", path);
93 		errflg++;
94 	}
95 	if (pkgnmchk(pkgabrv, NULL, 0) || strchr(pkgabrv, '.')) {
96 		progerr(gettext(ERR_PKGBAD), pkgabrv);
97 		errflg++;
98 	}
99 	(void) snprintf(pkgwild, sizeof (pkgwild), "%s.*", pkgabrv);
100 	if ((pkgname = getenv("NAME")) == NULL) {
101 		progerr(gettext(ERR_NOPARAM), "NAME", path);
102 		errflg++;
103 	}
104 	if ((pkgarch = getenv("ARCH")) == NULL) {
105 		progerr(gettext(ERR_NOPARAM), "ARCH", path);
106 		errflg++;
107 	}
108 	if ((pkgvers = getenv("VERSION")) == NULL) {
109 		progerr(gettext(ERR_NOPARAM), "VERSION", path);
110 		errflg++;
111 	}
112 	if (getenv("CATEGORY") == NULL) {
113 		progerr(gettext(ERR_NOPARAM), "CATEGORY", path);
114 		errflg++;
115 	}
116 	/*
117 	 * verify consistency between PKG parameter and pkginst that
118 	 * was determined from the directory structure
119 	 */
120 	(void) snprintf(param, sizeof (param), "%s.*", pkgabrv);
121 	if (pkgnmchk(pkginst, param, 0)) {
122 		progerr(gettext(ERR_PKGMTCH), pkgabrv, pkginst);
123 		errflg++;
124 	}
125 	return (errflg);
126 }
127