getinst.c revision 9781:ccf49524d5dc
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 <string.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <valtools.h>
36#include <locale.h>
37#include <libintl.h>
38#include <pkginfo.h>
39#include "install.h"
40#include <pkglib.h>
41#include "libadm.h"
42#include "libinst.h"
43#include "pkginstall.h"
44#include "messages.h"
45
46extern struct admin adm;
47extern char	*pkgarch, *pkgvers, *msgtext, *pkgabrv;
48extern int	opresvr4, maxinst;
49
50static char	newinst[PKGSIZ];
51static char	*nextinst(void);
52static char	*prompt(struct pkginfo *info, int npkgs);
53static int	same_pkg;	/* same PKG, ARCH and VERSION */
54
55/*
56 * This returns the correct package instance based on how many packages are
57 * already installed. If there are none (npkgs == 0), it just returns the
58 * package abbreviation. Otherwise, it interacts with the user (or reads the
59 * admin file) to determine if we should overwrite an instance which is
60 * already installed, or possibly install a new instance of this package
61 */
62char *
63getinst(int *updatingExisting, struct pkginfo *info, int npkgs,
64	boolean_t a_preinstallCheck)
65{
66	char	*inst;
67	char	*sameinst;
68	int	i;
69	int	nsamearch;
70	int	samearch;
71
72	/* entry debugging info */
73
74	same_pkg = 0;
75
76	/*
77	 * If this is the first instance of the package, it's called the by
78	 * the package abbreviation.
79	 */
80
81	if (npkgs == 0) {
82		return (pkgabrv);
83	}
84
85	/*
86	 * this package is already installed; determine how to handle the
87	 * new instance of the package to install
88	 */
89
90	if (ADM(instance, "newonly") || ADM(instance, "quit")) {
91		/*
92		 * new instance is required, or quit if not new
93		 */
94
95		msgtext = MSG_NEWONLY;
96		if (a_preinstallCheck == B_FALSE) {
97			ptext(stderr, msgtext, pkgabrv);
98		} else {
99			(void) fprintf(stdout, "install-new-only=true\n");
100			(void) fprintf(stdout, "ckinstance=4\n");
101		}
102		quit(4);
103	}
104
105	/*
106	 * package already installed and new instance not required
107	 * see if updating the same instance of the package
108	 */
109
110	samearch = nsamearch = 0;
111	sameinst  = NULL;
112	for (i = 0; i < npkgs; i++) {
113		if (strcmp(info[i].arch, pkgarch) == NULL) {
114			samearch = i;
115			nsamearch++;
116			if (strcmp(info[i].version, pkgvers) == NULL) {
117				sameinst = info[i].pkginst;
118			}
119		}
120	}
121
122	if (sameinst) {
123		/* same instance of package */
124		if (a_preinstallCheck == B_FALSE) {
125			ptext(stderr, MSG_SAME);
126		} else {
127			(void) fprintf(stdout, "install-same-instance=true\n");
128			(void) fprintf(stdout, "ckinstance=0\n");
129		}
130
131		inst = sameinst; /* can't be overwriting a pre-svr4 package */
132		same_pkg++;
133		(*updatingExisting)++;
134		return (inst);
135	}
136
137	if (ADM(instance, "overwrite")) {
138		/* not the same instance of the package */
139		if (npkgs == 1) {
140			samearch = 0; /* use only package we know about */
141		} else if (nsamearch != 1) {
142			/*
143			 * more than one instance of the same ARCH is already
144			 * installed on this machine
145			 */
146			msgtext = MSG_OVERWRITE;
147			if (a_preinstallCheck == B_FALSE) {
148				ptext(stderr, msgtext);
149			} else {
150				(void) fprintf(stdout,
151					"install-ovewrite=true\n");
152				(void) fprintf(stdout, "ckinstance=4\n");
153			}
154			quit(4);
155		}
156
157		inst = info[samearch].pkginst;
158		if (info[samearch].status == PI_PRESVR4) {
159			opresvr4++; /* overwriting a pre-svr4 package */
160		}
161
162		(*updatingExisting)++;
163		return (inst);
164	}
165
166	if (ADM(instance, "unique")) {
167		if (maxinst <= npkgs) {
168			/* too many instances */
169			msgtext = MSG_UNIQ1;
170			if (a_preinstallCheck == B_FALSE) {
171				ptext(stderr, msgtext, pkgabrv);
172			} else {
173				(void) fprintf(stdout,
174					"install-too-many-instances=true\n");
175				(void) fprintf(stdout, "ckinstance=4\n");
176			}
177			quit(4);
178		}
179		inst = nextinst();
180		return (inst);
181	}
182
183	if (a_preinstallCheck == B_FALSE) {
184		if (echoGetFlag() == B_FALSE) {
185			msgtext = MSG_NOINTERACT;
186			ptext(stderr, msgtext);
187			quit(5);
188		}
189	} else {
190		(void) fprintf(stdout, "install-new-instance=true\n");
191		(void) fprintf(stdout, "ckinstance=1\n");
192	}
193
194	inst = prompt(info, npkgs);
195	if (strcmp(inst, "new") == NULL) {
196		inst = nextinst();
197		return (inst);
198	}
199
200	(*updatingExisting)++;
201
202	/* see if this instance is presvr4 */
203	for (i = 0; i < npkgs; i++) {
204		if (strcmp(inst, info[i].pkginst) == NULL) {
205			if (info[i].status == PI_PRESVR4) {
206				opresvr4++;
207			}
208			break;
209		}
210	}
211
212	return (inst);
213}
214
215/*
216 * This informs the caller whether the package in question is the same
217 * version and architecture as an installed package of the same name.
218 */
219
220int
221is_samepkg(void) {
222	return (same_pkg);
223}
224
225static char *
226nextinst(void)
227{
228	struct pkginfo info;
229	int	n;
230
231	n = 2; /* requirements say start at 2 */
232
233	info.pkginst = NULL;
234	(void) strcpy(newinst, pkgabrv);
235	while (pkginfo(&info, newinst, NULL, NULL) == 0) {
236		(void) snprintf(newinst, sizeof (newinst),
237				"%s.%d", pkgabrv, n++);
238	}
239	return (newinst);
240}
241
242static char *
243prompt(struct pkginfo *info, int npkgs)
244{
245	CKMENU	*menup;
246	char	*inst;
247	char	ans[MAX_INPUT];
248	char	header[256];
249	char	temp[256];
250	int	i;
251	int	n;
252
253	if (maxinst > npkgs) {
254		/*
255		 * the user may choose to install a completely new
256		 * instance of this package
257		 */
258		n = ckyorn(ans, NULL, NULL, MSG_GETINST_HELP1,
259			MSG_GETINST_PROMPT1);
260		if (n != 0) {
261			quit(n);
262		}
263		if (strchr("yY", *ans) != NULL) {
264			return ("new");
265		}
266	}
267
268	(void) snprintf(header, sizeof (header), MSG_GETINST_HEADER, pkgabrv);
269	menup = allocmenu(header, CKALPHA);
270
271	for (i = 0; i < npkgs; i++) {
272		(void) snprintf(temp, sizeof (temp),
273				"%s %s\n(%s) %s", info[i].pkginst,
274			info[i].name, info[i].arch, info[i].version);
275		if (setitem(menup, temp)) {
276			progerr("no memory");
277			quit(99);
278		}
279	}
280
281	if (npkgs == 1) {
282		printmenu(menup);
283		if (n = ckyorn(ans, NULL, NULL, NULL, MSG_GETINST_PROMPT0))
284			quit(n);
285		if (strchr("yY", *ans) == NULL)
286			quit(3);
287		(void) strcpy(newinst, info[0].pkginst);
288	} else {
289		if (n = ckitem(menup, &inst, 1, NULL, NULL, MSG_GETINST_HELP2,
290		    MSG_GETINST_PROMPT2))
291			quit(n);
292		(void) strcpy(newinst, inst);
293	}
294	(void) setitem(menup, 0); /* clear resource usage */
295	free(menup); /* clear resource usage */
296
297	return (newinst);
298}
299