1 /* radare - LGPL - Copyright 2008-2019 pancake */
2 
3 #include "r_io.h"
4 #include "r_lib.h"
5 #include <sys/types.h>
6 
7 #if __ANDROID__ || EMSCRIPTEN
8 #undef __UNIX__
9 #define __UNIX__ 0
10 #endif
11 
12 // linux requires -lrt for this, but still it seems to not work as expected
13 // better not to enable it by default until we get enough time to properly
14 // make this work across all unixes without adding extra depenencies
15 #define USE_SHM_OPEN 0
16 
17 #if __UNIX__ && !defined (__QNX__) && !defined (__HAIKU__)
18 #include <sys/ipc.h>
19 #include <sys/shm.h>
20 #include <sys/mman.h>
21 
22 typedef struct {
23 	int fd;
24 	int id;
25 	ut8 *buf;
26 	ut32 size;
27 } RIOShm;
28 #define RIOSHM_FD(x) (((RIOShm*)(x))->fd)
29 
30 #define SHMATSZ 0x9000; // 32*1024*1024; /* 32MB : XXX not used correctly? */
31 
shm__write(RIO * io,RIODesc * fd,const ut8 * buf,int count)32 static int shm__write(RIO *io, RIODesc *fd, const ut8 *buf, int count) {
33 	r_return_val_if_fail (fd && fd->data, -1);
34 	RIOShm *shm = fd->data;
35 	if (shm->buf) {
36 		(void)memcpy (shm->buf + io->off, buf, count);
37 		return count;
38 	}
39 	return write (shm->fd, buf, count);
40 }
41 
shm__read(RIO * io,RIODesc * fd,ut8 * buf,int count)42 static int shm__read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
43 	r_return_val_if_fail (fd && fd->data, -1);
44 	RIOShm *shm = fd->data;
45 	if (io->off + count >= shm->size) {
46 		if (io->off > shm->size) {
47 			return -1;
48 		}
49 		count = shm->size - io->off;
50 	}
51 	if (shm->buf) {
52 		memcpy (buf, shm->buf+io->off , count);
53 		return count;
54 	}
55 	return read (shm->fd, buf, count);
56 }
57 
shm__close(RIODesc * fd)58 static int shm__close(RIODesc *fd) {
59 	r_return_val_if_fail (fd && fd->data, -1);
60 	int ret;
61 	RIOShm *shm = fd->data;
62 	if (shm->buf) {
63 		ret = shmdt (((RIOShm*)(fd->data))->buf);
64 	} else {
65 		ret = close (shm->fd);
66 	}
67 	R_FREE (fd->data);
68 	return ret;
69 }
70 
shm__lseek(RIO * io,RIODesc * fd,ut64 offset,int whence)71 static ut64 shm__lseek(RIO *io, RIODesc *fd, ut64 offset, int whence) {
72 	r_return_val_if_fail (fd && fd->data, -1);
73 	RIOShm *shm = fd->data;
74 	switch (whence) {
75 	case SEEK_SET:
76 		return io->off = offset;
77 	case SEEK_CUR:
78 		if (io->off + offset > shm->size) {
79 			return io->off = shm->size;
80 		}
81 		io->off += offset;
82 		return io->off;
83 	case SEEK_END:
84 		return 0xffffffff;
85 	}
86 	return io->off;
87 }
88 
shm__plugin_open(RIO * io,const char * pathname,bool many)89 static bool shm__plugin_open(RIO *io, const char *pathname, bool many) {
90 	return (!strncmp (pathname, "shm://", 6));
91 }
92 
getshmfd(RIOShm * shm)93 static inline int getshmfd (RIOShm *shm) {
94 	return (((int)(size_t)shm->buf) >> 4) & 0xfff;
95 }
96 
shm__open(RIO * io,const char * pathname,int rw,int mode)97 static RIODesc *shm__open(RIO *io, const char *pathname, int rw, int mode) {
98 	if (!strncmp (pathname, "shm://", 6)) {
99 		RIOShm *shm = R_NEW0 (RIOShm);
100 		if (!shm) {
101 			return NULL;
102 		}
103 		const char *ptr = pathname + 6;
104 		shm->id = atoi (ptr);
105 		if (!shm->id) {
106 			shm->id = r_str_hash (ptr);
107 		}
108 		shm->buf = shmat (shm->id, 0, 0);
109 		if (shm->buf == (void*)(size_t)-1) {
110 #if USE_SHM_OPEN
111 			shm->buf = NULL;
112 			shm->fd = shm_open (ptr, O_CREAT | (rw?O_RDWR:O_RDONLY), 0644);
113 #else
114 			shm->fd = -1;
115 #endif
116 
117 		} else {
118 			shm->fd = getshmfd (shm);
119 		}
120 		shm->size = SHMATSZ;
121 		if (shm->fd != -1) {
122 			eprintf ("Connected to shared memory 0x%08x\n", shm->id);
123 			return r_io_desc_new (io, &r_io_plugin_shm, pathname, rw, mode, shm);
124 		}
125 		eprintf ("Cannot connect to shared memory (%d)\n", shm->id);
126 		free (shm);
127 	}
128 	return NULL;
129 }
130 
131 RIOPlugin r_io_plugin_shm = {
132 	.name = "shm",
133 	.desc = "Shared memory resources plugin",
134 	.uris = "shm://",
135 	.license = "MIT",
136 	.open = shm__open,
137 	.close = shm__close,
138 	.read = shm__read,
139 	.check = shm__plugin_open,
140 	.lseek = shm__lseek,
141 	.write = shm__write,
142 };
143 
144 #else
145 RIOPlugin r_io_plugin_shm = {
146 	.name = "shm",
147 	.desc = "shared memory resources (not for w32)",
148 };
149 #endif
150 
151 #ifndef R2_PLUGIN_INCORE
152 R_API RLibStruct radare_plugin = {
153 	.type = R_LIB_TYPE_IO,
154 	.data = &r_io_plugin_shm,
155 	.version = R2_VERSION
156 };
157 #endif
158