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