Move vi modelines closer to the beginning, so they're more likely to be actually...
[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-2009 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
33 static kvm_t *kd = NULL;
34 int kd_init = 0, nkd_init = 0;
35 u_int32_t sensvalue;
36 char errbuf[_POSIX2_LINE_MAX];
37
38 static int init_kvm(void)
39 {
40         if (kd_init) {
41                 return 0;
42         }
43
44         kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
45         if (kd == NULL) {
46                 warnx("cannot kvm_openfiles: %s", errbuf);
47                 return -1;
48         }
49         kd_init = 1;
50         return 0;
51 }
52
53 static int swapmode(int *retavail, int *retfree)
54 {
55         int n;
56         struct swapent *sep;
57
58         *retavail = 0;
59         *retfree = 0;
60
61         n = swapctl(SWAP_NSWAP, 0, 0);
62
63         if (n < 1) {
64                 warn("could not get swap information");
65                 return 0;
66         }
67
68         sep = (struct swapent *) malloc(n * (sizeof(*sep)));
69
70         if (sep == NULL) {
71                 warn("memory allocation failed");
72                 return 0;
73         }
74
75         if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
76                 warn("could not get swap stats");
77                 return 0;
78         }
79         for (; n > 0; n--) {
80                 *retavail += (int) dbtob(sep[n - 1].se_nblks);
81                 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
82         }
83         *retavail = (int) (*retavail / 1024);
84         *retfree = (int) (*retfree / 1024);
85
86         return 1;
87 }
88
89 void prepare_update()
90 {
91 }
92
93 void update_uptime()
94 {
95         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
96         struct timeval boottime;
97         time_t now;
98         int size = sizeof(boottime);
99
100         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
101                         && (boottime.tv_sec != 0)) {
102                 time(&now);
103                 info.uptime = now - boottime.tv_sec;
104         } else {
105                 warn("could not get uptime");
106                 info.uptime = 0;
107         }
108 }
109
110 int check_mount(char *s)
111 {
112         /* stub */
113         return 0;
114 }
115
116 void update_meminfo()
117 {
118         int mib[] = { CTL_VM, VM_UVMEXP2 };
119         int total_pages, inactive_pages, free_pages;
120         int swap_avail, swap_free;
121         const int pagesize = getpagesize();
122         struct uvmexp_sysctl uvmexp;
123         size_t size = sizeof(uvmexp);
124
125         if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
126                 warn("could not get memory info");
127                 return;
128         }
129
130         total_pages = uvmexp.npages;
131         free_pages = uvmexp.free;
132         inactive_pages = uvmexp.inactive;
133
134         info.memmax = (total_pages * pagesize) >> 10;
135         info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
136         info.memeasyfree = info.memfree = info.memmax - info.mem;
137
138         if (swapmode(&swap_avail, &swap_free) >= 0) {
139                 info.swapmax = swap_avail;
140                 info.swap = (swap_avail - swap_free);
141                 info.swapfree = swap_free;
142         }
143 }
144
145 void update_net_stats()
146 {
147         int i;
148         double delta;
149         struct ifnet ifnet;
150         struct ifnet_head ifhead;       /* interfaces are in a tail queue */
151         u_long ifnetaddr;
152         static struct nlist namelist[] = {
153                 { "_ifnet" },
154                 { NULL }
155         };
156         static kvm_t *nkd;
157
158         if (!nkd_init) {
159                 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
160                 if (nkd == NULL) {
161                         warnx("cannot kvm_openfiles: %s", errbuf);
162                         warnx("maybe you need to setgid kmem this program?");
163                         return;
164                 } else if (kvm_nlist(nkd, namelist) != 0) {
165                         warn("cannot kvm_nlist");
166                         return;
167                 } else {
168                         nkd_init = 1;
169                 }
170         }
171
172         if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
173                         sizeof(ifhead)) < 0) {
174                 warn("cannot kvm_read");
175                 return;
176         }
177
178         /* get delta */
179         delta = current_update_time - last_update_time;
180         if (delta <= 0.0001) {
181                 return;
182         }
183
184         for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
185                         ifnet.if_list.tqe_next && i < 16;
186                         ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
187
188                 struct net_stat *ns;
189                 long long last_recv, last_trans;
190
191                 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
192                 ns = get_net_stat(ifnet.if_xname, NULL, NULL);
193                 ns->up = 1;
194                 last_recv = ns->recv;
195                 last_trans = ns->trans;
196
197                 if (ifnet.if_ibytes < ns->last_read_recv) {
198                         ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
199                                 ifnet.if_ibytes;
200                 } else {
201                         ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
202                 }
203
204                 ns->last_read_recv = ifnet.if_ibytes;
205
206                 if (ifnet.if_obytes < ns->last_read_trans) {
207                         ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
208                                 ifnet.if_obytes;
209                 } else {
210                         ns->trans += (ifnet.if_obytes - ns->last_read_trans);
211                 }
212
213                 ns->last_read_trans = ifnet.if_obytes;
214
215                 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
216                 ns->last_read_recv = ifnet.if_ibytes;
217                 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
218                 ns->last_read_trans = ifnet.if_obytes;
219
220                 ns->recv_speed = (ns->recv - last_recv) / delta;
221                 ns->trans_speed = (ns->trans - last_trans) / delta;
222         }
223 }
224
225 void update_total_processes()
226 {
227         /* It's easier to use kvm here than sysctl */
228
229         int n_processes;
230
231         info.procs = 0;
232
233         if (init_kvm() < 0) {
234                 return;
235         } else {
236                 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
237                         &n_processes);
238         }
239
240         info.procs = n_processes;
241 }
242
243 void update_running_processes()
244 {
245         struct kinfo_proc2 *p;
246         int n_processes;
247         int i, cnt = 0;
248
249         info.run_procs = 0;
250
251         if (init_kvm() < 0) {
252                 return;
253         } else {
254                 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
255                         &n_processes);
256                 for (i = 0; i < n_processes; i++) {
257                         if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
258                                         || p[i].p_stat == LSONPROC) {
259                                 cnt++;
260                         }
261                 }
262         }
263
264         info.run_procs = cnt;
265 }
266
267 struct cpu_load_struct {
268         unsigned long load[5];
269 };
270
271 struct cpu_load_struct fresh = {
272         {0, 0, 0, 0, 0}
273 };
274
275 long cpu_used, oldtotal, oldused;
276
277 void update_cpu_usage()
278 {
279         long used, total;
280         static u_int64_t cp_time[CPUSTATES];
281         size_t len = sizeof(cp_time);
282
283         info.cpu_usage = 0;
284
285         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
286                 warn("cannot get kern.cp_time");
287         }
288
289         fresh.load[0] = cp_time[CP_USER];
290         fresh.load[1] = cp_time[CP_NICE];
291         fresh.load[2] = cp_time[CP_SYS];
292         fresh.load[3] = cp_time[CP_IDLE];
293         fresh.load[4] = cp_time[CP_IDLE];
294
295         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
296         total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
297
298         if ((total - oldtotal) != 0) {
299                 info.cpu_usage = ((double) (used - oldused)) /
300                         (double) (total - oldtotal);
301         } else {
302                 info.cpu_usage = 0;
303         }
304
305         oldused = used;
306         oldtotal = total;
307 }
308
309 void update_load_average()
310 {
311         double v[3];
312
313         getloadavg(v, 3);
314
315         info.loadavg[0] = (float) v[0];
316         info.loadavg[1] = (float) v[1];
317         info.loadavg[2] = (float) v[2];
318 }
319
320 double get_acpi_temperature(int fd)
321 {
322         return -1;
323 }
324
325 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
326 {
327 }
328
329 int open_acpi_temperature(const char *name)
330 {
331         return -1;
332 }
333
334 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
335 {
336         if (!p_client_buffer || client_buffer_size <= 0) {
337                 return;
338         }
339
340         /* not implemented */
341         memset(p_client_buffer, 0, client_buffer_size);
342 }
343
344 /* char *get_acpi_fan() */
345 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
346 {
347         if (!p_client_buffer || client_buffer_size <= 0) {
348                 return;
349         }
350
351         /* not implemented */
352         memset(p_client_buffer, 0, client_buffer_size);
353 }
354
355 void update_entropy(void)
356 {
357 }