xref: /onnv-gate/usr/src/cmd/svr4pkg/pkgparam/pkgparam.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
32*9781SMoriah.Waterland@Sun.COM #include <string.h>
33*9781SMoriah.Waterland@Sun.COM #include <errno.h>
34*9781SMoriah.Waterland@Sun.COM #include <limits.h>
35*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
36*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
37*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
38*9781SMoriah.Waterland@Sun.COM #include <pkgstrct.h>
39*9781SMoriah.Waterland@Sun.COM #include <pkginfo.h>
40*9781SMoriah.Waterland@Sun.COM #include <locale.h>
41*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
42*9781SMoriah.Waterland@Sun.COM 
43*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
44*9781SMoriah.Waterland@Sun.COM #include <libadm.h>
45*9781SMoriah.Waterland@Sun.COM #include <libinst.h>
46*9781SMoriah.Waterland@Sun.COM 
47*9781SMoriah.Waterland@Sun.COM extern char	*pkgfile;
48*9781SMoriah.Waterland@Sun.COM 
49*9781SMoriah.Waterland@Sun.COM #define	ERR_ROOT_SET	"Could not set install root from the environment."
50*9781SMoriah.Waterland@Sun.COM #define	ERR_ROOT_CMD	"Command line install root contends with environment."
51*9781SMoriah.Waterland@Sun.COM #define	ERR_MESG	"unable to locate parameter information for \"%s\""
52*9781SMoriah.Waterland@Sun.COM #define	ERR_FLT		"parsing error in parameter file"
53*9781SMoriah.Waterland@Sun.COM #define	ERR_USAGE	"usage:\n" \
54*9781SMoriah.Waterland@Sun.COM 			"\t%s [-v] [-d device] pkginst [param [param ...]]\n" \
55*9781SMoriah.Waterland@Sun.COM 			"\t%s [-v] -f file [param [param ...]]\n"
56*9781SMoriah.Waterland@Sun.COM #define	HASHSIZE	151
57*9781SMoriah.Waterland@Sun.COM #define	BSZ		4
58*9781SMoriah.Waterland@Sun.COM 
59*9781SMoriah.Waterland@Sun.COM 
60*9781SMoriah.Waterland@Sun.COM static char	*device = NULL;
61*9781SMoriah.Waterland@Sun.COM static int	errflg = 0;
62*9781SMoriah.Waterland@Sun.COM static int	vflag = 0;
63*9781SMoriah.Waterland@Sun.COM 
64*9781SMoriah.Waterland@Sun.COM static void	print_entry(char *, char *);
65*9781SMoriah.Waterland@Sun.COM 
66*9781SMoriah.Waterland@Sun.COM static void
usage(void)67*9781SMoriah.Waterland@Sun.COM usage(void)
68*9781SMoriah.Waterland@Sun.COM {
69*9781SMoriah.Waterland@Sun.COM 	char	*prog = get_prog_name();
70*9781SMoriah.Waterland@Sun.COM 
71*9781SMoriah.Waterland@Sun.COM 	(void) fprintf(stderr, gettext(ERR_USAGE), prog, prog);
72*9781SMoriah.Waterland@Sun.COM 	exit(1);
73*9781SMoriah.Waterland@Sun.COM }
74*9781SMoriah.Waterland@Sun.COM 
75*9781SMoriah.Waterland@Sun.COM int
main(int argc,char * argv[])76*9781SMoriah.Waterland@Sun.COM main(int argc, char *argv[])
77*9781SMoriah.Waterland@Sun.COM {
78*9781SMoriah.Waterland@Sun.COM 	char *value, *pkginst;
79*9781SMoriah.Waterland@Sun.COM 	char *param, parambuf[128];
80*9781SMoriah.Waterland@Sun.COM 	int c;
81*9781SMoriah.Waterland@Sun.COM 
82*9781SMoriah.Waterland@Sun.COM 	pkgfile = NULL;
83*9781SMoriah.Waterland@Sun.COM 
84*9781SMoriah.Waterland@Sun.COM 	/* initialize locale mechanism */
85*9781SMoriah.Waterland@Sun.COM 
86*9781SMoriah.Waterland@Sun.COM 	(void) setlocale(LC_ALL, "");
87*9781SMoriah.Waterland@Sun.COM 
88*9781SMoriah.Waterland@Sun.COM #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
89*9781SMoriah.Waterland@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"
90*9781SMoriah.Waterland@Sun.COM #endif
91*9781SMoriah.Waterland@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
92*9781SMoriah.Waterland@Sun.COM 
93*9781SMoriah.Waterland@Sun.COM 	/* determine program name */
94*9781SMoriah.Waterland@Sun.COM 
95*9781SMoriah.Waterland@Sun.COM 	(void) set_prog_name(argv[0]);
96*9781SMoriah.Waterland@Sun.COM 
97*9781SMoriah.Waterland@Sun.COM 	/* establish installation root directory */
98*9781SMoriah.Waterland@Sun.COM 
99*9781SMoriah.Waterland@Sun.COM 	if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
100*9781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_ROOT_SET));
101*9781SMoriah.Waterland@Sun.COM 		exit(1);
102*9781SMoriah.Waterland@Sun.COM 	}
103*9781SMoriah.Waterland@Sun.COM 
104*9781SMoriah.Waterland@Sun.COM 	while ((c = getopt(argc, argv, "R:vd:f:?")) != EOF) {
105*9781SMoriah.Waterland@Sun.COM 		switch (c) {
106*9781SMoriah.Waterland@Sun.COM 		    case 'v':
107*9781SMoriah.Waterland@Sun.COM 			vflag++;
108*9781SMoriah.Waterland@Sun.COM 			break;
109*9781SMoriah.Waterland@Sun.COM 
110*9781SMoriah.Waterland@Sun.COM 		    case 'f':
111*9781SMoriah.Waterland@Sun.COM 			/* -f could specify filename to get parameters from */
112*9781SMoriah.Waterland@Sun.COM 			pkgfile = optarg;
113*9781SMoriah.Waterland@Sun.COM 			break;
114*9781SMoriah.Waterland@Sun.COM 
115*9781SMoriah.Waterland@Sun.COM 		    case 'd':
116*9781SMoriah.Waterland@Sun.COM 			/* -d could specify stream or mountable device */
117*9781SMoriah.Waterland@Sun.COM 			device = flex_device(optarg, 1);
118*9781SMoriah.Waterland@Sun.COM 			break;
119*9781SMoriah.Waterland@Sun.COM 
120*9781SMoriah.Waterland@Sun.COM 		    case 'R':
121*9781SMoriah.Waterland@Sun.COM 			if (!set_inst_root(optarg)) {
122*9781SMoriah.Waterland@Sun.COM 				progerr(gettext(ERR_ROOT_CMD));
123*9781SMoriah.Waterland@Sun.COM 				exit(1);
124*9781SMoriah.Waterland@Sun.COM 			}
125*9781SMoriah.Waterland@Sun.COM 			break;
126*9781SMoriah.Waterland@Sun.COM 
127*9781SMoriah.Waterland@Sun.COM 		    default:
128*9781SMoriah.Waterland@Sun.COM 		    case '?':
129*9781SMoriah.Waterland@Sun.COM 			usage();
130*9781SMoriah.Waterland@Sun.COM 		}
131*9781SMoriah.Waterland@Sun.COM 	}
132*9781SMoriah.Waterland@Sun.COM 
133*9781SMoriah.Waterland@Sun.COM 	set_PKGpaths(get_inst_root());
134*9781SMoriah.Waterland@Sun.COM 
135*9781SMoriah.Waterland@Sun.COM 	if (pkgfile) {
136*9781SMoriah.Waterland@Sun.COM 		if (device)
137*9781SMoriah.Waterland@Sun.COM 			usage();
138*9781SMoriah.Waterland@Sun.COM 		pkginst = pkgfile;
139*9781SMoriah.Waterland@Sun.COM 	} else {
140*9781SMoriah.Waterland@Sun.COM 		if ((optind+1) > argc)
141*9781SMoriah.Waterland@Sun.COM 			usage();
142*9781SMoriah.Waterland@Sun.COM 
143*9781SMoriah.Waterland@Sun.COM 		if (pkghead(device))
144*9781SMoriah.Waterland@Sun.COM 			return (1); /* couldn't obtain info about device */
145*9781SMoriah.Waterland@Sun.COM 		pkginst = argv[optind++];
146*9781SMoriah.Waterland@Sun.COM 	}
147*9781SMoriah.Waterland@Sun.COM 
148*9781SMoriah.Waterland@Sun.COM 	/* If a filename was specified or install db does not exist */
149*9781SMoriah.Waterland@Sun.COM 	do {
150*9781SMoriah.Waterland@Sun.COM 		param = argv[optind];
151*9781SMoriah.Waterland@Sun.COM 		if (!param) {
152*9781SMoriah.Waterland@Sun.COM 			param = parambuf;
153*9781SMoriah.Waterland@Sun.COM 			*param = '\0';
154*9781SMoriah.Waterland@Sun.COM 		}
155*9781SMoriah.Waterland@Sun.COM 		value = pkgparam(pkginst, param);
156*9781SMoriah.Waterland@Sun.COM 		if (value == NULL) {
157*9781SMoriah.Waterland@Sun.COM 			if (errno == EFAULT) {
158*9781SMoriah.Waterland@Sun.COM 				progerr(gettext(ERR_FLT));
159*9781SMoriah.Waterland@Sun.COM 				errflg++;
160*9781SMoriah.Waterland@Sun.COM 				break;
161*9781SMoriah.Waterland@Sun.COM 			} else if (errno != EINVAL) {
162*9781SMoriah.Waterland@Sun.COM 				/*
163*9781SMoriah.Waterland@Sun.COM 				 * some other error besides no value for this
164*9781SMoriah.Waterland@Sun.COM 				 * particular parameter
165*9781SMoriah.Waterland@Sun.COM 				 */
166*9781SMoriah.Waterland@Sun.COM 				progerr(gettext(ERR_MESG), pkginst);
167*9781SMoriah.Waterland@Sun.COM 				errflg++;
168*9781SMoriah.Waterland@Sun.COM 				break;
169*9781SMoriah.Waterland@Sun.COM 			}
170*9781SMoriah.Waterland@Sun.COM 			if (!argv[optind])
171*9781SMoriah.Waterland@Sun.COM 				break;
172*9781SMoriah.Waterland@Sun.COM 			continue;
173*9781SMoriah.Waterland@Sun.COM 		}
174*9781SMoriah.Waterland@Sun.COM 
175*9781SMoriah.Waterland@Sun.COM 		print_entry(param, value);
176*9781SMoriah.Waterland@Sun.COM 
177*9781SMoriah.Waterland@Sun.COM 	} while (!argv[optind] || (++optind < argc));
178*9781SMoriah.Waterland@Sun.COM 	(void) pkgparam(NULL, NULL); /* close open FDs so umount won't fail */
179*9781SMoriah.Waterland@Sun.COM 
180*9781SMoriah.Waterland@Sun.COM 	(void) pkghead(NULL);
181*9781SMoriah.Waterland@Sun.COM 	return (errflg ? 1 : 0);
182*9781SMoriah.Waterland@Sun.COM }
183*9781SMoriah.Waterland@Sun.COM 
184*9781SMoriah.Waterland@Sun.COM static void
print_entry(char * param,char * value)185*9781SMoriah.Waterland@Sun.COM print_entry(char *param, char *value)
186*9781SMoriah.Waterland@Sun.COM {
187*9781SMoriah.Waterland@Sun.COM 	if (vflag) {
188*9781SMoriah.Waterland@Sun.COM 		(void) printf("%s='", param);
189*9781SMoriah.Waterland@Sun.COM 		while (*value) {
190*9781SMoriah.Waterland@Sun.COM 			if (*value == '\'') {
191*9781SMoriah.Waterland@Sun.COM 				(void) printf("'\"'\"'");
192*9781SMoriah.Waterland@Sun.COM 				value++;
193*9781SMoriah.Waterland@Sun.COM 			} else
194*9781SMoriah.Waterland@Sun.COM 				(void) putchar(*value++);
195*9781SMoriah.Waterland@Sun.COM 		}
196*9781SMoriah.Waterland@Sun.COM 		(void) printf("'\n");
197*9781SMoriah.Waterland@Sun.COM 	} else
198*9781SMoriah.Waterland@Sun.COM 		(void) printf("%s\n", value);
199*9781SMoriah.Waterland@Sun.COM }
200*9781SMoriah.Waterland@Sun.COM 
201*9781SMoriah.Waterland@Sun.COM void
quit(int retval)202*9781SMoriah.Waterland@Sun.COM quit(int retval)
203*9781SMoriah.Waterland@Sun.COM {
204*9781SMoriah.Waterland@Sun.COM 	exit(retval);
205*9781SMoriah.Waterland@Sun.COM }
206