Rizin
unix-like reverse engineering framework and cli tools
worker.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <uv.h>
7 
10 
11 typedef struct {
14 } write_req_t;
15 
17  write_req_t *wr = (write_req_t*) req;
18  free(wr->buf.base);
19  free(wr);
20 }
21 
22 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
23  buf->base = malloc(suggested_size);
24  buf->len = suggested_size;
25 }
26 
28  if (status) {
29  fprintf(stderr, "Write error %s\n", uv_err_name(status));
30  }
32 }
33 
34 void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
35  if (nread > 0) {
37  req->buf = uv_buf_init(buf->base, nread);
38  uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
39  return;
40  }
41 
42  if (nread < 0) {
43  if (nread != UV_EOF)
44  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
45  uv_close((uv_handle_t*) client, NULL);
46  }
47 
48  free(buf->base);
49 }
50 
52  if (nread < 0) {
53  if (nread != UV_EOF)
54  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
55  uv_close((uv_handle_t*) q, NULL);
56  return;
57  }
58 
59  uv_pipe_t *pipe = (uv_pipe_t*) q;
61  fprintf(stderr, "No pending count\n");
62  return;
63  }
64 
66  assert(pending == UV_TCP);
67 
68  uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
69  uv_tcp_init(loop, client);
70  if (uv_accept(q, (uv_stream_t*) client) == 0) {
71  uv_os_fd_t fd;
72  uv_fileno((const uv_handle_t*) client, &fd);
73  fprintf(stderr, "Worker %d: Accepted fd %d\n", getpid(), fd);
75  }
76  else {
77  uv_close((uv_handle_t*) client, NULL);
78  }
79 }
80 
81 int main() {
83 
84  uv_pipe_init(loop, &queue, 1 /* ipc */);
85  uv_pipe_open(&queue, 0);
87  return uv_run(loop, UV_RUN_DEFAULT);
88 }
static mcore_handle handle
Definition: asm_mcore.c:8
#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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname pipe
Definition: sflib.h:73
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
assert(limit<=UINT32_MAX/2)
int ssize_t
Definition: sftypes.h:39
Definition: unix.h:123
char * base
Definition: unix.h:124
Definition: uv.h:1780
Definition: uv.h:767
Definition: uv.h:547
Definition: uv.h:525
uv_write_t req
Definition: worker.c:12
uv_buf_t buf
Definition: worker.c:13
int uv_os_fd_t
Definition: unix.h:130
@ UV_RUN_DEFAULT
Definition: uv.h:255
uv_handle_type
Definition: uv.h:189
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: stream.c:1492
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: stream.c:1555
UV_EXTERN int uv_pipe_pending_count(uv_pipe_t *handle)
Definition: pipe.c:298
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 const char * uv_err_name(int err)
Definition: uv-common.c:189
UV_EXTERN uv_handle_type uv_pipe_pending_type(uv_pipe_t *handle)
Definition: pipe.c:315
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
UV_EXTERN int uv_pipe_open(uv_pipe_t *, uv_file file)
Definition: pipe.c:137
UV_EXTERN int uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd)
Definition: core.c:767
void on_new_connection(uv_stream_t *q, ssize_t nread, const uv_buf_t *buf)
Definition: worker.c:51
void echo_write(uv_write_t *req, int status)
Definition: worker.c:27
uv_pipe_t queue
Definition: worker.c:9
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: worker.c:22
uv_loop_t * loop
Definition: worker.c:8
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
Definition: worker.c:34
int main()
Definition: worker.c:81
void free_write_req(uv_write_t *req)
Definition: worker.c:16
static const z80_opcode fd[]
Definition: z80_tab.h:997