]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/crunch/crunchgen/crunched_main.c
ZFS: MFV 2.0-rc1-ga00c61
[FreeBSD/FreeBSD.git] / usr.sbin / crunch / crunchgen / crunched_main.c
1 /*
2  * Copyright (c) 1994 University of Maryland
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of U.M. not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  U.M. makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  *
15  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  * Author: James da Silva, Systems Design and Analysis Group
23  *                         Computer Science Department
24  *                         University of Maryland at College Park
25  */
26 /*-
27  * SPDX-License-Identifier: BSD-2-Clause
28  *
29  * Copyright 2020 Alex Richardson <arichardson@FreeBSD.org>
30  *
31  * This software was developed by SRI International and the University of
32  * Cambridge Computer Laboratory (Department of Computer Science and
33  * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
34  * DARPA SSITH research programme.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  */
58 /*
59  * crunched_main.c - main program for crunched binaries, it branches to a
60  *      particular subprogram based on the value of argv[0].  Also included
61  *      is a little program invoked when the crunched binary is called via
62  *      its EXECNAME.  This one prints out the list of compiled-in binaries,
63  *      or calls one of them based on argv[1].   This allows the testing of
64  *      the crunched binary without creating all the links.
65  */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69
70 #include <sys/param.h>
71 #include <sys/auxv.h>
72 #include <sys/sysctl.h>
73
74 #include <err.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78
79 struct stub {
80         char *name;
81         int (*f)();
82 };
83
84 extern const char *__progname;
85 extern struct stub entry_points[];
86
87 static void crunched_usage(void);
88
89 static struct stub *
90 find_entry_point(const char *basename)
91 {
92         struct stub *ep = NULL;
93
94         for (ep = entry_points; ep->name != NULL; ep++)
95                 if (!strcmp(basename, ep->name))
96                         break;
97
98         return (ep);
99 }
100
101 static const char *
102 get_basename(const char *exe_path)
103 {
104         const char *slash = strrchr(exe_path, '/');
105         return (slash ? slash + 1 : exe_path);
106 }
107
108 int
109 main(int argc, char **argv, char **envp)
110 {
111         struct stub *ep = NULL;
112         const char *basename = NULL;
113
114         /*
115          * Look at __progname first (this will be set if the crunched binary is
116          * invoked directly).
117          */
118         if (__progname) {
119                 basename = get_basename(__progname);
120                 ep = find_entry_point(basename);
121         }
122
123         /*
124          * Otherwise try to find entry point based on argv[0] (this works for
125          * both symlinks as well as hardlinks). However, it does not work when
126          * su invokes a crunched shell because it sets argv[0] to _su when
127          * invoking the shell. In that case we look at AT_EXECPATH as a
128          * fallback.
129          */
130         if (ep == NULL) {
131                 basename = get_basename(argv[0]);
132                 ep = find_entry_point(basename);
133         }
134
135         /*
136          * If we didn't find the entry point based on __progname or argv[0],
137          * try AT_EXECPATH to get the actual binary that was executed.
138          */
139         if (ep == NULL) {
140                 char buf[MAXPATHLEN];
141                 int error = elf_aux_info(AT_EXECPATH, &buf, sizeof(buf));
142
143                 if (error == 0) {
144                         const char *exe_name = get_basename(buf);
145                         /*
146                          * Keep using argv[0] if AT_EXECPATH is the crunched
147                          * binary so that symlinks to the crunched binary report
148                          * "not compiled in" instead of invoking
149                          * crunched_main().
150                          */
151                         if (strcmp(exe_name, EXECNAME) != 0) {
152                                 basename = exe_name;
153                                 ep = find_entry_point(basename);
154                         }
155                 } else {
156                         warnc(error, "elf_aux_info(AT_EXECPATH) failed");
157                 }
158         }
159
160         if (basename == NULL || *basename == '\0')
161                 crunched_usage();
162
163         if (ep != NULL) {
164                 return ep->f(argc, argv, envp);
165         } else {
166                 fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename);
167                 crunched_usage();
168         }
169 }
170
171 int
172 crunched_main(int argc, char **argv, char **envp)
173 {
174         if (argc <= 1)
175                 crunched_usage();
176
177         __progname = get_basename(argv[1]);
178         return main(--argc, ++argv, envp);
179 }
180
181 static void
182 crunched_usage()
183 {
184         int columns, len;
185         struct stub *ep;
186
187         fprintf(stderr,
188             "usage: %s <prog> <args> ..., where <prog> is one of:\n", EXECNAME);
189         columns = 0;
190         for (ep = entry_points; ep->name != NULL; ep++) {
191                 len = strlen(ep->name) + 1;
192                 if (columns + len < 80)
193                         columns += len;
194                 else {
195                         fprintf(stderr, "\n");
196                         columns = len;
197                 }
198                 fprintf(stderr, " %s", ep->name);
199         }
200         fprintf(stderr, "\n");
201         exit(1);
202 }
203
204 /* end of crunched_main.c */