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