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