xref: /titanic_41/usr/src/cmd/svr4pkg/pkgremove/check.c (revision 5fbb41393be5d63f75952b1d72d4df2642d22557)
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 2003 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 <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #ifndef SUNOS41
36 #include <utmpx.h>
37 #endif
38 #include <dirent.h>
39 #include <sys/types.h>
40 #include <pkgstrct.h>
41 #include <locale.h>
42 #include <libintl.h>
43 #include "install.h"
44 #include <pkglib.h>
45 #include "libadm.h"
46 #include "libinst.h"
47 #include "wsreg_pkgrm.h"
48 #include "messages.h"
49 
50 extern struct admin adm;
51 /* extern struct cfent **eptlist; */
52 
53 extern char	pkgloc[], *pkginst, *msgtext;
54 
55 static boolean_t	preremoveCheck = B_FALSE;
56 static char		*zoneName = (char *)NULL;
57 
58 
59 void
60 rcksetPreremoveCheck(boolean_t a_preremoveCheck)
61 {
62 	preremoveCheck = a_preremoveCheck;
63 }
64 
65 void
66 rcksetZoneName(char *a_zoneName)
67 {
68 	zoneName = a_zoneName;
69 }
70 
71 int
72 rckrunlevel(void)
73 {
74 	struct utmpx utmpx;
75 	struct utmpx *putmpx;
76 	char	ans[MAX_INPUT];
77 	char	*pt;
78 	char	*rstates;
79 	int	n;
80 	char	*uxstate;
81 
82 	if (ADM(runlevel, "nocheck")) {
83 		return (0);
84 	}
85 
86 	pt = getenv("RSTATES");
87 	if (pt == NULL) {
88 		return (0);
89 	}
90 
91 	utmpx.ut_type = RUN_LVL;
92 	putmpx = getutxid(&utmpx);
93 	if (putmpx == NULL) {
94 		progerr(ERR_RUNSTATE);
95 		return (99);
96 	}
97 	uxstate = strtok(&putmpx->ut_line[10], " \t\n");
98 
99 	rstates = qstrdup(pt);
100 	if ((pt = strtok(pt, " \t\n, ")) == NULL)
101 		return (0); /* no list is no list */
102 	do {
103 		if (strcmp(pt, uxstate) == NULL) {
104 			free(rstates);
105 			return (0);
106 		}
107 	} while (pt = strtok(NULL, " \t\n, "));
108 
109 	if (preremoveCheck == B_FALSE) {
110 		msgtext = MSG_PKGREMOVE_RUNLEVEL;
111 		ptext(stderr, msgtext, uxstate);
112 	} else {
113 		(void) fprintf(stdout, "runlevel=%s", uxstate);
114 	}
115 
116 	pt = strtok(rstates, " \t\n, ");
117 	do {
118 		if (preremoveCheck == B_FALSE) {
119 			ptext(stderr, "\\t%s", pt);
120 		} else {
121 			(void) fprintf(stdout, ":%s", pt);
122 		}
123 	} while (pt = strtok(NULL, " \t\n, "));
124 
125 	if (preremoveCheck == B_TRUE) {
126 		(void) fprintf(stdout, "\n");
127 	}
128 
129 	free(rstates);
130 
131 	if (ADM(runlevel, "quit")) {
132 		return (4);
133 	}
134 
135 	if (echoGetFlag() == B_FALSE) {
136 		return (5);
137 	}
138 
139 	msgtext = NULL;
140 
141 	n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_RUNLEVEL,
142 	    ASK_PKGREMOVE_CONTINUE);
143 
144 	if (n != 0) {
145 		return (n);
146 	}
147 
148 	if (strchr("yY", *ans) == NULL) {
149 		return (3);
150 	}
151 
152 	return (0);
153 }
154 
155 int
156 rckpatchpkg(char *p, char *pt)
157 {
158 	int n;
159 	char ans[MAX_INPUT];
160 
161 	ptext(stderr, WRN_PKGREMOVE_PATCHES, p, p, p, pt);
162 
163 	n = ckyorn(ans, NULL, NULL, NULL, ASK_PKGREMOVE_CONTINUE);
164 
165 	if (n != 0) {
166 		return (n);
167 	}
168 
169 	if (strchr("yY", *ans) == NULL) {
170 		return (3);
171 	}
172 
173 	return (0);
174 }
175 
176 int
177 rckdepend(void)
178 {
179 	int	n;
180 	char	ans[MAX_INPUT];
181 	char	**id, **name;
182 
183 	if (ADM(rdepend, "nocheck")) {
184 		return (0);
185 	}
186 
187 	if (zoneName == (char *)NULL) {
188 		echo(MSG_CHECKREMOVE_PKG_IN_GZ, pkginst);
189 	} else {
190 		echo(MSG_CHECKREMOVE_PKG_IN_ZONE, pkginst, zoneName);
191 	}
192 
193 	if (wsreg_pkgrm_check(get_inst_root(), pkginst, &id, &name) > 0) {
194 		int i;
195 
196 		if (ADM(rdepend, "quit")) {
197 			return (4);
198 		}
199 
200 		if (echoGetFlag() == B_FALSE) {
201 			return (5);
202 		}
203 
204 		msgtext = MSG_PKGREMOVE_WSDEPEND;
205 		echo(msgtext);
206 
207 		(void) printf("%-36s  %s", MSG_PKGREMOVE_ID_STR,
208 		    MSG_PKGREMOVE_NAME_STR);
209 		(void) printf("\n------------------------------------  "
210 		    "--------------------------------------\n");
211 
212 		for (i = 0; id[i] != NULL; i++) {
213 			(void) printf("%-36s  %s\n", id[i],
214 			    (name[i])?(name[i]):"");
215 			free(id[i]);
216 			if (name[i]) {
217 				free(name[i]);
218 			}
219 		}
220 
221 		free(id);
222 		free(name);
223 
224 		msgtext = NULL;
225 
226 		n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_WSDEPEND,
227 		    ASK_PKGREMOVE_CONTINUE);
228 
229 		if (n != 0) {
230 			return (n);
231 		}
232 
233 		if (strchr("yY", *ans) == NULL) {
234 			return (3);
235 		}
236 	}
237 
238 	if (dockdeps(pkginst, 1, preremoveCheck)) {
239 		msgtext = MSG_PKGREMOVE_DEPEND;
240 
241 		if (preremoveCheck == B_FALSE) {
242 			echo(msgtext);
243 		}
244 
245 		if (ADM(rdepend, "quit")) {
246 			return (4);
247 		}
248 
249 		if (echoGetFlag() == B_FALSE) {
250 			return (5);
251 		}
252 
253 		msgtext = NULL;
254 
255 		n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_DEPEND,
256 		    ASK_PKGREMOVE_CONTINUE);
257 
258 		if (n != 0) {
259 			return (n);
260 		}
261 
262 		if (strchr("yY", *ans) == NULL) {
263 			return (3);
264 		}
265 	}
266 
267 	return (0);
268 }
269 
270 int
271 rckpriv(void)
272 {
273 	struct dirent	*dp;
274 	DIR		*dirfp;
275 	int		n;
276 	char		found;
277 	char		ans[MAX_INPUT];
278 	char		path[PATH_MAX];
279 
280 	if (ADM(action, "nocheck")) {
281 		return (0);
282 	}
283 
284 	(void) snprintf(path, sizeof (path), "%s/install", pkgloc);
285 	if ((dirfp = opendir(path)) == NULL)
286 		return (0);
287 
288 	found = 0;
289 	while ((dp = readdir(dirfp)) != NULL) {
290 		if ((strcmp(dp->d_name, "preremove") == NULL) ||
291 		    (strcmp(dp->d_name, "postremove") == NULL) ||
292 		    (strncmp(dp->d_name, "r.", 2) == NULL)) {
293 			found++;
294 			break;
295 		}
296 	}
297 	(void) closedir(dirfp);
298 
299 	if (found) {
300 		if (preremoveCheck == B_FALSE) {
301 			ptext(stderr, MSG_PKGREMOVE_PRIV);
302 		}
303 		msgtext = MSG_PKGSCRIPTS_FOUND;
304 
305 		if (ADM(action, "quit")) {
306 			return (4);
307 		}
308 
309 		if (echoGetFlag() == B_FALSE) {
310 			return (5);
311 		}
312 
313 		msgtext = NULL;
314 
315 		n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_PRIV,
316 		    ASK_PKGREMOVE_CONTINUE);
317 
318 		if (n != 0) {
319 			return (n);
320 		}
321 
322 		if (strchr("yY", *ans) == NULL) {
323 			return (3);
324 		}
325 	}
326 
327 	return (0);
328 }
329