]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
b24eb750 JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
b24eb750 | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
b3378a02 | 19 | **/ |
b24eb750 JF |
20 | /* }}} */ |
21 | ||
20052ff7 JF |
22 | #include "cycript.hpp" |
23 | ||
7cfc264c | 24 | #include <dlfcn.h> |
63129b67 | 25 | #include <pthread.h> |
b24eb750 JF |
26 | #include <unistd.h> |
27 | #include <sstream> | |
28 | ||
f8d45a20 | 29 | #include <sys/stat.h> |
bb1c419c JF |
30 | #include <sys/types.h> |
31 | #include <sys/socket.h> | |
32 | #include <netinet/in.h> | |
33 | #include <sys/un.h> | |
34 | ||
938d0626 JF |
35 | #ifdef __APPLE__ |
36 | #include <CoreFoundation/CoreFoundation.h> | |
37 | #endif | |
38 | ||
8a392978 | 39 | #include "Driver.hpp" |
b12a9965 | 40 | #include "JavaScript.hpp" |
20052ff7 | 41 | #include "Syntax.hpp" |
b12a9965 JF |
42 | #include "Pooling.hpp" |
43 | ||
b24eb750 | 44 | struct CYExecute_ { |
b799113b | 45 | CYPool &pool_; |
b24eb750 | 46 | const char * volatile data_; |
938d0626 JF |
47 | pthread_mutex_t mutex_; |
48 | pthread_cond_t condition_; | |
b24eb750 JF |
49 | }; |
50 | ||
938d0626 JF |
51 | void CYPerform(void *arg) { |
52 | CYExecute_ *execute(reinterpret_cast<CYExecute_ *>(arg)); | |
b24eb750 | 53 | |
b24eb750 JF |
54 | const char *data(execute->data_); |
55 | execute->data_ = NULL; | |
89d16b11 | 56 | execute->data_ = CYExecute(CYGetJSContext(), execute->pool_, CYUTF8String(data)); |
b24eb750 | 57 | |
938d0626 JF |
58 | pthread_mutex_lock(&execute->mutex_); |
59 | pthread_cond_signal(&execute->condition_); | |
60 | pthread_mutex_unlock(&execute->mutex_); | |
61 | } | |
b24eb750 JF |
62 | |
63 | struct CYClient : | |
64 | CYData | |
65 | { | |
66 | int socket_; | |
63129b67 | 67 | pthread_t thread_; |
b24eb750 JF |
68 | |
69 | CYClient(int socket) : | |
70 | socket_(socket) | |
71 | { | |
72 | } | |
73 | ||
74 | ~CYClient() { | |
75 | _syscall(close(socket_)); | |
76 | } | |
77 | ||
37954781 | 78 | void Handle() { |
bb1c419c | 79 | bool dispatch; |
938d0626 JF |
80 | #ifdef __APPLE__ |
81 | CFRunLoopRef loop(CFRunLoopGetMain()); | |
82 | if (CFStringRef mode = CFRunLoopCopyCurrentMode(loop)) { | |
bb1c419c JF |
83 | dispatch = true; |
84 | CFRelease(mode); | |
85 | } else | |
938d0626 | 86 | #endif |
bb1c419c JF |
87 | dispatch = false; |
88 | ||
b24eb750 | 89 | for (;;) { |
9d79cefc | 90 | uint32_t size; |
b24eb750 JF |
91 | if (!CYRecvAll(socket_, &size, sizeof(size))) |
92 | return; | |
93 | ||
97f6a005 | 94 | CYLocalPool pool; |
b24eb750 JF |
95 | char *data(new(pool) char[size + 1]); |
96 | if (!CYRecvAll(socket_, data, size)) | |
97 | return; | |
98 | data[size] = '\0'; | |
99 | ||
0df6a201 JF |
100 | std::string code(data, size); |
101 | CYExecute_ execute = {pool, code.c_str()}; | |
938d0626 JF |
102 | |
103 | pthread_mutex_init(&execute.mutex_, NULL); | |
104 | pthread_cond_init(&execute.condition_, NULL); | |
105 | ||
106 | if (!dispatch) | |
107 | CYPerform(&execute); | |
108 | #ifdef __APPLE__ | |
109 | else { | |
110 | CFRunLoopSourceContext context; | |
111 | memset(&context, 0, sizeof(context)); | |
112 | context.version = 0; | |
113 | context.info = &execute; | |
114 | context.perform = &CYPerform; | |
115 | ||
116 | CFRunLoopSourceRef source(CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context)); | |
117 | ||
118 | pthread_mutex_lock(&execute.mutex_); | |
119 | ||
120 | CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes); | |
121 | CFRunLoopSourceSignal(source); | |
122 | ||
123 | CFRunLoopWakeUp(loop); | |
124 | pthread_cond_wait(&execute.condition_, &execute.mutex_); | |
125 | pthread_mutex_unlock(&execute.mutex_); | |
126 | ||
127 | CFRunLoopRemoveSource(loop, source, kCFRunLoopCommonModes); | |
128 | CFRelease(source); | |
129 | } | |
130 | #endif | |
131 | ||
132 | pthread_cond_destroy(&execute.condition_); | |
133 | pthread_mutex_destroy(&execute.mutex_); | |
0df6a201 JF |
134 | |
135 | const char *json(execute.data_); | |
136 | size = json == NULL ? _not(uint32_t) : strlen(json); | |
137 | ||
b24eb750 JF |
138 | if (!CYSendAll(socket_, &size, sizeof(size))) |
139 | return; | |
140 | if (json != NULL) | |
141 | if (!CYSendAll(socket_, json, size)) | |
142 | return; | |
143 | } | |
144 | } | |
145 | }; | |
146 | ||
0cbeddf8 | 147 | static void *OnClient(void *data) { |
b24eb750 JF |
148 | CYClient *client(reinterpret_cast<CYClient *>(data)); |
149 | client->Handle(); | |
150 | delete client; | |
151 | return NULL; | |
152 | } | |
153 | ||
d9c91152 | 154 | void CYHandleClient(int socket) { |
3429e25c JF |
155 | // XXX: this leaks memory... really? |
156 | CYPool *pool(new CYPool()); | |
157 | CYClient *client(new(*pool) CYClient(socket)); | |
63129b67 | 158 | _assert(pthread_create(&client->thread_, NULL, &OnClient, client) == 0); |
b24eb750 | 159 | } |
994434e5 | 160 | |
f8d45a20 | 161 | static void CYHandleSocket(const char *path) { |
7cfc264c JF |
162 | int socket(_syscall(::socket(PF_UNIX, SOCK_STREAM, 0))); |
163 | ||
164 | struct sockaddr_un address; | |
165 | memset(&address, 0, sizeof(address)); | |
166 | address.sun_family = AF_UNIX; | |
f8d45a20 | 167 | strcpy(address.sun_path, path); |
7cfc264c | 168 | |
e2ce853b | 169 | _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), sizeof(address))); |
f8d45a20 JF |
170 | |
171 | CYInitializeDynamic(); | |
7cfc264c | 172 | CYHandleClient(socket); |
994434e5 | 173 | } |
666eedb9 | 174 | |
d9c91152 | 175 | _extern void CYHandleServer(pid_t pid) { try { |
f8d45a20 JF |
176 | char path[1024]; |
177 | sprintf(path, "/tmp/.s.cy.%u", pid); | |
178 | CYHandleSocket(path); | |
7cfc264c JF |
179 | } catch (const CYException &error) { |
180 | CYPool pool; | |
181 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
182 | } } | |
183 | ||
d9c91152 | 184 | _extern char *MSmain0(int argc, char *argv[]) { try { |
7cfc264c | 185 | _assert(argc == 2); |
f8d45a20 | 186 | CYHandleSocket(argv[1]); |
7cfc264c JF |
187 | |
188 | static void *handle(NULL); | |
189 | if (handle == NULL) { | |
190 | Dl_info info; | |
191 | _assert(dladdr(reinterpret_cast<void *>(&MSmain0), &info) != 0); | |
e2ce853b JF |
192 | #ifdef __ANDROID__ |
193 | handle = dlopen(info.dli_fname, 0); | |
194 | #else | |
7cfc264c | 195 | handle = dlopen(info.dli_fname, RTLD_NOLOAD); |
e2ce853b | 196 | #endif |
7cfc264c JF |
197 | } |
198 | ||
7cfc264c JF |
199 | return NULL; |
200 | } catch (const CYException &error) { | |
201 | CYPool pool; | |
202 | return strdup(error.PoolCString(pool)); | |
203 | } } | |
204 | ||
666eedb9 JF |
205 | struct CYServer { |
206 | pthread_t thread_; | |
207 | uint16_t port_; | |
208 | int socket_; | |
209 | ||
210 | CYServer(uint16_t port) : | |
211 | port_(port), | |
212 | socket_(-1) | |
213 | { | |
214 | } | |
215 | ||
216 | ~CYServer() { | |
217 | if (socket_ != -1) | |
218 | _syscall(close(socket_)); | |
219 | } | |
220 | ||
221 | void Listen() { | |
222 | socket_ = _syscall(::socket(PF_INET, SOCK_STREAM, 0)); try { | |
30d46816 JF |
223 | int value; |
224 | _syscall(::setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &(value = 1), sizeof(value))); | |
225 | ||
666eedb9 JF |
226 | sockaddr_in address; |
227 | address.sin_family = AF_INET; | |
228 | address.sin_addr.s_addr = INADDR_ANY; | |
229 | address.sin_port = htons(port_); | |
230 | _syscall(::bind(socket_, reinterpret_cast<sockaddr *>(&address), sizeof(address))); | |
231 | ||
232 | _syscall(::listen(socket_, -1)); | |
233 | ||
234 | for (;;) { | |
235 | socklen_t length(sizeof(address)); | |
236 | int socket(_syscall(::accept(socket_, reinterpret_cast<sockaddr *>(&address), &length))); | |
237 | CYHandleClient(socket); | |
238 | } | |
239 | } catch (const CYException &error) { | |
240 | CYPool pool; | |
241 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
242 | } | |
243 | } | |
244 | }; | |
245 | ||
246 | static void *OnServer(void *data) { | |
247 | CYServer *server(reinterpret_cast<CYServer *>(data)); | |
248 | server->Listen(); | |
249 | delete server; | |
250 | return NULL; | |
251 | } | |
252 | ||
d9c91152 | 253 | _extern void CYListenServer(short port) { |
666eedb9 JF |
254 | CYInitializeDynamic(); |
255 | ||
256 | CYServer *server(new CYServer(port)); | |
257 | _assert(pthread_create(&server->thread_, NULL, &OnServer, server) == 0); | |
258 | } |