8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libzonecfg / common / scratchops.c
blob04e5724660bb259f95666ed33c4e60afcfb6bf13
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This module contains functions used for reading and writing the scratch zone
31 * translation files. These files are used by Live Upgrade to keep track of
32 * mappings between actual kernel zone names and the zones in an alternate boot
33 * environment.
35 * The functions are MT-safe.
37 * The file format looks like this:
39 * <zonename> <kernel-zonename> <alt-root>
41 * The expected usage model is:
43 * fp = zonecfg_open_scratch("", B_TRUE);
44 * zonecfg_lock_scratch(fp);
45 * if (zonecfg_find_scratch(fp, zonename, altroot, NULL, 0) == 0) {
46 * handle error; zone already mounted
47 * }
48 * mount zone here
49 * zonecfg_add_scratch(fp, zonename, kernname, altroot);
50 * zonecfg_close_scratch(fp);
51 * fp = zonecfg_open_scratch(zoneroot, B_TRUE);
52 * ftruncate(fileno(fp), 0);
53 * zonecfg_add_scratch(fp, zonename, kernname, "/");
54 * zonecfg_close_scratch(fp);
57 #include <stdio.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 #include <string.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/param.h>
65 #include <libzonecfg.h>
67 #define PATH_MAPFILE "tmp/.alt.lu-zone-map"
69 static int
70 lock_op(int fd, int type)
72 struct flock lock;
74 lock.l_type = type;
75 lock.l_whence = SEEK_SET;
76 lock.l_start = 0;
77 lock.l_len = 0;
79 return (fcntl(fd, F_SETLKW, &lock));
82 FILE *
83 zonecfg_open_scratch(const char *rootpath, boolean_t createfile)
85 mode_t oldmask = umask(0);
86 struct stat lbuf, fbuf;
87 int fd, flags;
88 FILE *fp;
89 char mapfile[MAXPATHLEN];
91 (void) snprintf(mapfile, sizeof (mapfile), "%s/" PATH_MAPFILE,
92 rootpath);
94 flags = O_RDWR | O_NOFOLLOW | O_NOLINKS;
95 if (createfile)
96 flags |= O_EXCL | O_CREAT;
97 if ((fd = open(mapfile, flags, 0644)) == -1) {
98 if (!createfile) {
99 errno = ENOENT;
100 goto failure;
102 if (lstat(mapfile, &lbuf) == -1)
103 goto failure;
104 if (!S_ISREG(lbuf.st_mode) || lbuf.st_nlink != 1 ||
105 lbuf.st_uid != 0) {
106 errno = EINVAL;
107 goto failure;
109 fd = open(mapfile, O_RDWR);
110 if (fd == -1)
111 goto failure;
112 if (fstat(fd, &fbuf) == -1)
113 goto failure;
114 if (lbuf.st_ino != fbuf.st_ino || lbuf.st_dev != fbuf.st_dev) {
115 errno = EINVAL;
116 goto failure;
119 if (lock_op(fd, F_RDLCK) == -1)
120 goto failure;
121 (void) umask(oldmask);
122 if ((fp = fdopen(fd, "r+")) == NULL)
123 (void) close(fd);
124 return (fp);
126 failure:
127 if (fd != -1)
128 (void) close(fd);
129 (void) umask(oldmask);
130 return (NULL);
134 zonecfg_lock_scratch(FILE *fp)
136 if (fflush(fp) != 0)
137 return (-1);
138 return (lock_op(fileno(fp), F_WRLCK));
141 void
142 zonecfg_close_scratch(FILE *fp)
144 (void) fclose(fp);
148 zonecfg_get_scratch(FILE *fp, char *zonename, size_t namelen, char *kernname,
149 size_t kernlen, char *altroot, size_t altlen)
151 char line[2 * ZONENAME_MAX + MAXPATHLEN + 2];
152 char *cp, *cp2;
154 /* We always hold at least a read lock on the file */
155 for (;;) {
156 if (fgets(line, sizeof (line), fp) == NULL)
157 return (-1);
158 if ((cp = strchr(line, '\n')) == NULL)
159 return (-1);
160 *cp = '\0';
161 if ((cp = strchr(line, ' ')) == NULL)
162 cp = line + strlen(line);
163 else
164 *cp++ = '\0';
165 if (zonename != NULL &&
166 strlcpy(zonename, line, namelen) >= namelen)
167 continue;
168 if ((cp2 = strchr(cp, ' ')) == NULL)
169 cp2 = cp + strlen(cp);
170 else
171 *cp2++ = '\0';
172 if (kernname != NULL &&
173 strlcpy(kernname, cp, kernlen) >= kernlen)
174 continue;
175 if (altroot != NULL && strlcpy(altroot, cp2, altlen) >= altlen)
176 continue;
177 break;
179 return (0);
183 zonecfg_find_scratch(FILE *fp, const char *zonename, const char *altroot,
184 char *kernzone, size_t kernlen)
186 char zone[ZONENAME_MAX];
187 char aroot[MAXPATHLEN];
189 rewind(fp);
190 while (zonecfg_get_scratch(fp, zone, sizeof (zone), kernzone, kernlen,
191 aroot, sizeof (aroot)) == 0) {
192 if (strcmp(zone, zonename) == 0 && strcmp(altroot, aroot) == 0)
193 return (0);
195 return (-1);
199 zonecfg_reverse_scratch(FILE *fp, const char *kernzone, char *zonename,
200 size_t namelen, char *altroot, size_t altlen)
202 char kzone[ZONENAME_MAX];
204 rewind(fp);
205 while (zonecfg_get_scratch(fp, zonename, namelen, kzone,
206 sizeof (kzone), altroot, altlen) == 0) {
207 if (strcmp(kzone, kernzone) == 0)
208 return (0);
210 return (-1);
214 zonecfg_add_scratch(FILE *fp, const char *zonename, const char *kernzone,
215 const char *altroot)
217 if (fseek(fp, 0, SEEK_END) == -1)
218 return (-1);
219 if (fprintf(fp, "%s %s %s\n", zonename, kernzone, altroot) == EOF)
220 return (-1);
221 if (fflush(fp) != 0)
222 return (-1);
223 return (0);
227 zonecfg_delete_scratch(FILE *fp, const char *kernzone)
229 char zone[ZONENAME_MAX];
230 char kzone[ZONENAME_MAX];
231 char aroot[MAXPATHLEN];
232 long roffs, woffs;
235 * The implementation here is intentionally quite simple. We could
236 * allocate a buffer that's big enough to hold the data up to
237 * stat.st_size and then write back out the part we need to, but there
238 * seems to be little point.
240 rewind(fp);
241 roffs = 0;
242 do {
243 woffs = roffs;
244 if (zonecfg_get_scratch(fp, NULL, 0, kzone, sizeof (kzone),
245 NULL, 0) != 0)
246 return (-1);
247 roffs = ftell(fp);
248 } while (strcmp(kzone, kernzone) != 0);
249 while (zonecfg_get_scratch(fp, zone, sizeof (zone), kzone,
250 sizeof (kzone), aroot, sizeof aroot) == 0) {
251 roffs = ftell(fp);
252 if (fseek(fp, woffs, SEEK_SET) == -1)
253 break;
254 if (fprintf(fp, "%s %s %s\n", zone, kzone, aroot) == EOF)
255 break;
256 woffs = ftell(fp);
257 if (fseek(fp, roffs, SEEK_SET) == -1)
258 break;
260 (void) ftruncate(fileno(fp), woffs);
261 return (0);
264 boolean_t
265 zonecfg_is_scratch(const char *kernzone)
267 return (strncmp(kernzone, "SUNWlu", 6) == 0);