xref: /titanic_44/usr/src/cmd/svr4pkg/pkgremove/check.c (revision 4656d4747c8743290bfbe910c64cd75eb4e4af8d)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland #include <stdio.h>
32*5c51f124SMoriah Waterland #include <stdlib.h>
33*5c51f124SMoriah Waterland #include <unistd.h>
34*5c51f124SMoriah Waterland #include <string.h>
35*5c51f124SMoriah Waterland #include <utmpx.h>
36*5c51f124SMoriah Waterland #include <dirent.h>
37*5c51f124SMoriah Waterland #include <sys/types.h>
38*5c51f124SMoriah Waterland #include <pkgstrct.h>
39*5c51f124SMoriah Waterland #include <locale.h>
40*5c51f124SMoriah Waterland #include <libintl.h>
41*5c51f124SMoriah Waterland #include "install.h"
42*5c51f124SMoriah Waterland #include <pkglib.h>
43*5c51f124SMoriah Waterland #include "libadm.h"
44*5c51f124SMoriah Waterland #include "libinst.h"
45*5c51f124SMoriah Waterland #include "messages.h"
46*5c51f124SMoriah Waterland 
47*5c51f124SMoriah Waterland extern struct admin adm;
48*5c51f124SMoriah Waterland 
49*5c51f124SMoriah Waterland extern char	pkgloc[], *pkginst, *msgtext;
50*5c51f124SMoriah Waterland 
51*5c51f124SMoriah Waterland static boolean_t	preremoveCheck = B_FALSE;
52*5c51f124SMoriah Waterland static char		*zoneName = (char *)NULL;
53*5c51f124SMoriah Waterland 
54*5c51f124SMoriah Waterland 
55*5c51f124SMoriah Waterland void
rcksetPreremoveCheck(boolean_t a_preremoveCheck)56*5c51f124SMoriah Waterland rcksetPreremoveCheck(boolean_t a_preremoveCheck)
57*5c51f124SMoriah Waterland {
58*5c51f124SMoriah Waterland 	preremoveCheck = a_preremoveCheck;
59*5c51f124SMoriah Waterland }
60*5c51f124SMoriah Waterland 
61*5c51f124SMoriah Waterland void
rcksetZoneName(char * a_zoneName)62*5c51f124SMoriah Waterland rcksetZoneName(char *a_zoneName)
63*5c51f124SMoriah Waterland {
64*5c51f124SMoriah Waterland 	zoneName = a_zoneName;
65*5c51f124SMoriah Waterland }
66*5c51f124SMoriah Waterland 
67*5c51f124SMoriah Waterland int
rckrunlevel(void)68*5c51f124SMoriah Waterland rckrunlevel(void)
69*5c51f124SMoriah Waterland {
70*5c51f124SMoriah Waterland 	struct utmpx utmpx;
71*5c51f124SMoriah Waterland 	struct utmpx *putmpx;
72*5c51f124SMoriah Waterland 	char	ans[MAX_INPUT];
73*5c51f124SMoriah Waterland 	char	*pt;
74*5c51f124SMoriah Waterland 	char	*rstates;
75*5c51f124SMoriah Waterland 	int	n;
76*5c51f124SMoriah Waterland 	char	*uxstate;
77*5c51f124SMoriah Waterland 
78*5c51f124SMoriah Waterland 	if (ADM(runlevel, "nocheck")) {
79*5c51f124SMoriah Waterland 		return (0);
80*5c51f124SMoriah Waterland 	}
81*5c51f124SMoriah Waterland 
82*5c51f124SMoriah Waterland 	pt = getenv("RSTATES");
83*5c51f124SMoriah Waterland 	if (pt == NULL) {
84*5c51f124SMoriah Waterland 		return (0);
85*5c51f124SMoriah Waterland 	}
86*5c51f124SMoriah Waterland 
87*5c51f124SMoriah Waterland 	utmpx.ut_type = RUN_LVL;
88*5c51f124SMoriah Waterland 	putmpx = getutxid(&utmpx);
89*5c51f124SMoriah Waterland 	if (putmpx == NULL) {
90*5c51f124SMoriah Waterland 		progerr(ERR_RUNSTATE);
91*5c51f124SMoriah Waterland 		return (99);
92*5c51f124SMoriah Waterland 	}
93*5c51f124SMoriah Waterland 	uxstate = strtok(&putmpx->ut_line[10], " \t\n");
94*5c51f124SMoriah Waterland 
95*5c51f124SMoriah Waterland 	rstates = qstrdup(pt);
96*5c51f124SMoriah Waterland 	if ((pt = strtok(pt, " \t\n, ")) == NULL)
97*5c51f124SMoriah Waterland 		return (0); /* no list is no list */
98*5c51f124SMoriah Waterland 	do {
99*5c51f124SMoriah Waterland 		if (strcmp(pt, uxstate) == NULL) {
100*5c51f124SMoriah Waterland 			free(rstates);
101*5c51f124SMoriah Waterland 			return (0);
102*5c51f124SMoriah Waterland 		}
103*5c51f124SMoriah Waterland 	} while (pt = strtok(NULL, " \t\n, "));
104*5c51f124SMoriah Waterland 
105*5c51f124SMoriah Waterland 	if (preremoveCheck == B_FALSE) {
106*5c51f124SMoriah Waterland 		msgtext = MSG_PKGREMOVE_RUNLEVEL;
107*5c51f124SMoriah Waterland 		ptext(stderr, msgtext, uxstate);
108*5c51f124SMoriah Waterland 	} else {
109*5c51f124SMoriah Waterland 		(void) fprintf(stdout, "runlevel=%s", uxstate);
110*5c51f124SMoriah Waterland 	}
111*5c51f124SMoriah Waterland 
112*5c51f124SMoriah Waterland 	pt = strtok(rstates, " \t\n, ");
113*5c51f124SMoriah Waterland 	do {
114*5c51f124SMoriah Waterland 		if (preremoveCheck == B_FALSE) {
115*5c51f124SMoriah Waterland 			ptext(stderr, "\\t%s", pt);
116*5c51f124SMoriah Waterland 		} else {
117*5c51f124SMoriah Waterland 			(void) fprintf(stdout, ":%s", pt);
118*5c51f124SMoriah Waterland 		}
119*5c51f124SMoriah Waterland 	} while (pt = strtok(NULL, " \t\n, "));
120*5c51f124SMoriah Waterland 
121*5c51f124SMoriah Waterland 	if (preremoveCheck == B_TRUE) {
122*5c51f124SMoriah Waterland 		(void) fprintf(stdout, "\n");
123*5c51f124SMoriah Waterland 	}
124*5c51f124SMoriah Waterland 
125*5c51f124SMoriah Waterland 	free(rstates);
126*5c51f124SMoriah Waterland 
127*5c51f124SMoriah Waterland 	if (ADM(runlevel, "quit")) {
128*5c51f124SMoriah Waterland 		return (4);
129*5c51f124SMoriah Waterland 	}
130*5c51f124SMoriah Waterland 
131*5c51f124SMoriah Waterland 	if (echoGetFlag() == B_FALSE) {
132*5c51f124SMoriah Waterland 		return (5);
133*5c51f124SMoriah Waterland 	}
134*5c51f124SMoriah Waterland 
135*5c51f124SMoriah Waterland 	msgtext = NULL;
136*5c51f124SMoriah Waterland 
137*5c51f124SMoriah Waterland 	n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_RUNLEVEL,
138*5c51f124SMoriah Waterland 	    ASK_PKGREMOVE_CONTINUE);
139*5c51f124SMoriah Waterland 
140*5c51f124SMoriah Waterland 	if (n != 0) {
141*5c51f124SMoriah Waterland 		return (n);
142*5c51f124SMoriah Waterland 	}
143*5c51f124SMoriah Waterland 
144*5c51f124SMoriah Waterland 	if (strchr("yY", *ans) == NULL) {
145*5c51f124SMoriah Waterland 		return (3);
146*5c51f124SMoriah Waterland 	}
147*5c51f124SMoriah Waterland 
148*5c51f124SMoriah Waterland 	return (0);
149*5c51f124SMoriah Waterland }
150*5c51f124SMoriah Waterland 
151*5c51f124SMoriah Waterland int
rckdepend(void)152*5c51f124SMoriah Waterland rckdepend(void)
153*5c51f124SMoriah Waterland {
154*5c51f124SMoriah Waterland 	int	n;
155*5c51f124SMoriah Waterland 	char	ans[MAX_INPUT];
156*5c51f124SMoriah Waterland 	char	**id, **name;
157*5c51f124SMoriah Waterland 
158*5c51f124SMoriah Waterland 	if (ADM(rdepend, "nocheck")) {
159*5c51f124SMoriah Waterland 		return (0);
160*5c51f124SMoriah Waterland 	}
161*5c51f124SMoriah Waterland 
162*5c51f124SMoriah Waterland 	if (zoneName == (char *)NULL) {
163*5c51f124SMoriah Waterland 		echo(MSG_CHECKREMOVE_PKG_IN_GZ, pkginst);
164*5c51f124SMoriah Waterland 	} else {
165*5c51f124SMoriah Waterland 		echo(MSG_CHECKREMOVE_PKG_IN_ZONE, pkginst, zoneName);
166*5c51f124SMoriah Waterland 	}
167*5c51f124SMoriah Waterland 
168*5c51f124SMoriah Waterland 	if (dockdeps(pkginst, 1, preremoveCheck)) {
169*5c51f124SMoriah Waterland 		msgtext = MSG_PKGREMOVE_DEPEND;
170*5c51f124SMoriah Waterland 
171*5c51f124SMoriah Waterland 		if (preremoveCheck == B_FALSE) {
172*5c51f124SMoriah Waterland 			echo(msgtext);
173*5c51f124SMoriah Waterland 		}
174*5c51f124SMoriah Waterland 
175*5c51f124SMoriah Waterland 		if (ADM(rdepend, "quit")) {
176*5c51f124SMoriah Waterland 			return (4);
177*5c51f124SMoriah Waterland 		}
178*5c51f124SMoriah Waterland 
179*5c51f124SMoriah Waterland 		if (echoGetFlag() == B_FALSE) {
180*5c51f124SMoriah Waterland 			return (5);
181*5c51f124SMoriah Waterland 		}
182*5c51f124SMoriah Waterland 
183*5c51f124SMoriah Waterland 		msgtext = NULL;
184*5c51f124SMoriah Waterland 
185*5c51f124SMoriah Waterland 		n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_DEPEND,
186*5c51f124SMoriah Waterland 		    ASK_PKGREMOVE_CONTINUE);
187*5c51f124SMoriah Waterland 
188*5c51f124SMoriah Waterland 		if (n != 0) {
189*5c51f124SMoriah Waterland 			return (n);
190*5c51f124SMoriah Waterland 		}
191*5c51f124SMoriah Waterland 
192*5c51f124SMoriah Waterland 		if (strchr("yY", *ans) == NULL) {
193*5c51f124SMoriah Waterland 			return (3);
194*5c51f124SMoriah Waterland 		}
195*5c51f124SMoriah Waterland 	}
196*5c51f124SMoriah Waterland 
197*5c51f124SMoriah Waterland 	return (0);
198*5c51f124SMoriah Waterland }
199*5c51f124SMoriah Waterland 
200*5c51f124SMoriah Waterland int
rckpriv(void)201*5c51f124SMoriah Waterland rckpriv(void)
202*5c51f124SMoriah Waterland {
203*5c51f124SMoriah Waterland 	struct dirent	*dp;
204*5c51f124SMoriah Waterland 	DIR		*dirfp;
205*5c51f124SMoriah Waterland 	int		n;
206*5c51f124SMoriah Waterland 	char		found;
207*5c51f124SMoriah Waterland 	char		ans[MAX_INPUT];
208*5c51f124SMoriah Waterland 	char		path[PATH_MAX];
209*5c51f124SMoriah Waterland 
210*5c51f124SMoriah Waterland 	if (ADM(action, "nocheck")) {
211*5c51f124SMoriah Waterland 		return (0);
212*5c51f124SMoriah Waterland 	}
213*5c51f124SMoriah Waterland 
214*5c51f124SMoriah Waterland 	(void) snprintf(path, sizeof (path), "%s/install", pkgloc);
215*5c51f124SMoriah Waterland 	if ((dirfp = opendir(path)) == NULL)
216*5c51f124SMoriah Waterland 		return (0);
217*5c51f124SMoriah Waterland 
218*5c51f124SMoriah Waterland 	found = 0;
219*5c51f124SMoriah Waterland 	while ((dp = readdir(dirfp)) != NULL) {
220*5c51f124SMoriah Waterland 		if ((strcmp(dp->d_name, "preremove") == NULL) ||
221*5c51f124SMoriah Waterland 		    (strcmp(dp->d_name, "postremove") == NULL) ||
222*5c51f124SMoriah Waterland 		    (strncmp(dp->d_name, "r.", 2) == NULL)) {
223*5c51f124SMoriah Waterland 			found++;
224*5c51f124SMoriah Waterland 			break;
225*5c51f124SMoriah Waterland 		}
226*5c51f124SMoriah Waterland 	}
227*5c51f124SMoriah Waterland 	(void) closedir(dirfp);
228*5c51f124SMoriah Waterland 
229*5c51f124SMoriah Waterland 	if (found) {
230*5c51f124SMoriah Waterland 		if (preremoveCheck == B_FALSE) {
231*5c51f124SMoriah Waterland 			ptext(stderr, MSG_PKGREMOVE_PRIV);
232*5c51f124SMoriah Waterland 		}
233*5c51f124SMoriah Waterland 		msgtext = MSG_PKGSCRIPTS_FOUND;
234*5c51f124SMoriah Waterland 
235*5c51f124SMoriah Waterland 		if (ADM(action, "quit")) {
236*5c51f124SMoriah Waterland 			return (4);
237*5c51f124SMoriah Waterland 		}
238*5c51f124SMoriah Waterland 
239*5c51f124SMoriah Waterland 		if (echoGetFlag() == B_FALSE) {
240*5c51f124SMoriah Waterland 			return (5);
241*5c51f124SMoriah Waterland 		}
242*5c51f124SMoriah Waterland 
243*5c51f124SMoriah Waterland 		msgtext = NULL;
244*5c51f124SMoriah Waterland 
245*5c51f124SMoriah Waterland 		n = ckyorn(ans, NULL, NULL, HLP_PKGREMOVE_PRIV,
246*5c51f124SMoriah Waterland 		    ASK_PKGREMOVE_CONTINUE);
247*5c51f124SMoriah Waterland 
248*5c51f124SMoriah Waterland 		if (n != 0) {
249*5c51f124SMoriah Waterland 			return (n);
250*5c51f124SMoriah Waterland 		}
251*5c51f124SMoriah Waterland 
252*5c51f124SMoriah Waterland 		if (strchr("yY", *ans) == NULL) {
253*5c51f124SMoriah Waterland 			return (3);
254*5c51f124SMoriah Waterland 		}
255*5c51f124SMoriah Waterland 	}
256*5c51f124SMoriah Waterland 
257*5c51f124SMoriah Waterland 	return (0);
258*5c51f124SMoriah Waterland }
259