]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c
MFV r324198: 8081 Compiler warnings in zdb
[FreeBSD/FreeBSD.git] / usr.sbin / bsnmpd / modules / snmp_hostres / hostres_snmp.c
1 /*-
2  * SPDX-License-Identifier: (BSD-2-Clause AND Beerware)
3  *
4  * Copyright (c) 2005-2006 The FreeBSD Project
5  * All rights reserved.
6  *
7  * Author: Victor Cruceru <soc-victor@freebsd.org>
8  *
9  * Redistribution of this software and documentation and use in source and
10  * binary forms, with or without modification, are permitted provided that
11  * the following conditions are met:
12  *
13  * 1. Redistributions of source code or documentation must retain the above
14  *    copyright notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * This C file contains code developed by Poul-Henning Kamp under the
32  * following license:
33  *
34  * FreeBSD: src/sbin/mdconfig/mdconfig.c,v 1.33.2.1 2004/09/14 03:32:21 jmg Exp
35  * ----------------------------------------------------------------------------
36  * "THE BEER-WARE LICENSE" (Revision 42):
37  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
38  * can do whatever you want with this stuff. If we meet some day, and you think
39  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
40  * ----------------------------------------------------------------------------
41  *
42  * $FreeBSD$
43  */
44
45 /*
46  * Host Resources MIB implementation for bsnmpd.
47  */
48
49 #include <paths.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <unistd.h>
54
55 #include "hostres_snmp.h"
56 #include "hostres_oid.h"
57 #include "hostres_tree.h"
58
59 /* Internal id got after we'll register this module with the agent */
60 static u_int host_registration_id = 0;
61
62 /* This our hostres module */
63 static struct lmodule *hostres_module;
64
65 /* See the generated file hostres_oid.h */
66 static const struct asn_oid oid_host = OIDX_host;
67
68 /* descriptor to access kernel memory */
69 kvm_t *hr_kd;
70
71 /*
72  * HOST RESOURCES mib module finalization hook.
73  * Returns 0 on success, < 0 on error
74  */
75 static int
76 hostres_fini(void)
77 {
78
79         if (hr_kd != NULL)
80                 (void)kvm_close(hr_kd);
81
82         fini_storage_tbl();
83         fini_fs_tbl();
84         fini_processor_tbl();
85         fini_disk_storage_tbl();
86         fini_device_tbl();
87         fini_partition_tbl();
88         fini_network_tbl();
89         fini_printer_tbl();
90
91         fini_swrun_tbl();
92         fini_swins_tbl();
93
94         fini_scalars();
95
96         if (host_registration_id > 0)
97                 or_unregister(host_registration_id);
98
99         HRDBG("done.");
100         return (0);
101 }
102
103 /*
104  * HOST RESOURCES mib module initialization hook.
105  * Returns 0 on success, < 0 on error
106  */
107 static int
108 hostres_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
109 {
110
111         hostres_module = mod;
112
113         /*
114          * NOTE: order of these calls is important here!
115          */
116         if ((hr_kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY,
117             "kvm_open")) == NULL) {
118                 syslog(LOG_ERR, "kvm_open failed: %m ");
119                 return (-1);
120         }
121
122         /*
123          * The order is relevant here, because some table depend on each other.
124          */
125         init_device_tbl();
126
127         /* populates partition table too */
128         if (init_disk_storage_tbl()) {
129                 hostres_fini();
130                 return (-1);
131         }
132         init_processor_tbl();
133         init_printer_tbl();
134
135         /*
136          * populate storage and FS tables. Must be done after device
137          * initialisation because the FS refresh code calls into the
138          * partition refresh code.
139          */
140         init_storage_tbl();
141
142
143         /* also the hrSWRunPerfTable's support is initialized here */
144         init_swrun_tbl();
145         init_swins_tbl();
146
147         HRDBG("done.");
148
149         return (0);
150 }
151
152 /*
153  * HOST RESOURCES mib module start operation
154  * returns nothing
155  */
156 static void
157 hostres_start(void)
158 {
159
160         host_registration_id = or_register(&oid_host,
161             "The MIB module for Host Resource MIB (RFC 2790).",
162             hostres_module);
163
164         start_device_tbl(hostres_module);
165         start_processor_tbl(hostres_module);
166         start_network_tbl();
167
168         HRDBG("done.");
169 }
170
171 /* this identifies the HOST RESOURCES mib module */
172 const struct snmp_module config = {
173         "This module implements the host resource mib (rfc 2790)",
174         hostres_init,
175         hostres_fini,
176         NULL,                   /* idle function, do not use it */
177         NULL,
178         NULL,
179         hostres_start,
180         NULL,              /* proxy a PDU */
181         hostres_ctree,    /* see the generated hostres_tree.h */
182         hostres_CTREE_SIZE,     /* see the generated hostres_tree.h */
183         NULL
184 };
185
186 /**
187  * Make an SNMP DateAndTime from a struct tm. This should be in the library.
188  */
189 int
190 make_date_time(u_char *str, const struct tm *tm, u_int decisecs)
191 {
192
193         str[0] = (u_char)((tm->tm_year + 1900) >> 8);
194         str[1] = (u_char)(tm->tm_year + 1900);
195         str[2] = tm->tm_mon + 1;
196         str[3] = tm->tm_mday;
197         str[4] = tm->tm_hour;
198         str[5] = tm->tm_min;
199         str[6] = tm->tm_sec;
200         str[7] = decisecs;
201         if (tm->tm_gmtoff < 0)
202                 str[8] = '-';
203         else
204                 str[8] = '+';
205
206         str[9] = (u_char)(labs(tm->tm_gmtoff) / 3600);
207         str[10] = (u_char)((labs(tm->tm_gmtoff) % 3600) / 60);
208
209         return (11);
210 }