1 | #include <sys/errno.h> |
1 | #include <errno.h> |
2 | #include <unistd.h> |
2 | #include <unistd.h> |
3 | #include <fcntl.h> |
3 | #include <fcntl.h> |
4 | |
4 | |
5 | #include <mysql.h> |
5 | #include <mysql.h> |
6 | |
6 | |
7 | #include "EXTERN.h" |
7 | #include "EXTERN.h" |
8 | #include "perl.h" |
8 | #include "perl.h" |
9 | #include "XSUB.h" |
9 | #include "XSUB.h" |
10 | |
10 | |
|
|
11 | #if HAVE_EV |
|
|
12 | # include "EVAPI.h" |
|
|
13 | # include "CoroAPI.h" |
|
|
14 | #endif |
|
|
15 | |
11 | #define IN_DESTRUCT PL_dirty |
16 | #define IN_DESTRUCT PL_dirty |
12 | |
17 | |
13 | typedef U16 uint16; |
18 | typedef U16 uint16; |
14 | |
19 | |
15 | /* cached function gv's */ |
20 | /* cached function gv's */ |
16 | static CV *readable, *writable; |
21 | static CV *readable, *writable; |
|
|
22 | static int use_ev; |
17 | |
23 | |
18 | #include "violite.h" |
24 | #include "violite.h" |
19 | |
25 | |
20 | #define DESC_OFFSET 22 |
|
|
21 | |
|
|
22 | #define CoMy_MAGIC 0x436f4d79 |
26 | #define CoMy_MAGIC 0x436f4d79 |
23 | |
27 | |
24 | typedef struct { |
28 | typedef struct { |
|
|
29 | #if DESC_IS_PTR |
|
|
30 | char desc[30]; |
|
|
31 | const char *old_desc; |
|
|
32 | #endif |
25 | int magic; |
33 | int magic; |
26 | SV *corosocket; |
34 | SV *corohandle_sv, *corohandle; |
27 | int bufofs, bufcnt; |
35 | int bufofs, bufcnt; |
|
|
36 | #if HAVE_EV |
|
|
37 | ev_io rw, ww; |
|
|
38 | #endif |
28 | char buf[VIO_READ_BUFFER_SIZE]; |
39 | char buf[VIO_READ_BUFFER_SIZE]; |
|
|
40 | size_t (*old_read)(Vio*, uchar *, size_t); |
|
|
41 | size_t (*old_write)(Vio*, const uchar *, size_t); |
|
|
42 | int (*old_vioclose)(Vio*); |
29 | } ourdata; |
43 | } ourdata; |
30 | |
44 | |
|
|
45 | #if DESC_IS_PTR |
|
|
46 | # define OURDATAPTR (*(ourdata **)&((vio)->desc)) |
|
|
47 | #else |
|
|
48 | # define DESC_OFFSET 22 |
31 | #define OURDATAPTR (*((ourdata **)((vio)->desc + DESC_OFFSET))) |
49 | # define OURDATAPTR (*((ourdata **)((vio)->desc + DESC_OFFSET))) |
|
|
50 | #endif |
32 | |
51 | |
33 | static int |
52 | static xlen |
34 | our_read (Vio *vio, gptr p, int len) |
53 | our_read (Vio *vio, xgptr p, xlen len) |
35 | { |
54 | { |
36 | ourdata *our = OURDATAPTR; |
55 | ourdata *our = OURDATAPTR; |
37 | |
56 | |
38 | if (!our->bufcnt) |
57 | if (!our->bufcnt) |
39 | { |
58 | { |
… | |
… | |
47 | rd = recv (vio->sd, our->buf, sizeof (our->buf), 0); |
66 | rd = recv (vio->sd, our->buf, sizeof (our->buf), 0); |
48 | |
67 | |
49 | if (rd >= 0 || errno != EAGAIN) |
68 | if (rd >= 0 || errno != EAGAIN) |
50 | break; |
69 | break; |
51 | |
70 | |
|
|
71 | #if HAVE_EV |
|
|
72 | if (use_ev) |
52 | { |
73 | { |
|
|
74 | our->rw.data = (void *)sv_2mortal (SvREFCNT_inc (CORO_CURRENT)); |
|
|
75 | ev_io_start (EV_DEFAULT_UC, &(our->rw)); |
|
|
76 | CORO_SCHEDULE; |
|
|
77 | ev_io_stop (EV_DEFAULT_UC, &(our->rw)); /* avoids races */ |
|
|
78 | } |
|
|
79 | else |
|
|
80 | #endif |
|
|
81 | { |
53 | dSP; |
82 | dSP; |
54 | PUSHMARK (SP); |
83 | PUSHMARK (SP); |
55 | XPUSHs (our->corosocket); |
84 | XPUSHs (our->corohandle); |
56 | PUTBACK; |
85 | PUTBACK; |
57 | call_sv ((SV *)readable, G_VOID | G_DISCARD); |
86 | call_sv ((SV *)readable, G_VOID | G_DISCARD); |
58 | } |
87 | } |
59 | } |
88 | } |
60 | |
89 | |
61 | if (rd <= 0) |
90 | if (rd <= 0) |
62 | return rd; |
91 | return rd; |
63 | |
92 | |
… | |
… | |
73 | our->bufcnt -= len; |
102 | our->bufcnt -= len; |
74 | |
103 | |
75 | return len; |
104 | return len; |
76 | } |
105 | } |
77 | |
106 | |
78 | static int |
107 | static xlen |
79 | our_write (Vio *vio, const gptr p, int len) |
108 | our_write (Vio *vio, cxgptr p, xlen len) |
80 | { |
109 | { |
81 | char *ptr = (char *)p; |
110 | char *ptr = (char *)p; |
82 | my_bool dummy; |
111 | my_bool dummy; |
83 | |
112 | |
84 | vio->vioblocking (vio, 0, &dummy); |
113 | vio->vioblocking (vio, 0, &dummy); |
… | |
… | |
92 | ptr += wr; |
121 | ptr += wr; |
93 | len -= wr; |
122 | len -= wr; |
94 | } |
123 | } |
95 | else if (errno == EAGAIN) |
124 | else if (errno == EAGAIN) |
96 | { |
125 | { |
|
|
126 | ourdata *our = OURDATAPTR; |
|
|
127 | |
|
|
128 | #if HAVE_EV |
|
|
129 | if (use_ev) |
|
|
130 | { |
|
|
131 | our->ww.data = (void *)sv_2mortal (SvREFCNT_inc (CORO_CURRENT)); |
|
|
132 | ev_io_start (EV_DEFAULT_UC, &(our->ww)); |
|
|
133 | CORO_SCHEDULE; |
|
|
134 | ev_io_stop (EV_DEFAULT_UC, &(our->ww)); /* avoids races */ |
|
|
135 | } |
|
|
136 | else |
|
|
137 | #endif |
|
|
138 | { |
97 | dSP; |
139 | dSP; |
98 | PUSHMARK (SP); |
140 | PUSHMARK (SP); |
99 | XPUSHs (OURDATAPTR->corosocket); |
141 | XPUSHs (our->corohandle); |
100 | PUTBACK; |
142 | PUTBACK; |
101 | call_sv ((SV *)writable, G_VOID | G_DISCARD); |
143 | call_sv ((SV *)writable, G_VOID | G_DISCARD); |
|
|
144 | } |
102 | } |
145 | } |
103 | else if (ptr == (char *)p) |
146 | else if (ptr == (char *)p) |
104 | return -1; |
147 | return -1; |
105 | else |
148 | else |
106 | break; |
149 | break; |
107 | } |
150 | } |
108 | |
151 | |
109 | return ptr - (char *)p; |
152 | return ptr - (char *)p; |
110 | } |
153 | } |
111 | |
154 | |
|
|
155 | static int |
|
|
156 | our_close (Vio *vio) |
|
|
157 | { |
|
|
158 | ourdata *our = OURDATAPTR; |
|
|
159 | |
|
|
160 | if (vio->read != our_read) |
|
|
161 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
|
|
162 | |
|
|
163 | if (vio->write != our_write) |
|
|
164 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
|
|
165 | |
|
|
166 | if (vio->vioclose != our_close) |
|
|
167 | croak ("vio.vioclose has unexpected content during unpatch - wtf?"); |
|
|
168 | |
|
|
169 | #if HAVE_EV |
|
|
170 | if (use_ev) |
|
|
171 | { |
|
|
172 | ev_io_stop (EV_DEFAULT_UC, &(our->rw)); |
|
|
173 | ev_io_stop (EV_DEFAULT_UC, &(our->ww)); |
|
|
174 | } |
|
|
175 | #endif |
|
|
176 | |
|
|
177 | SvREFCNT_dec (our->corohandle); |
|
|
178 | SvREFCNT_dec (our->corohandle_sv); |
|
|
179 | |
|
|
180 | #if DESC_IS_PTR |
|
|
181 | vio->desc = our->old_desc; |
|
|
182 | #endif |
|
|
183 | |
|
|
184 | vio->vioclose = our->old_vioclose; |
|
|
185 | vio->write = our->old_write; |
|
|
186 | vio->read = our->old_read; |
|
|
187 | |
|
|
188 | Safefree (our); |
|
|
189 | |
|
|
190 | vio->vioclose (vio); |
|
|
191 | } |
|
|
192 | |
|
|
193 | #if HAVE_EV |
|
|
194 | static void |
|
|
195 | iocb (EV_P_ ev_io *w, int revents) |
|
|
196 | { |
|
|
197 | ev_io_stop (EV_A, w); |
|
|
198 | CORO_READY ((SV *)w->data); |
|
|
199 | } |
|
|
200 | #endif |
|
|
201 | |
112 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
202 | MODULE = Coro::Mysql PACKAGE = Coro::Mysql |
113 | |
203 | |
114 | BOOT: |
204 | BOOT: |
115 | { |
205 | { |
116 | readable = get_cv ("Coro::Mysql::readable", 0); |
206 | readable = get_cv ("Coro::Mysql::readable", 0); |
… | |
… | |
118 | } |
208 | } |
119 | |
209 | |
120 | PROTOTYPES: ENABLE |
210 | PROTOTYPES: ENABLE |
121 | |
211 | |
122 | void |
212 | void |
123 | _patch (IV sock, int fd, SV *corosocket) |
213 | _use_ev () |
|
|
214 | PPCODE: |
|
|
215 | { |
|
|
216 | static int onceonly; |
|
|
217 | |
|
|
218 | if (!onceonly) |
|
|
219 | { |
|
|
220 | onceonly = 1; |
|
|
221 | #if HAVE_EV |
|
|
222 | I_EV_API ("Coro::Mysql"); |
|
|
223 | I_CORO_API ("Coro::Mysql"); |
|
|
224 | use_ev = 1; |
|
|
225 | #endif |
|
|
226 | } |
|
|
227 | |
|
|
228 | XPUSHs (use_ev ? &PL_sv_yes : &PL_sv_no); |
|
|
229 | } |
|
|
230 | |
|
|
231 | void |
|
|
232 | _patch (IV sock, int fd, unsigned long client_version, SV *corohandle_sv, SV *corohandle) |
124 | CODE: |
233 | CODE: |
125 | { |
234 | { |
126 | MYSQL *my = (MYSQL *)sock; |
235 | MYSQL *my = (MYSQL *)sock; |
127 | Vio *vio = my->net.vio; |
236 | Vio *vio = my->net.vio; |
128 | ourdata *our; |
237 | ourdata *our; |
129 | |
238 | |
|
|
239 | /* matching versions are required but not sufficient */ |
|
|
240 | if (client_version != mysql_get_client_version ()) |
|
|
241 | croak ("DBD::mysql linked against different libmysqlclient library than Coro::Mysql (%lu vs. %lu).", |
|
|
242 | client_version, mysql_get_client_version ()); |
|
|
243 | |
130 | if (fd != my->net.fd) |
244 | if (fd != my->net.fd) |
131 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
245 | croak ("DBD::mysql fd and libmysql disagree - library mismatch, unsupported transport or API changes?"); |
132 | |
246 | |
133 | if (fd != vio->sd) |
247 | if (fd != vio->sd) |
134 | croak ("DBD::mysql fd and vio-sd disagree - library mismatch, unsupported transport or API changes?"); |
248 | croak ("DBD::mysql fd and vio-sd disagree - library mismatch, unsupported transport or API changes?"); |
|
|
249 | #if MYSQL_VERSION_ID < 100010 && !defined(MARIADB_BASE_VERSION) |
|
|
250 | if (vio->vioclose != vio_close) |
|
|
251 | croak ("vio.vioclose has unexpected content - library mismatch, unsupported transport or API changes?"); |
135 | |
252 | |
136 | if (vio->write != vio_write) |
253 | if (vio->write != vio_write) |
137 | croak ("vio.write has unexpected content - library mismatch, unsupported transport or API changes?"); |
254 | croak ("vio.write has unexpected content - library mismatch, unsupported transport or API changes?"); |
138 | |
255 | |
139 | if (vio->read != vio_read && vio->read != vio_read_buff) |
256 | if (vio->read != vio_read |
|
|
257 | && vio->read != vio_read_buff) |
140 | croak ("vio.read has unexpected content - library mismatch, unsupported transport or API changes?"); |
258 | croak ("vio.read has unexpected content - library mismatch, unsupported transport or API changes?"); |
|
|
259 | #endif |
141 | |
260 | |
142 | Newz (0, our, 1, ourdata); |
261 | Newz (0, our, 1, ourdata); |
143 | our->magic = CoMy_MAGIC; |
262 | our->magic = CoMy_MAGIC; |
|
|
263 | our->corohandle_sv = newSVsv (corohandle_sv); |
144 | our->corosocket = newSVsv (corosocket); |
264 | our->corohandle = newSVsv (corohandle); |
145 | |
265 | #if HAVE_EV |
|
|
266 | if (use_ev) |
|
|
267 | { |
|
|
268 | ev_io_init (&(our->rw), iocb, vio->sd, EV_READ); |
|
|
269 | ev_io_init (&(our->ww), iocb, vio->sd, EV_WRITE); |
|
|
270 | } |
|
|
271 | #endif |
|
|
272 | #if DESC_IS_PTR |
|
|
273 | our->old_desc = vio->desc; |
|
|
274 | strncpy (our->desc, vio->desc, sizeof (our->desc)); |
|
|
275 | our->desc [sizeof (our->desc) - 1] = 0; |
|
|
276 | #else |
146 | vio->desc [DESC_OFFSET - 1] = 0; |
277 | vio->desc [DESC_OFFSET - 1] = 0; |
|
|
278 | #endif |
147 | OURDATAPTR = our; |
279 | OURDATAPTR = our; |
148 | |
280 | |
|
|
281 | our->old_vioclose = vio->vioclose; |
|
|
282 | our->old_write = vio->write; |
|
|
283 | our->old_read = vio->read; |
|
|
284 | |
|
|
285 | vio->vioclose = our_close; |
149 | vio->write = our_write; |
286 | vio->write = our_write; |
150 | vio->read = our_read; |
287 | vio->read = our_read; |
151 | } |
288 | } |
152 | |
289 | |
153 | void |
|
|
154 | _unpatch (IV sock) |
|
|
155 | CODE: |
|
|
156 | if (IN_DESTRUCT) |
|
|
157 | { |
|
|
158 | /* we currently leak data during global destruction */ |
|
|
159 | /* perl makes it extremely hard to do otherwise, though */ |
|
|
160 | MYSQL *my = (MYSQL *)sock; |
|
|
161 | Vio *vio = my->net.vio; |
|
|
162 | my_bool dummy; |
|
|
163 | |
|
|
164 | if (vio->read != our_read) |
|
|
165 | croak ("vio.read has unexpected content during unpatch - wtf?"); |
|
|
166 | |
|
|
167 | if (vio->write != our_write) |
|
|
168 | croak ("vio.write has unexpected content during unpatch - wtf?"); |
|
|
169 | |
|
|
170 | SvREFCNT_dec (OURDATAPTR->corosocket); |
|
|
171 | |
|
|
172 | Safefree (OURDATAPTR); |
|
|
173 | |
|
|
174 | vio->read = vio_read; |
|
|
175 | vio->write = vio_write; |
|
|
176 | } |
|
|
177 | |
|
|
178 | |
|
|
179 | |
|
|