Removing old svn keywords.
[monky] / src / netbsd.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "netbsd.h"
29
30 static kvm_t *kd = NULL;
31 int kd_init = 0, nkd_init = 0;
32 u_int32_t sensvalue;
33 char errbuf[_POSIX2_LINE_MAX];
34
35 static int init_kvm(void)
36 {
37         if (kd_init) {
38                 return 0;
39         }
40
41         kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
42         if (kd == NULL) {
43                 warnx("cannot kvm_openfiles: %s", errbuf);
44                 return -1;
45         }
46         kd_init = 1;
47         return 0;
48 }
49
50 static int swapmode(int *retavail, int *retfree)
51 {
52         int n;
53         struct swapent *sep;
54
55         *retavail = 0;
56         *retfree = 0;
57
58         n = swapctl(SWAP_NSWAP, 0, 0);
59
60         if (n < 1) {
61                 warn("could not get swap information");
62                 return 0;
63         }
64
65         sep = (struct swapent *) malloc(n * (sizeof(*sep)));
66
67         if (sep == NULL) {
68                 warn("memory allocation failed");
69                 return 0;
70         }
71
72         if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
73                 warn("could not get swap stats");
74                 return 0;
75         }
76         for (; n > 0; n--) {
77                 *retavail += (int) dbtob(sep[n - 1].se_nblks);
78                 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
79         }
80         *retavail = (int) (*retavail / 1024);
81         *retfree = (int) (*retfree / 1024);
82
83         return 1;
84 }
85
86 void prepare_update()
87 {
88 }
89
90 void update_uptime()
91 {
92         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
93         struct timeval boottime;
94         time_t now;
95         int size = sizeof(boottime);
96
97         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
98                         && (boottime.tv_sec != 0)) {
99                 time(&now);
100                 info.uptime = now - boottime.tv_sec;
101         } else {
102                 warn("could not get uptime");
103                 info.uptime = 0;
104         }
105 }
106
107 int check_mount(char *s)
108 {
109         /* stub */
110         return 0;
111 }
112
113 void update_meminfo()
114 {
115         int mib[] = { CTL_VM, VM_UVMEXP2 };
116         int total_pages, inactive_pages, free_pages;
117         int swap_avail, swap_free;
118         const int pagesize = getpagesize();
119         struct uvmexp_sysctl uvmexp;
120         size_t size = sizeof(uvmexp);
121
122         if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
123                 warn("could not get memory info");
124                 return;
125         }
126
127         total_pages = uvmexp.npages;
128         free_pages = uvmexp.free;
129         inactive_pages = uvmexp.inactive;
130
131         info.memmax = (total_pages * pagesize) >> 10;
132         info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
133         info.memeasyfree = info.memfree = info.memmax - info.mem;
134
135         if (swapmode(&swap_avail, &swap_free) >= 0) {
136                 info.swapmax = swap_avail;
137                 info.swap = (swap_avail - swap_free);
138         }
139 }
140
141 void update_net_stats()
142 {
143         int i;
144         double delta;
145         struct ifnet ifnet;
146         struct ifnet_head ifhead;       /* interfaces are in a tail queue */
147         u_long ifnetaddr;
148         static struct nlist namelist[] = {
149                 { "_ifnet" },
150                 { NULL }
151         };
152         static kvm_t *nkd;
153
154         if (!nkd_init) {
155                 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
156                 if (nkd == NULL) {
157                         warnx("cannot kvm_openfiles: %s", errbuf);
158                         warnx("maybe you need to setgid kmem this program?");
159                         return;
160                 } else if (kvm_nlist(nkd, namelist) != 0) {
161                         warn("cannot kvm_nlist");
162                         return;
163                 } else {
164                         nkd_init = 1;
165                 }
166         }
167
168         if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
169                         sizeof(ifhead)) < 0) {
170                 warn("cannot kvm_read");
171                 return;
172         }
173
174         /* get delta */
175         delta = current_update_time - last_update_time;
176         if (delta <= 0.0001) {
177                 return;
178         }
179
180         for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
181                         ifnet.if_list.tqe_next && i < 16;
182                         ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
183
184                 struct net_stat *ns;
185                 long long last_recv, last_trans;
186
187                 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
188                 ns = get_net_stat(ifnet.if_xname);
189                 ns->up = 1;
190                 last_recv = ns->recv;
191                 last_trans = ns->trans;
192
193                 if (ifnet.if_ibytes < ns->last_read_recv) {
194                         ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
195                                 ifnet.if_ibytes;
196                 } else {
197                         ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
198                 }
199
200                 ns->last_read_recv = ifnet.if_ibytes;
201
202                 if (ifnet.if_obytes < ns->last_read_trans) {
203                         ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
204                                 ifnet.if_obytes;
205                 } else {
206                         ns->trans += (ifnet.if_obytes - ns->last_read_trans);
207                 }
208
209                 ns->last_read_trans = ifnet.if_obytes;
210
211                 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
212                 ns->last_read_recv = ifnet.if_ibytes;
213                 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
214                 ns->last_read_trans = ifnet.if_obytes;
215
216                 ns->recv_speed = (ns->recv - last_recv) / delta;
217                 ns->trans_speed = (ns->trans - last_trans) / delta;
218         }
219 }
220
221 void update_total_processes()
222 {
223         /* It's easier to use kvm here than sysctl */
224
225         int n_processes;
226
227         info.procs = 0;
228
229         if (init_kvm() < 0) {
230                 return;
231         } else {
232                 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
233                         &n_processes);
234         }
235
236         info.procs = n_processes;
237 }
238
239 void update_running_processes()
240 {
241         struct kinfo_proc2 *p;
242         int n_processes;
243         int i, cnt = 0;
244
245         info.run_procs = 0;
246
247         if (init_kvm() < 0) {
248                 return;
249         } else {
250                 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
251                         &n_processes);
252                 for (i = 0; i < n_processes; i++) {
253                         if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
254                                         || p[i].p_stat == LSONPROC) {
255                                 cnt++;
256                         }
257                 }
258         }
259
260         info.run_procs = cnt;
261 }
262
263 struct cpu_load_struct {
264         unsigned long load[5];
265 };
266
267 struct cpu_load_struct fresh = {
268         {0, 0, 0, 0, 0}
269 };
270
271 long cpu_used, oldtotal, oldused;
272
273 void update_cpu_usage()
274 {
275         long used, total;
276         static u_int64_t cp_time[CPUSTATES];
277         size_t len = sizeof(cp_time);
278
279         info.cpu_usage = 0;
280
281         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
282                 warn("cannot get kern.cp_time");
283         }
284
285         fresh.load[0] = cp_time[CP_USER];
286         fresh.load[1] = cp_time[CP_NICE];
287         fresh.load[2] = cp_time[CP_SYS];
288         fresh.load[3] = cp_time[CP_IDLE];
289         fresh.load[4] = cp_time[CP_IDLE];
290
291         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
292         total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
293
294         if ((total - oldtotal) != 0) {
295                 info.cpu_usage = ((double) (used - oldused)) /
296                         (double) (total - oldtotal);
297         } else {
298                 info.cpu_usage = 0;
299         }
300
301         oldused = used;
302         oldtotal = total;
303 }
304
305 double get_sysfs_info(int *fd, int div, char *devtype)
306 {
307         return -1;
308 }
309
310 void update_load_average()
311 {
312         double v[3];
313
314         getloadavg(v, 3);
315
316         info.loadavg[0] = (float) v[0];
317         info.loadavg[1] = (float) v[1];
318         info.loadavg[2] = (float) v[2];
319 }
320
321 double get_acpi_temperature(int fd)
322 {
323         return -1;
324 }
325
326 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
327 {
328 }
329
330 int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
331                 int *div, char *devtype)
332 {
333         return -1;
334 }
335
336 int open_acpi_temperature(const char *name)
337 {
338         return -1;
339 }
340
341 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
342 {
343         if (!p_client_buffer || client_buffer_size <= 0) {
344                 return;
345         }
346
347         /* not implemented */
348         memset(p_client_buffer, 0, client_buffer_size);
349 }
350
351 /* char *get_acpi_fan() */
352 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
353 {
354         if (!p_client_buffer || client_buffer_size <= 0) {
355                 return;
356         }
357
358         /* not implemented */
359         memset(p_client_buffer, 0, client_buffer_size);
360 }
361
362 void update_entropy(void)
363 {
364 }