Rizin
unix-like reverse engineering framework and cli tools
freebsd.c File Reference
#include "uv.h"
#include "internal.h"
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <paths.h>
#include <sys/user.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <vm/vm_param.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

Go to the source code of this file.

Macros

#define CPUSTATES   5U
 
#define CP_USER   0
 
#define CP_NICE   1
 
#define CP_SYS   2
 
#define CP_IDLE   3
 
#define CP_INTR   4
 

Functions

int uv__platform_loop_init (uv_loop_t *loop)
 
void uv__platform_loop_delete (uv_loop_t *loop)
 
int uv_exepath (char *buffer, size_t *size)
 
uint64_t uv_get_free_memory (void)
 
uint64_t uv_get_total_memory (void)
 
uint64_t uv_get_constrained_memory (void)
 
void uv_loadavg (double avg[3])
 
int uv_resident_set_memory (size_t *rss)
 
int uv_uptime (double *uptime)
 
int uv_cpu_info (uv_cpu_info_t **cpu_infos, int *count)
 
int uv__sendmmsg (int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
 
int uv__recvmmsg (int fd, struct uv__mmsghdr *mmsg, unsigned int vlen)
 

Macro Definition Documentation

◆ CP_IDLE

#define CP_IDLE   3

Definition at line 46 of file freebsd.c.

◆ CP_INTR

#define CP_INTR   4

Definition at line 47 of file freebsd.c.

◆ CP_NICE

#define CP_NICE   1

Definition at line 44 of file freebsd.c.

◆ CP_SYS

#define CP_SYS   2

Definition at line 45 of file freebsd.c.

◆ CP_USER

#define CP_USER   0

Definition at line 43 of file freebsd.c.

◆ CPUSTATES

#define CPUSTATES   5U

Definition at line 40 of file freebsd.c.

Function Documentation

◆ uv__platform_loop_delete()

void uv__platform_loop_delete ( uv_loop_t loop)

Definition at line 56 of file freebsd.c.

56  {
57 }

◆ uv__platform_loop_init()

int uv__platform_loop_init ( uv_loop_t loop)

Definition at line 51 of file freebsd.c.

51  {
52  return uv__kqueue_init(loop);
53 }
uv_loop_t * loop
Definition: main.c:7
int uv__kqueue_init(uv_loop_t *loop)
Definition: kqueue.c:51

References loop, and uv__kqueue_init().

◆ uv__recvmmsg()

int uv__recvmmsg ( int  fd,
struct uv__mmsghdr *  mmsg,
unsigned int  vlen 
)

Definition at line 276 of file freebsd.c.

276  {
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 NULL
Definition: cris-opc.c:27
static const z80_opcode fd[]
Definition: z80_tab.h:997

References fd, and NULL.

◆ uv__sendmmsg()

int uv__sendmmsg ( int  fd,
struct uv__mmsghdr *  mmsg,
unsigned int  vlen 
)

Definition at line 267 of file freebsd.c.

267  {
268 #if __FreeBSD__ >= 11
269  return sendmmsg(fd, mmsg, vlen, /* flags */ 0);
270 #else
271  return errno = ENOSYS, -1;
272 #endif
273 }

References fd.

◆ uv_cpu_info()

int uv_cpu_info ( uv_cpu_info_t **  cpu_infos,
int count 
)

Definition at line 172 of file freebsd.c.

172  {
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 }
lzma_index ** i
Definition: index.h:629
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
#define CP_INTR
Definition: freebsd.c:47
#define CP_NICE
Definition: freebsd.c:44
#define CPUSTATES
Definition: freebsd.c:40
#define CP_IDLE
Definition: freebsd.c:46
#define CP_USER
Definition: freebsd.c:43
voidpf void uLong size
Definition: ioapi.h:138
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28
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
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

References count, CP_IDLE, CP_INTR, CP_NICE, CP_SYS, CP_USER, uv_cpu_info_s::cpu_times, CPUSTATES, i, uv_cpu_times_s::idle, int, uv_cpu_times_s::irq, long, uv_cpu_info_s::model, uv_cpu_times_s::nice, NULL, uv_cpu_info_s::speed, uv_cpu_times_s::sys, uv_cpu_times_s::user, UV__ERR, uv__free(), uv__malloc(), and uv__strdup().

◆ uv_exepath()

int uv_exepath ( char *  buffer,
size_t size 
)

Definition at line 59 of file freebsd.c.

59  {
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 }
#define ARRAY_SIZE(a)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
assert(limit<=UINT32_MAX/2)
Definition: buffer.h:15

References ARRAY_SIZE, assert(), memcpy(), NULL, and UV__ERR.

◆ uv_get_constrained_memory()

uint64_t uv_get_constrained_memory ( void  )

Definition at line 114 of file freebsd.c.

114  {
115  return 0; /* Memory constraints are unknown. */
116 }

◆ uv_get_free_memory()

uint64_t uv_get_free_memory ( void  )

Definition at line 89 of file freebsd.c.

89  {
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 }

References NULL, and UV__ERR.

◆ uv_get_total_memory()

uint64_t uv_get_total_memory ( void  )

Definition at line 101 of file freebsd.c.

101  {
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 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
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

References ARRAY_SIZE, info(), NULL, UV__ERR, and which.

◆ uv_loadavg()

void uv_loadavg ( double  avg[3])

Definition at line 119 of file freebsd.c.

119  {
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 }

References ARRAY_SIZE, info(), test-lz4-speed::loadavg, NULL, and which.

◆ uv_resident_set_memory()

int uv_resident_set_memory ( size_t rss)

Definition at line 132 of file freebsd.c.

132  {
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 }

References ARRAY_SIZE, NULL, and UV__ERR.

◆ uv_uptime()

int uv_uptime ( double *  uptime)

Definition at line 160 of file freebsd.c.

160  {
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 }
#define r
Definition: crypto_rc6.c:12
static int sp
Definition: z80asm.c:91

References r, sp, and UV__ERR.