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