1 /* $NetBSD: target.c,v 1.51 2006/10/23 19:44:57 he Exp $ */
4 * Copyright 1997 Jonathan Stone
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
19 * 4. The name of Jonathan Stone may not be used to endorse
20 * or promote products derived from this software without specific prior
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.
37 /* Copyright below applies to the realpath() code */
40 * Copyright (c) 1989, 1991, 1993, 1995
41 * The Regents of the University of California. All rights reserved.
43 * This code is derived from software contributed to Berkeley by
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
72 #include <sys/cdefs.h>
73 #if defined(LIBC_SCCS) && !defined(lint)
74 __RCSID("$NetBSD: target.c,v 1.51 2006/10/23 19:44:57 he Exp $");
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 /.
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() */
94 #include <curses.h> /* defines TRUE, but checks */
100 #include "msg_defs.h"
101 #include "menu_defs.h"
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 */
114 void unwind_mounts(void);
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... */
122 /* Unwind-mount stack */
123 struct unwind_mount
*unwind_mountlist
= NULL
;
128 /*#define DEBUG_ROOT*/ /* turn on what-is-root? debugging. */
129 /*#define DEBUG_UNWIND*/ /* turn on unwind-target-mount debugging. */
132 * debugging helper. curses...
134 #if defined(DEBUG) || defined(DEBUG_ROOT)
139 fflush(stdout
); /* curses does not leave stdout linebuffered. */
140 getchar(); /* wait for user to press return */
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 "diskdev" to find the selected device for
153 target_already_root(void)
156 if (strcmp(diskdev
, "") == 0)
157 /* No root partition was ever selected.
158 * Assume that the currently mounted one should be used
162 return is_active_rootpart(diskdev
, rootpart
);
167 * Is this device partition (e.g., "sd0a") mounted as root?
170 is_active_rootpart(const char *dev
, int ptn
)
173 char rootdev
[SSTRSIZE
];
178 mib
[1] = KERN_ROOT_DEVICE
;
179 varlen
= sizeof(rootdev
);
180 if (sysctl(mib
, 2, rootdev
, &varlen
, NULL
, 0) < 0)
183 if (strcmp(dev
, rootdev
) != 0)
186 mib
[1] = KERN_ROOT_PARTITION
;
187 varlen
= sizeof rootptn
;
188 if (sysctl(mib
, 2, &rootptn
, &varlen
, NULL
, 0) < 0)
191 return ptn
== rootptn
;
195 * Pathname prefixing glue to support installation either
196 * from in-ramdisk miniroots or on-disk diskimages.
197 * If our root is on the target disk, the install target is mounted
198 * on /targetroot and we need to prefix installed pathnames with /targetroot.
199 * otherwise we are installing to the currently-active root and
200 * no prefix is needed.
206 * XXX fetch sysctl variable for current root, and compare
207 * to the devicename of the install target disk.
209 return(target_already_root() ? "" : targetroot_mnt
);
213 * concatenate two pathnames.
214 * XXX returns either input args or result in a static buffer.
215 * The caller must copy if it wants to use the pathname past the
216 * next call to a target-prefixing function, or to modify the inputs..
217 * Used only internally so this is probably safe.
220 concat_paths(const char *prefix
, const char *suffix
)
222 static char real_path
[MAXPATHLEN
];
224 /* absolute prefix and null suffix? */
225 if (prefix
[0] == '/' && suffix
[0] == 0)
228 /* null prefix and absolute suffix? */
229 if (prefix
[0] == 0 && suffix
[0] == '/')
233 if (suffix
[0] == '/' || suffix
[0] == 0)
234 snprintf(real_path
, sizeof(real_path
), "%s%s", prefix
, suffix
);
236 snprintf(real_path
, sizeof(real_path
), "%s/%s",
242 * Do target prefix expansion on a pathname.
243 * XXX uses concat_paths and so returns result in a static buffer.
244 * The caller must copy if it wants to use the pathname past the
245 * next call to a target-prefixing function, or to modify the inputs..
246 * Used only internally so this is probably safe.
248 * Not static so other functions can generate target related file names.
251 target_expand(const char *tgtpath
)
254 return concat_paths(target_prefix(), tgtpath
);
257 /* Make a directory, with a prefix like "/targetroot" or possibly just "". */
259 make_prefixed_dir(const char *prefix
, const char *path
)
262 run_program(0, "/bin/mkdir -p %s", concat_paths(prefix
, path
));
265 /* Make a directory with a pathname relative to the installation target. */
267 make_target_dir(const char *path
)
270 make_prefixed_dir(target_prefix(), path
);
275 do_target_chdir(const char *dir
, int must_succeed
)
281 tgt_dir
= target_expand(dir
);
284 /* chdir returns -1 on error and sets errno. */
285 if (chdir(tgt_dir
) < 0)
288 fprintf(logfp
, "cd to %s\n", tgt_dir
);
292 scripting_fprintf(NULL
, "cd %s\n", tgt_dir
);
296 if (error
&& must_succeed
) {
297 fprintf(stderr
, msg_string(MSG_realdir
),
298 target_prefix(), strerror(error
));
300 fprintf(logfp
, msg_string(MSG_realdir
),
301 target_prefix(), strerror(error
));
307 printf("target_chdir (%s)\n", tgt_dir
);
313 target_chdir_or_die(const char *dir
)
316 (void)do_target_chdir(dir
, 1);
321 target_chdir(const char *dir
)
324 return do_target_chdir(dir
, 0);
329 * Copy a file from the current root into the target system,
330 * where the destination pathname is relative to the target root.
331 * Does not check for copy-to-self when target is current root.
334 cp_to_target(const char *srcpath
, const char *tgt_path
)
336 const char *real_path
= target_expand(tgt_path
);
338 return run_program(0, "/bin/cp %s %s", srcpath
, real_path
);
342 * Duplicate a file from the current root to the same pathname
343 * in the target system. Pathname must be an absolute pathname.
344 * If we're running in the target, do nothing.
347 dup_file_into_target(const char *filename
)
350 if (!target_already_root())
351 cp_to_target(filename
, filename
);
356 * Do a mv where both pathnames are within the target filesystem.
359 mv_within_target_or_die(const char *frompath
, const char *topath
)
361 char realfrom
[STRSIZE
];
362 char realto
[STRSIZE
];
364 strlcpy(realfrom
, target_expand(frompath
), sizeof realfrom
);
365 strlcpy(realto
, target_expand(topath
), sizeof realto
);
367 run_program(RUN_FATAL
, "mv %s %s", realfrom
, realto
);
370 /* Do a cp where both pathnames are within the target filesystem. */
372 cp_within_target(const char *frompath
, const char *topath
, int optional
)
374 char realfrom
[STRSIZE
];
375 char realto
[STRSIZE
];
377 strncpy(realfrom
, target_expand(frompath
), STRSIZE
);
378 strncpy(realto
, target_expand(topath
), STRSIZE
);
380 if (access(realfrom
, R_OK
) == -1 && optional
)
382 return (run_program(0, "cp -p %s %s", realfrom
, realto
));
385 /* fopen a pathname in the target. */
387 target_fopen(const char *filename
, const char *type
)
390 return fopen(target_expand(filename
), type
);
394 * Do a mount onto a mountpoint in the install target.
395 * Record mountpoint so we can unmount when finished.
396 * NB: does not prefix mount-from, which probably breaks nullfs mounts.
399 target_mount(const char *opts
, const char *from
, int ptn
, const char *on
)
401 struct unwind_mount
*m
;
406 m
= malloc(sizeof *m
+ len
);
408 return (ENOMEM
); /* XXX */
410 memcpy(m
->um_mountpoint
, on
, len
+ 1);
414 fprintf(stderr
, "mounting %s with unwind\n", on
);
418 error
= run_program(0, "/sbin/mount %s /dev/%s%c %s%s",
419 opts
, from
, 'a' + ptn
, target_prefix(), on
);
424 m
->um_prev
= unwind_mountlist
;
425 unwind_mountlist
= m
;
430 * unwind the mount stack, unmounting mounted filesystems.
431 * For now, ignore any errors in unmount.
432 * (Why would we be unable to unmount? The user has suspended
433 * us and forked shell sitting somewhere in the target root?)
438 struct unwind_mount
*m
;
439 static volatile int unwind_in_progress
= 0;
442 if (unwind_in_progress
)
444 unwind_in_progress
= 1;
446 while ((m
= unwind_mountlist
) != NULL
) {
447 unwind_mountlist
= m
->um_prev
;
450 fprintf(stderr
, "unmounting %s\n", m
->um_mountpoint
);
453 run_program(0, "/sbin/umount %s%s",
454 target_prefix(), m
->um_mountpoint
);
457 unwind_in_progress
= 0;
461 target_collect_file(int kind
, char **buffer
, const char *name
)
463 const char *realname
= target_expand(name
);
466 printf("collect real name %s\n", realname
);
468 return collect(kind
, buffer
, realname
);
472 * Verify a pathname already exists in the target root filesystem,
473 * by running test "testflag" on the expanded target pathname.
476 target_test(unsigned int mode
, const char *path
)
478 const char *real_path
= target_expand(path
);
481 result
= !file_mode_match(real_path
, mode
);
482 scripting_fprintf(NULL
, "if [ $? != 0 ]; then echo \"%s does not exist!\"; fi\n", real_path
);
485 printf("target_test(%o, %s) returning %d\n", mode
, real_path
, result
);
491 * Verify a directory already exists in the target root
492 * filesystem. Do not create the directory if it doesn't exist.
493 * Assumes that sysinst has already mounted the target root.
496 target_test_dir(const char *path
)
499 return target_test(S_IFDIR
, path
);
503 * Verify an ordinary file already exists in the target root
504 * filesystem. Do not create the directory if it doesn't exist.
505 * Assumes that sysinst has already mounted the target root.
508 target_test_file(const char *path
)
511 return target_test(S_IFREG
, path
);
515 target_test_symlink(const char *path
)
518 return target_test(S_IFLNK
, path
);
522 target_file_exists_p(const char *path
)
525 return (target_test_file(path
) == 0);
529 target_dir_exists_p(const char *path
)
532 return (target_test_dir(path
) == 0);
536 target_symlink_exists_p(const char *path
)
539 return (target_test_symlink(path
) == 0);