xref: /NextBSD/usr.sbin/asf/asf_prog.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 /*-
2  * Copyright (c) 2002, 2003 Greg Lehey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * This software is provided by the author ``as is'' and any express
15  * or implied warranties, including, but not limited to, the implied
16  * warranties of merchantability and fitness for a particular purpose
17  * are disclaimed.  In no event shall the author be liable for any
18  * direct, indirect, incidental, special, exemplary, or consequential
19  * damages (including, but not limited to, procurement of substitute
20  * goods or services; loss of use, data, or profits; or business
21  * interruption) however caused and on any theory of liability,
22  * whether in contract, strict liability, or tort (including
23  * negligence or otherwise) arising in any way out of the use of this
24  * software, even if advised of the possibility of such damage.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <err.h>
32 #include <inttypes.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include "asf.h"
38 
39 /*
40  * Get the linker file list from kldstat(8) output.
41  * The "run" flag tells if kldstat(8) should run now.
42  * Of course, kldstat(1) can run in a live system only, but its output
43  * can be saved beforehand and fed to this function later via stdin.
44  */
45 void
asf_prog(int run)46 asf_prog(int run)
47 {
48 	char	buf[LINE_MAX];
49 	char   *token[MAXTOKEN];
50 	char   *endp;
51 	FILE   *kldstat;
52 	caddr_t	base;
53 	int	tokens;
54 
55 	if (run) {
56 		if ((kldstat = popen("kldstat", "r")) == NULL)
57 			err(2, "can't start kldstat");
58 	} else
59 		kldstat = stdin;
60 
61 	while (fgets(buf, sizeof(buf), kldstat)) {
62 		/* Skip header line and main kernel file */
63 		if (buf[0] == 'I' || strstr(buf, KERNFILE))
64 			continue;
65 		tokens = tokenize(buf, token, MAXTOKEN);
66 		if (tokens < 4)
67 			continue;
68 		base = (caddr_t)(uintptr_t)strtoumax(token[2], &endp, 16);
69 		if (endp == NULL || *endp != '\0')
70 			continue;
71 		kfile_add(token[4], base);
72 	}
73 }
74