added , , for crypto freaks
[monky] / src / netbsd.c
1 /* NetBSD port */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <err.h>
10 #include <limits.h>
11 #include <paths.h>
12
13 #include <kvm.h>
14 #include <nlist.h>
15
16 #include <sys/time.h>
17 #include <sys/param.h>
18 #include <sys/sysctl.h>
19 #include <sys/types.h>
20 #include <sys/user.h>
21 #include <sys/socket.h>
22 #include <sys/swap.h>
23 #include <sys/sched.h>
24 #include <sys/envsys.h>
25
26 #include <net/if.h>
27
28 #include <uvm/uvm_extern.h>
29
30 #include <machine/param.h>
31
32 #include "conky.h"
33
34
35 static kvm_t *kd = NULL;
36 int kd_init = 0, nkd_init = 0;
37 u_int32_t sensvalue;
38 char errbuf[_POSIX2_LINE_MAX];
39
40 static int init_kvm(void)
41 {
42         if (kd_init)
43                 return 0;
44
45         kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
46         if (kd == NULL) {
47                 (void) 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                 (void) 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                 (void) warn("memory allocation failed");
73                 return 0;
74         }
75
76         if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
77                 (void) 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 +=
83                     (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
84         }
85         *retavail = (int) (*retavail / 1024);
86         *retfree = (int) (*retfree / 1024);
87
88         return 1;
89 }
90
91
92 void prepare_update()
93 {
94 }
95
96 void update_uptime()
97 {
98         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
99         struct timeval boottime;
100         time_t now;
101         int size = sizeof(boottime);
102
103         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
104             && (boottime.tv_sec != 0)) {
105                 (void) time(&now);
106                 info.uptime = now - boottime.tv_sec;
107         } else {
108                 (void) warn("could not get uptime");
109                 info.uptime = 0;
110         }
111 }
112
113
114 void update_meminfo()
115 {
116         int mib[] = { CTL_VM, VM_UVMEXP2 };
117         int total_pages, inactive_pages, free_pages;
118         int swap_avail, swap_free;
119         const int pagesize = getpagesize();
120         struct uvmexp_sysctl uvmexp;
121         size_t size = sizeof(uvmexp);
122
123         info.memmax = info.mem = 0;
124         info.swapmax = info.swap = 0;
125
126
127         if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
128                 warn("could not get memory info");
129                 return;
130         }
131
132         total_pages = uvmexp.npages;
133         free_pages = uvmexp.free;
134         inactive_pages = uvmexp.inactive;
135
136         info.memmax = (total_pages * pagesize) >> 10;
137         info.mem =
138             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
139
140         if (swapmode(&swap_avail, &swap_free) >= 0) {
141                 info.swapmax = swap_avail;
142                 info.swap = (swap_avail - 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                         (void) warnx("cannot kvm_openfiles: %s", errbuf);
163                         (void)
164                             warnx
165                             ("maybe you need to setgid kmem this program?");
166                         return;
167                 } else if (kvm_nlist(nkd, namelist) != 0) {
168                         (void) warn("cannot kvm_nlist");
169                         return;
170                 } else
171                         nkd_init = 1;
172         }
173
174         if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
175                      sizeof(ifhead)) < 0) {
176                 (void) warn("cannot kvm_read");
177                 return;
178         }
179
180         /* get delta */
181         delta = current_update_time - last_update_time;
182         if (delta <= 0.0001)
183                 return;
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                 (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
193                                 sizeof(ifnet));
194                 ns = get_net_stat(ifnet.if_xname);
195                 ns->up = 1;
196                 last_recv = ns->recv;
197                 last_trans = ns->trans;
198
199                 if (ifnet.if_ibytes < ns->last_read_recv)
200                         ns->recv +=
201                             ((long long) 4294967295U -
202                              ns->last_read_recv) + ifnet.if_ibytes;
203                 else
204                         ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
205
206                 ns->last_read_recv = ifnet.if_ibytes;
207
208                 if (ifnet.if_obytes < ns->last_read_trans)
209                         ns->trans +=
210                             ((long long) 4294967295U -
211                              ns->last_read_trans) + ifnet.if_obytes;
212                 else
213                         ns->trans +=
214                             (ifnet.if_obytes - ns->last_read_trans);
215
216                 ns->last_read_trans = ifnet.if_obytes;
217
218                 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
219                 ns->last_read_recv = ifnet.if_ibytes;
220                 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
221                 ns->last_read_trans = ifnet.if_obytes;
222
223                 ns->recv_speed = (ns->recv - last_recv) / delta;
224                 ns->trans_speed = (ns->trans - last_trans) / delta;
225         }
226 }
227
228 void update_total_processes()
229 {
230         /* It's easier to use kvm here than sysctl */
231
232         int n_processes;
233
234         info.procs = 0;
235
236         if (init_kvm() < 0)
237                 return;
238         else
239                 kvm_getproc2(kd, KERN_PROC_ALL, 0,
240                              sizeof(struct kinfo_proc2), &n_processes);
241
242         info.procs = n_processes;
243 }
244
245 void update_running_processes()
246 {
247         struct kinfo_proc2 *p;
248         int n_processes;
249         int i, cnt = 0;
250
251         info.run_procs = 0;
252
253         if (init_kvm() < 0)
254                 return;
255         else {
256                 p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
257                                  sizeof(struct kinfo_proc2), &n_processes);
258                 for (i = 0; i < n_processes; i++)
259                         if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
260                             p[i].p_stat == LSONPROC)
261                                 cnt++;
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                 (void) 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 =
297             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
298
299         if ((total - oldtotal) != 0)
300                 info.cpu_usage =
301                     ((double) (used - oldused)) / (double) (total -
302                                                             oldtotal);
303         else
304                 info.cpu_usage = 0;
305
306         oldused = used;
307         oldtotal = total;
308
309 }
310
311 double get_i2c_info(int *fd, int div, char *devtype)
312 {
313         return -1;
314 }
315
316 void update_load_average()
317 {
318         double v[3];
319         getloadavg(v, 3);
320
321         info.loadavg[0] = (float) v[0];
322         info.loadavg[1] = (float) v[1];
323         info.loadavg[2] = (float) v[2];
324 }
325
326 double get_acpi_temperature(int fd)
327 {
328         return -1;
329 }
330
331 void get_battery_stuff(char *buf, unsigned int n, const char *bat)
332 {
333 }
334
335 int
336 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
337                 char *devtype)
338 {
339         return -1;
340 }
341
342 int open_acpi_temperature(const char *name)
343 {
344         return -1;
345 }
346
347 void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
348 {
349         if ( !p_client_buffer || client_buffer_size <= 0 )
350                 return;
351
352         /* not implemented */
353         memset(p_client_buffer,0,client_buffer_size);
354
355         return;
356 }
357
358 /*char *get_acpi_fan()*/
359 void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
360 {
361         if ( !p_client_buffer || client_buffer_size <= 0 )
362                 return;
363
364         /* not implemented */
365         memset(p_client_buffer,0,client_buffer_size);
366
367         return;
368 }
369
370 void update_entropy (void)
371 {
372 }