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

Go to the source code of this file.

Functions

int uv__platform_loop_init (uv_loop_t *loop)
 
void uv__platform_loop_delete (uv_loop_t *loop)
 
void uv_loadavg (double avg[3])
 
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)
 
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__random_sysctl (void *buf, size_t len)
 

Function Documentation

◆ uv__platform_loop_delete()

void uv__platform_loop_delete ( uv_loop_t loop)

Definition at line 49 of file netbsd.c.

49  {
50 }

◆ uv__platform_loop_init()

int uv__platform_loop_init ( uv_loop_t loop)

Definition at line 44 of file netbsd.c.

44  {
45  return uv__kqueue_init(loop);
46 }
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__random_sysctl()

int uv__random_sysctl ( void *  buf,
size_t  len 
)

Definition at line 238 of file netbsd.c.

238  {
239  static int name[] = {CTL_KERN, KERN_ARND};
240  size_t count, req;
241  unsigned char* p;
242 
243  p = buf;
244  while (len) {
245  req = len < 32 ? len : 32;
246  count = req;
247 
248  if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1)
249  return UV__ERR(errno);
250 
251  if (count != req)
252  return UV_EIO; /* Can't happen. */
253 
254  p += count;
255  len -= count;
256  }
257 
258  return 0;
259 }
size_t len
Definition: 6502dis.c:15
#define ARRAY_SIZE(a)
#define NULL
Definition: cris-opc.c:27
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 static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
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
#define UV__ERR(x)
Definition: errno.h:29
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
Definition: z80asm.h:102

◆ uv_cpu_info()

int uv_cpu_info ( uv_cpu_info_t **  cpu_infos,
int count 
)

Definition at line 180 of file netbsd.c.

180  {
181  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK);
182  unsigned int multiplier = ((uint64_t)1000L / ticks);
183  unsigned int cur = 0;
184  uv_cpu_info_t* cpu_info;
185  u_int64_t* cp_times;
186  char model[512];
187  u_int64_t cpuspeed;
188  int numcpus;
189  size_t size;
190  int i;
191 
192  size = sizeof(model);
193  if (sysctlbyname("machdep.cpu_brand", &model, &size, NULL, 0) &&
194  sysctlbyname("hw.model", &model, &size, NULL, 0)) {
195  return UV__ERR(errno);
196  }
197 
198  size = sizeof(numcpus);
199  if (sysctlbyname("hw.ncpu", &numcpus, &size, NULL, 0))
200  return UV__ERR(errno);
201  *count = numcpus;
202 
203  /* Only i386 and amd64 have machdep.tsc_freq */
204  size = sizeof(cpuspeed);
205  if (sysctlbyname("machdep.tsc_freq", &cpuspeed, &size, NULL, 0))
206  cpuspeed = 0;
207 
208  size = numcpus * CPUSTATES * sizeof(*cp_times);
209  cp_times = uv__malloc(size);
210  if (cp_times == NULL)
211  return UV_ENOMEM;
212 
213  if (sysctlbyname("kern.cp_time", cp_times, &size, NULL, 0))
214  return UV__ERR(errno);
215 
216  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
217  if (!(*cpu_infos)) {
218  uv__free(cp_times);
219  uv__free(*cpu_infos);
220  return UV_ENOMEM;
221  }
222 
223  for (i = 0; i < numcpus; i++) {
224  cpu_info = &(*cpu_infos)[i];
225  cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
226  cpu_info->cpu_times.nice = (uint64_t)(cp_times[CP_NICE+cur]) * multiplier;
227  cpu_info->cpu_times.sys = (uint64_t)(cp_times[CP_SYS+cur]) * multiplier;
228  cpu_info->cpu_times.idle = (uint64_t)(cp_times[CP_IDLE+cur]) * multiplier;
229  cpu_info->cpu_times.irq = (uint64_t)(cp_times[CP_INTR+cur]) * multiplier;
230  cpu_info->model = uv__strdup(model);
231  cpu_info->speed = (int)(cpuspeed/(uint64_t) 1e6);
232  cur += CPUSTATES;
233  }
234  uv__free(cp_times);
235  return 0;
236 }
lzma_index ** i
Definition: index.h:629
#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
#define u_int64_t
Definition: sha2.h:77
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, uv_cpu_info_s::model, uv_cpu_times_s::nice, NULL, uv_cpu_info_s::speed, uv_cpu_times_s::sys, u_int64_t, 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 66 of file netbsd.c.

66  {
67  /* Intermediate buffer, retrieving partial path name does not work
68  * As of NetBSD-8(beta), vnode->path translator does not handle files
69  * with longer names than 31 characters.
70  */
71  char int_buf[PATH_MAX];
72  size_t int_size;
73  int mib[4];
74 
75  if (buffer == NULL || size == NULL || *size == 0)
76  return UV_EINVAL;
77 
78  mib[0] = CTL_KERN;
79  mib[1] = KERN_PROC_ARGS;
80  mib[2] = -1;
81  mib[3] = KERN_PROC_PATHNAME;
82  int_size = ARRAY_SIZE(int_buf);
83 
84  if (sysctl(mib, 4, int_buf, &int_size, NULL, 0))
85  return UV__ERR(errno);
86 
87  /* Copy string from the intermediate buffer to outer one with appropriate
88  * length.
89  */
90  /* TODO(bnoordhuis) Check uv__strscpy() return value. */
91  uv__strscpy(buffer, int_buf, *size);
92 
93  /* Set new size. */
94  *size = strlen(buffer);
95 
96  return 0;
97 }
ssize_t uv__strscpy(char *d, const char *s, size_t n)
Definition: strscpy.c:25
Definition: buffer.h:15

References ARRAY_SIZE, NULL, UV__ERR, and uv__strscpy().

◆ uv_get_constrained_memory()

uint64_t uv_get_constrained_memory ( void  )

Definition at line 129 of file netbsd.c.

129  {
130  return 0; /* Memory constraints are unknown. */
131 }

◆ uv_get_free_memory()

uint64_t uv_get_free_memory ( void  )

Definition at line 100 of file netbsd.c.

100  {
101  struct uvmexp info;
102  size_t size = sizeof(info);
103  int which[] = {CTL_VM, VM_UVMEXP};
104 
105  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
106  return UV__ERR(errno);
107 
108  return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
109 }
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_get_total_memory()

uint64_t uv_get_total_memory ( void  )

Definition at line 112 of file netbsd.c.

112  {
113 #if defined(HW_PHYSMEM64)
114  uint64_t info;
115  int which[] = {CTL_HW, HW_PHYSMEM64};
116 #else
117  unsigned int info;
118  int which[] = {CTL_HW, HW_PHYSMEM};
119 #endif
120  size_t size = sizeof(info);
121 
122  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
123  return UV__ERR(errno);
124 
125  return (uint64_t) info;
126 }

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

◆ uv_loadavg()

void uv_loadavg ( double  avg[3])

Definition at line 53 of file netbsd.c.

53  {
54  struct loadavg info;
55  size_t size = sizeof(info);
56  int which[] = {CTL_VM, VM_LOADAVG};
57 
58  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) == -1) return;
59 
60  avg[0] = (double) info.ldavg[0] / info.fscale;
61  avg[1] = (double) info.ldavg[1] / info.fscale;
62  avg[2] = (double) info.ldavg[2] / info.fscale;
63 }

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 134 of file netbsd.c.

134  {
135  kvm_t *kd = NULL;
136  struct kinfo_proc2 *kinfo = NULL;
137  pid_t pid;
138  int nprocs;
139  int max_size = sizeof(struct kinfo_proc2);
140  int page_size;
141 
142  page_size = getpagesize();
143  pid = getpid();
144 
145  kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
146 
147  if (kd == NULL) goto error;
148 
149  kinfo = kvm_getproc2(kd, KERN_PROC_PID, pid, max_size, &nprocs);
150  if (kinfo == NULL) goto error;
151 
152  *rss = kinfo->p_vm_rssize * page_size;
153 
154  kvm_close(kd);
155 
156  return 0;
157 
158 error:
159  if (kd) kvm_close(kd);
160  return UV_EPERM;
161 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
int pid_t
Definition: sftypes.h:38
void error(const char *msg)
Definition: untgz.c:593

References error(), NULL, and pid.

◆ uv_uptime()

int uv_uptime ( double *  uptime)

Definition at line 164 of file netbsd.c.

164  {
165  time_t now;
166  struct timeval info;
167  size_t size = sizeof(info);
168  static int which[] = {CTL_KERN, KERN_BOOTTIME};
169 
170  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
171  return UV__ERR(errno);
172 
173  now = time(NULL);
174 
175  *uptime = (double)(now - info.tv_sec);
176  return 0;
177 }
static static fork const void static count static fd const char const char static newpath char char char static envp time
Definition: sflib.h:42
int time_t
Definition: sftypes.h:66

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