xref: /freebsd/usr.sbin/boottrace/boottrace.c (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
1*13ec1e31SMitchell Horne /*-
2*13ec1e31SMitchell Horne  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*13ec1e31SMitchell Horne  *
4*13ec1e31SMitchell Horne  * Copyright (c) 2022 NetApp, Inc.
5*13ec1e31SMitchell Horne  *
6*13ec1e31SMitchell Horne  * Redistribution and use in source and binary forms, with or without
7*13ec1e31SMitchell Horne  * modification, are permitted provided that the following conditions
8*13ec1e31SMitchell Horne  * are met:
9*13ec1e31SMitchell Horne  * 1. Redistributions of source code must retain the above copyright
10*13ec1e31SMitchell Horne  *    notice, this list of conditions and the following disclaimer.
11*13ec1e31SMitchell Horne  * 2. Redistributions in binary form must reproduce the above copyright
12*13ec1e31SMitchell Horne  *    notice, this list of conditions and the following disclaimer in the
13*13ec1e31SMitchell Horne  *    documentation and/or other materials provided with the distribution.
14*13ec1e31SMitchell Horne  *
15*13ec1e31SMitchell Horne  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*13ec1e31SMitchell Horne  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*13ec1e31SMitchell Horne  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*13ec1e31SMitchell Horne  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*13ec1e31SMitchell Horne  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*13ec1e31SMitchell Horne  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*13ec1e31SMitchell Horne  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*13ec1e31SMitchell Horne  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*13ec1e31SMitchell Horne  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*13ec1e31SMitchell Horne  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*13ec1e31SMitchell Horne  * SUCH DAMAGE.
26*13ec1e31SMitchell Horne  */
27*13ec1e31SMitchell Horne 
28*13ec1e31SMitchell Horne #include <sys/types.h>
29*13ec1e31SMitchell Horne #include <sys/boottrace.h>
30*13ec1e31SMitchell Horne #include <sys/sysctl.h>
31*13ec1e31SMitchell Horne #include <sys/wait.h>
32*13ec1e31SMitchell Horne 
33*13ec1e31SMitchell Horne #include <err.h>
34*13ec1e31SMitchell Horne #include <errno.h>
35*13ec1e31SMitchell Horne #include <stdio.h>
36*13ec1e31SMitchell Horne #include <stdlib.h>
37*13ec1e31SMitchell Horne #include <string.h>
38*13ec1e31SMitchell Horne #include <unistd.h>
39*13ec1e31SMitchell Horne 
40*13ec1e31SMitchell Horne static void
41*13ec1e31SMitchell Horne usage(void)
42*13ec1e31SMitchell Horne {
43*13ec1e31SMitchell Horne 	fprintf(stderr, "usage: boottrace utility [argument ...]\n");
44*13ec1e31SMitchell Horne 	exit(1);
45*13ec1e31SMitchell Horne }
46*13ec1e31SMitchell Horne 
47*13ec1e31SMitchell Horne int
48*13ec1e31SMitchell Horne main(int argc, char **argv)
49*13ec1e31SMitchell Horne {
50*13ec1e31SMitchell Horne 	pid_t pid;
51*13ec1e31SMitchell Horne 	int status;
52*13ec1e31SMitchell Horne 
53*13ec1e31SMitchell Horne 	if (argc < 2)
54*13ec1e31SMitchell Horne 		usage();
55*13ec1e31SMitchell Horne 	argv++;
56*13ec1e31SMitchell Horne 
57*13ec1e31SMitchell Horne 	BOOTTRACE("%s start", *argv);
58*13ec1e31SMitchell Horne 	pid = fork();
59*13ec1e31SMitchell Horne 	if (pid == -1) {
60*13ec1e31SMitchell Horne 		exit(1);
61*13ec1e31SMitchell Horne 	} else if (pid == 0) {
62*13ec1e31SMitchell Horne 		execvp(*argv, argv);
63*13ec1e31SMitchell Horne 		err(errno == ENOENT ? 127 : 126, "execvp %s", *argv);
64*13ec1e31SMitchell Horne 	}
65*13ec1e31SMitchell Horne 	waitpid(pid, &status, 0);
66*13ec1e31SMitchell Horne 	if (!WIFEXITED(status))
67*13ec1e31SMitchell Horne 		warnx("command terminated abnormally");
68*13ec1e31SMitchell Horne 	BOOTTRACE("%s done", *argv);
69*13ec1e31SMitchell Horne 
70*13ec1e31SMitchell Horne 	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
71*13ec1e31SMitchell Horne }
72