zones_paths.c revision af1222373b60d56d6b0e630911372d4162b7787b
/*
* 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
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* System includes
*/
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <assert.h>
#include <locale.h>
#include <libintl.h>
/*
* local includes
*/
#include "instzones_lib.h"
#include "zones_strings.h"
(!x[2] || (x[2] == '/')))
/*
* forward declarations
*/
static char **inheritedFileSystems = (char **)NULL;
static int numInheritedFileSystems = 0;
/*
* *****************************************************************************
* global external (public) functions
* *****************************************************************************
*/
/*
* Name: z_get_inherited_file_systems
* Description: Return list of file systems inherited from the global zone;
* These file systems are entered into the list when the function
* pkgAddInheritedFileSystem() is called.
* Arguments: void
* Returns: char **
* - pointer to array of character pointers, each pointer
* being a pointer to a string representing a file
* system that is inherited from the global zone
* the last entry will be (char *)NULL
* - (char **)NULL - no file systems inherited
*
*/
char **
{
return (inheritedFileSystems);
}
/*
* Name: z_add_inherited_file_system
* Description: Add specified package to internal list of inherited file systems
* Arguments: a_inheritedFileSystem - absolute path to file systen "inherited"
*
* This function is called to register a directory (or
* file system) as being inherited from the global zone
* into the non-global zone being operated on. The
* inherited directory must be specified relative to the
* root file system ("/"). For example, if "/usr" is
* inherited, then the path specified would be "/usr".
*
* Any path subsequently checked for being present in a
* directory inherited read-only from the global zone:
*
* -- will NOT have $PKG_INSTALL_ROOT prepended to it
* -- if $PKG_INSTALL_ROOT is set and $BASEDIR is not set.
* -- WILL have $BASEDIR prepended to it (if set).
* -- $BASEDIR always has $PKG_INSTALL_ROOT included in it.
* -- For example, if $PKG_INSTALL_ROOT is set to /a, and
* -- the base install directory is set to "/opt", then the
*
* Any path that is checked for being present in an inherited
* directory will be specified relative to the root file system
* of the non-global zone in which the path is located.
*
* When a path to update is checked for being present in
* an inherited directory, $PKG_INSTALL_ROOT is stripped
* off the path before it is checked.
*
* If the non-global zone is not running, the scratch zone
* is used to access the non-global zone. In this case,
* $PKG_INSTALL_ROOT will be set to "/a" and both the
* non-global zone's root file system and all inherited
* directories will be mounted on "/a". When a path is checked
* for being inherited, it will have $PKG_INSTALL_ROOT stripped
* from the beginning, so any inherited directories must be
* specified relative to "/" and not $PKG_INSTALL_ROOT.
*
* If the non-global zone is running, the non-global zone
* is used directly. In this case, $PKG_INSTALL_ROOT will
* be set to "/" and both the non-global zone's root file
* system and all inherited directories will be mounted on
* "/". $PKG_INSTALL_ROOT is set to "/" so the path is unchanged
* before being checked against the list of inherited directories.
*
* Returns: boolean_t
* B_TRUE - file system successfully added to list
* B_FALSE - failed to add file system to list
*/
{
int n;
/* file system cannot be empty */
return (B_FALSE);
}
/* file system must be absolute */
if (*a_inheritedFileSystem != '/') {
return (B_FALSE);
}
/* make a local copy of the path and canonize it */
if (n > IPMAX) {
return (B_FALSE);
}
assert(n > 0); /* path must have at least 1 byte in it */
/* add trailing "/" if it's not already there */
rp[n++] = '/';
}
/* null terminate the string */
rp[n] = '\0';
/* add file system to internal list */
if (inheritedFileSystems == (char **)NULL) {
inheritedFileSystems = (char **)_z_calloc(
2 * (sizeof (char **)));
} else {
sizeof (char **)*(numInheritedFileSystems+2));
}
/* add this entry to the end of the list */
/* make sure end of the list is properly terminated */
/* exit debugging info */
return (B_TRUE);
}
/*
* Name: z_path_is_inherited
* Description: Determine if the specified path is in a file system that is
* in the internal list of inherited file systems
* Arguments: a_path - pointer to string representing path to verify
* a_ftype - file "type" if known otherwise '\0'
* Type can be "f" (file), or "d" (directory)
* a_rootDir - pointer to string representing root directory where
* a_path is relative to - typically this would either be
* "/" or the path specified as an alternative root to -R
* Returns: boolean_t
* B_TRUE - the path is in inherited file system space
* B_FALSE - the path is NOT in inherited file system space
*/
{
int n;
/* entry assertions */
/* if no inherited file systems, there can be no match */
if (numInheritedFileSystems == 0) {
return (B_FALSE);
}
/* normalize root directory */
a_rootDir = "/";
}
/*
* The loop below represents our best effort to identify real path of
* a file, which doesn't need to exist. realpath() returns error for
* nonexistent path, therefore we need to cut off trailing components
* of path until we get path which exists and can be resolved by
* realpath(). Lookup of "/dir/symlink/nonexistent-file" would fail
* to resolve symlink without this.
*/
break;
break;
}
if (found) {
/*
* In the loop above we always strip trailing path component,
* so the type of real_path is always 'd'.
*/
a_ftype = 'd';
} else {
}
/*
* if path resides on an inherited filesystem then
* it must be read-only.
*/
if (z_isPathWritable(path2use) != 0) {
return (B_FALSE);
}
/*
* remove the root path from the target path before comparing:
* Example 1:
* Example 2:
* -- root path is "/"
*/
/* advance past given root directory if path begins with it */
char *p;
/* advance past the root path */
p = path2use + n;
/* go back to the first occurance of the path separator */
while ((*p != '/') && (p > path2use)) {
p--;
}
/* use this location in the path to compare */
path2use = p;
}
/*
* see if this path is in any inherited file system path
* note that all paths in the inherited list are directories
* so they end in "/" to prevent a partial match, such as
* occur. This complicates matters when the object to compare is a
* directory - in this case, comparing "/usr" with "/usr/" will fail,
* so if the object is a directory, compare one less byte from the
* inherited file system so that the trailing "/" is ignored.
*/
for (n = 0; n < numInheritedFileSystems; n++) {
int fslen;
/* get target fs len; adjust -1 if directory */
fslen = inheritedFileSystemsLen[n];
fslen--;
}
inheritedFileSystems[n]);
return (B_TRUE);
}
}
/* path is not in inherited file system space */
return (B_FALSE);
}
/*
* Name: z_make_zone_root
* Description: Given its zonepath, generate a string representing the
* mountpoint of where the root path for a nonglobal zone is
* mounted. The zone is mounted using 'zoneadm', which mounts
* the zone's filesystems wrt <zonepath>/lu/a
* Arguments: zone_path - non-NULL pointer to string representing zonepath
* Returns: char * - pointer to string representing zonepath of zone
* NULL - if zone_path is NULL.
* Notes: The string returned is in static storage and should not be
* free()ed by the caller.
*/
char *
z_make_zone_root(char *zone_path)
{
static char zone_root_buf[MAXPATHLEN];
return (NULL);
(zone_path[0] != '\0' &&
return (zone_root_buf);
}
void
z_path_canonize(char *a_file)
{
char *pt;
char *last;
int level;
/* remove references such as "./" and "../" and "//" */
level = 0;
do {
level++;
last += 2;
if (*last) {
last++;
}
--pt; /* point to previous '/' */
while (level--) {
return;
}
;
}
if (*pt == '/') {
pt++;
}
} else {
pt++;
}
if (*pt == '/') {
}
pt++;
}
}
}
*pt = '\0';
}
}
void
z_canoninplace(char *src)
{
char *dst;
char *src_start;
/* keep a ptr to the beginning of the src string */
while (*src) {
if (*src == '/') {
*dst++ = '/';
while (*src == '/')
src++;
} else
}
/*
* remove any trailing slashes, unless the whole string is just "/".
* If the whole string is "/" (i.e. if the last '/' cahr in dst
* in the beginning of the original string), just terminate it
* and return "/".
*/
dst--;
*dst = '\0';
}
void
{
int i;
for (i = 0; i < numInheritedFileSystems; i++) {
free(inheritedFileSystems[i]);
}
}