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 
10 
12  fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
13  uv_close((uv_handle_t*) req->data, NULL);
15 }
16 
17 void invoke_cgi_script(uv_tcp_t *client) {
18  size_t size = 500;
19  char path[size];
20  uv_exepath(path, &size);
21  strcpy(path + (strlen(path) - strlen("cgi")), "tick");
22 
23  char* args[2];
24  args[0] = path;
25  args[1] = NULL;
26 
27  /* ... finding the executable path and setting up arguments ... */
28 
29  options.stdio_count = 3;
30  uv_stdio_container_t child_stdio[3];
31  child_stdio[0].flags = UV_IGNORE;
32  child_stdio[1].flags = UV_INHERIT_STREAM;
33  child_stdio[1].data.stream = (uv_stream_t*) client;
34  child_stdio[2].flags = UV_IGNORE;
35  options.stdio = child_stdio;
36 
38  options.file = args[0];
39  options.args = args;
40 
41  // Set this so we can close the socket after the child process exits.
42  child_req.data = (void*) client;
43  int r;
44  if ((r = uv_spawn(loop, &child_req, &options))) {
45  fprintf(stderr, "%s\n", uv_strerror(r));
46  return;
47  }
48 }
49 
50 void on_new_connection(uv_stream_t *server, int status) {
51  if (status == -1) {
52  // error!
53  return;
54  }
55 
56  uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
57  uv_tcp_init(loop, client);
58  if (uv_accept(server, (uv_stream_t*) client) == 0) {
59  invoke_cgi_script(client);
60  }
61  else {
62  uv_close((uv_handle_t*) client, NULL);
63  }
64 }
65 
66 int main() {
68 
69  uv_tcp_t server;
70  uv_tcp_init(loop, &server);
71 
72  struct sockaddr_in bind_addr;
73  uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
74  uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
75  int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection);
76  if (r) {
77  fprintf(stderr, "Listen error %s\n", uv_err_name(r));
78  return 1;
79  }
80  return uv_run(loop, UV_RUN_DEFAULT);
81 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
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 uLong size
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
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
Definition: uv.h:1780
const char * file
Definition: uv.h:953
uv_stdio_container_t * stdio
Definition: uv.h:986
char ** args
Definition: uv.h:960
uv_exit_cb exit_cb
Definition: uv.h:952
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
uv_process_options_t options
Definition: main.c:9
void invoke_cgi_script(uv_tcp_t *client)
Definition: main.c:17
void on_new_connection(uv_stream_t *server, int status)
Definition: main.c:50
void cleanup_handles(uv_process_t *req, int64_t exit_status, int term_signal)
Definition: main.c:11
uv_loop_t * loop
Definition: main.c:7
uv_process_t child_req
Definition: main.c:8
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
UV_EXTERN int uv_exepath(char *buffer, size_t *size)
Definition: aix-common.c:60
@ UV_IGNORE
Definition: uv.h:922
@ UV_INHERIT_STREAM
Definition: uv.h:925
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 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 void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108
UV_EXTERN const char * uv_strerror(int err)
Definition: uv-common.c:212
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