2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * This module contains functions used for reading and writing the scratch zone
2N/A * translation files. These files are used by Live Upgrade to keep track of
2N/A * mappings between actual kernel zone names and the zones in an alternate boot
2N/A * environment.
2N/A *
2N/A * The functions are MT-safe.
2N/A *
2N/A * The file format looks like this:
2N/A *
2N/A * <zonename> <kernel-zonename> <alt-root>
2N/A *
2N/A * The expected usage model is:
2N/A *
2N/A * fp = zonecfg_open_scratch("", B_TRUE);
2N/A * zonecfg_lock_scratch(fp);
2N/A * if (zonecfg_find_scratch(fp, zonename, altroot, NULL, 0) == 0) {
2N/A * handle error; zone already mounted
2N/A * }
2N/A * mount zone here
2N/A * zonecfg_add_scratch(fp, zonename, kernname, altroot);
2N/A * zonecfg_close_scratch(fp);
2N/A * fp = zonecfg_open_scratch(zoneroot, B_TRUE);
2N/A * ftruncate(fileno(fp), 0);
2N/A * zonecfg_add_scratch(fp, zonename, kernname, "/");
2N/A * zonecfg_close_scratch(fp);
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/param.h>
2N/A#include <libzonecfg.h>
2N/A
2N/A#define PATH_MAPFILE "tmp/.alt.lu-zone-map"
2N/A
2N/Astatic int
2N/Alock_op(int fd, int type)
2N/A{
2N/A struct flock lock;
2N/A
2N/A lock.l_type = type;
2N/A lock.l_whence = SEEK_SET;
2N/A lock.l_start = 0;
2N/A lock.l_len = 0;
2N/A
2N/A return (fcntl(fd, F_SETLKW, &lock));
2N/A}
2N/A
2N/AFILE *
2N/Azonecfg_open_scratch(const char *rootpath, boolean_t createfile)
2N/A{
2N/A mode_t oldmask = umask(0);
2N/A struct stat lbuf, fbuf;
2N/A int fd, flags;
2N/A FILE *fp;
2N/A char mapfile[MAXPATHLEN];
2N/A
2N/A (void) snprintf(mapfile, sizeof (mapfile), "%s/" PATH_MAPFILE,
2N/A rootpath);
2N/A
2N/A flags = O_RDWR | O_NOFOLLOW | O_NOLINKS;
2N/A if (createfile)
2N/A flags |= O_EXCL | O_CREAT;
2N/A if ((fd = open(mapfile, flags, 0644)) == -1) {
2N/A if (!createfile) {
2N/A errno = ENOENT;
2N/A goto failure;
2N/A }
2N/A if (lstat(mapfile, &lbuf) == -1)
2N/A goto failure;
2N/A if (!S_ISREG(lbuf.st_mode) || lbuf.st_nlink != 1 ||
2N/A lbuf.st_uid != 0) {
2N/A errno = EINVAL;
2N/A goto failure;
2N/A }
2N/A fd = open(mapfile, O_RDWR);
2N/A if (fd == -1)
2N/A goto failure;
2N/A if (fstat(fd, &fbuf) == -1)
2N/A goto failure;
2N/A if (lbuf.st_ino != fbuf.st_ino || lbuf.st_dev != fbuf.st_dev) {
2N/A errno = EINVAL;
2N/A goto failure;
2N/A }
2N/A }
2N/A if (lock_op(fd, F_RDLCK) == -1)
2N/A goto failure;
2N/A (void) umask(oldmask);
2N/A if ((fp = fdopen(fd, "r+")) == NULL)
2N/A (void) close(fd);
2N/A return (fp);
2N/A
2N/Afailure:
2N/A if (fd != -1)
2N/A (void) close(fd);
2N/A (void) umask(oldmask);
2N/A return (NULL);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_lock_scratch(FILE *fp)
2N/A{
2N/A if (fflush(fp) != 0)
2N/A return (-1);
2N/A return (lock_op(fileno(fp), F_WRLCK));
2N/A}
2N/A
2N/Avoid
2N/Azonecfg_close_scratch(FILE *fp)
2N/A{
2N/A (void) fclose(fp);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_get_scratch(FILE *fp, char *zonename, size_t namelen, char *kernname,
2N/A size_t kernlen, char *altroot, size_t altlen)
2N/A{
2N/A char line[2 * ZONENAME_MAX + MAXPATHLEN + 2];
2N/A char *cp, *cp2;
2N/A
2N/A /* We always hold at least a read lock on the file */
2N/A for (;;) {
2N/A if (fgets(line, sizeof (line), fp) == NULL)
2N/A return (-1);
2N/A if ((cp = strchr(line, '\n')) == NULL)
2N/A return (-1);
2N/A *cp = '\0';
2N/A if ((cp = strchr(line, ' ')) == NULL)
2N/A cp = line + strlen(line);
2N/A else
2N/A *cp++ = '\0';
2N/A if (zonename != NULL &&
2N/A strlcpy(zonename, line, namelen) >= namelen)
2N/A continue;
2N/A if ((cp2 = strchr(cp, ' ')) == NULL)
2N/A cp2 = cp + strlen(cp);
2N/A else
2N/A *cp2++ = '\0';
2N/A if (kernname != NULL &&
2N/A strlcpy(kernname, cp, kernlen) >= kernlen)
2N/A continue;
2N/A if (altroot != NULL && strlcpy(altroot, cp2, altlen) >= altlen)
2N/A continue;
2N/A break;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_find_scratch(FILE *fp, const char *zonename, const char *altroot,
2N/A char *kernzone, size_t kernlen)
2N/A{
2N/A char zone[ZONENAME_MAX];
2N/A char aroot[MAXPATHLEN];
2N/A
2N/A rewind(fp);
2N/A while (zonecfg_get_scratch(fp, zone, sizeof (zone), kernzone, kernlen,
2N/A aroot, sizeof (aroot)) == 0) {
2N/A if (strcmp(zone, zonename) == 0 && strcmp(altroot, aroot) == 0)
2N/A return (0);
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_reverse_scratch(FILE *fp, const char *kernzone, char *zonename,
2N/A size_t namelen, char *altroot, size_t altlen)
2N/A{
2N/A char kzone[ZONENAME_MAX];
2N/A
2N/A rewind(fp);
2N/A while (zonecfg_get_scratch(fp, zonename, namelen, kzone,
2N/A sizeof (kzone), altroot, altlen) == 0) {
2N/A if (strcmp(kzone, kernzone) == 0)
2N/A return (0);
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_add_scratch(FILE *fp, const char *zonename, const char *kernzone,
2N/A const char *altroot)
2N/A{
2N/A if (fseek(fp, 0, SEEK_END) == -1)
2N/A return (-1);
2N/A if (fprintf(fp, "%s %s %s\n", zonename, kernzone, altroot) == EOF)
2N/A return (-1);
2N/A if (fflush(fp) != 0)
2N/A return (-1);
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azonecfg_delete_scratch(FILE *fp, const char *kernzone)
2N/A{
2N/A char zone[ZONENAME_MAX];
2N/A char kzone[ZONENAME_MAX];
2N/A char aroot[MAXPATHLEN];
2N/A long roffs, woffs;
2N/A
2N/A /*
2N/A * The implementation here is intentionally quite simple. We could
2N/A * allocate a buffer that's big enough to hold the data up to
2N/A * stat.st_size and then write back out the part we need to, but there
2N/A * seems to be little point.
2N/A */
2N/A rewind(fp);
2N/A roffs = 0;
2N/A do {
2N/A woffs = roffs;
2N/A if (zonecfg_get_scratch(fp, NULL, 0, kzone, sizeof (kzone),
2N/A NULL, 0) != 0)
2N/A return (-1);
2N/A roffs = ftell(fp);
2N/A } while (strcmp(kzone, kernzone) != 0);
2N/A while (zonecfg_get_scratch(fp, zone, sizeof (zone), kzone,
2N/A sizeof (kzone), aroot, sizeof aroot) == 0) {
2N/A roffs = ftell(fp);
2N/A if (fseek(fp, woffs, SEEK_SET) == -1)
2N/A break;
2N/A if (fprintf(fp, "%s %s %s\n", zone, kzone, aroot) == EOF)
2N/A break;
2N/A woffs = ftell(fp);
2N/A if (fseek(fp, roffs, SEEK_SET) == -1)
2N/A break;
2N/A }
2N/A (void) ftruncate(fileno(fp), woffs);
2N/A return (0);
2N/A}
2N/A
2N/Aboolean_t
2N/Azonecfg_is_scratch(const char *kernzone)
2N/A{
2N/A return (strncmp(kernzone, "SUNWlu", 6) == 0);
2N/A}