aboutsummaryrefslogtreecommitdiffstats
path: root/sysutils/ascpu/files/patch-ascpu_x.c
blob: 11365eb02340bd6873572dd68913247285218cb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
--- ascpu_x.c.orig  2009-12-23 12:38:21.000000000 +0300
+++ ascpu_x.c   2009-12-23 12:38:35.000000000 +0300
@@ -18,6 +18,12 @@
 #include <nlist.h>
 #include <fcntl.h>
 #include <kvm.h>
+#include <sys/types.h>
+#include <err.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
 #endif
 
 #ifdef __hpux__
@@ -81,6 +87,8 @@ time_t last_time = 0;
 int update_request = 0;
 
 #ifdef __FreeBSD__
+static int     cp_time_mib[2];
+static int     cp_times_mib[2];
 static kvm_t       *kd;
 static struct nlist    nlst[] = {
    {"_cp_time"}, {0}
@@ -554,12 +562,45 @@ void read_stat()
    memcpy(&last, &fresh, sizeof(last));
 
 #ifdef __FreeBSD__
-   if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
-       printf("ascpu : cannot read kvm\n");
-       exit(1);
+   if (state.cpu_number >= 0 && cp_times_mib[0] != 0) {
+       long cp_times[MAX_CPU][CPUSTATES];
+       size_t cp_times_len = sizeof(cp_times);
+       int error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
+       if (error) {
+           printf("ascpu: cannot sysctl cp_times\n");
+           exit(1);
+       }
+
+       long *cp_time = cp_times[state.cpu_number];
+       fresh.load[0] = cp_time[CP_USER];
+       fresh.load[1] = cp_time[CP_NICE];
+       fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+       fresh.load[3] = cp_time[CP_IDLE];
+   } else if (state.cpu_number == -1 && cp_time_mib[0] != 0) {
+       long cp_time[CPUSTATES];
+       size_t cp_time_len = sizeof(cp_time);
+       int error = sysctl(cp_time_mib, 2, cp_time, &cp_time_len, NULL, 0);
+       if (error) {
+           printf("ascpu: cannot sysctl cp_time\n");
+           exit(1);
+       }
+
+       fresh.load[0] = cp_time[CP_USER];
+       fresh.load[1] = cp_time[CP_NICE];
+       fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+       fresh.load[3] = cp_time[CP_IDLE];
+   } else {
+       if (nlst[0].n_type == 0) {
+           printf("ascpu : cannot get nlist\n");
+           exit(1);
+       }
+       if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
+           printf("ascpu : cannot read kvm\n");
+           exit(1);
+       }
+       /* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
+       fresh.load[3] = fresh.load[4];
    }
-   /* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
-   fresh.load[3] = fresh.load[4];
 #endif
 
 #ifdef __hpux__
@@ -677,6 +718,12 @@ void ExecuteExternal()
 #ifdef DEBUG
    printf("ascpu: system(%s)\n",Command);
 #endif
+
+   if (setgid(getgid()) != 0)
+       err(1, "Can't drop setgid privileges");
+   if (setuid(getuid()) != 0)
+       err(1, "Can't drop setuid privileges");
+
    if( ! Command ) {
        return;
    }
@@ -1087,14 +1134,12 @@ void ascpu_initialize(
        ascpu_cleanup();
    }
 #ifdef __FreeBSD__
-   if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
-       printf("ascpu : cannot open kvm\n");
-       exit(1);
-   }
-   kvm_nlist(kd, nlst);
-   if (nlst[0].n_type == 0) {
-       printf("ascpu : cannot get nlist\n");
-       exit(1);
+   size_t len = 2;
+   sysctlnametomib("kern.cp_times", cp_times_mib, &len);
+   len = 2;
+   sysctlnametomib("kern.cp_time", cp_time_mib, &len);
+   if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) != NULL) {
+       kvm_nlist(kd, nlst);
    }
 #endif