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