xref: /onnv-gate/usr/src/lib/libzonecfg/common/scratchops.c (revision 766:c521de78a32f)
1*766Scarlsonj /*
2*766Scarlsonj  * CDDL HEADER START
3*766Scarlsonj  *
4*766Scarlsonj  * The contents of this file are subject to the terms of the
5*766Scarlsonj  * Common Development and Distribution License (the "License").
6*766Scarlsonj  * You may not use this file except in compliance with the License.
7*766Scarlsonj  *
8*766Scarlsonj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*766Scarlsonj  * or http://www.opensolaris.org/os/licensing.
10*766Scarlsonj  * See the License for the specific language governing permissions
11*766Scarlsonj  * and limitations under the License.
12*766Scarlsonj  *
13*766Scarlsonj  * When distributing Covered Code, include this CDDL HEADER in each
14*766Scarlsonj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*766Scarlsonj  * If applicable, add the following below this CDDL HEADER, with the
16*766Scarlsonj  * fields enclosed by brackets "[]" replaced with your own identifying
17*766Scarlsonj  * information: Portions Copyright [yyyy] [name of copyright owner]
18*766Scarlsonj  *
19*766Scarlsonj  * CDDL HEADER END
20*766Scarlsonj  */
21*766Scarlsonj 
22*766Scarlsonj /*
23*766Scarlsonj  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*766Scarlsonj  * Use is subject to license terms.
25*766Scarlsonj  */
26*766Scarlsonj 
27*766Scarlsonj #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*766Scarlsonj 
29*766Scarlsonj /*
30*766Scarlsonj  * This module contains functions used for reading and writing the scratch zone
31*766Scarlsonj  * translation files.  These files are used by Live Upgrade to keep track of
32*766Scarlsonj  * mappings between actual kernel zone names and the zones in an alternate boot
33*766Scarlsonj  * environment.
34*766Scarlsonj  *
35*766Scarlsonj  * The functions are MT-safe.
36*766Scarlsonj  *
37*766Scarlsonj  * The file format looks like this:
38*766Scarlsonj  *
39*766Scarlsonj  *	<zonename>	<kernel-zonename>	<alt-root>
40*766Scarlsonj  *
41*766Scarlsonj  * The expected usage model is:
42*766Scarlsonj  *
43*766Scarlsonj  *	fp = zonecfg_open_scratch("", B_TRUE);
44*766Scarlsonj  *	zonecfg_lock_scratch(fp);
45*766Scarlsonj  *	if (zonecfg_find_scratch(fp, zonename, altroot, NULL, 0) == 0) {
46*766Scarlsonj  *		handle error; zone already mounted
47*766Scarlsonj  *	}
48*766Scarlsonj  *	mount zone here
49*766Scarlsonj  *	zonecfg_add_scratch(fp, zonename, kernname, altroot);
50*766Scarlsonj  *	zonecfg_close_scratch(fp);
51*766Scarlsonj  *	fp = zonecfg_open_scratch(zoneroot, B_TRUE);
52*766Scarlsonj  *	ftruncate(fileno(fp), 0);
53*766Scarlsonj  *	zonecfg_add_scratch(fp, zonename, kernname, "/");
54*766Scarlsonj  *	zonecfg_close_scratch(fp);
55*766Scarlsonj  */
56*766Scarlsonj 
57*766Scarlsonj #include <stdio.h>
58*766Scarlsonj #include <unistd.h>
59*766Scarlsonj #include <fcntl.h>
60*766Scarlsonj #include <errno.h>
61*766Scarlsonj #include <string.h>
62*766Scarlsonj #include <sys/types.h>
63*766Scarlsonj #include <sys/stat.h>
64*766Scarlsonj #include <sys/param.h>
65*766Scarlsonj #include <libzonecfg.h>
66*766Scarlsonj 
67*766Scarlsonj #define	PATH_MAPFILE	"tmp/.alt.lu-zone-map"
68*766Scarlsonj 
69*766Scarlsonj static int
lock_op(int fd,int type)70*766Scarlsonj lock_op(int fd, int type)
71*766Scarlsonj {
72*766Scarlsonj 	struct flock lock;
73*766Scarlsonj 
74*766Scarlsonj 	lock.l_type = type;
75*766Scarlsonj 	lock.l_whence = SEEK_SET;
76*766Scarlsonj 	lock.l_start = 0;
77*766Scarlsonj 	lock.l_len = 0;
78*766Scarlsonj 
79*766Scarlsonj 	return (fcntl(fd, F_SETLKW, &lock));
80*766Scarlsonj }
81*766Scarlsonj 
82*766Scarlsonj FILE *
zonecfg_open_scratch(const char * rootpath,boolean_t createfile)83*766Scarlsonj zonecfg_open_scratch(const char *rootpath, boolean_t createfile)
84*766Scarlsonj {
85*766Scarlsonj 	mode_t oldmask = umask(0);
86*766Scarlsonj 	struct stat lbuf, fbuf;
87*766Scarlsonj 	int fd, flags;
88*766Scarlsonj 	FILE *fp;
89*766Scarlsonj 	char mapfile[MAXPATHLEN];
90*766Scarlsonj 
91*766Scarlsonj 	(void) snprintf(mapfile, sizeof (mapfile), "%s/" PATH_MAPFILE,
92*766Scarlsonj 	    rootpath);
93*766Scarlsonj 
94*766Scarlsonj 	flags = O_RDWR | O_NOFOLLOW | O_NOLINKS;
95*766Scarlsonj 	if (createfile)
96*766Scarlsonj 		flags |= O_EXCL | O_CREAT;
97*766Scarlsonj 	if ((fd = open(mapfile, flags, 0644)) == -1) {
98*766Scarlsonj 		if (!createfile) {
99*766Scarlsonj 			errno = ENOENT;
100*766Scarlsonj 			goto failure;
101*766Scarlsonj 		}
102*766Scarlsonj 		if (lstat(mapfile, &lbuf) == -1)
103*766Scarlsonj 			goto failure;
104*766Scarlsonj 		if (!S_ISREG(lbuf.st_mode) || lbuf.st_nlink != 1 ||
105*766Scarlsonj 		    lbuf.st_uid != 0) {
106*766Scarlsonj 			errno = EINVAL;
107*766Scarlsonj 			goto failure;
108*766Scarlsonj 		}
109*766Scarlsonj 		fd = open(mapfile, O_RDWR);
110*766Scarlsonj 		if (fd == -1)
111*766Scarlsonj 			goto failure;
112*766Scarlsonj 		if (fstat(fd, &fbuf) == -1)
113*766Scarlsonj 			goto failure;
114*766Scarlsonj 		if (lbuf.st_ino != fbuf.st_ino || lbuf.st_dev != fbuf.st_dev) {
115*766Scarlsonj 			errno = EINVAL;
116*766Scarlsonj 			goto failure;
117*766Scarlsonj 		}
118*766Scarlsonj 	}
119*766Scarlsonj 	if (lock_op(fd, F_RDLCK) == -1)
120*766Scarlsonj 		goto failure;
121*766Scarlsonj 	(void) umask(oldmask);
122*766Scarlsonj 	if ((fp = fdopen(fd, "r+")) == NULL)
123*766Scarlsonj 		(void) close(fd);
124*766Scarlsonj 	return (fp);
125*766Scarlsonj 
126*766Scarlsonj failure:
127*766Scarlsonj 	if (fd != -1)
128*766Scarlsonj 		(void) close(fd);
129*766Scarlsonj 	(void) umask(oldmask);
130*766Scarlsonj 	return (NULL);
131*766Scarlsonj }
132*766Scarlsonj 
133*766Scarlsonj int
zonecfg_lock_scratch(FILE * fp)134*766Scarlsonj zonecfg_lock_scratch(FILE *fp)
135*766Scarlsonj {
136*766Scarlsonj 	if (fflush(fp) != 0)
137*766Scarlsonj 		return (-1);
138*766Scarlsonj 	return (lock_op(fileno(fp), F_WRLCK));
139*766Scarlsonj }
140*766Scarlsonj 
141*766Scarlsonj void
zonecfg_close_scratch(FILE * fp)142*766Scarlsonj zonecfg_close_scratch(FILE *fp)
143*766Scarlsonj {
144*766Scarlsonj 	(void) fclose(fp);
145*766Scarlsonj }
146*766Scarlsonj 
147*766Scarlsonj int
zonecfg_get_scratch(FILE * fp,char * zonename,size_t namelen,char * kernname,size_t kernlen,char * altroot,size_t altlen)148*766Scarlsonj zonecfg_get_scratch(FILE *fp, char *zonename, size_t namelen, char *kernname,
149*766Scarlsonj     size_t kernlen, char *altroot, size_t altlen)
150*766Scarlsonj {
151*766Scarlsonj 	char line[2 * ZONENAME_MAX + MAXPATHLEN + 2];
152*766Scarlsonj 	char *cp, *cp2;
153*766Scarlsonj 
154*766Scarlsonj 	/* We always hold at least a read lock on the file */
155*766Scarlsonj 	for (;;) {
156*766Scarlsonj 		if (fgets(line, sizeof (line), fp) == NULL)
157*766Scarlsonj 			return (-1);
158*766Scarlsonj 		if ((cp = strchr(line, '\n')) == NULL)
159*766Scarlsonj 			return (-1);
160*766Scarlsonj 		*cp = '\0';
161*766Scarlsonj 		if ((cp = strchr(line, ' ')) == NULL)
162*766Scarlsonj 			cp = line + strlen(line);
163*766Scarlsonj 		else
164*766Scarlsonj 			*cp++ = '\0';
165*766Scarlsonj 		if (zonename != NULL &&
166*766Scarlsonj 		    strlcpy(zonename, line, namelen) >= namelen)
167*766Scarlsonj 			continue;
168*766Scarlsonj 		if ((cp2 = strchr(cp, ' ')) == NULL)
169*766Scarlsonj 			cp2 = cp + strlen(cp);
170*766Scarlsonj 		else
171*766Scarlsonj 			*cp2++ = '\0';
172*766Scarlsonj 		if (kernname != NULL &&
173*766Scarlsonj 		    strlcpy(kernname, cp, kernlen) >= kernlen)
174*766Scarlsonj 			continue;
175*766Scarlsonj 		if (altroot != NULL && strlcpy(altroot, cp2, altlen) >= altlen)
176*766Scarlsonj 			continue;
177*766Scarlsonj 		break;
178*766Scarlsonj 	}
179*766Scarlsonj 	return (0);
180*766Scarlsonj }
181*766Scarlsonj 
182*766Scarlsonj int
zonecfg_find_scratch(FILE * fp,const char * zonename,const char * altroot,char * kernzone,size_t kernlen)183*766Scarlsonj zonecfg_find_scratch(FILE *fp, const char *zonename, const char *altroot,
184*766Scarlsonj     char *kernzone, size_t kernlen)
185*766Scarlsonj {
186*766Scarlsonj 	char zone[ZONENAME_MAX];
187*766Scarlsonj 	char aroot[MAXPATHLEN];
188*766Scarlsonj 
189*766Scarlsonj 	rewind(fp);
190*766Scarlsonj 	while (zonecfg_get_scratch(fp, zone, sizeof (zone), kernzone, kernlen,
191*766Scarlsonj 	    aroot, sizeof (aroot)) == 0) {
192*766Scarlsonj 		if (strcmp(zone, zonename) == 0 && strcmp(altroot, aroot) == 0)
193*766Scarlsonj 			return (0);
194*766Scarlsonj 	}
195*766Scarlsonj 	return (-1);
196*766Scarlsonj }
197*766Scarlsonj 
198*766Scarlsonj int
zonecfg_reverse_scratch(FILE * fp,const char * kernzone,char * zonename,size_t namelen,char * altroot,size_t altlen)199*766Scarlsonj zonecfg_reverse_scratch(FILE *fp, const char *kernzone, char *zonename,
200*766Scarlsonj     size_t namelen, char *altroot, size_t altlen)
201*766Scarlsonj {
202*766Scarlsonj 	char kzone[ZONENAME_MAX];
203*766Scarlsonj 
204*766Scarlsonj 	rewind(fp);
205*766Scarlsonj 	while (zonecfg_get_scratch(fp, zonename, namelen, kzone,
206*766Scarlsonj 	    sizeof (kzone), altroot, altlen) == 0) {
207*766Scarlsonj 		if (strcmp(kzone, kernzone) == 0)
208*766Scarlsonj 			return (0);
209*766Scarlsonj 	}
210*766Scarlsonj 	return (-1);
211*766Scarlsonj }
212*766Scarlsonj 
213*766Scarlsonj int
zonecfg_add_scratch(FILE * fp,const char * zonename,const char * kernzone,const char * altroot)214*766Scarlsonj zonecfg_add_scratch(FILE *fp, const char *zonename, const char *kernzone,
215*766Scarlsonj     const char *altroot)
216*766Scarlsonj {
217*766Scarlsonj 	if (fseek(fp, 0, SEEK_END) == -1)
218*766Scarlsonj 		return (-1);
219*766Scarlsonj 	if (fprintf(fp, "%s %s %s\n", zonename, kernzone, altroot) == EOF)
220*766Scarlsonj 		return (-1);
221*766Scarlsonj 	if (fflush(fp) != 0)
222*766Scarlsonj 		return (-1);
223*766Scarlsonj 	return (0);
224*766Scarlsonj }
225*766Scarlsonj 
226*766Scarlsonj int
zonecfg_delete_scratch(FILE * fp,const char * kernzone)227*766Scarlsonj zonecfg_delete_scratch(FILE *fp, const char *kernzone)
228*766Scarlsonj {
229*766Scarlsonj 	char zone[ZONENAME_MAX];
230*766Scarlsonj 	char kzone[ZONENAME_MAX];
231*766Scarlsonj 	char aroot[MAXPATHLEN];
232*766Scarlsonj 	long roffs, woffs;
233*766Scarlsonj 
234*766Scarlsonj 	/*
235*766Scarlsonj 	 * The implementation here is intentionally quite simple.  We could
236*766Scarlsonj 	 * allocate a buffer that's big enough to hold the data up to
237*766Scarlsonj 	 * stat.st_size and then write back out the part we need to, but there
238*766Scarlsonj 	 * seems to be little point.
239*766Scarlsonj 	 */
240*766Scarlsonj 	rewind(fp);
241*766Scarlsonj 	roffs = 0;
242*766Scarlsonj 	do {
243*766Scarlsonj 		woffs = roffs;
244*766Scarlsonj 		if (zonecfg_get_scratch(fp, NULL, 0, kzone, sizeof (kzone),
245*766Scarlsonj 		    NULL, 0) != 0)
246*766Scarlsonj 			return (-1);
247*766Scarlsonj 		roffs = ftell(fp);
248*766Scarlsonj 	} while (strcmp(kzone, kernzone) != 0);
249*766Scarlsonj 	while (zonecfg_get_scratch(fp, zone, sizeof (zone), kzone,
250*766Scarlsonj 	    sizeof (kzone), aroot, sizeof aroot) == 0) {
251*766Scarlsonj 		roffs = ftell(fp);
252*766Scarlsonj 		if (fseek(fp, woffs, SEEK_SET) == -1)
253*766Scarlsonj 			break;
254*766Scarlsonj 		if (fprintf(fp, "%s %s %s\n", zone, kzone, aroot) == EOF)
255*766Scarlsonj 			break;
256*766Scarlsonj 		woffs = ftell(fp);
257*766Scarlsonj 		if (fseek(fp, roffs, SEEK_SET) == -1)
258*766Scarlsonj 			break;
259*766Scarlsonj 	}
260*766Scarlsonj 	(void) ftruncate(fileno(fp), woffs);
261*766Scarlsonj 	return (0);
262*766Scarlsonj }
263*766Scarlsonj 
264*766Scarlsonj boolean_t
zonecfg_is_scratch(const char * kernzone)265*766Scarlsonj zonecfg_is_scratch(const char *kernzone)
266*766Scarlsonj {
267*766Scarlsonj 	return (strncmp(kernzone, "SUNWlu", 6) == 0);
268*766Scarlsonj }
269