Rizin
unix-like reverse engineering framework and cli tools
socket_proc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2012 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 /* XXX : move to rz_util??? rename method names.. to long? */
5 /* proc IO is not related to socket io.. */
6 
7 #include <rz_socket.h>
8 #include <rz_util.h>
9 #include <signal.h>
10 
11 #if __UNIX__
12 #include <sys/wait.h>
13 #endif
14 
15 #define BUFFER_SIZE 4096
16 
18 #if __UNIX__ && HAVE_FORK
20 
21  if (!sp) {
22  return NULL;
23  }
24 
25  if (rz_sys_pipe(sp->fd0, true) == -1) {
26  perror("pipe");
27  goto error;
28  }
29 
30  if (rz_sys_pipe(sp->fd1, true) == -1) {
31  perror("pipe");
32  goto error;
33  }
34 
35  sp->pid = rz_sys_fork();
36  switch (sp->pid) {
37  case 0:
38  close(0);
39  dup2(sp->fd0[0], 0);
40  close(1);
41  dup2(sp->fd1[1], 1);
42  rz_sys_execv(argv[0], argv);
43  exit(1);
44  break;
45  case -1:
46  perror("fork");
48  goto error;
49  // rz_socket_block_time (sp, false, 0);
50  }
51  return sp;
52 error:
53  free(sp);
54  return NULL;
55 #else
56  return NULL;
57 #endif
58 }
59 
61 #if __UNIX__
62  /* this is wrong */
63  kill(sp->pid, SIGKILL);
64  waitpid(sp->pid, NULL, 0); // WNOHANG);
65  close(sp->fd0[0]);
66  close(sp->fd0[1]);
67  // close(sp->fd1[0]);
68  close(sp->fd1[1]);
69  // sp->fd[0] = -1;
70  // sp->fd[1] = -1;
71 #endif
72  return 0;
73 }
74 
75 RZ_API int rz_socket_proc_read(RzSocketProc *sp, unsigned char *buf, int len) {
76  RzSocket s;
77  s.is_ssl = false;
78  s.fd = sp->fd1[0];
79  return rz_socket_read(&s, buf, len);
80 }
81 
83  RzSocket s;
84  s.is_ssl = false;
85  s.fd = sp->fd1[0];
86  return rz_socket_gets(&s, buf, size);
87 }
88 
90  RzSocket s;
91  s.is_ssl = false;
92  s.fd = sp->fd0[1];
93  return rz_socket_write(&s, buf, len);
94 }
95 
96 RZ_API void rz_socket_proc_printf(RzSocketProc *sp, const char *fmt, ...) {
97  RzSocket s;
98  char buf[BUFFER_SIZE];
99  va_list ap;
100  s.is_ssl = false;
101  s.fd = sp->fd0[1];
102  if (s.fd != RZ_INVALID_SOCKET) {
103  va_start(ap, fmt);
104  vsnprintf(buf, BUFFER_SIZE, fmt, ap);
105  rz_socket_write(&s, buf, strlen(buf));
106  va_end(ap);
107  }
108 }
109 
110 RZ_API int rz_socket_proc_ready(RzSocketProc *sp, int secs, int usecs) {
111  RzSocket s;
112  s.is_ssl = false;
113  s.fd = sp->fd1[0];
114  return rz_socket_ready(&s, secs, usecs);
115 }
size_t len
Definition: 6502dis.c:15
#define SIGKILL
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count close
Definition: sflib.h:33
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366
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 kill
Definition: sflib.h:64
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
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 unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path dup2
Definition: sflib.h:94
static RzSocket * s
Definition: rtr.c:28
RZ_API int rz_socket_ready(RzSocket *s, int secs, int usecs)
Definition: socket.c:688
RZ_API int rz_socket_gets(RzSocket *s, char *buf, int size)
Definition: socket.c:830
RZ_API void RZ_API int rz_socket_read(RzSocket *s, ut8 *read, int len)
Definition: socket.c:783
#define RZ_INVALID_SOCKET
Definition: rz_socket.h:48
RZ_API int rz_socket_write(RzSocket *s, void *buf, int len)
Definition: socket.c:724
RZ_API int rz_sys_execv(const char *pathname, char *const argv[])
Definition: sys.c:1483
RZ_API int rz_sys_pipe(int pipefd[2], bool close_on_exec)
Definition: sys.c:1458
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
#define RZ_NEW(x)
Definition: rz_types.h:285
RZ_API void rz_socket_proc_printf(RzSocketProc *sp, const char *fmt,...)
Definition: socket_proc.c:96
RZ_API int rz_socket_proc_write(RzSocketProc *sp, void *buf, int len)
Definition: socket_proc.c:89
RZ_API int rz_socket_proc_close(struct rz_socket_proc_t *sp)
Definition: socket_proc.c:60
#define BUFFER_SIZE
Definition: socket_proc.c:15
RZ_API int rz_socket_proc_read(RzSocketProc *sp, unsigned char *buf, int len)
Definition: socket_proc.c:75
RZ_API int rz_socket_proc_gets(RzSocketProc *sp, char *buf, int size)
Definition: socket_proc.c:82
RZ_API struct rz_socket_proc_t * rz_socket_proc_open(char *const argv[])
Definition: socket_proc.c:17
RZ_API int rz_socket_proc_ready(RzSocketProc *sp, int secs, int usecs)
Definition: socket_proc.c:110
bool is_ssl
Definition: rz_socket.h:68
void error(const char *msg)
Definition: untgz.c:593
static int sp
Definition: z80asm.c:91