source: trunk/third/sysinfo/bsd-sysctl.c @ 12269

Revision 12269, 6.3 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12268, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (c) 1992-1998 Michael A. Cooper.
3 * This software may be freely used and distributed provided it is not
4 * sold for profit or used in part or in whole for commercial gain
5 * without prior written agreement, and the author is credited
6 * appropriately.
7 */
8
9#ifndef lint
10static char *RCSid = "$Revision: 1.1.1.1 $";
11#endif
12
13/*
14 * sysctl(3) functions common to BSD implimentations (BSD/OS, FreeBSD, etc.)
15 */
16
17#include "defs.h"
18#include <sys/types.h>
19#include <sys/sysctl.h>
20#include <sys/time.h>
21#include <sys/socket.h>         /* BSD/OS 3.1 */
22#include <net/if.h>
23#include <net/if_dl.h>
24#include <net/route.h>
25#if     defined(freebsd)
26#include <net/ethernet.h>
27#endif  /* freebsd */
28#if     defined(VM_SWAPSTATS)
29#include <vm/swap_pager.h>
30#endif  /* VM_SWAPSTATS */
31
32/*
33 * Use sysctl() to get MODEL.
34 */
35extern char *GetModelSysCtl()
36{
37    int                         Query[2];
38    static char                 Model[128];
39    size_t                      Length = sizeof(Model);
40
41    Query[0] = CTL_HW;
42    Query[1] = HW_MODEL;
43    if (sysctl(Query, 2, Model, &Length, NULL, 0) == -1) {
44        SImsg(SIM_GERR, "sysctl CTL_HW.HW_MODEL failed: %s", SYSERR);
45        return((char *) NULL);
46    }
47
48    return(Model);
49}
50
51/*
52 * Use sysctl() to get Number of CPUs
53 */
54extern char *GetNcpuSysCtl()
55{
56    int                         Query[2];
57    int                         NumCpu = 0;
58    size_t                      Length = sizeof(NumCpu);
59
60    Query[0] = CTL_HW;
61    Query[1] = HW_NCPU;
62    if (sysctl(Query, 2, &NumCpu, &Length, NULL, 0) == -1) {
63        SImsg(SIM_GERR, "sysctl CTL_HW.HW_NCPU failed: %s", SYSERR);
64        return((char *) NULL);
65    }
66
67    return(itoa(NumCpu));
68}
69
70/*
71 * Use sysctl() to get the Kernel Version
72 */
73extern char *GetKernVerSysCtl()
74{
75    int                         Query[2];
76    static char                 Version[256];
77    size_t                      Length = sizeof(Version);
78
79    Query[0] = CTL_KERN;
80    Query[1] = KERN_VERSION;
81    if (sysctl(Query, 2, Version, &Length, NULL, 0) == -1) {
82        SImsg(SIM_GERR, "sysctl CTL_KERN.KERN_MODEL failed: %s", SYSERR);
83        return((char *) NULL);
84    }
85
86    return(Version);
87}
88
89/*
90 * Use sysctl() to get the OS Distribution
91 */
92extern char *GetOSDistSysCtl()
93{
94    int                         Query[2];
95    int                         Dist = 0;
96    size_t                      Length = sizeof(Dist);
97
98    Query[0] = CTL_KERN;
99    Query[1] = KERN_OSREV;
100    if (sysctl(Query, 2, &Dist, &Length, NULL, 0) == -1) {
101        SImsg(SIM_GERR, "sysctl CTL_KERN.KERN_OSREV failed: %s", SYSERR);
102        return((char *) NULL);
103    }
104
105    return(itoa(Dist));
106}
107
108/*
109 * Use sysctl() to get the Memory
110 */
111extern char *GetMemorySysCtl()
112{
113    int                         Query[2];
114    int                         Memory = 0;
115    Large_t                     Amount;
116    size_t                      Length = sizeof(Memory);
117
118    Query[0] = CTL_HW;
119    Query[1] = HW_PHYSMEM;
120    if (sysctl(Query, 2, &Memory, &Length, NULL, 0) == -1) {
121        SImsg(SIM_GERR, "sysctl CTL_HW.HW_PHYSMEM failed: %s", SYSERR);
122        return((char *) NULL);
123    }
124
125    Amount = DivRndUp(Memory, (Large_t)MBYTES);
126
127    return(GetMemoryStr(Amount));
128}
129
130/*
131 * Use sysctl() to get the Virtual Memory
132 */
133extern char *GetVirtMemSysCtl()
134{
135#if     defined(VM_SWAPSTATS)
136    int                         Query[2];
137    struct swapstats            Swap;
138    size_t                      Length = sizeof(Swap);
139    Large_t                     Amount;
140
141    Query[0] = CTL_VM;
142    Query[1] = VM_SWAPSTATS;
143    if (sysctl(Query, 2, &Swap, &Length, NULL, 0) == -1) {
144        SImsg(SIM_GERR, "sysctl CTL_Vm.VM_SWAPSTATS failed: %s", SYSERR);
145        return((char *) NULL);
146    }
147
148    Amount = DivRndUp(Swap.swap_total * 512, (Large_t)MBYTES);
149
150    return(GetMemoryStr(Amount));
151#else
152    return((char *) NULL);
153#endif  /* VM_SWAPSTATS */
154}
155
156/*
157 * Use sysctl() to get the Boot Time
158 */
159extern char *GetBootTimeSysCtl()
160{
161    int                         Query[2];
162    struct timeval              TimeVal;
163    time_t                      Time;
164    size_t                      Length = sizeof(TimeVal);
165    char                       *TimeStr;
166    register char              *cp;
167
168    Query[0] = CTL_KERN;
169    Query[1] = KERN_BOOTTIME;
170    if (sysctl(Query, 2, &TimeVal, &Length, NULL, 0) == -1) {
171        SImsg(SIM_GERR, "sysctl CTL_KERN.KERN_BOOTTIME failed: %s", SYSERR);
172        return((char *) NULL);
173    }
174
175    Time = (time_t) TimeVal.tv_sec;
176    TimeStr = ctime(&Time);
177    if (cp = strchr(TimeStr, '\n'))
178        *cp = CNULL;
179
180    return(TimeStr);
181}
182
183/*
184 * Use sysctl(3) to retrieve the MAC Info for a network interface.
185 * This is based on code from FreeBSD 2.2.6 ifconfig(8).
186 * This is totally convoluted!  Yuck!
187 */
188extern void SetMacInfoSysCtl(DevInfo, NetIf)
189     DevInfo_t                 *DevInfo;
190     NetIF_t                   *NetIf;
191{
192    static char                 Ether[128];
193    static char                 EtherName[128];
194#if     defined(HAVE_ETHER_NTOHOST)
195    struct ether_addr           EtherAddr;
196#endif  /* HAVE_ETHER_NTOHOST */
197    register char              *cp;
198    register int                n;
199    register int                Len;
200    struct if_msghdr           *ifm, *nextifm;
201    struct ifa_msghdr          *ifam;
202    struct sockaddr_dl         *sdl;
203    char                       *buf, *lim, *next;
204    size_t                      needed;
205    int                         mib[6];
206
207    mib[0] = CTL_NET;
208    mib[1] = PF_ROUTE;
209    mib[2] = 0;
210    mib[3] = AF_INET;   /* address family */
211    mib[4] = NET_RT_IFLIST;
212    mib[5] = 0;
213    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
214        SImsg(SIM_GERR, "%s: sysctl get iflist estimate failed: %s",
215              DevInfo->Name, SYSERR);
216        return;
217    }
218    buf = (char *) xmalloc(needed);
219    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
220        SImsg(SIM_GERR, "%s: sysctl retrieve iflist table failed: %s",
221              DevInfo->Name, SYSERR);
222        return;
223    }
224    lim = buf + needed;
225   
226    next = buf;
227    while (next < lim) {
228       
229        ifm = (struct if_msghdr *)next;
230       
231        if (ifm->ifm_type == RTM_IFINFO) {
232            sdl = (struct sockaddr_dl *)(ifm + 1);
233        } else {
234            SImsg(SIM_GERR,
235            "%s: Cannot get MAC info: out of sync parsing NET_RT_IFLIST.",
236                  DevInfo->Name);
237            return;
238        }
239       
240        next += ifm->ifm_msglen;
241        ifam = NULL;
242        while (next < lim) {
243           
244            nextifm = (struct if_msghdr *)next;
245           
246            if (nextifm->ifm_type != RTM_NEWADDR)
247                break;
248           
249            if (ifam == NULL)
250                ifam = (struct ifa_msghdr *)nextifm;
251           
252            next += nextifm->ifm_msglen;
253        }
254
255        if (EQN(DevInfo->Name, sdl->sdl_data, sdl->sdl_nlen)) {
256            /* Found it */
257            cp = (char *)LLADDR(sdl);
258            if ((n = sdl->sdl_alen) > 0) {
259                Ether[0] = CNULL;
260                while (--n >= 0) {
261                    Len = strlen(Ether);
262                    (void) snprintf(&Ether[Len], sizeof(Ether)-Len,
263                                    "%02x%s", *cp++ & 0xff, n > 0 ? ":" : "");
264                }
265
266                NetIf->MACaddr = strdup(Ether);
267#if     defined(HAVE_ETHER_NTOHOST)
268                memcpy(&EtherAddr.octet, LLADDR(sdl), ETHER_ADDR_LEN);
269                if (ether_ntohost(EtherName, &EtherAddr) == 0)
270                    NetIf->MACname = strdup(EtherName);
271#endif  /* HAVE_ETHER_NTOHOST */
272            }
273            break;
274        }
275    }
276    (void) free(buf);
277}
Note: See TracBrowser for help on using the repository browser.