xref: /titanic_41/usr/src/lib/libpkg/common/pkgmount.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
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 2009 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 
32*5c51f124SMoriah Waterland #include <stdio.h>
33*5c51f124SMoriah Waterland #include <errno.h>
34*5c51f124SMoriah Waterland #include <string.h>
35*5c51f124SMoriah Waterland #include <stdlib.h>
36*5c51f124SMoriah Waterland #include <unistd.h>
37*5c51f124SMoriah Waterland #include <pkgdev.h>
38*5c51f124SMoriah Waterland #include <pkginfo.h>
39*5c51f124SMoriah Waterland #include <sys/types.h>
40*5c51f124SMoriah Waterland #include <devmgmt.h>
41*5c51f124SMoriah Waterland #include <sys/mount.h>
42*5c51f124SMoriah Waterland #include "pkglib.h"
43*5c51f124SMoriah Waterland #include "pkglibmsgs.h"
44*5c51f124SMoriah Waterland #include "pkglocale.h"
45*5c51f124SMoriah Waterland 
46*5c51f124SMoriah Waterland extern void	quit(int retcode); 	/* Expected to be declared by caller! */
47*5c51f124SMoriah Waterland /* libadm.a */
48*5c51f124SMoriah Waterland extern int	getvol(char *device, char *label, int options, char *prompt);
49*5c51f124SMoriah Waterland 
50*5c51f124SMoriah Waterland #define	CMDSIZ	256
51*5c51f124SMoriah Waterland 
52*5c51f124SMoriah Waterland int
53*5c51f124SMoriah Waterland pkgmount(struct pkgdev *devp, char *pkg, int part, int nparts, int getvolflg)
54*5c51f124SMoriah Waterland {
55*5c51f124SMoriah Waterland 	int	n;
56*5c51f124SMoriah Waterland 	char	*pt, prompt[64], cmd[CMDSIZ];
57*5c51f124SMoriah Waterland 	FILE	*pp;
58*5c51f124SMoriah Waterland 
59*5c51f124SMoriah Waterland 	if (getuid()) {
60*5c51f124SMoriah Waterland 		progerr(pkg_gt(ERR_NOTROOT));
61*5c51f124SMoriah Waterland 		return (99);
62*5c51f124SMoriah Waterland 	}
63*5c51f124SMoriah Waterland 
64*5c51f124SMoriah Waterland 	if (part && nparts) {
65*5c51f124SMoriah Waterland 		if (pkg) {
66*5c51f124SMoriah Waterland 			(void) sprintf(prompt, pkg_gt(LABEL0), part,
67*5c51f124SMoriah Waterland 			    nparts, pkg);
68*5c51f124SMoriah Waterland 		} else {
69*5c51f124SMoriah Waterland 			(void) sprintf(prompt, pkg_gt(LABEL1), part,
70*5c51f124SMoriah Waterland 			    nparts);
71*5c51f124SMoriah Waterland 		}
72*5c51f124SMoriah Waterland 	} else if (pkg)
73*5c51f124SMoriah Waterland 		(void) sprintf(prompt, pkg_gt(LABEL2), pkg);
74*5c51f124SMoriah Waterland 	else
75*5c51f124SMoriah Waterland 		(void) sprintf(prompt, pkg_gt(LABEL3));
76*5c51f124SMoriah Waterland 
77*5c51f124SMoriah Waterland 	n = 0;
78*5c51f124SMoriah Waterland 	for (;;) {
79*5c51f124SMoriah Waterland 		if (!getvolflg && n)
80*5c51f124SMoriah Waterland 			/*
81*5c51f124SMoriah Waterland 			 * Return to caller if not prompting
82*5c51f124SMoriah Waterland 			 * and error was encountered.
83*5c51f124SMoriah Waterland 			 */
84*5c51f124SMoriah Waterland 			return (-1);
85*5c51f124SMoriah Waterland 		if (getvolflg && (n = getvol(devp->bdevice, NULL,
86*5c51f124SMoriah Waterland 		    (devp->rdonly ? 0 : DM_FORMFS|DM_WLABEL), prompt))) {
87*5c51f124SMoriah Waterland 			if (n == 3)
88*5c51f124SMoriah Waterland 				return (3);
89*5c51f124SMoriah Waterland 			if (n == 2)
90*5c51f124SMoriah Waterland 				progerr(pkg_gt("unknown device <%s>"),
91*5c51f124SMoriah Waterland 				    devp->bdevice);
92*5c51f124SMoriah Waterland 			else
93*5c51f124SMoriah Waterland 				progerr(
94*5c51f124SMoriah Waterland 				    pkg_gt("unable to obtain package volume"));
95*5c51f124SMoriah Waterland 			return (99);
96*5c51f124SMoriah Waterland 		}
97*5c51f124SMoriah Waterland 
98*5c51f124SMoriah Waterland 		if (devp->fstyp == NULL) {
99*5c51f124SMoriah Waterland 			(void) sprintf(cmd, "%s %s", FSTYP, devp->bdevice);
100*5c51f124SMoriah Waterland 			if ((pp = epopen(cmd, "r")) == NULL) {
101*5c51f124SMoriah Waterland 				rpterr();
102*5c51f124SMoriah Waterland 				logerr(pkg_gt(ERR_FSTYP), devp->bdevice);
103*5c51f124SMoriah Waterland 				n = -1;
104*5c51f124SMoriah Waterland 				continue;
105*5c51f124SMoriah Waterland 			}
106*5c51f124SMoriah Waterland 			cmd[0] = '\0';
107*5c51f124SMoriah Waterland 			if (fgets(cmd, CMDSIZ, pp) == NULL) {
108*5c51f124SMoriah Waterland 				logerr(pkg_gt(ERR_FSTYP), devp->bdevice);
109*5c51f124SMoriah Waterland 				(void) pclose(pp);
110*5c51f124SMoriah Waterland 				n = -1;
111*5c51f124SMoriah Waterland 				continue;
112*5c51f124SMoriah Waterland 			}
113*5c51f124SMoriah Waterland 			if (epclose(pp)) {
114*5c51f124SMoriah Waterland 				rpterr();
115*5c51f124SMoriah Waterland 				logerr(pkg_gt(ERR_FSTYP), devp->bdevice);
116*5c51f124SMoriah Waterland 				n = -1;
117*5c51f124SMoriah Waterland 				continue;
118*5c51f124SMoriah Waterland 			}
119*5c51f124SMoriah Waterland 			if (pt = strpbrk(cmd, " \t\n"))
120*5c51f124SMoriah Waterland 				*pt = '\0';
121*5c51f124SMoriah Waterland 			if (cmd[0] == '\0') {
122*5c51f124SMoriah Waterland 				logerr(pkg_gt(ERR_FSTYP), devp->bdevice);
123*5c51f124SMoriah Waterland 				n = -1;
124*5c51f124SMoriah Waterland 				continue;
125*5c51f124SMoriah Waterland 			}
126*5c51f124SMoriah Waterland 			devp->fstyp = strdup(cmd);
127*5c51f124SMoriah Waterland 		}
128*5c51f124SMoriah Waterland 
129*5c51f124SMoriah Waterland 		if (devp->rdonly) {
130*5c51f124SMoriah Waterland 			n = pkgexecl(NULL, NULL, NULL, NULL, MOUNT, "-r", "-F",
131*5c51f124SMoriah Waterland 			    devp->fstyp, devp->bdevice, devp->mount, NULL);
132*5c51f124SMoriah Waterland 		} else {
133*5c51f124SMoriah Waterland 			n = pkgexecl(NULL, NULL, NULL, NULL, MOUNT, "-F",
134*5c51f124SMoriah Waterland 			    devp->fstyp, devp->bdevice, devp->mount, NULL);
135*5c51f124SMoriah Waterland 		}
136*5c51f124SMoriah Waterland 		if (n) {
137*5c51f124SMoriah Waterland 			progerr(pkg_gt("mount of %s failed"), devp->bdevice);
138*5c51f124SMoriah Waterland 			continue;
139*5c51f124SMoriah Waterland 		}
140*5c51f124SMoriah Waterland 		devp->mntflg++;
141*5c51f124SMoriah Waterland 		break;
142*5c51f124SMoriah Waterland 	}
143*5c51f124SMoriah Waterland 	return (0);
144*5c51f124SMoriah Waterland }
145*5c51f124SMoriah Waterland 
146*5c51f124SMoriah Waterland int
147*5c51f124SMoriah Waterland pkgumount(struct pkgdev *devp)
148*5c51f124SMoriah Waterland {
149*5c51f124SMoriah Waterland 	int	n = 1;
150*5c51f124SMoriah Waterland 	int	retry = 10;
151*5c51f124SMoriah Waterland 
152*5c51f124SMoriah Waterland 	if (!devp->mntflg)
153*5c51f124SMoriah Waterland 		return (0);
154*5c51f124SMoriah Waterland 
155*5c51f124SMoriah Waterland 	while (n != 0 && retry-- > 0) {
156*5c51f124SMoriah Waterland 		n = pkgexecl(NULL, NULL, NULL, NULL, UMOUNT, devp->bdevice,
157*5c51f124SMoriah Waterland 		    NULL);
158*5c51f124SMoriah Waterland 		if (n != 0) {
159*5c51f124SMoriah Waterland 			progerr(pkg_gt("retrying umount of %s"),
160*5c51f124SMoriah Waterland 			    devp->bdevice);
161*5c51f124SMoriah Waterland 			sleep(5);
162*5c51f124SMoriah Waterland 		}
163*5c51f124SMoriah Waterland 	}
164*5c51f124SMoriah Waterland 	if (n == 0)
165*5c51f124SMoriah Waterland 		devp->mntflg = 0;
166*5c51f124SMoriah Waterland 	return (n);
167*5c51f124SMoriah Waterland }
168