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