1*e447d670Schristos /* $NetBSD: getbootfile.c,v 1.6 2016/01/25 18:14:04 christos Exp $ */
2b1593d98Swiz
3b1593d98Swiz /*-
4b1593d98Swiz * Copyright (c) 2000 The NetBSD Foundation, Inc.
5b1593d98Swiz * All rights reserved.
6b1593d98Swiz *
7b1593d98Swiz * This code is derived from software contributed to The NetBSD Foundation
8b1593d98Swiz * by Thomas Klausner.
9b1593d98Swiz *
10b1593d98Swiz * Redistribution and use in source and binary forms, with or without
11b1593d98Swiz * modification, are permitted provided that the following conditions
12b1593d98Swiz * are met:
13b1593d98Swiz * 1. Redistributions of source code must retain the above copyright
14b1593d98Swiz * notice, this list of conditions and the following disclaimer.
15b1593d98Swiz * 2. Redistributions in binary form must reproduce the above copyright
16b1593d98Swiz * notice, this list of conditions and the following disclaimer in the
17b1593d98Swiz * documentation and/or other materials provided with the distribution.
18b1593d98Swiz *
19b1593d98Swiz * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b1593d98Swiz * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b1593d98Swiz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b1593d98Swiz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b1593d98Swiz * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b1593d98Swiz * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b1593d98Swiz * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b1593d98Swiz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b1593d98Swiz * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b1593d98Swiz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b1593d98Swiz * POSSIBILITY OF SUCH DAMAGE.
30b1593d98Swiz */
31b1593d98Swiz
32b1593d98Swiz #include <sys/cdefs.h>
33b1593d98Swiz #if defined(LIBC_SCCS) && !defined(lint)
34*e447d670Schristos __RCSID("$NetBSD: getbootfile.c,v 1.6 2016/01/25 18:14:04 christos Exp $");
35b1593d98Swiz #endif
36b1593d98Swiz
37b1593d98Swiz #include <sys/param.h>
38b1593d98Swiz #include <sys/sysctl.h>
39*e447d670Schristos #include <sys/cpu.h>
402bb9ea73Spetrov #include <string.h>
41b1593d98Swiz #include <paths.h>
42b1593d98Swiz #include <util.h>
43b1593d98Swiz
44b1593d98Swiz #ifdef CPU_BOOTED_KERNEL
45b1593d98Swiz static char name[MAXPATHLEN];
46b1593d98Swiz #endif
47b1593d98Swiz
48b1593d98Swiz const char *
getbootfile(void)49b1593d98Swiz getbootfile(void)
50b1593d98Swiz {
51b1593d98Swiz #ifdef CPU_BOOTED_KERNEL
52b1593d98Swiz int mib[2];
53b1593d98Swiz size_t size;
54b1593d98Swiz #endif
55b1593d98Swiz const char *kernel;
56b1593d98Swiz
57b1593d98Swiz kernel = _PATH_UNIX;
58b1593d98Swiz #ifdef CPU_BOOTED_KERNEL
59b1593d98Swiz /* find real boot-kernel name */
60b1593d98Swiz mib[0] = CTL_MACHDEP;
61b1593d98Swiz mib[1] = CPU_BOOTED_KERNEL;
62b1593d98Swiz size = sizeof(name) - 1;
6303831599Sfvdl if (sysctl(mib, 2, name + 1, &size, NULL, 0) == 0) {
64b1593d98Swiz /*
65b1593d98Swiz * traditionally, this sysctl returns the relative
66b1593d98Swiz * path of the kernel with the leading slash stripped
67b1593d98Swiz * -- could be empty, though (e.g. when netbooting).
68b1593d98Swiz */
69b1593d98Swiz if (name[1] != '\0') {
70b1593d98Swiz name[0] = '/';
71b1593d98Swiz kernel = name;
72b1593d98Swiz }
73b1593d98Swiz
74b1593d98Swiz /* check if we got a valid and 'secure' filename */
75d571f02fSchristos if (strcmp(kernel, _PATH_UNIX) != 0 &&
76d571f02fSchristos secure_path(kernel) != 0) {
77b1593d98Swiz /* doesn't seems so, fall back to default */
78b1593d98Swiz kernel = _PATH_UNIX;
79b1593d98Swiz }
80b1593d98Swiz }
81b1593d98Swiz #endif
82b1593d98Swiz
83b1593d98Swiz return (kernel);
84b1593d98Swiz }
85