summaryrefslogtreecommitdiff
path: root/usr/src/lib/libzonecfg/common/scratchops.c
blob: 04e5724660bb259f95666ed33c4e60afcfb6bf13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This module contains functions used for reading and writing the scratch zone
 * translation files.  These files are used by Live Upgrade to keep track of
 * mappings between actual kernel zone names and the zones in an alternate boot
 * environment.
 *
 * The functions are MT-safe.
 *
 * The file format looks like this:
 *
 *	<zonename>	<kernel-zonename>	<alt-root>
 *
 * The expected usage model is:
 *
 *	fp = zonecfg_open_scratch("", B_TRUE);
 *	zonecfg_lock_scratch(fp);
 *	if (zonecfg_find_scratch(fp, zonename, altroot, NULL, 0) == 0) {
 *		handle error; zone already mounted
 *	}
 *	mount zone here
 *	zonecfg_add_scratch(fp, zonename, kernname, altroot);
 *	zonecfg_close_scratch(fp);
 *	fp = zonecfg_open_scratch(zoneroot, B_TRUE);
 *	ftruncate(fileno(fp), 0);
 *	zonecfg_add_scratch(fp, zonename, kernname, "/");
 *	zonecfg_close_scratch(fp);
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <libzonecfg.h>

#define	PATH_MAPFILE	"tmp/.alt.lu-zone-map"

static int
lock_op(int fd, int type)
{
	struct flock lock;

	lock.l_type = type;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	return (fcntl(fd, F_SETLKW, &lock));
}

FILE *
zonecfg_open_scratch(const char *rootpath, boolean_t createfile)
{
	mode_t oldmask = umask(0);
	struct stat lbuf, fbuf;
	int fd, flags;
	FILE *fp;
	char mapfile[MAXPATHLEN];

	(void) snprintf(mapfile, sizeof (mapfile), "%s/" PATH_MAPFILE,
	    rootpath);

	flags = O_RDWR | O_NOFOLLOW | O_NOLINKS;
	if (createfile)
		flags |= O_EXCL | O_CREAT;
	if ((fd = open(mapfile, flags, 0644)) == -1) {
		if (!createfile) {
			errno = ENOENT;
			goto failure;
		}
		if (lstat(mapfile, &lbuf) == -1)
			goto failure;
		if (!S_ISREG(lbuf.st_mode) || lbuf.st_nlink != 1 ||
		    lbuf.st_uid != 0) {
			errno = EINVAL;
			goto failure;
		}
		fd = open(mapfile, O_RDWR);
		if (fd == -1)
			goto failure;
		if (fstat(fd, &fbuf) == -1)
			goto failure;
		if (lbuf.st_ino != fbuf.st_ino || lbuf.st_dev != fbuf.st_dev) {
			errno = EINVAL;
			goto failure;
		}
	}
	if (lock_op(fd, F_RDLCK) == -1)
		goto failure;
	(void) umask(oldmask);
	if ((fp = fdopen(fd, "r+")) == NULL)
		(void) close(fd);
	return (fp);

failure:
	if (fd != -1)
		(void) close(fd);
	(void) umask(oldmask);
	return (NULL);
}

int
zonecfg_lock_scratch(FILE *fp)
{
	if (fflush(fp) != 0)
		return (-1);
	return (lock_op(fileno(fp), F_WRLCK));
}

void
zonecfg_close_scratch(FILE *fp)
{
	(void) fclose(fp);
}

int
zonecfg_get_scratch(FILE *fp, char *zonename, size_t namelen, char *kernname,
    size_t kernlen, char *altroot, size_t altlen)
{
	char line[2 * ZONENAME_MAX + MAXPATHLEN + 2];
	char *cp, *cp2;

	/* We always hold at least a read lock on the file */
	for (;;) {
		if (fgets(line, sizeof (line), fp) == NULL)
			return (-1);
		if ((cp = strchr(line, '\n')) == NULL)
			return (-1);
		*cp = '\0';
		if ((cp = strchr(line, ' ')) == NULL)
			cp = line + strlen(line);
		else
			*cp++ = '\0';
		if (zonename != NULL &&
		    strlcpy(zonename, line, namelen) >= namelen)
			continue;
		if ((cp2 = strchr(cp, ' ')) == NULL)
			cp2 = cp + strlen(cp);
		else
			*cp2++ = '\0';
		if (kernname != NULL &&
		    strlcpy(kernname, cp, kernlen) >= kernlen)
			continue;
		if (altroot != NULL && strlcpy(altroot, cp2, altlen) >= altlen)
			continue;
		break;
	}
	return (0);
}

int
zonecfg_find_scratch(FILE *fp, const char *zonename, const char *altroot,
    char *kernzone, size_t kernlen)
{
	char zone[ZONENAME_MAX];
	char aroot[MAXPATHLEN];

	rewind(fp);
	while (zonecfg_get_scratch(fp, zone, sizeof (zone), kernzone, kernlen,
	    aroot, sizeof (aroot)) == 0) {
		if (strcmp(zone, zonename) == 0 && strcmp(altroot, aroot) == 0)
			return (0);
	}
	return (-1);
}

int
zonecfg_reverse_scratch(FILE *fp, const char *kernzone, char *zonename,
    size_t namelen, char *altroot, size_t altlen)
{
	char kzone[ZONENAME_MAX];

	rewind(fp);
	while (zonecfg_get_scratch(fp, zonename, namelen, kzone,
	    sizeof (kzone), altroot, altlen) == 0) {
		if (strcmp(kzone, kernzone) == 0)
			return (0);
	}
	return (-1);
}

int
zonecfg_add_scratch(FILE *fp, const char *zonename, const char *kernzone,
    const char *altroot)
{
	if (fseek(fp, 0, SEEK_END) == -1)
		return (-1);
	if (fprintf(fp, "%s %s %s\n", zonename, kernzone, altroot) == EOF)
		return (-1);
	if (fflush(fp) != 0)
		return (-1);
	return (0);
}

int
zonecfg_delete_scratch(FILE *fp, const char *kernzone)
{
	char zone[ZONENAME_MAX];
	char kzone[ZONENAME_MAX];
	char aroot[MAXPATHLEN];
	long roffs, woffs;

	/*
	 * The implementation here is intentionally quite simple.  We could
	 * allocate a buffer that's big enough to hold the data up to
	 * stat.st_size and then write back out the part we need to, but there
	 * seems to be little point.
	 */
	rewind(fp);
	roffs = 0;
	do {
		woffs = roffs;
		if (zonecfg_get_scratch(fp, NULL, 0, kzone, sizeof (kzone),
		    NULL, 0) != 0)
			return (-1);
		roffs = ftell(fp);
	} while (strcmp(kzone, kernzone) != 0);
	while (zonecfg_get_scratch(fp, zone, sizeof (zone), kzone,
	    sizeof (kzone), aroot, sizeof aroot) == 0) {
		roffs = ftell(fp);
		if (fseek(fp, woffs, SEEK_SET) == -1)
			break;
		if (fprintf(fp, "%s %s %s\n", zone, kzone, aroot) == EOF)
			break;
		woffs = ftell(fp);
		if (fseek(fp, roffs, SEEK_SET) == -1)
			break;
	}
	(void) ftruncate(fileno(fp), woffs);
	return (0);
}

boolean_t
zonecfg_is_scratch(const char *kernzone)
{
	return (strncmp(kernzone, "SUNWlu", 6) == 0);
}