Rizin
unix-like reverse engineering framework and cli tools
openbsd.c File Reference
#include "uv.h"
#include "internal.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/sched.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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)
 

Function Documentation

◆ uv__platform_loop_delete()

void uv__platform_loop_delete ( uv_loop_t loop)

Definition at line 44 of file openbsd.c.

44  {
45 }

◆ uv__platform_loop_init()

int uv__platform_loop_init ( uv_loop_t loop)

Definition at line 39 of file openbsd.c.

39  {
40  return uv__kqueue_init(loop);
41 }
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_cpu_info()

int uv_cpu_info ( uv_cpu_info_t **  cpu_infos,
int count 
)

Definition at line 179 of file openbsd.c.

179  {
180  unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
181  multiplier = ((uint64_t)1000L / ticks), cpuspeed;
183  char model[512];
184  int numcpus = 1;
185  int which[] = {CTL_HW,HW_MODEL};
186  int percpu[] = {CTL_KERN,KERN_CPTIME2,0};
187  size_t size;
188  int i, j;
189  uv_cpu_info_t* cpu_info;
190 
191  size = sizeof(model);
192  if (sysctl(which, ARRAY_SIZE(which), &model, &size, NULL, 0))
193  return UV__ERR(errno);
194 
195  which[1] = HW_NCPUONLINE;
196  size = sizeof(numcpus);
197  if (sysctl(which, ARRAY_SIZE(which), &numcpus, &size, NULL, 0))
198  return UV__ERR(errno);
199 
200  *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos));
201  if (!(*cpu_infos))
202  return UV_ENOMEM;
203 
204  i = 0;
205  *count = numcpus;
206 
207  which[1] = HW_CPUSPEED;
208  size = sizeof(cpuspeed);
209  if (sysctl(which, ARRAY_SIZE(which), &cpuspeed, &size, NULL, 0))
210  goto error;
211 
212  size = sizeof(info);
213  for (i = 0; i < numcpus; i++) {
214  percpu[2] = i;
215  if (sysctl(percpu, ARRAY_SIZE(percpu), &info, &size, NULL, 0))
216  goto error;
217 
218  cpu_info = &(*cpu_infos)[i];
219 
220  cpu_info->cpu_times.user = (uint64_t)(info[CP_USER]) * multiplier;
221  cpu_info->cpu_times.nice = (uint64_t)(info[CP_NICE]) * multiplier;
222  cpu_info->cpu_times.sys = (uint64_t)(info[CP_SYS]) * multiplier;
223  cpu_info->cpu_times.idle = (uint64_t)(info[CP_IDLE]) * multiplier;
224  cpu_info->cpu_times.irq = (uint64_t)(info[CP_INTR]) * multiplier;
225 
226  cpu_info->model = uv__strdup(model);
227  cpu_info->speed = cpuspeed;
228  }
229 
230  return 0;
231 
232 error:
233  *count = 0;
234  for (j = 0; j < i; j++)
235  uv__free((*cpu_infos)[j].model);
236 
237  uv__free(*cpu_infos);
238  *cpu_infos = NULL;
239  return UV__ERR(errno);
240 }
#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
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
#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
void error(const char *msg)
Definition: untgz.c:593
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 ARRAY_SIZE, count, CP_IDLE, CP_INTR, CP_NICE, CP_SYS, CP_USER, uv_cpu_info_s::cpu_times, CPUSTATES, error(), i, uv_cpu_times_s::idle, info(), 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, uv_cpu_times_s::user, UV__ERR, uv__free(), uv__malloc(), uv__strdup(), and which.

◆ uv_exepath()

int uv_exepath ( char *  buffer,
size_t size 
)

Definition at line 61 of file openbsd.c.

61  {
62  int mib[4];
63  char **argsbuf = NULL;
64  size_t argsbuf_size = 100U;
65  size_t exepath_size;
66  pid_t mypid;
67  int err;
68 
69  if (buffer == NULL || size == NULL || *size == 0)
70  return UV_EINVAL;
71 
72  mypid = getpid();
73  for (;;) {
74  err = UV_ENOMEM;
75  argsbuf = uv__reallocf(argsbuf, argsbuf_size);
76  if (argsbuf == NULL)
77  goto out;
78  mib[0] = CTL_KERN;
79  mib[1] = KERN_PROC_ARGS;
80  mib[2] = mypid;
81  mib[3] = KERN_PROC_ARGV;
82  if (sysctl(mib, ARRAY_SIZE(mib), argsbuf, &argsbuf_size, NULL, 0) == 0) {
83  break;
84  }
85  if (errno != ENOMEM) {
86  err = UV__ERR(errno);
87  goto out;
88  }
89  argsbuf_size *= 2U;
90  }
91 
92  if (argsbuf[0] == NULL) {
93  err = UV_EINVAL; /* FIXME(bnoordhuis) More appropriate error. */
94  goto out;
95  }
96 
97  *size -= 1;
98  exepath_size = strlen(argsbuf[0]);
99  if (*size > exepath_size)
100  *size = exepath_size;
101 
102  memcpy(buffer, argsbuf[0], *size);
103  buffer[*size] = '\0';
104  err = 0;
105 
106 out:
107  uv__free(argsbuf);
108 
109  return err;
110 }
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define ENOMEM
Definition: sftypes.h:122
int pid_t
Definition: sftypes.h:38
Definition: buffer.h:15
void * uv__reallocf(void *ptr, size_t size)
Definition: uv-common.c:103

References ARRAY_SIZE, ENOMEM, err, memcpy(), NULL, out, UV__ERR, uv__free(), and uv__reallocf().

◆ uv_get_constrained_memory()

uint64_t uv_get_constrained_memory ( void  )

Definition at line 137 of file openbsd.c.

137  {
138  return 0; /* Memory constraints are unknown. */
139 }

◆ uv_get_free_memory()

uint64_t uv_get_free_memory ( void  )

Definition at line 113 of file openbsd.c.

113  {
114  struct uvmexp info;
115  size_t size = sizeof(info);
116  int which[] = {CTL_VM, VM_UVMEXP};
117 
118  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
119  return UV__ERR(errno);
120 
121  return (uint64_t) info.free * sysconf(_SC_PAGESIZE);
122 }

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

◆ uv_get_total_memory()

uint64_t uv_get_total_memory ( void  )

Definition at line 125 of file openbsd.c.

125  {
126  uint64_t info;
127  int which[] = {CTL_HW, HW_PHYSMEM64};
128  size_t size = sizeof(info);
129 
130  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
131  return UV__ERR(errno);
132 
133  return (uint64_t) info;
134 }

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

◆ uv_loadavg()

void uv_loadavg ( double  avg[3])

Definition at line 48 of file openbsd.c.

48  {
49  struct loadavg info;
50  size_t size = sizeof(info);
51  int which[] = {CTL_VM, VM_LOADAVG};
52 
53  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0) < 0) return;
54 
55  avg[0] = (double) info.ldavg[0] / info.fscale;
56  avg[1] = (double) info.ldavg[1] / info.fscale;
57  avg[2] = (double) info.ldavg[2] / info.fscale;
58 }

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 142 of file openbsd.c.

142  {
143  struct kinfo_proc kinfo;
144  size_t page_size = getpagesize();
145  size_t size = sizeof(struct kinfo_proc);
146  int mib[6];
147 
148  mib[0] = CTL_KERN;
149  mib[1] = KERN_PROC;
150  mib[2] = KERN_PROC_PID;
151  mib[3] = getpid();
152  mib[4] = sizeof(struct kinfo_proc);
153  mib[5] = 1;
154 
155  if (sysctl(mib, ARRAY_SIZE(mib), &kinfo, &size, NULL, 0) < 0)
156  return UV__ERR(errno);
157 
158  *rss = kinfo.p_vm_rssize * page_size;
159  return 0;
160 }

References ARRAY_SIZE, NULL, and UV__ERR.

◆ uv_uptime()

int uv_uptime ( double *  uptime)

Definition at line 163 of file openbsd.c.

163  {
164  time_t now;
165  struct timeval info;
166  size_t size = sizeof(info);
167  static int which[] = {CTL_KERN, KERN_BOOTTIME};
168 
169  if (sysctl(which, ARRAY_SIZE(which), &info, &size, NULL, 0))
170  return UV__ERR(errno);
171 
172  now = time(NULL);
173 
174  *uptime = (double)(now - info.tv_sec);
175  return 0;
176 }
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.