xref: /illumos-gate/usr/src/cmd/svr4pkg/pkgparam/pkgparam.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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 2009 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 <string.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <pkgstrct.h>
39 #include <pkginfo.h>
40 #include <locale.h>
41 #include <libintl.h>
42 
43 #include <pkglib.h>
44 #include <libadm.h>
45 #include <libinst.h>
46 
47 extern char	*pkgfile;
48 
49 #define	ERR_ROOT_SET	"Could not set install root from the environment."
50 #define	ERR_ROOT_CMD	"Command line install root contends with environment."
51 #define	ERR_MESG	"unable to locate parameter information for \"%s\""
52 #define	ERR_FLT		"parsing error in parameter file"
53 #define	ERR_USAGE	"usage:\n" \
54 			"\t%s [-v] [-d device] pkginst [param [param ...]]\n" \
55 			"\t%s [-v] -f file [param [param ...]]\n"
56 #define	HASHSIZE	151
57 #define	BSZ		4
58 
59 
60 static char	*device = NULL;
61 static int	errflg = 0;
62 static int	vflag = 0;
63 
64 static void	print_entry(char *, char *);
65 
66 static void
67 usage(void)
68 {
69 	char	*prog = get_prog_name();
70 
71 	(void) fprintf(stderr, gettext(ERR_USAGE), prog, prog);
72 	exit(1);
73 }
74 
75 int
76 main(int argc, char *argv[])
77 {
78 	char *value, *pkginst;
79 	char *param, parambuf[128];
80 	int c;
81 
82 	pkgfile = NULL;
83 
84 	/* initialize locale mechanism */
85 
86 	(void) setlocale(LC_ALL, "");
87 
88 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
89 #define	TEXT_DOMAIN "SYS_TEST"
90 #endif
91 	(void) textdomain(TEXT_DOMAIN);
92 
93 	/* determine program name */
94 
95 	(void) set_prog_name(argv[0]);
96 
97 	/* establish installation root directory */
98 
99 	if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
100 		progerr(gettext(ERR_ROOT_SET));
101 		exit(1);
102 	}
103 
104 	while ((c = getopt(argc, argv, "R:vd:f:?")) != EOF) {
105 		switch (c) {
106 		    case 'v':
107 			vflag++;
108 			break;
109 
110 		    case 'f':
111 			/* -f could specify filename to get parameters from */
112 			pkgfile = optarg;
113 			break;
114 
115 		    case 'd':
116 			/* -d could specify stream or mountable device */
117 			device = flex_device(optarg, 1);
118 			break;
119 
120 		    case 'R':
121 			if (!set_inst_root(optarg)) {
122 				progerr(gettext(ERR_ROOT_CMD));
123 				exit(1);
124 			}
125 			break;
126 
127 		    default:
128 		    case '?':
129 			usage();
130 		}
131 	}
132 
133 	set_PKGpaths(get_inst_root());
134 
135 	if (pkgfile) {
136 		if (device)
137 			usage();
138 		pkginst = pkgfile;
139 	} else {
140 		if ((optind+1) > argc)
141 			usage();
142 
143 		if (pkghead(device))
144 			return (1); /* couldn't obtain info about device */
145 		pkginst = argv[optind++];
146 	}
147 
148 	/* If a filename was specified or install db does not exist */
149 	do {
150 		param = argv[optind];
151 		if (!param) {
152 			param = parambuf;
153 			*param = '\0';
154 		}
155 		value = pkgparam(pkginst, param);
156 		if (value == NULL) {
157 			if (errno == EFAULT) {
158 				progerr(gettext(ERR_FLT));
159 				errflg++;
160 				break;
161 			} else if (errno != EINVAL) {
162 				/*
163 				 * some other error besides no value for this
164 				 * particular parameter
165 				 */
166 				progerr(gettext(ERR_MESG), pkginst);
167 				errflg++;
168 				break;
169 			}
170 			if (!argv[optind])
171 				break;
172 			continue;
173 		}
174 
175 		print_entry(param, value);
176 
177 	} while (!argv[optind] || (++optind < argc));
178 	(void) pkgparam(NULL, NULL); /* close open FDs so umount won't fail */
179 
180 	(void) pkghead(NULL);
181 	return (errflg ? 1 : 0);
182 }
183 
184 static void
185 print_entry(char *param, char *value)
186 {
187 	if (vflag) {
188 		(void) printf("%s='", param);
189 		while (*value) {
190 			if (*value == '\'') {
191 				(void) printf("'\"'\"'");
192 				value++;
193 			} else
194 				(void) putchar(*value++);
195 		}
196 		(void) printf("'\n");
197 	} else
198 		(void) printf("%s\n", value);
199 }
200 
201 void
202 quit(int retval)
203 {
204 	exit(retval);
205 }
206