Rizin
unix-like reverse engineering framework and cli tools
iob_pipe.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2017 LemonBoy <thatlemon@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <rz_types.h>
7 #include <rz_util.h>
8 #include "transport.h"
9 
10 #if __WINDOWS__
11 #include <rz_windows.h>
12 
13 static void *iob_pipe_open(const char *path) {
14  HANDLE hPipe;
15  LPTSTR path_ = rz_sys_conv_utf8_to_win(path);
16 
17  hPipe = CreateFile(path_, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
18  free(path_);
19  return hPipe != INVALID_HANDLE_VALUE ? (void *)(HANDLE)hPipe : NULL;
20 }
21 
22 static bool iob_pipe_close(void *p) {
23  return CloseHandle(p);
24 }
25 
26 static int iob_pipe_read(void *p, uint8_t *buf, const uint64_t count, const int timeout) {
27  DWORD c = 0;
28  OVERLAPPED ov = { 0 };
29  ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
30  if (!ov.hEvent) {
31  return 0;
32  }
33  if (!ReadFile(p, buf, count, NULL, &ov) &&
34  GetLastError() != ERROR_IO_PENDING) {
35  rz_sys_perror("ReadFile");
36  return -1;
37  }
38  if (WaitForSingleObject(ov.hEvent, timeout) == WAIT_TIMEOUT) {
39  CancelIo(p);
40  }
41  GetOverlappedResult(p, &ov, &c, TRUE);
42  CloseHandle(ov.hEvent);
43  return c;
44 }
45 
46 static int iob_pipe_write(void *p, const uint8_t *buf, const uint64_t count, const int timeout) {
47  DWORD cbWrited = 0;
48  OVERLAPPED ov = { 0 };
49  if (!WriteFile(p, buf, count, NULL, &ov) &&
50  GetLastError() != ERROR_IO_PENDING) {
51  rz_sys_perror("WriteFile");
52  return -1;
53  }
54  GetOverlappedResult(p, &ov, &cbWrited, TRUE);
55  return cbWrited;
56 }
57 #else
58 #include <errno.h>
59 #include <sys/socket.h>
60 #include <sys/select.h>
61 #include <sys/un.h>
62 
63 static void *iob_pipe_open(const char *path) {
64  int sock;
65  struct sockaddr_un sa;
66 
67  sock = socket(AF_UNIX, SOCK_STREAM, 0);
68  if (sock == -1) {
69  perror("socket");
70  return 0;
71  }
72 
73  memset(&sa, 0, sizeof(struct sockaddr_un));
74 
75  sa.sun_family = AF_UNIX;
76  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
77  sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
78  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) == -1) {
79  perror("connect");
80  close(sock);
81  return 0;
82  }
83  return (void *)(size_t)sock;
84 }
85 
86 static bool iob_pipe_close(void *p) {
87  return close((int)(size_t)p) == 0;
88 }
89 
90 static int iob_pipe_read(void *p, uint8_t *buf, const uint64_t count, const int timeout) {
91  int result;
92  fd_set readset;
93  int fd = (int)(size_t)p;
94  struct timeval tv;
95  tv.tv_sec = 0;
96  // Convert from ms
97  tv.tv_usec = timeout * 1000;
98  for (;;) {
99  FD_ZERO(&readset);
100  FD_SET(fd, &readset);
101  result = select(fd + 1, &readset, NULL, NULL, &tv);
102  if (result < 1) {
103  if (errno == EINTR) {
104  continue;
105  }
106  if (result == 0) {
107  return 0;
108  }
109  return -1;
110  }
111  if (FD_ISSET(fd, &readset)) {
112  return recv((int)(size_t)p, buf, count, 0);
113  }
114  }
115  return EINTR;
116 }
117 
118 static int iob_pipe_write(void *p, const uint8_t *buf, const uint64_t count, const int timeout) {
119  int ret = send((int)(size_t)p, buf, count, 0);
120  if (ret < 1) {
121  rz_sys_perror("iob_pipe_write, send");
122  if (errno == EPIPE) {
123  exit(1);
124  }
125  }
126  return ret;
127 }
128 #endif
129 
131  .name = "pipe",
132  .type = KD_IO_PIPE,
133  .init = NULL,
134  .deinit = NULL,
135  .config = NULL,
136  .open = &iob_pipe_open,
137  .close = &iob_pipe_close,
138  .read = &iob_pipe_read,
139  .write = &iob_pipe_write,
140 };
#define NULL
Definition: cris-opc.c:27
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 tv
Definition: sflib.h:79
static static fork const void static count close
Definition: sflib.h:33
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
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 socket
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
static int iob_pipe_read(void *p, uint8_t *buf, const uint64_t count, const int timeout)
Definition: iob_pipe.c:90
static int iob_pipe_write(void *p, const uint8_t *buf, const uint64_t count, const int timeout)
Definition: iob_pipe.c:118
static bool iob_pipe_close(void *p)
Definition: iob_pipe.c:86
io_backend_t iob_pipe
Definition: iob_pipe.c:130
static void * iob_pipe_open(const char *path)
Definition: iob_pipe.c:63
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz select
Definition: sflib.h:108
#define TRUE
Definition: mybfd.h:103
#define FALSE
Definition: mybfd.h:102
#define rz_sys_perror(x)
Definition: rz_types.h:336
static struct sockaddr static addrlen static backlog send
Definition: sfsocketcall.h:119
static int
Definition: sfsocketcall.h:114
#define AF_UNIX
Definition: sftypes.h:285
#define FD_ZERO(set)
Definition: sftypes.h:204
#define FD_ISSET(d, set)
Definition: sftypes.h:214
#define FD_SET(d, set)
Definition: sftypes.h:212
int size_t
Definition: sftypes.h:40
#define EINTR
Definition: sftypes.h:114
#define EPIPE
Definition: sftypes.h:142
@ SOCK_STREAM
Definition: sftypes.h:224
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define c(i)
Definition: sha256.c:43
const char * name
Definition: transport.h:28
char sun_path[108]
Definition: sftypes.h:356
uv_timer_t timeout
Definition: main.c:9
#define KD_IO_PIPE
Definition: transport.h:17
DWORD * HANDLE
DWORD
static const z80_opcode fd[]
Definition: z80_tab.h:997