1 /* $NetBSD: target.c,v 1.6 2019/06/15 08:20:33 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.6 2019/06/15 08:20:33 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 last_pm = pm; 167 last_res = 1; 168 169 parts = pm->parts; 170 if (pm->no_part || parts == NULL) { 171 last_res = 0; 172 return 0; 173 } 174 if (pm->parts->pscheme->secondary_partitions != NULL) 175 parts = pm->parts->pscheme->secondary_partitions(parts, 176 pm->ptstart, false); 177 178 for (ptn = 0; ptn < parts->num_part; ptn++) { 179 if (!parts->pscheme->get_part_info(parts, ptn, &info)) 180 continue; 181 if (info.nat_type->generic_ptype != PT_root) 182 continue; 183 if (strcmp(info.last_mounted, "/") != 0) 184 continue; 185 186 if (!parts->pscheme->get_part_device(parts, ptn, 187 dev, sizeof dev, &rootpart, plain_name, false)) 188 continue; 189 190 last_res = is_active_rootpart(dev, rootpart); 191 return last_res; 192 } 193 194 return 1; 195 } 196 197 198 /* 199 * Is this device partition (e.g., "sd0a") mounted as root? 200 */ 201 int 202 is_active_rootpart(const char *dev, int ptn) 203 { 204 int mib[2]; 205 char rootdev[SSTRSIZE]; 206 int rootptn; 207 size_t varlen; 208 209 mib[0] = CTL_KERN; 210 mib[1] = KERN_ROOT_DEVICE; 211 varlen = sizeof(rootdev); 212 if (sysctl(mib, 2, rootdev, &varlen, NULL, 0) < 0) 213 return 1; 214 215 if (strcmp(dev, rootdev) != 0) 216 return 0; 217 218 if (ptn < 0) 219 return 1; /* device only check, or wedge */ 220 221 mib[1] = KERN_ROOT_PARTITION; 222 varlen = sizeof rootptn; 223 rootptn = -1; 224 if (sysctl(mib, 2, &rootptn, &varlen, NULL, 0) < 0) 225 return 1; 226 227 return ptn == rootptn; 228 } 229 230 /* 231 * Pathname prefixing glue to support installation either 232 * from in-ramdisk miniroots or on-disk diskimages. 233 * If our root is on the target disk, the install target is mounted 234 * on /targetroot and we need to prefix installed pathnames with /targetroot. 235 * otherwise we are installing to the currently-active root and 236 * no prefix is needed. 237 */ 238 const char * 239 target_prefix(void) 240 { 241 /* 242 * XXX fetch sysctl variable for current root, and compare 243 * to the devicename of the install target disk. 244 */ 245 return(target_already_root() ? "" : targetroot_mnt); 246 } 247 248 /* 249 * concatenate two pathnames. 250 * XXX returns either input args or result in a static buffer. 251 * The caller must copy if it wants to use the pathname past the 252 * next call to a target-prefixing function, or to modify the inputs.. 253 * Used only internally so this is probably safe. 254 */ 255 const char * 256 concat_paths(const char *prefix, const char *suffix) 257 { 258 static char real_path[MAXPATHLEN]; 259 260 /* absolute prefix and null suffix? */ 261 if (prefix[0] == '/' && suffix[0] == 0) 262 return prefix; 263 264 /* null prefix and absolute suffix? */ 265 if (prefix[0] == 0 && suffix[0] == '/') 266 return suffix; 267 268 /* avoid "//" */ 269 if (suffix[0] == '/' || suffix[0] == 0) 270 snprintf(real_path, sizeof(real_path), "%s%s", prefix, suffix); 271 else 272 snprintf(real_path, sizeof(real_path), "%s/%s", 273 prefix, suffix); 274 return (real_path); 275 } 276 277 /* 278 * Do target prefix expansion on a pathname. 279 * XXX uses concat_paths and so returns result in a static buffer. 280 * The caller must copy if it wants to use the pathname past the 281 * next call to a target-prefixing function, or to modify the inputs.. 282 * Used only internally so this is probably safe. 283 * 284 * Not static so other functions can generate target related file names. 285 */ 286 const char * 287 target_expand(const char *tgtpath) 288 { 289 290 return concat_paths(target_prefix(), tgtpath); 291 } 292 293 /* Make a directory, with a prefix like "/targetroot" or possibly just "". */ 294 static void 295 make_prefixed_dir(const char *prefix, const char *path) 296 { 297 298 run_program(0, "/bin/mkdir -p %s", concat_paths(prefix, path)); 299 } 300 301 /* Make a directory with a pathname relative to the installation target. */ 302 void 303 make_target_dir(const char *path) 304 { 305 306 make_prefixed_dir(target_prefix(), path); 307 } 308 309 310 static int 311 do_target_chdir(const char *dir, int must_succeed) 312 { 313 const char *tgt_dir; 314 int error; 315 316 error = 0; 317 tgt_dir = target_expand(dir); 318 319 #ifdef DEBUG 320 printf("target_chdir (%s)\n", tgt_dir); 321 //return (0); 322 #endif 323 /* chdir returns -1 on error and sets errno. */ 324 if (chdir(tgt_dir) < 0) 325 error = errno; 326 if (logfp) { 327 fprintf(logfp, "cd to %s\n", tgt_dir); 328 fflush(logfp); 329 } 330 if (script) { 331 scripting_fprintf(NULL, "cd %s\n", tgt_dir); 332 fflush(script); 333 } 334 335 if (error && must_succeed) { 336 const char *args[] = { target_prefix(), strerror(error) }; 337 char *err = str_arg_subst(msg_string(MSG_realdir), 338 __arraycount(args), args); 339 fprintf(stderr, "%s\n", err); 340 if (logfp) 341 fprintf(logfp, "%s\n", err); 342 free(err); 343 exit(1); 344 } 345 errno = error; 346 return (error); 347 } 348 349 void 350 target_chdir_or_die(const char *dir) 351 { 352 353 (void)do_target_chdir(dir, 1); 354 } 355 356 #ifdef notdef 357 int 358 target_chdir(const char *dir) 359 { 360 361 return do_target_chdir(dir, 0); 362 } 363 #endif 364 365 /* 366 * Copy a file from the current root into the target system, 367 * where the destination pathname is relative to the target root. 368 * Does not check for copy-to-self when target is current root. 369 */ 370 int 371 cp_to_target(const char *srcpath, const char *tgt_path) 372 { 373 const char *real_path = target_expand(tgt_path); 374 375 return run_program(0, "/bin/cp %s %s", srcpath, real_path); 376 } 377 378 /* 379 * Duplicate a file from the current root to the same pathname 380 * in the target system. Pathname must be an absolute pathname. 381 * If we're running in the target, do nothing. 382 */ 383 void 384 dup_file_into_target(const char *filename) 385 { 386 387 if (!target_already_root()) 388 cp_to_target(filename, filename); 389 } 390 391 392 /* 393 * Do a mv where both pathnames are within the target filesystem. 394 */ 395 void 396 mv_within_target_or_die(const char *frompath, const char *topath) 397 { 398 char realfrom[STRSIZE]; 399 char realto[STRSIZE]; 400 401 strlcpy(realfrom, target_expand(frompath), sizeof realfrom); 402 strlcpy(realto, target_expand(topath), sizeof realto); 403 404 run_program(RUN_FATAL, "mv %s %s", realfrom, realto); 405 } 406 407 /* Do a cp where both pathnames are within the target filesystem. */ 408 int 409 cp_within_target(const char *frompath, const char *topath, int optional) 410 { 411 char realfrom[STRSIZE]; 412 char realto[STRSIZE]; 413 414 strncpy(realfrom, target_expand(frompath), STRSIZE); 415 strncpy(realto, target_expand(topath), STRSIZE); 416 417 if (access(realfrom, R_OK) == -1 && optional) 418 return 0; 419 return (run_program(0, "cp -p %s %s", realfrom, realto)); 420 } 421 422 /* fopen a pathname in the target. */ 423 FILE * 424 target_fopen(const char *filename, const char *type) 425 { 426 427 return fopen(target_expand(filename), type); 428 } 429 430 /* 431 * Do a mount onto a mountpoint in the install target. 432 * Record mountpoint so we can unmount when finished. 433 * NB: does not prefix mount-from, which probably breaks nullfs mounts. 434 */ 435 int 436 target_mount_do(const char *opts, const char *from, const char *on) 437 { 438 struct unwind_mount *m; 439 int error; 440 int len; 441 442 len = strlen(on); 443 m = malloc(sizeof *m + len); 444 if (m == 0) 445 return (ENOMEM); /* XXX */ 446 447 memcpy(m->um_mountpoint, on, len + 1); 448 449 #ifdef DEBUG_UNWIND 450 endwin(); 451 fprintf(stderr, "mounting %s with unwind\n", on); 452 backtowin(); 453 #endif 454 455 error = run_program(0, "/sbin/mount %s %s %s%s", 456 opts, from, target_prefix(), on); 457 if (error) { 458 free(m); 459 return error; 460 } 461 m->um_prev = unwind_mountlist; 462 unwind_mountlist = m; 463 return 0; 464 } 465 466 int 467 target_mount(const char *opts, const char *from, const char *on) 468 { 469 return target_mount_do(opts, from, on); 470 } 471 472 /* 473 * unwind the mount stack, unmounting mounted filesystems. 474 * For now, ignore any errors in unmount. 475 * (Why would we be unable to unmount? The user has suspended 476 * us and forked shell sitting somewhere in the target root?) 477 */ 478 void 479 unwind_mounts(void) 480 { 481 struct unwind_mount *m; 482 static volatile int unwind_in_progress = 0; 483 484 /* signal safety */ 485 if (unwind_in_progress) 486 return; 487 unwind_in_progress = 1; 488 489 while ((m = unwind_mountlist) != NULL) { 490 unwind_mountlist = m->um_prev; 491 #ifdef DEBUG_UNWIND 492 endwin(); 493 fprintf(stderr, "unmounting %s\n", m->um_mountpoint); 494 backtowin(); 495 #endif 496 run_program(0, "/sbin/umount %s%s", 497 target_prefix(), m->um_mountpoint); 498 free(m); 499 } 500 unwind_in_progress = 0; 501 } 502 503 int 504 target_collect_file(int kind, char **buffer, const char *name) 505 { 506 const char *realname = target_expand(name); 507 508 #ifdef DEBUG 509 printf("collect real name %s\n", realname); 510 #endif 511 return collect(kind, buffer, "%s", realname); 512 } 513 514 /* 515 * Verify a pathname already exists in the target root filesystem, 516 * by running test "testflag" on the expanded target pathname. 517 */ 518 int 519 target_test(unsigned int mode, const char *path) 520 { 521 const char *real_path = target_expand(path); 522 register int result; 523 524 result = !file_mode_match(real_path, mode); 525 scripting_fprintf(NULL, "if [ $? != 0 ]; then echo \"%s does not exist!\"; fi\n", real_path); 526 527 #if defined(DEBUG) 528 printf("target_test(%o, %s) returning %d\n", mode, real_path, result); 529 #endif 530 return (result); 531 } 532 533 /* 534 * Verify a directory already exists in the target root 535 * filesystem. Do not create the directory if it doesn't exist. 536 * Assumes that sysinst has already mounted the target root. 537 */ 538 int 539 target_test_dir(const char *path) 540 { 541 542 return target_test(S_IFDIR, path); 543 } 544 545 /* 546 * Verify an ordinary file already exists in the target root 547 * filesystem. Do not create the directory if it doesn't exist. 548 * Assumes that sysinst has already mounted the target root. 549 */ 550 int 551 target_test_file(const char *path) 552 { 553 554 return target_test(S_IFREG, path); 555 } 556 557 int 558 target_test_symlink(const char *path) 559 { 560 561 return target_test(S_IFLNK, path); 562 } 563 564 int 565 target_file_exists_p(const char *path) 566 { 567 568 return (target_test_file(path) == 0); 569 } 570 571 int 572 target_dir_exists_p(const char *path) 573 { 574 575 return (target_test_dir(path) == 0); 576 } 577 578 int 579 target_symlink_exists_p(const char *path) 580 { 581 582 return (target_test_symlink(path) == 0); 583 } 584 585 int 586 target_mounted(void) 587 { 588 return (unwind_mountlist != NULL); 589 } 590