Rizin
unix-like reverse engineering framework and cli tools
main.c File Reference
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>

Go to the source code of this file.

Classes

struct  child_worker
 

Functions

void close_process_handle (uv_process_t *req, int64_t exit_status, int term_signal)
 
void alloc_buffer (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
 
void on_new_connection (uv_stream_t *server, int status)
 
void setup_workers ()
 
int main ()
 

Variables

uv_loop_tloop
 
struct child_workerworkers
 
int round_robin_counter
 
int child_worker_count
 
uv_buf_t dummy_buf
 
char worker_path [500]
 

Function Documentation

◆ alloc_buffer()

void alloc_buffer ( uv_handle_t handle,
size_t  suggested_size,
uv_buf_t buf 
)

Definition at line 26 of file main.c.

26  {
27  buf->base = malloc(suggested_size);
28  buf->len = suggested_size;
29 }
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123

References malloc().

◆ close_process_handle()

void close_process_handle ( uv_process_t req,
int64_t  exit_status,
int  term_signal 
)

Definition at line 21 of file main.c.

21  {
22  fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
24 }
#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
#define PRId64
Definition: macros.h:17
static enum exit_status_type exit_status
Exit status to use. This can be changed with set_exit_status().
Definition: main.c:17
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108

References exit_status, NULL, PRId64, req, and uv_close().

Referenced by setup_workers().

◆ main()

int main ( void  )

Definition at line 97 of file main.c.

97  {
99 
100  setup_workers();
101 
102  uv_tcp_t server;
103  uv_tcp_init(loop, &server);
104 
105  struct sockaddr_in bind_addr;
106  uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
107  uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
108  int r;
109  if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
110  fprintf(stderr, "Listen error %s\n", uv_err_name(r));
111  return 2;
112  }
113  return uv_run(loop, UV_RUN_DEFAULT);
114 }
#define r
Definition: crypto_rc6.c:12
Definition: uv.h:547
void on_new_connection(uv_stream_t *server, int status)
Definition: main.c:50
uv_loop_t * loop
Definition: main.c:7
void setup_workers()
Definition: main.c:51
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
@ UV_RUN_DEFAULT
Definition: uv.h:255
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: stream.c:656
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN const char * uv_err_name(int err)
Definition: uv-common.c:189
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: tcp.c:143
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763

References loop, on_new_connection(), r, setup_workers(), uv_default_loop(), uv_err_name(), uv_ip4_addr(), uv_listen(), uv_run(), UV_RUN_DEFAULT, uv_tcp_bind(), and uv_tcp_init().

◆ on_new_connection()

void on_new_connection ( uv_stream_t server,
int  status 
)

Definition at line 31 of file main.c.

31  {
32  if (status == -1) {
33  // error!
34  return;
35  }
36 
37  uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
38  uv_tcp_init(loop, client);
39  if (uv_accept(server, (uv_stream_t*) client) == 0) {
41  dummy_buf = uv_buf_init("a", 1);
43  uv_write2(write_req, (uv_stream_t*) &worker->pipe, &dummy_buf, 1, (uv_stream_t*) client, NULL);
45  }
46  else {
47  uv_close((uv_handle_t*) client, NULL);
48  }
49 }
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
Definition: uv.h:525
struct child_worker * workers
int child_worker_count
Definition: main.c:16
int round_robin_counter
Definition: main.c:15
uv_buf_t dummy_buf
Definition: main.c:18
uv_write_t write_req
Definition: main.c:9
static void worker(void *arg)
Definition: threadpool.c:57
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
UV_EXTERN int uv_write2(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_stream_t *send_handle, uv_write_cb cb)
Definition: stream.c:1393
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: stream.c:591

References child_worker_count, dummy_buf, loop, malloc(), NULL, round_robin_counter, status, uv_accept(), uv_buf_init(), uv_close(), uv_tcp_init(), uv_write2(), worker(), workers, and write_req.

◆ setup_workers()

void setup_workers ( )

Definition at line 51 of file main.c.

51  {
52  size_t path_size = 500;
53  uv_exepath(worker_path, &path_size);
54  strcpy(worker_path + (strlen(worker_path) - strlen("multi-echo-server")), "worker");
55  fprintf(stderr, "Worker path: %s\n", worker_path);
56 
57  char* args[2];
58  args[0] = worker_path;
59  args[1] = NULL;
60 
62 
63  // ...
64 
65  // launch same number of workers as number of CPUs
67  int cpu_count;
68  uv_cpu_info(&info, &cpu_count);
69  uv_free_cpu_info(info, cpu_count);
70 
71  child_worker_count = cpu_count;
72 
73  workers = calloc(cpu_count, sizeof(struct child_worker));
74  while (cpu_count--) {
75  struct child_worker *worker = &workers[cpu_count];
76  uv_pipe_init(loop, &worker->pipe, 1);
77 
78  uv_stdio_container_t child_stdio[3];
79  child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
80  child_stdio[0].data.stream = (uv_stream_t*) &worker->pipe;
81  child_stdio[1].flags = UV_IGNORE;
82  child_stdio[2].flags = UV_INHERIT_FD;
83  child_stdio[2].data.fd = 2;
84 
85  worker->options.stdio = child_stdio;
86  worker->options.stdio_count = 3;
87 
88  worker->options.exit_cb = close_process_handle;
89  worker->options.file = args[0];
90  worker->options.args = args;
91 
92  uv_spawn(loop, &worker->req, &worker->options);
93  fprintf(stderr, "Started worker %d\n", worker->req.pid);
94  }
95 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
uv_stream_t * stream
Definition: uv.h:946
uv_stdio_flags flags
Definition: uv.h:943
union uv_stdio_container_s::@397 data
void close_process_handle(uv_process_t *req, int64_t exit_status, int term_signal)
Definition: main.c:21
char worker_path[500]
Definition: main.c:19
struct Arg * args
Definition: mipsasm.c:18
UV_EXTERN int uv_exepath(char *buffer, size_t *size)
Definition: aix-common.c:60
UV_EXTERN int uv_cpu_info(uv_cpu_info_t **cpu_infos, int *count)
Definition: aix.c:1044
@ UV_INHERIT_FD
Definition: uv.h:924
@ UV_CREATE_PIPE
Definition: uv.h:923
@ UV_IGNORE
Definition: uv.h:922
@ UV_READABLE_PIPE
Definition: uv.h:932
UV_EXTERN int uv_spawn(uv_loop_t *loop, uv_process_t *handle, const uv_process_options_t *options)
Definition: process.c:408
UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t *cpu_infos, int count)
Definition: uv-common.c:846
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: pipe.c:33

References args, calloc(), child_worker_count, close_process_handle(), uv_stdio_container_s::data, uv_stdio_container_s::fd, uv_stdio_container_s::flags, info(), loop, NULL, round_robin_counter, uv_stdio_container_s::stream, uv_cpu_info(), UV_CREATE_PIPE, uv_exepath(), uv_free_cpu_info(), UV_IGNORE, UV_INHERIT_FD, uv_pipe_init(), UV_READABLE_PIPE, uv_spawn(), worker(), worker_path, and workers.

Referenced by main().

Variable Documentation

◆ child_worker_count

int child_worker_count

Definition at line 16 of file main.c.

Referenced by on_new_connection(), and setup_workers().

◆ dummy_buf

uv_buf_t dummy_buf

Definition at line 18 of file main.c.

Referenced by on_new_connection().

◆ loop

uv_loop_t* loop

Definition at line 7 of file main.c.

◆ round_robin_counter

int round_robin_counter

Definition at line 15 of file main.c.

Referenced by on_new_connection(), and setup_workers().

◆ worker_path

char worker_path[500]

Definition at line 19 of file main.c.

Referenced by setup_workers().

◆ workers

struct child_worker * workers