Home | History | Annotate | Line # | Download | only in sysinst
target.c revision 1.2.2.2
      1  1.2.2.2  tls /*	$NetBSD: target.c,v 1.2.2.2 2014/08/10 07:00:24 tls Exp $	*/
      2  1.2.2.2  tls 
      3  1.2.2.2  tls /*
      4  1.2.2.2  tls  * Copyright 1997 Jonathan Stone
      5  1.2.2.2  tls  * All rights reserved.
      6  1.2.2.2  tls  *
      7  1.2.2.2  tls  * Redistribution and use in source and binary forms, with or without
      8  1.2.2.2  tls  * modification, are permitted provided that the following conditions
      9  1.2.2.2  tls  * are met:
     10  1.2.2.2  tls  * 1. Redistributions of source code must retain the above copyright
     11  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer.
     12  1.2.2.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.2.2  tls  *    documentation and/or other materials provided with the distribution.
     15  1.2.2.2  tls  * 3. All advertising materials mentioning features or use of this software
     16  1.2.2.2  tls  *    must display the following acknowledgement:
     17  1.2.2.2  tls  *      This product includes software developed for the NetBSD Project by
     18  1.2.2.2  tls  *      Jonathan Stone.
     19  1.2.2.2  tls  * 4. The name of Jonathan Stone may not be used to endorse
     20  1.2.2.2  tls  *    or promote products derived from this software without specific prior
     21  1.2.2.2  tls  *    written permission.
     22  1.2.2.2  tls  *
     23  1.2.2.2  tls  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
     24  1.2.2.2  tls  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.2.2.2  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.2.2.2  tls  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     27  1.2.2.2  tls  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.2.2.2  tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.2.2.2  tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.2.2.2  tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.2.2.2  tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.2.2.2  tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33  1.2.2.2  tls  * THE POSSIBILITY OF SUCH DAMAGE.
     34  1.2.2.2  tls  *
     35  1.2.2.2  tls  */
     36  1.2.2.2  tls 
     37  1.2.2.2  tls /* Copyright below applies to the realpath() code */
     38  1.2.2.2  tls 
     39  1.2.2.2  tls /*
     40  1.2.2.2  tls  * Copyright (c) 1989, 1991, 1993, 1995
     41  1.2.2.2  tls  *      The Regents of the University of California.  All rights reserved.
     42  1.2.2.2  tls  *
     43  1.2.2.2  tls  * This code is derived from software contributed to Berkeley by
     44  1.2.2.2  tls  * Jan-Simon Pendry.
     45  1.2.2.2  tls  *
     46  1.2.2.2  tls  * Redistribution and use in source and binary forms, with or without
     47  1.2.2.2  tls  * modification, are permitted provided that the following conditions
     48  1.2.2.2  tls  * are met:
     49  1.2.2.2  tls  * 1. Redistributions of source code must retain the above copyright
     50  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer.
     51  1.2.2.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer in the
     53  1.2.2.2  tls  *    documentation and/or other materials provided with the distribution.
     54  1.2.2.2  tls  * 3. Neither the name of the University nor the names of its contributors
     55  1.2.2.2  tls  *    may be used to endorse or promote products derived from this software
     56  1.2.2.2  tls  *    without specific prior written permission.
     57  1.2.2.2  tls  *
     58  1.2.2.2  tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.2.2.2  tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.2.2.2  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.2.2.2  tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.2.2.2  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.2.2.2  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.2.2.2  tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.2.2.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.2.2.2  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.2.2.2  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.2.2.2  tls  * SUCH DAMAGE.
     69  1.2.2.2  tls  */
     70  1.2.2.2  tls 
     71  1.2.2.2  tls 
     72  1.2.2.2  tls #include <sys/cdefs.h>
     73  1.2.2.2  tls #if defined(LIBC_SCCS) && !defined(lint)
     74  1.2.2.2  tls __RCSID("$NetBSD: target.c,v 1.2.2.2 2014/08/10 07:00:24 tls Exp $");
     75  1.2.2.2  tls #endif
     76  1.2.2.2  tls 
     77  1.2.2.2  tls /*
     78  1.2.2.2  tls  * target.c -- path-prefixing routines to access the target installation
     79  1.2.2.2  tls  *  filesystems. Makes the install tools more independent of whether
     80  1.2.2.2  tls  *  we're installing into a separate filesystem hierarchy mounted under
     81  1.2.2.2  tls  * /targetroot, or into the currently active root mounted on /.
     82  1.2.2.2  tls  */
     83  1.2.2.2  tls 
     84  1.2.2.2  tls #include <sys/param.h>			/* XXX vm_param.h always defines TRUE*/
     85  1.2.2.2  tls #include <sys/types.h>
     86  1.2.2.2  tls #include <sys/sysctl.h>
     87  1.2.2.2  tls #include <sys/stat.h>			/* stat() */
     88  1.2.2.2  tls #include <sys/mount.h>			/* statfs() */
     89  1.2.2.2  tls 
     90  1.2.2.2  tls #include <fcntl.h>
     91  1.2.2.2  tls #include <stdio.h>
     92  1.2.2.2  tls #include <stdarg.h>
     93  1.2.2.2  tls #include <unistd.h>
     94  1.2.2.2  tls #include <curses.h>			/* defines TRUE, but checks  */
     95  1.2.2.2  tls #include <errno.h>
     96  1.2.2.2  tls 
     97  1.2.2.2  tls 
     98  1.2.2.2  tls #include "defs.h"
     99  1.2.2.2  tls #include "md.h"
    100  1.2.2.2  tls #include "msg_defs.h"
    101  1.2.2.2  tls #include "menu_defs.h"
    102  1.2.2.2  tls 
    103  1.2.2.2  tls /*
    104  1.2.2.2  tls  * local  prototypes
    105  1.2.2.2  tls  */
    106  1.2.2.2  tls 
    107  1.2.2.2  tls static void make_prefixed_dir (const char *prefix, const char *path);
    108  1.2.2.2  tls static int do_target_chdir (const char *dir, int flag);
    109  1.2.2.2  tls int	target_test(unsigned int mode, const char *path);
    110  1.2.2.2  tls int	target_test_dir (const char *path);	/* deprecated */
    111  1.2.2.2  tls int	target_test_file (const char *path);	/* deprecated */
    112  1.2.2.2  tls int	target_test_symlink (const char *path);	/* deprecated */
    113  1.2.2.2  tls 
    114  1.2.2.2  tls void unwind_mounts(void);
    115  1.2.2.2  tls 
    116  1.2.2.2  tls /* Record a mount for later unwinding of target mounts. */
    117  1.2.2.2  tls struct unwind_mount {
    118  1.2.2.2  tls 	struct unwind_mount *um_prev;
    119  1.2.2.2  tls 	char um_mountpoint[4];		/* Allocated longer... */
    120  1.2.2.2  tls };
    121  1.2.2.2  tls 
    122  1.2.2.2  tls /* Unwind-mount stack */
    123  1.2.2.2  tls struct unwind_mount *unwind_mountlist = NULL;
    124  1.2.2.2  tls 
    125  1.2.2.2  tls /*
    126  1.2.2.2  tls  * Debugging options
    127  1.2.2.2  tls  */
    128  1.2.2.2  tls /*#define DEBUG_ROOT*/		/* turn on what-is-root? debugging. */
    129  1.2.2.2  tls /*#define DEBUG_UNWIND*/	/* turn on unwind-target-mount debugging. */
    130  1.2.2.2  tls 
    131  1.2.2.2  tls /*
    132  1.2.2.2  tls  * debugging helper. curses...
    133  1.2.2.2  tls  */
    134  1.2.2.2  tls #if defined(DEBUG)  ||	defined(DEBUG_ROOT)
    135  1.2.2.2  tls void
    136  1.2.2.2  tls backtowin(void)
    137  1.2.2.2  tls {
    138  1.2.2.2  tls 
    139  1.2.2.2  tls 	fflush(stdout);	/* curses does not leave stdout linebuffered. */
    140  1.2.2.2  tls 	getchar();	/* wait for user to press return */
    141  1.2.2.2  tls 	wrefresh(stdscr);
    142  1.2.2.2  tls }
    143  1.2.2.2  tls #endif
    144  1.2.2.2  tls 
    145  1.2.2.2  tls 
    146  1.2.2.2  tls /*
    147  1.2.2.2  tls  * Is the root partition we're running from the same as the root
    148  1.2.2.2  tls  * which the user has selected to install/upgrade?
    149  1.2.2.2  tls  * Uses global variable "pm->diskdev" to find the selected device for
    150  1.2.2.2  tls  * install/upgrade.
    151  1.2.2.2  tls  */
    152  1.2.2.2  tls int
    153  1.2.2.2  tls target_already_root(void)
    154  1.2.2.2  tls {
    155  1.2.2.2  tls 
    156  1.2.2.2  tls 	if (strcmp(pm->diskdev, "") == 0)
    157  1.2.2.2  tls 		/* No root partition was ever selected.
    158  1.2.2.2  tls 		 * Assume that the currently mounted one should be used
    159  1.2.2.2  tls 		 */
    160  1.2.2.2  tls 		return 1;
    161  1.2.2.2  tls 
    162  1.2.2.2  tls 	return is_active_rootpart(pm->diskdev, pm->rootpart);
    163  1.2.2.2  tls }
    164  1.2.2.2  tls 
    165  1.2.2.2  tls 
    166  1.2.2.2  tls /*
    167  1.2.2.2  tls  * Is this device partition (e.g., "sd0a") mounted as root?
    168  1.2.2.2  tls  */
    169  1.2.2.2  tls int
    170  1.2.2.2  tls is_active_rootpart(const char *dev, int ptn)
    171  1.2.2.2  tls {
    172  1.2.2.2  tls 	int mib[2];
    173  1.2.2.2  tls 	char rootdev[SSTRSIZE];
    174  1.2.2.2  tls 	int rootptn;
    175  1.2.2.2  tls 	size_t varlen;
    176  1.2.2.2  tls 
    177  1.2.2.2  tls 	mib[0] = CTL_KERN;
    178  1.2.2.2  tls 	mib[1] = KERN_ROOT_DEVICE;
    179  1.2.2.2  tls 	varlen = sizeof(rootdev);
    180  1.2.2.2  tls 	if (sysctl(mib, 2, rootdev, &varlen, NULL, 0) < 0)
    181  1.2.2.2  tls 		return 1;
    182  1.2.2.2  tls 
    183  1.2.2.2  tls 	if (strcmp(dev, rootdev) != 0)
    184  1.2.2.2  tls 		return 0;
    185  1.2.2.2  tls 
    186  1.2.2.2  tls 	mib[1] = KERN_ROOT_PARTITION;
    187  1.2.2.2  tls 	varlen = sizeof rootptn;
    188  1.2.2.2  tls 	if (sysctl(mib, 2, &rootptn, &varlen, NULL, 0) < 0)
    189  1.2.2.2  tls 		return 1;
    190  1.2.2.2  tls 
    191  1.2.2.2  tls 	return ptn == rootptn;
    192  1.2.2.2  tls }
    193  1.2.2.2  tls 
    194  1.2.2.2  tls /*
    195  1.2.2.2  tls  * Pathname  prefixing glue to support installation either
    196  1.2.2.2  tls  * from in-ramdisk miniroots or on-disk diskimages.
    197  1.2.2.2  tls  * If our root is on the target disk, the install target is mounted
    198  1.2.2.2  tls  * on /targetroot and we need to prefix installed pathnames with /targetroot.
    199  1.2.2.2  tls  * otherwise we are installing to the currently-active root and
    200  1.2.2.2  tls  * no prefix is needed.
    201  1.2.2.2  tls  */
    202  1.2.2.2  tls const char *
    203  1.2.2.2  tls target_prefix(void)
    204  1.2.2.2  tls {
    205  1.2.2.2  tls 	/*
    206  1.2.2.2  tls 	 * XXX fetch sysctl variable for current root, and compare
    207  1.2.2.2  tls 	 * to the devicename of the install target disk.
    208  1.2.2.2  tls 	 */
    209  1.2.2.2  tls 	return(target_already_root() ? "" : targetroot_mnt);
    210  1.2.2.2  tls }
    211  1.2.2.2  tls 
    212  1.2.2.2  tls /*
    213  1.2.2.2  tls  * concatenate two pathnames.
    214  1.2.2.2  tls  * XXX returns either input args or result in a static buffer.
    215  1.2.2.2  tls  * The caller must copy if it wants to use the pathname past the
    216  1.2.2.2  tls  * next call to a target-prefixing  function, or to modify the inputs..
    217  1.2.2.2  tls  * Used only  internally so this is probably safe.
    218  1.2.2.2  tls  */
    219  1.2.2.2  tls const char *
    220  1.2.2.2  tls concat_paths(const char *prefix, const char *suffix)
    221  1.2.2.2  tls {
    222  1.2.2.2  tls 	static char real_path[MAXPATHLEN];
    223  1.2.2.2  tls 
    224  1.2.2.2  tls 	/* absolute prefix and null suffix? */
    225  1.2.2.2  tls 	if (prefix[0] == '/' && suffix[0] == 0)
    226  1.2.2.2  tls 		return prefix;
    227  1.2.2.2  tls 
    228  1.2.2.2  tls 	/* null prefix and absolute suffix? */
    229  1.2.2.2  tls 	if (prefix[0] == 0 && suffix[0] == '/')
    230  1.2.2.2  tls 		return suffix;
    231  1.2.2.2  tls 
    232  1.2.2.2  tls 	/* avoid "//" */
    233  1.2.2.2  tls 	if (suffix[0] == '/' || suffix[0] == 0)
    234  1.2.2.2  tls 		snprintf(real_path, sizeof(real_path), "%s%s", prefix, suffix);
    235  1.2.2.2  tls 	else
    236  1.2.2.2  tls 		snprintf(real_path, sizeof(real_path), "%s/%s",
    237  1.2.2.2  tls 		    prefix, suffix);
    238  1.2.2.2  tls 	return (real_path);
    239  1.2.2.2  tls }
    240  1.2.2.2  tls 
    241  1.2.2.2  tls /*
    242  1.2.2.2  tls  * Do target prefix expansion on a pathname.
    243  1.2.2.2  tls  * XXX uses concat_paths and so returns result in a static buffer.
    244  1.2.2.2  tls  * The caller must copy if it wants to use the pathname past the
    245  1.2.2.2  tls  * next call to a target-prefixing  function, or to modify the inputs..
    246  1.2.2.2  tls  * Used only  internally so this is probably safe.
    247  1.2.2.2  tls  *
    248  1.2.2.2  tls  * Not static so other functions can generate target related file names.
    249  1.2.2.2  tls  */
    250  1.2.2.2  tls const char *
    251  1.2.2.2  tls target_expand(const char *tgtpath)
    252  1.2.2.2  tls {
    253  1.2.2.2  tls 
    254  1.2.2.2  tls 	return concat_paths(target_prefix(), tgtpath);
    255  1.2.2.2  tls }
    256  1.2.2.2  tls 
    257  1.2.2.2  tls /* Make a directory, with a prefix like "/targetroot" or possibly just "". */
    258  1.2.2.2  tls static void
    259  1.2.2.2  tls make_prefixed_dir(const char *prefix, const char *path)
    260  1.2.2.2  tls {
    261  1.2.2.2  tls 
    262  1.2.2.2  tls 	run_program(0, "/bin/mkdir -p %s", concat_paths(prefix, path));
    263  1.2.2.2  tls }
    264  1.2.2.2  tls 
    265  1.2.2.2  tls /* Make a directory with a pathname relative to the installation target. */
    266  1.2.2.2  tls void
    267  1.2.2.2  tls make_target_dir(const char *path)
    268  1.2.2.2  tls {
    269  1.2.2.2  tls 
    270  1.2.2.2  tls 	make_prefixed_dir(target_prefix(), path);
    271  1.2.2.2  tls }
    272  1.2.2.2  tls 
    273  1.2.2.2  tls 
    274  1.2.2.2  tls static int
    275  1.2.2.2  tls do_target_chdir(const char *dir, int must_succeed)
    276  1.2.2.2  tls {
    277  1.2.2.2  tls 	const char *tgt_dir;
    278  1.2.2.2  tls 	int error;
    279  1.2.2.2  tls 
    280  1.2.2.2  tls 	error = 0;
    281  1.2.2.2  tls 	tgt_dir = target_expand(dir);
    282  1.2.2.2  tls 
    283  1.2.2.2  tls #ifdef DEBUG
    284  1.2.2.2  tls 	printf("target_chdir (%s)\n", tgt_dir);
    285  1.2.2.2  tls 	//return (0);
    286  1.2.2.2  tls #endif
    287  1.2.2.2  tls 	/* chdir returns -1 on error and sets errno. */
    288  1.2.2.2  tls 	if (chdir(tgt_dir) < 0)
    289  1.2.2.2  tls 		error = errno;
    290  1.2.2.2  tls 	if (logfp) {
    291  1.2.2.2  tls 		fprintf(logfp, "cd to %s\n", tgt_dir);
    292  1.2.2.2  tls 		fflush(logfp);
    293  1.2.2.2  tls 	}
    294  1.2.2.2  tls 	if (script) {
    295  1.2.2.2  tls 		scripting_fprintf(NULL, "cd %s\n", tgt_dir);
    296  1.2.2.2  tls 		fflush(script);
    297  1.2.2.2  tls 	}
    298  1.2.2.2  tls 
    299  1.2.2.2  tls 	if (error && must_succeed) {
    300  1.2.2.2  tls 		fprintf(stderr, msg_string(MSG_realdir),
    301  1.2.2.2  tls 		       target_prefix(), strerror(error));
    302  1.2.2.2  tls 		if (logfp)
    303  1.2.2.2  tls 			fprintf(logfp, msg_string(MSG_realdir),
    304  1.2.2.2  tls 			       target_prefix(), strerror(error));
    305  1.2.2.2  tls 		exit(1);
    306  1.2.2.2  tls 	}
    307  1.2.2.2  tls 	errno = error;
    308  1.2.2.2  tls 	return (error);
    309  1.2.2.2  tls }
    310  1.2.2.2  tls 
    311  1.2.2.2  tls void
    312  1.2.2.2  tls target_chdir_or_die(const char *dir)
    313  1.2.2.2  tls {
    314  1.2.2.2  tls 
    315  1.2.2.2  tls 	(void)do_target_chdir(dir, 1);
    316  1.2.2.2  tls }
    317  1.2.2.2  tls 
    318  1.2.2.2  tls #ifdef notdef
    319  1.2.2.2  tls int
    320  1.2.2.2  tls target_chdir(const char *dir)
    321  1.2.2.2  tls {
    322  1.2.2.2  tls 
    323  1.2.2.2  tls 	return do_target_chdir(dir, 0);
    324  1.2.2.2  tls }
    325  1.2.2.2  tls #endif
    326  1.2.2.2  tls 
    327  1.2.2.2  tls /*
    328  1.2.2.2  tls  * Copy a file from the current root into the target system,
    329  1.2.2.2  tls  * where the  destination pathname is relative to the target root.
    330  1.2.2.2  tls  * Does not check for copy-to-self when target is  current root.
    331  1.2.2.2  tls  */
    332  1.2.2.2  tls int
    333  1.2.2.2  tls cp_to_target(const char *srcpath, const char *tgt_path)
    334  1.2.2.2  tls {
    335  1.2.2.2  tls 	const char *real_path = target_expand(tgt_path);
    336  1.2.2.2  tls 
    337  1.2.2.2  tls 	return run_program(0, "/bin/cp %s %s", srcpath, real_path);
    338  1.2.2.2  tls }
    339  1.2.2.2  tls 
    340  1.2.2.2  tls /*
    341  1.2.2.2  tls  * Duplicate a file from the current root to the same pathname
    342  1.2.2.2  tls  * in the target system.  Pathname must be an absolute pathname.
    343  1.2.2.2  tls  * If we're running in the target, do nothing.
    344  1.2.2.2  tls  */
    345  1.2.2.2  tls void
    346  1.2.2.2  tls dup_file_into_target(const char *filename)
    347  1.2.2.2  tls {
    348  1.2.2.2  tls 
    349  1.2.2.2  tls 	if (!target_already_root())
    350  1.2.2.2  tls 		cp_to_target(filename, filename);
    351  1.2.2.2  tls }
    352  1.2.2.2  tls 
    353  1.2.2.2  tls 
    354  1.2.2.2  tls /*
    355  1.2.2.2  tls  * Do a mv where both pathnames are within the target filesystem.
    356  1.2.2.2  tls  */
    357  1.2.2.2  tls void
    358  1.2.2.2  tls mv_within_target_or_die(const char *frompath, const char *topath)
    359  1.2.2.2  tls {
    360  1.2.2.2  tls 	char realfrom[STRSIZE];
    361  1.2.2.2  tls 	char realto[STRSIZE];
    362  1.2.2.2  tls 
    363  1.2.2.2  tls 	strlcpy(realfrom, target_expand(frompath), sizeof realfrom);
    364  1.2.2.2  tls 	strlcpy(realto, target_expand(topath), sizeof realto);
    365  1.2.2.2  tls 
    366  1.2.2.2  tls 	run_program(RUN_FATAL, "mv %s %s", realfrom, realto);
    367  1.2.2.2  tls }
    368  1.2.2.2  tls 
    369  1.2.2.2  tls /* Do a cp where both pathnames are within the target filesystem. */
    370  1.2.2.2  tls int
    371  1.2.2.2  tls cp_within_target(const char *frompath, const char *topath, int optional)
    372  1.2.2.2  tls {
    373  1.2.2.2  tls 	char realfrom[STRSIZE];
    374  1.2.2.2  tls 	char realto[STRSIZE];
    375  1.2.2.2  tls 
    376  1.2.2.2  tls 	strncpy(realfrom, target_expand(frompath), STRSIZE);
    377  1.2.2.2  tls 	strncpy(realto, target_expand(topath), STRSIZE);
    378  1.2.2.2  tls 
    379  1.2.2.2  tls 	if (access(realfrom, R_OK) == -1 && optional)
    380  1.2.2.2  tls 		return 0;
    381  1.2.2.2  tls 	return (run_program(0, "cp -p %s %s", realfrom, realto));
    382  1.2.2.2  tls }
    383  1.2.2.2  tls 
    384  1.2.2.2  tls /* fopen a pathname in the target. */
    385  1.2.2.2  tls FILE *
    386  1.2.2.2  tls target_fopen(const char *filename, const char *type)
    387  1.2.2.2  tls {
    388  1.2.2.2  tls 
    389  1.2.2.2  tls 	return fopen(target_expand(filename), type);
    390  1.2.2.2  tls }
    391  1.2.2.2  tls 
    392  1.2.2.2  tls /*
    393  1.2.2.2  tls  * Do a mount onto a mountpoint in the install target.
    394  1.2.2.2  tls  * Record mountpoint so we can unmount when finished.
    395  1.2.2.2  tls  * NB: does not prefix mount-from, which probably breaks nullfs mounts.
    396  1.2.2.2  tls  */
    397  1.2.2.2  tls int
    398  1.2.2.2  tls target_mount_do(const char *opts, const char *from, const char *on)
    399  1.2.2.2  tls {
    400  1.2.2.2  tls 	struct unwind_mount *m;
    401  1.2.2.2  tls 	int error;
    402  1.2.2.2  tls 	int len;
    403  1.2.2.2  tls 
    404  1.2.2.2  tls 	len = strlen(on);
    405  1.2.2.2  tls 	m = malloc(sizeof *m + len);
    406  1.2.2.2  tls 	if (m == 0)
    407  1.2.2.2  tls 		return (ENOMEM);	/* XXX */
    408  1.2.2.2  tls 
    409  1.2.2.2  tls 	memcpy(m->um_mountpoint, on, len + 1);
    410  1.2.2.2  tls 
    411  1.2.2.2  tls #ifdef DEBUG_UNWIND
    412  1.2.2.2  tls 	endwin();
    413  1.2.2.2  tls 	fprintf(stderr, "mounting %s with unwind\n", on);
    414  1.2.2.2  tls 	backtowin();
    415  1.2.2.2  tls #endif
    416  1.2.2.2  tls 
    417  1.2.2.2  tls 	error = run_program(0, "/sbin/mount %s %s %s%s",
    418  1.2.2.2  tls 			opts, from, target_prefix(), on);
    419  1.2.2.2  tls 	if (error) {
    420  1.2.2.2  tls 		free(m);
    421  1.2.2.2  tls 		return error;
    422  1.2.2.2  tls 	}
    423  1.2.2.2  tls 	m->um_prev = unwind_mountlist;
    424  1.2.2.2  tls 	unwind_mountlist = m;
    425  1.2.2.2  tls 	return 0;
    426  1.2.2.2  tls }
    427  1.2.2.2  tls 
    428  1.2.2.2  tls int
    429  1.2.2.2  tls target_mount(const char *opts, const char *from, int ptn, const char *on)
    430  1.2.2.2  tls {
    431  1.2.2.2  tls 	int error;
    432  1.2.2.2  tls 	char *frompath;
    433  1.2.2.2  tls 	asprintf (&frompath, "/dev/%s%c", from, (ptn < 0)? 0 : 'a' + ptn);
    434  1.2.2.2  tls 	if (frompath == 0)
    435  1.2.2.2  tls 		return (ENOMEM);
    436  1.2.2.2  tls 	error = target_mount_do(opts, frompath, on);
    437  1.2.2.2  tls 	free(frompath);
    438  1.2.2.2  tls 	return error;
    439  1.2.2.2  tls }
    440  1.2.2.2  tls 
    441  1.2.2.2  tls /*
    442  1.2.2.2  tls  * unwind the mount stack, unmounting mounted filesystems.
    443  1.2.2.2  tls  * For now, ignore any errors in unmount.
    444  1.2.2.2  tls  * (Why would we be unable to unmount?  The user has suspended
    445  1.2.2.2  tls  *  us and forked shell sitting somewhere in the target root?)
    446  1.2.2.2  tls  */
    447  1.2.2.2  tls void
    448  1.2.2.2  tls unwind_mounts(void)
    449  1.2.2.2  tls {
    450  1.2.2.2  tls 	struct unwind_mount *m;
    451  1.2.2.2  tls 	static volatile int unwind_in_progress = 0;
    452  1.2.2.2  tls 
    453  1.2.2.2  tls 	/* signal safety */
    454  1.2.2.2  tls 	if (unwind_in_progress)
    455  1.2.2.2  tls 		return;
    456  1.2.2.2  tls 	unwind_in_progress = 1;
    457  1.2.2.2  tls 
    458  1.2.2.2  tls 	while ((m = unwind_mountlist) != NULL) {
    459  1.2.2.2  tls 		unwind_mountlist = m->um_prev;
    460  1.2.2.2  tls #ifdef DEBUG_UNWIND
    461  1.2.2.2  tls 		endwin();
    462  1.2.2.2  tls 		fprintf(stderr, "unmounting %s\n", m->um_mountpoint);
    463  1.2.2.2  tls 		backtowin();
    464  1.2.2.2  tls #endif
    465  1.2.2.2  tls 		run_program(0, "/sbin/umount %s%s",
    466  1.2.2.2  tls 			target_prefix(), m->um_mountpoint);
    467  1.2.2.2  tls 		free(m);
    468  1.2.2.2  tls 	}
    469  1.2.2.2  tls 	unwind_in_progress = 0;
    470  1.2.2.2  tls }
    471  1.2.2.2  tls 
    472  1.2.2.2  tls int
    473  1.2.2.2  tls target_collect_file(int kind, char **buffer, const char *name)
    474  1.2.2.2  tls {
    475  1.2.2.2  tls 	const char *realname = target_expand(name);
    476  1.2.2.2  tls 
    477  1.2.2.2  tls #ifdef	DEBUG
    478  1.2.2.2  tls 	printf("collect real name %s\n", realname);
    479  1.2.2.2  tls #endif
    480  1.2.2.2  tls 	return collect(kind, buffer, "%s", realname);
    481  1.2.2.2  tls }
    482  1.2.2.2  tls 
    483  1.2.2.2  tls /*
    484  1.2.2.2  tls  * Verify a pathname already exists in the target root filesystem,
    485  1.2.2.2  tls  * by running  test "testflag" on the expanded target pathname.
    486  1.2.2.2  tls  */
    487  1.2.2.2  tls int
    488  1.2.2.2  tls target_test(unsigned int mode, const char *path)
    489  1.2.2.2  tls {
    490  1.2.2.2  tls 	const char *real_path = target_expand(path);
    491  1.2.2.2  tls 	register int result;
    492  1.2.2.2  tls 
    493  1.2.2.2  tls 	result = !file_mode_match(real_path, mode);
    494  1.2.2.2  tls 	scripting_fprintf(NULL, "if [ $? != 0 ]; then echo \"%s does not exist!\"; fi\n", real_path);
    495  1.2.2.2  tls 
    496  1.2.2.2  tls #if defined(DEBUG)
    497  1.2.2.2  tls 	printf("target_test(%o, %s) returning %d\n", mode, real_path, result);
    498  1.2.2.2  tls #endif
    499  1.2.2.2  tls 	return (result);
    500  1.2.2.2  tls }
    501  1.2.2.2  tls 
    502  1.2.2.2  tls /*
    503  1.2.2.2  tls  * Verify a directory already exists in the target root
    504  1.2.2.2  tls  * filesystem. Do not create the directory if it doesn't  exist.
    505  1.2.2.2  tls  * Assumes that sysinst has already mounted the target root.
    506  1.2.2.2  tls  */
    507  1.2.2.2  tls int
    508  1.2.2.2  tls target_test_dir(const char *path)
    509  1.2.2.2  tls {
    510  1.2.2.2  tls 
    511  1.2.2.2  tls  	return target_test(S_IFDIR, path);
    512  1.2.2.2  tls }
    513  1.2.2.2  tls 
    514  1.2.2.2  tls /*
    515  1.2.2.2  tls  * Verify an ordinary file already exists in the target root
    516  1.2.2.2  tls  * filesystem. Do not create the directory if it doesn't  exist.
    517  1.2.2.2  tls  * Assumes that sysinst has already mounted the target root.
    518  1.2.2.2  tls  */
    519  1.2.2.2  tls int
    520  1.2.2.2  tls target_test_file(const char *path)
    521  1.2.2.2  tls {
    522  1.2.2.2  tls 
    523  1.2.2.2  tls  	return target_test(S_IFREG, path);
    524  1.2.2.2  tls }
    525  1.2.2.2  tls 
    526  1.2.2.2  tls int
    527  1.2.2.2  tls target_test_symlink(const char *path)
    528  1.2.2.2  tls {
    529  1.2.2.2  tls 
    530  1.2.2.2  tls  	return target_test(S_IFLNK, path);
    531  1.2.2.2  tls }
    532  1.2.2.2  tls 
    533  1.2.2.2  tls int
    534  1.2.2.2  tls target_file_exists_p(const char *path)
    535  1.2.2.2  tls {
    536  1.2.2.2  tls 
    537  1.2.2.2  tls 	return (target_test_file(path) == 0);
    538  1.2.2.2  tls }
    539  1.2.2.2  tls 
    540  1.2.2.2  tls int
    541  1.2.2.2  tls target_dir_exists_p(const char *path)
    542  1.2.2.2  tls {
    543  1.2.2.2  tls 
    544  1.2.2.2  tls 	return (target_test_dir(path) == 0);
    545  1.2.2.2  tls }
    546  1.2.2.2  tls 
    547  1.2.2.2  tls int
    548  1.2.2.2  tls target_symlink_exists_p(const char *path)
    549  1.2.2.2  tls {
    550  1.2.2.2  tls 
    551  1.2.2.2  tls 	return (target_test_symlink(path) == 0);
    552  1.2.2.2  tls }
    553  1.2.2.2  tls 
    554  1.2.2.2  tls int
    555  1.2.2.2  tls target_mounted(void)
    556  1.2.2.2  tls {
    557  1.2.2.2  tls 	return (unwind_mountlist != NULL);
    558  1.2.2.2  tls }
    559