Rizin
unix-like reverse engineering framework and cli tools
main.c
Go to the documentation of this file.
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <uv.h>
6 
8 
9 struct child_worker {
14 
17 
19 char worker_path[500];
20 
22  fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
24 }
25 
26 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
27  buf->base = malloc(suggested_size);
28  buf->len = suggested_size;
29 }
30 
31 void on_new_connection(uv_stream_t *server, int status) {
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 }
50 
51 void setup_workers() {
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 }
96 
97 int main() {
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 }
static mcore_handle handle
Definition: asm_mcore.c:8
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 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
voidpf void * buf
Definition: ioapi.h:138
#define PRId64
Definition: macros.h:17
int main(int argc, const char **argv)
Definition: main.c:340
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
long int64_t
Definition: sftypes.h:32
uv_process_t req
Definition: main.c:10
uv_pipe_t pipe
Definition: main.c:12
uv_process_options_t options
Definition: main.c:11
Definition: unix.h:123
Definition: uv.h:1780
Definition: uv.h:767
uv_stream_t * stream
Definition: uv.h:946
uv_stdio_flags flags
Definition: uv.h:943
union uv_stdio_container_s::@397 data
Definition: uv.h:547
Definition: uv.h:525
void on_new_connection(uv_stream_t *server, int status)
Definition: main.c:50
uv_loop_t * loop
Definition: main.c:7
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: main.c:8
void close_process_handle(uv_process_t *req, int64_t exit_status, int term_signal)
Definition: main.c:21
struct child_worker * workers
char worker_path[500]
Definition: main.c:19
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
void setup_workers()
Definition: main.c:51
uv_write_t write_req
Definition: main.c:9
struct Arg * args
Definition: mipsasm.c:18
static enum exit_status_type exit_status
Exit status to use. This can be changed with set_exit_status().
Definition: main.c:17
static void worker(void *arg)
Definition: threadpool.c:57
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_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_spawn(uv_loop_t *loop, uv_process_t *handle, const uv_process_options_t *options)
Definition: process.c:408
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: stream.c:656
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
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
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 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 void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: stream.c:591
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