Rizin
unix-like reverse engineering framework and cli tools
freebsd.c
Go to the documentation of this file.
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  * Permission is hereby granted, free of charge, to any person obtaining a copy
3  * of this software and associated documentation files (the "Software"), to
4  * deal in the Software without restriction, including without limitation the
5  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6  * sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20 
21 #include "uv.h"
22 #include "internal.h"
23 
24 #include <assert.h>
25 #include <string.h>
26 #include <errno.h>
27 
28 #include <paths.h>
29 #include <sys/user.h>
30 #include <sys/types.h>
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <vm/vm_param.h> /* VM_LOADAVG */
34 #include <time.h>
35 #include <stdlib.h>
36 #include <unistd.h> /* sysconf */
37 #include <fcntl.h>
38 
39 #ifndef CPUSTATES
40 # define CPUSTATES 5U
41 #endif
42 #ifndef CP_USER
43 # define CP_USER 0
44 # define CP_NICE 1
45 # define CP_SYS 2
46 # define CP_IDLE 3
47 # define CP_INTR 4
48 #endif
49 
50 
52  return uv__kqueue_init(loop);
53 }
54 
55 
57 }
58 
59 int uv_exepath(char* buffer, size_t* size) {
60  char abspath[PATH_MAX * 2 + 1];
61  int mib[4];
62  size_t abspath_size;
63 
64  if (buffer == NULL || size == NULL || *size == 0)
65  return UV_EINVAL;
66 
67  mib[0] = CTL_KERN;
68  mib[1] = KERN_PROC;
69  mib[2] = KERN_PROC_PATHNAME;
70  mib[3] = -1;
71 
72  abspath_size = sizeof abspath;
73  if (sysctl(mib, ARRAY_SIZE(mib), abspath, &abspath_size, NULL, 0))
74  return UV__ERR(errno);
75 
76  assert(abspath_size > 0);
77  abspath_size -= 1;
78  *size -= 1;
79 
80  if (*size > abspath_size)
81  *size = abspath_size;
82 
83  memcpy(buffer, abspath, *size);
84  buffer[*size] = '\0';
85 
86  return 0;
87 }
88 
90  int freecount;
91  size_t size = sizeof(freecount);
92 
93  if (sysctlbyname("vm.stats.vm.v_free_count", &freecount, &size, NULL, 0))
94  return UV__ERR(errno);
95 
96  return (uint64_t) freecount * sysconf(_SC_PAGESIZE);
97 
98 }
99 
100 
102  unsigned long info;
103  int which[] = {CTL_HW, HW_PHYSMEM};
104 
105  size_t size = sizeof(info);
106 
107  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
108  return UV__ERR(errno);
109 
110  return (uint64_t) info;
111 }
112 
113 
115  return 0; /* Memory constraints are unknown. */
116 }
117 
118 
119 void uv_loadavg(double avg[3]) {
120  struct loadavg info;
121  size_t size = sizeof(info);
122  int which[] = {CTL_VM, VM_LOADAVG};
123 
124  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
125 
126  avg[0] = (double) info.ldavg[0] / info.fscale;
127  avg[1] = (double) info.ldavg[1] / info.fscale;
128  avg[2] = (double) info.ldavg[2] / info.fscale;
129 }
130 
131 
132 int uv_resident_set_memory(size_t* rss) {
133  struct kinfo_proc kinfo;
134  size_t page_size;
135  size_t kinfo_size;
136  int mib[4];
137 
138  mib[0] = CTL_KERN;
139  mib[1] = KERN_PROC;
140  mib[2] = KERN_PROC_PID;
141  mib[3] = getpid();
142 
143  kinfo_size = sizeof(kinfo);
144 
145  if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &kinfo_size, NULL, 0))
146  return UV__ERR(errno);
147 
148  page_size = getpagesize();
149 
150 #ifdef __DragonFly__
151  *rss = kinfo.kp_vm_rssize * page_size;
152 #else
153  *rss = kinfo.ki_rssize * page_size;
154 #endif
155 
156  return 0;
157 }
158 
159 
160 int uv_uptime(double* uptime) {
161  int r;
162  struct timespec sp;
163  r = clock_gettime(CLOCK_MONOTONIC, &sp);
164  if (r)
165  return UV__ERR(errno);
166 
167  *uptime = sp.tv_sec;
168  return 0;
169 }
170 
171 
172 int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
173  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
174  multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
175  cur = 0;
176  uv_cpu_info_t* cpu_info;
177  const char* maxcpus_key;
178  const char* cptimes_key;
179  const char* model_key;
180  char model[512];
181  long* cp_times;
182  int numcpus;
183  size_t size;
184  int i;
185 
186 #if defined(__DragonFly__)
187  /* This is not quite correct but DragonFlyBSD doesn't seem to have anything
188  * comparable to kern.smp.maxcpus or kern.cp_times (kern.cp_time is a total,
189  * not per CPU). At least this stops uv_cpu_info() from failing completely.
190  */
191  maxcpus_key = "hw.ncpu";
192  cptimes_key = "kern.cp_time";
193 #else
194  maxcpus_key = "kern.smp.maxcpus";
195  cptimes_key = "kern.cp_times";
196 #endif
197 
198 #if defined(__arm__) || defined(__aarch64__)
199  /* The key hw.model and hw.clockrate are not available on FreeBSD ARM. */
200  model_key = "hw.machine";
201  cpuspeed = 0;
202 #else
203  model_key = "hw.model";
204 
205  size = sizeof(cpuspeed);
206  if (sysctlbyname("hw.clockrate", &cpuspeed, &size, NULL, 0))
207  return -errno;
208 #endif
209 
210  size = sizeof(model);
211  if (sysctlbyname(model_key, &model, &size, NULL, 0))
212  return UV__ERR(errno);
213 
214  size = sizeof(numcpus);
215  if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
216  return UV__ERR(errno);
217 
218  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
219  if (!(*cpu_infos))
220  return UV_ENOMEM;
221 
222  *count = numcpus;
223 
224  /* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of
225  * ncpu.
226  */
227  size = sizeof(maxcpus);
228  if (sysctlbyname(maxcpus_key, &maxcpus, &size, NULL, 0)) {
229  uv__free(*cpu_infos);
230  return UV__ERR(errno);
231  }
232 
233  size = maxcpus * CPUSTATES * sizeof(long);
234 
235  cp_times = uv__malloc(size);
236  if (cp_times == NULL) {
237  uv__free(*cpu_infos);
238  return UV_ENOMEM;
239  }
240 
241  if (sysctlbyname(cptimes_key, cp_times, &size, NULL, 0)) {
242  uv__free(cp_times);
243  uv__free(*cpu_infos);
244  return UV__ERR(errno);
245  }
246 
247  for (i = 0; i < numcpus; i++) {
248  cpu_info = &(*cpu_infos)[i];
249 
250  cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
251  cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
252  cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
253  cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
254  cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
255 
256  cpu_info->model = uv__strdup(model);
257  cpu_info->speed = cpuspeed;
258 
259  cur+=CPUSTATES;
260  }
261 
262  uv__free(cp_times);
263  return 0;
264 }
265 
266 
267 int uv__sendmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {
268 #if __FreeBSD__ >= 11
269  return sendmmsg(fd, mmsg, vlen, /* flags */ 0);
270 #else
271  return errno = ENOSYS, -1;
272 #endif
273 }
274 
275 
276 int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {
277 #if __FreeBSD__ >= 11
278  return recvmmsg(fd, mmsg, vlen, 0 /* flags */, NULL /* timeout */);
279 #else
280  return errno = ENOSYS, -1;
281 #endif
282 }
#define ARRAY_SIZE(a)
lzma_index ** i
Definition: index.h:629
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol which
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags long
Definition: sflib.h:79
#define UV__ERR(x)
Definition: errno.h:29
#define CP_SYS
Definition: freebsd.c:45
uint64_t uv_get_total_memory(void)
Definition: freebsd.c:101
void uv_loadavg(double avg[3])
Definition: freebsd.c:119
uint64_t uv_get_free_memory(void)
Definition: freebsd.c:89
int uv__sendmmsg(int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
Definition: freebsd.c:267
#define CP_INTR
Definition: freebsd.c:47
int uv__recvmmsg(int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
Definition: freebsd.c:276
int uv__platform_loop_init(uv_loop_t *loop)
Definition: freebsd.c:51
int uv_uptime(double *uptime)
Definition: freebsd.c:160
#define CP_NICE
Definition: freebsd.c:44
#define CPUSTATES
Definition: freebsd.c:40
int uv_cpu_info(uv_cpu_info_t **cpu_infos, int *count)
Definition: freebsd.c:172
uint64_t uv_get_constrained_memory(void)
Definition: freebsd.c:114
void uv__platform_loop_delete(uv_loop_t *loop)
Definition: freebsd.c:56
#define CP_IDLE
Definition: freebsd.c:46
#define CP_USER
Definition: freebsd.c:43
int uv_exepath(char *buffer, size_t *size)
Definition: freebsd.c:59
int uv_resident_set_memory(size_t *rss)
Definition: freebsd.c:132
voidpf void uLong size
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
assert(limit<=UINT32_MAX/2)
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28
Definition: buffer.h:15
struct uv_cpu_times_s cpu_times
Definition: uv.h:1093
char * model
Definition: uv.h:1091
int speed
Definition: uv.h:1092
uint64_t nice
Definition: uv.h:1084
uint64_t sys
Definition: uv.h:1085
uint64_t idle
Definition: uv.h:1086
uint64_t user
Definition: uv.h:1083
uint64_t irq
Definition: uv.h:1087
Definition: uv.h:1780
uv_loop_t * loop
Definition: main.c:7
int uv__kqueue_init(uv_loop_t *loop)
Definition: kqueue.c:51
char * uv__strdup(const char *s)
Definition: uv-common.c:55
void * uv__malloc(size_t size)
Definition: uv-common.c:75
void uv__free(void *ptr)
Definition: uv-common.c:81
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int sp
Definition: z80asm.c:91