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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <strings.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/types.h>
2N/A#include <sys/mount.h>
2N/A#include <sys/mntent.h>
2N/A#include <unistd.h>
2N/A
2N/A/*
2N/A * This program aids the Solaris 10 patch tools (specifically
2N/A * /usr/lib/patch/patch_common_lib) in DAP patching.
2N/A *
2N/A * Whenever the patch tools replace a critical system component (e.g.,
2N/A * /lib/libc.so.1), they move the old component to a temporary location,
2N/A * move the new component to where the old component was, and establish
2N/A * an overlay mount of the old component on top of the new component.
2N/A * The patch tools do this with a shell script; consequently, the three
2N/A * operations occur in three processes.
2N/A *
2N/A * This doesn't work inside Solaris 10 Containers (S10Cs). Suppose the
2N/A * patch tools need to replace /lib/libc.so.1. The tools will move the old
2N/A * libc.so.1 to a temporary location. But when they try to move the new
2N/A * libc.so.1, they fork a mv(1) process, which loads the solaris10 brand's
2N/A * emulation library. The emulation library will try to load the zone's
2N/A * libc.so.1, but the library no longer exists; consequently, the emulation
2N/A * library aborts and the zone's users won't be able to start any processes.
2N/A *
2N/A * This program solves the problem by combining the move and mount operations
2N/A * into a single process. The emulation library will already have loaded
2N/A * libc.so.1 for the process by the time the process starts to replace
2N/A * libc.so.1.
2N/A *
2N/A * This program takes six parameters that correspond to six variables within
2N/A * /usr/lib/patch/patch_common_lib:InstallSafemodeObject():
2N/A *
2N/A * argv[1] - dstActual (the path to the file that will be replaced)
2N/A * argv[2] - tmp_file (the temporary location to which the file will be
2N/A * moved)
2N/A * argv[3] - tmpDst (the path to the replacement file)
2N/A * argv[4] - tmpFile (the path to a temporary copy of the running system's
2N/A * version of the file being replaced; the source [special] of
2N/A * the overlay mount)
2N/A * argv[5] - cksumTmpDst (checksum of the file represented by tmpDst)
2N/A * argv[6] - cksumTmpFile (checksum of the file represented by tmpFile)
2N/A *
2N/A * NOTE: This program will only establish an overlay mount if argv[4] or argv[5]
2N/A * is emtpy or if argv[4] and argv[5] differ.
2N/A *
2N/A * This program returns zero when it succeeds. Non-negative values indicate
2N/A * failure.
2N/A */
2N/Aint
2N/Amain(int argc, char **argv)
2N/A{
2N/A struct stat statbuf;
2N/A char mntoptions[MAX_MNTOPT_STR];
2N/A
2N/A /*
2N/A * Check the number of arguments that were passed to s10_replacefile.
2N/A */
2N/A if (argc != 7) {
2N/A (void) fprintf(stderr, "Usage: %s dstActual tmp_file tmpDst "
2N/A "tmpFile cksumTmpDst cksumTmpFile\n", argv[0]);
2N/A return (1);
2N/A }
2N/A
2N/A /*
2N/A * Move the destination file (dstActual) out of the way and move the
2N/A * new file (tmpDst) into its place.
2N/A *
2N/A * NOTE: s10_replacefile won't print error messages here because
2N/A * the Solaris 10 patch tools will.
2N/A */
2N/A if (rename(argv[1], argv[2]) != 0)
2N/A return (2);
2N/A if (rename(argv[3], argv[1]) != 0)
2N/A return (3);
2N/A
2N/A /*
2N/A * If there was a lofs mount on dstActual (which we just moved), then
2N/A * s10_replacefile should reestablish the lofs mount. A lofs mount
2N/A * existed if tmpFile exists.
2N/A */
2N/A if (stat(argv[4], &statbuf) == 0 && (statbuf.st_mode & S_IFREG)) {
2N/A /*
2N/A * Create a lofs overlay mount only if the checksums of the
2N/A * old file at dstActual and the new file at dstActual differ.
2N/A */
2N/A if (argv[5][0] == '\0' || argv[6][0] == '\0' ||
2N/A strcmp(argv[5], argv[6]) != 0) {
2N/A mntoptions[0] = '\0';
2N/A if (mount(argv[4], argv[1], MS_OVERLAY | MS_OPTIONSTR,
2N/A MNTTYPE_LOFS, NULL, 0, mntoptions,
2N/A sizeof (mntoptions)) != 0) {
2N/A /*
2N/A * Although the patch tools will print error
2N/A * messages, the tools won't know that
2N/A * s10_replacefile failed to establish an
2N/A * overlay mount. Printing an error message
2N/A * here clarifies the problem for the user.
2N/A */
2N/A (void) fprintf(stderr, "ERROR: Failed to "
2N/A "overlay mount %s onto %s\n", argv[4],
2N/A argv[1]);
2N/A return (4);
2N/A }
2N/A } else {
2N/A /*
2N/A * dstActual does not need an overlay mount. Delete
2N/A * tmpFile.
2N/A */
2N/A (void) unlink(argv[4]);
2N/A }
2N/A }
2N/A return (0);
2N/A}