xref: /illumos-gate/usr/src/lib/libpkg/common/pkgmount.c (revision 4f364e7c95ee7fd9d5bbeddc1940e92405bb0e72)
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 /* libadm.a */
47 extern int	getvol(char *device, char *label, int options, char *prompt);
48 
49 #define	CMDSIZ	256
50 
51 int
52 pkgmount(struct pkgdev *devp, char *pkg, int part, int nparts, int getvolflg)
53 {
54 	int	n;
55 	char	*pt, prompt[64], cmd[CMDSIZ];
56 	FILE	*pp;
57 
58 	if (getuid()) {
59 		progerr(pkg_gt(ERR_NOTROOT));
60 		return (99);
61 	}
62 
63 	if (part && nparts) {
64 		if (pkg) {
65 			(void) snprintf(prompt, sizeof (prompt),
66 			    pkg_gt(LABEL0), part, nparts, pkg);
67 		} else {
68 			(void) snprintf(prompt, sizeof (prompt),
69 			    pkg_gt(LABEL1), part, nparts);
70 		}
71 	} else if (pkg)
72 		(void) snprintf(prompt, sizeof (prompt), pkg_gt(LABEL2), pkg);
73 	else
74 		(void) snprintf(prompt, sizeof (prompt), pkg_gt(LABEL3));
75 
76 	n = 0;
77 	for (;;) {
78 		if (!getvolflg && n)
79 			/*
80 			 * Return to caller if not prompting
81 			 * and error was encountered.
82 			 */
83 			return (-1);
84 		if (getvolflg && (n = getvol(devp->bdevice, NULL,
85 		    (devp->rdonly ? 0 : DM_FORMFS|DM_WLABEL), prompt))) {
86 			if (n == 3)
87 				return (3);
88 			if (n == 2)
89 				progerr(pkg_gt("unknown device <%s>"),
90 				    devp->bdevice);
91 			else
92 				progerr(
93 				    pkg_gt("unable to obtain package volume"));
94 			return (99);
95 		}
96 
97 		if (devp->fstyp == NULL) {
98 			(void) snprintf(cmd, sizeof (cmd),
99 			    "%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 			(void) sleep(5);
162 		}
163 	}
164 	if (n == 0)
165 		devp->mntflg = 0;
166 	return (n);
167 }
168