-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
php_uv.c
6497 lines (5357 loc) · 166 KB
/
php_uv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Shuhei Tanuma <chobieeee@php.net> |
| Bob Weinand <bobwei9@hotmail.com> |
+----------------------------------------------------------------------+
*/
#pragma GCC diagnostic ignored "-Wmissing-braces"
#include "php_uv.h"
#include "php_main.h"
#include "ext/standard/info.h"
#include "zend_smart_str.h"
#ifndef PHP_UV_DEBUG
#define PHP_UV_DEBUG 0
#endif
#if defined(ZTS) && PHP_VERSION_ID < 80000
#undef TSRMLS_C
#undef TSRMLS_CC
#undef TSRMLS_D
#undef TSRMLS_DC
#define TSRMLS_C tsrm_ls
#define TSRMLS_CC , TSRMLS_C
#define TSRMLS_D void *tsrm_ls
#define TSRMLS_DC , TSRMLS_D
#ifdef COMPILE_DL_UV
ZEND_TSRMLS_CACHE_DEFINE()
#endif
#endif
ZEND_DECLARE_MODULE_GLOBALS(uv);
#ifndef GC_ADDREF
#define GC_ADDREF(ref) ++GC_REFCOUNT(ref)
#endif
#if PHP_VERSION_ID < 70100
#define uv_zend_wrong_parameter_class_error(throw, ...) zend_wrong_paramer_class_error(__VA_ARGS__)
#elif PHP_VERSION_ID < 70200 || PHP_VERSION_ID >= 70300
#define uv_zend_wrong_parameter_class_error(throw, ...) zend_wrong_parameter_class_error(__VA_ARGS__)
#else
#define uv_zend_wrong_parameter_class_error(...) zend_wrong_parameter_class_error(__VA_ARGS__)
#endif
#if PHP_VERSION_ID < 70200
#define UV_PARAM_PROLOGUE Z_PARAM_PROLOGUE(0)
#else
#define UV_PARAM_PROLOGUE Z_PARAM_PROLOGUE(0, 0)
#endif
#if PHP_VERSION_ID < 70400
#define _error_code error_code
#endif
#if PHP_VERSION_ID >= 80000
#define zend_internal_type_error(strict_types, ...) zend_type_error(__VA_ARGS__)
#endif
#define UV_PARAM_OBJ_EX(dest, type, check_null, ce, ...) \
{ \
zval *zv; \
UV_PARAM_PROLOGUE \
if (UNEXPECTED(!uv_parse_arg_object(_arg, &zv, check_null, ce, ##__VA_ARGS__, NULL))) { \
if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
zend_string *names = php_uv_concat_ce_names(ce, ##__VA_ARGS__, NULL); \
uv_zend_wrong_parameter_class_error(_flags & ZEND_PARSE_PARAMS_THROW, _i, ZSTR_VAL(names), _arg); \
zend_string_release(names); \
} \
_error_code = ZPP_ERROR_FAILURE; \
break; \
} \
if (GC_FLAGS(Z_OBJ_P(zv)) & IS_OBJ_DESTRUCTOR_CALLED) { \
if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
php_error_docref(NULL, E_WARNING, "passed %s handle is already closed", ZSTR_VAL(Z_OBJCE_P(_arg)->name)); \
} \
_error_code = ZPP_ERROR_FAILURE; \
break; \
} \
dest = zv == NULL ? NULL : (type *) Z_OBJ_P(zv); \
}
#define UV_PARAM_OBJ(dest, type, ...) UV_PARAM_OBJ_EX(dest, type, 0, ##__VA_ARGS__, NULL)
#define UV_PARAM_OBJ_NULL(dest, type, ...) UV_PARAM_OBJ_EX(dest, type, 1, ##__VA_ARGS__, NULL)
static ZEND_COLD zend_string *php_uv_concat_ce_names(zend_class_entry *ce, zend_class_entry *next, ...) {
va_list va;
smart_str buf = {0};
va_start(va, next);
if (!next) {
return zend_string_copy(ce->name);
}
goto start;
do {
if (next) {
smart_str_appends(&buf, ", ");
} else {
smart_str_appends(&buf, " or ");
}
start:
smart_str_append(&buf, ce->name);
ce = next;
next = (zend_class_entry *) va_arg(va, zend_class_entry *);
} while (next);
va_end(va);
smart_str_0(&buf);
return buf.s;
}
/* gcc complains: sorry, unimplemented: function ‘uv_parse_arg_object’ can never be inlined because it uses variable argument lists */
#ifdef __clang__
static zend_always_inline int uv_parse_arg_object(zval *arg, zval **dest, int check_null, zend_class_entry *ce, ...) {
#else
static int uv_parse_arg_object(zval *arg, zval **dest, int check_null, zend_class_entry *ce, ...) {
#endif
if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
va_list va;
zend_class_entry *argce = Z_OBJCE_P(arg);
va_start(va, ce);
do {
if (instanceof_function(argce, ce)) {
*dest = arg;
return 1;
}
ce = (zend_class_entry *) va_arg(va, zend_class_entry *);
} while (ce);
} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
*dest = NULL;
return 1;
}
return 0;
}
#define PHP_UV_DEINIT_UV(uv) \
clean_uv_handle(uv); \
OBJ_RELEASE(&uv->std);
#define PHP_UV_INIT_GENERIC(dest, type, ce) \
do { \
zval zv; \
object_init_ex(&zv, ce); \
dest = (type *) Z_OBJ(zv); \
} while (0)
#define PHP_UV_INIT_UV(uv, ce) PHP_UV_INIT_GENERIC(uv, php_uv_t, ce)
#define PHP_UV_INIT_UV_EX(_uv, ce, cb, ...) \
do { \
int r; \
PHP_UV_INIT_UV(_uv, ce); \
r = cb(&loop->loop, (void *) &_uv->uv.handle, ##__VA_ARGS__); \
if (r) { \
PHP_UV_DEINIT_UV(_uv); \
php_error_docref(NULL, E_WARNING, #cb " failed"); \
RETURN_FALSE; \
} \
} while (0)
#define PHP_UV_INIT_CONNECT(req, uv) \
req = (uv_connect_t *) emalloc(sizeof(uv_connect_t)); \
req->data = uv;
#define PHP_UV_INIT_WRITE_REQ(w, uv, str, strlen, cb) \
w = (write_req_t *) emalloc(sizeof(write_req_t)); \
w->req.data = uv; \
w->buf = uv_buf_init(estrndup(str, strlen), strlen); \
w->cb = cb; \
#define PHP_UV_INIT_SEND_REQ(w, uv, str, strlen) \
w = (send_req_t *) emalloc(sizeof(send_req_t)); \
w->req.data = uv; \
w->buf = uv_buf_init(estrndup(str, strlen), strlen); \
#define PHP_UV_FETCH_UV_DEFAULT_LOOP(loop) \
if (loop == NULL) { \
loop = php_uv_default_loop(); \
} \
#define PHP_UV_INIT_LOCK(lock, lock_type) \
PHP_UV_INIT_GENERIC(lock, php_uv_lock_t, uv_lock_ce); \
lock->type = lock_type;
#define PHP_UV_CHECK_VALID_FD(fd, zstream) \
if (fd < 0) { \
php_error_docref(NULL, E_WARNING, "invalid variable passed. can't convert to fd."); \
PHP_UV_DEINIT_UV(uv); \
RETURN_FALSE; \
} \
if (Z_ISUNDEF(uv->fs_fd)) { \
ZVAL_COPY(&uv->fs_fd, zstream); \
}
#define PHP_UV_ZVAL_TO_VALID_POLL_FD(fd, zstream) \
{ \
fd = php_uv_zval_to_valid_poll_fd(zstream); \
PHP_UV_CHECK_VALID_FD(fd, zstream) \
}
#define PHP_UV_ZVAL_TO_FD(fd, zstream) \
{ \
fd = php_uv_zval_to_fd(zstream); \
PHP_UV_CHECK_VALID_FD(fd, zstream) \
}
#define PHP_UV_FS_ASYNC(loop, func, ...) \
error = uv_fs_##func(&loop->loop, (uv_fs_t*)&uv->uv.fs, __VA_ARGS__, php_uv_fs_cb); \
if (error) { \
PHP_UV_DEINIT_UV(uv); \
php_error_docref(NULL, E_WARNING, "uv_" #func " failed"); \
return; \
}
#define PHP_UV_INIT_ZVALS(uv) \
{ \
int ix = 0;\
for (ix = 0; ix < PHP_UV_CB_MAX; ix++) {\
uv->callback[ix] = NULL;\
} \
ZVAL_UNDEF(&uv->fs_fd); \
ZVAL_UNDEF(&uv->fs_fd_alt); \
}
#if PHP_VERSION_ID < 70300
#define PHP_UV_SKIP_DTOR(uv) do { GC_FLAGS(&uv->std) |= IS_OBJ_DESTRUCTOR_CALLED; } while (0)
#else
#define PHP_UV_SKIP_DTOR(uv) do { GC_ADD_FLAGS(&uv->std, IS_OBJ_DESTRUCTOR_CALLED); } while (0)
#endif
#define PHP_UV_IS_DTORED(uv) (GC_FLAGS(&uv->std) & IS_OBJ_DESTRUCTOR_CALLED)
#define PHP_UV_SOCKADDR_IPV4_INIT(sockaddr) PHP_UV_INIT_GENERIC(sockaddr, php_uv_sockaddr_t, uv_sockaddr_ipv4_ce);
#define PHP_UV_SOCKADDR_IPV6_INIT(sockaddr) PHP_UV_INIT_GENERIC(sockaddr, php_uv_sockaddr_t, uv_sockaddr_ipv6_ce);
#define PHP_UV_SOCKADDR_IS_IPV4(sockaddr) (sockaddr->std.ce == uv_sockaddr_ipv4_ce)
#define PHP_UV_SOCKADDR_IS_IPV6(sockaddr) (sockaddr->std.ce == uv_sockaddr_ipv6_ce)
#define PHP_UV_SOCKADDR_IPV4(sockaddr) sockaddr->addr.ipv4
#define PHP_UV_SOCKADDR_IPV4_P(sockaddr) &sockaddr->addr.ipv4
#define PHP_UV_SOCKADDR_IPV6(sockaddr) sockaddr->addr.ipv6
#define PHP_UV_SOCKADDR_IPV6_P(sockaddr) &sockaddr->addr.ipv6
#define PHP_UV_LOCK_RWLOCK_P(_lock) &_lock->lock.rwlock
#define PHP_UV_LOCK_MUTEX_P(_lock) &_lock->lock.mutex
#define PHP_UV_LOCK_SEM_P(_lock) &_lock->lock.semaphore
#define PHP_UV_FD_TO_ZVAL(zv, fd) { php_stream *_stream = php_stream_fopen_from_fd(fd, "w+", NULL); zval *_z = (zv); php_stream_to_zval(_stream, _z); }
#if PHP_UV_DEBUG>=1
#define PHP_UV_DEBUG_PRINT(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
#else
#define PHP_UV_DEBUG_PRINT(format, ...)
#endif
#if PHP_UV_DEBUG>=1
#define PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(handler, uv) \
{ \
PHP_UV_DEBUG_PRINT("# %s add(%p - %s): %u->%u\n", #handler, uv, ZSTR_VAL(uv->std.ce->name), GC_REFCOUNT(&(uv)->std) - 1, GC_REFCOUNT(&(uv)->std)); \
}
#define PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(handler, uv) \
{ \
PHP_UV_DEBUG_PRINT("# %s del(%p - %s): %u->%u\n", #handler, uv, ZSTR_VAL(uv->std.ce->name), GC_REFCOUNT(&(uv)->std), GC_REFCOUNT(&(uv)->std) - 1); \
}
#else
#define PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(hander, uv)
#define PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(hander, uv)
#endif
#if defined(ZTS) && PHP_VERSION_ID < 80000
#define UV_FETCH_ALL(ls, id, type) ((type) (*((void ***) ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])
#define UV_FETCH_CTX(ls, id, type, element) (((type) (*((void ***) ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
#define UV_CG(ls, v) UV_FETCH_CTX(ls, compiler_globals_id, zend_compiler_globals*, v)
#define UV_CG_ALL(ls) UV_FETCH_ALL(ls, compiler_globals_id, zend_compiler_globals*)
#define UV_EG(ls, v) UV_FETCH_CTX(ls, executor_globals_id, zend_executor_globals*, v)
#define UV_SG(ls, v) UV_FETCH_CTX(ls, sapi_globals_id, sapi_globals_struct*, v)
#define UV_EG_ALL(ls) UV_FETCH_ALL(ls, executor_globals_id, zend_executor_globals*)
#endif
#if !defined(PHP_WIN32) && !(defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
# if PHP_VERSION_ID >= 80000
__attribute__((weak)) zend_class_entry *socket_ce = NULL;
# else
int (*php_sockets_le_socket_ptr)(void) = NULL;
int php_sockets_le_socket(void) __attribute__((weak));
# endif
#endif
/* objects */
extern void php_uv_init(zend_class_entry *uv_ce);
static zend_object_handlers uv_default_handlers;
static zend_class_entry *uv_ce;
static zend_object_handlers uv_handlers;
static zend_class_entry *uv_stream_ce;
static zend_class_entry *uv_tcp_ce;
static zend_class_entry *uv_udp_ce;
static zend_class_entry *uv_pipe_ce;
static zend_class_entry *uv_idle_ce;
static zend_class_entry *uv_timer_ce;
static zend_class_entry *uv_async_ce;
static zend_class_entry *uv_addrinfo_ce;
static zend_class_entry *uv_process_ce;
static zend_class_entry *uv_prepare_ce;
static zend_class_entry *uv_check_ce;
static zend_class_entry *uv_work_ce;
static zend_class_entry *uv_fs_ce;
static zend_class_entry *uv_fs_event_ce;
static zend_class_entry *uv_tty_ce;
static zend_class_entry *uv_fs_poll_ce;
static zend_class_entry *uv_poll_ce;
static zend_class_entry *uv_signal_ce;
static zend_class_entry *uv_loop_ce;
static zend_object_handlers uv_loop_handlers;
static zend_class_entry *uv_sockaddr_ce;
static zend_class_entry *uv_sockaddr_ipv4_ce;
static zend_class_entry *uv_sockaddr_ipv6_ce;
static zend_class_entry *uv_lock_ce;
static zend_object_handlers uv_lock_handlers;
static zend_class_entry *uv_stdio_ce;
static zend_object_handlers uv_stdio_handlers;
typedef struct {
uv_write_t req;
uv_buf_t buf;
php_uv_cb_t *cb;
} write_req_t;
typedef struct {
uv_udp_send_t req;
uv_buf_t buf;
} send_req_t;
enum php_uv_socket_type {
PHP_UV_TCP_IPV4 = 1,
PHP_UV_TCP_IPV6 = 2,
PHP_UV_TCP = 3,
PHP_UV_UDP_IPV4 = 16,
PHP_UV_UDP_IPV6 = 32,
PHP_UV_UDP = 48,
};
/* declarations */
static void php_uv_fs_cb(uv_fs_t* req);
/**
* execute callback
*
* @param zval* retval_ptr non-initialized pointer. this will be allocate from zend_call_function
* @param php_uv_cb_t* callback callable object
* @param zval* params parameters.
* @param int param_count
* @return int (maybe..)
*/
static int php_uv_do_callback(zval *retval_ptr, php_uv_cb_t *callback, zval *params, int param_count TSRMLS_DC);
void static destruct_uv(zend_object *obj);
void static clean_uv_handle(php_uv_t *uv);
static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status);
static void php_uv_write_cb(uv_write_t* req, int status);
static void php_uv_listen_cb(uv_stream_t* server, int status);
static void php_uv_shutdown_cb(uv_shutdown_t* req, int status);
static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread,const uv_buf_t* buf);
/* unused: static void php_uv_read2_cb(uv_pipe_t* handle, ssize_t nread, uv_buf_t buf, uv_handle_type pending); */
static void php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
static void php_uv_close(php_uv_t *uv);
static void php_uv_timer_cb(uv_timer_t *handle);
static void php_uv_idle_cb(uv_timer_t *handle);
static void php_uv_signal_cb(uv_signal_t *handle, int sig_num);
static php_uv_loop_t *php_uv_default_loop()
{
if (UV_G(default_loop) == NULL) {
zval zv;
object_init_ex(&zv, uv_loop_ce);
UV_G(default_loop) = (php_uv_loop_t *) Z_OBJ(zv);
}
return UV_G(default_loop);
}
static php_socket_t php_uv_zval_to_valid_poll_fd(zval *ptr)
{
php_socket_t fd = -1;
php_stream *stream;
/* Validate Checks */
#if !defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
php_socket *socket;
#endif
/* TODO: is this correct on windows platform? */
if (Z_TYPE_P(ptr) == IS_RESOURCE) {
if ((stream = (php_stream *) zend_fetch_resource_ex(ptr, NULL, php_file_le_stream()))) {
/* make sure only valid resource streams are passed - plainfiles and most php streams are invalid */
if (stream->wrapper && !strcmp((char *)stream->wrapper->wops->label, "PHP") && (!stream->orig_path || (strncmp(stream->orig_path, "php://std", sizeof("php://std") - 1) && strncmp(stream->orig_path, "php://fd", sizeof("php://fd") - 1)))) {
php_error_docref(NULL, E_WARNING, "invalid resource passed, this resource is not supported");
return -1;
}
/* Some streams (specifically STDIO and encrypted streams) can be cast to FDs */
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) == SUCCESS && fd >= 0) {
if (stream->wrapper && !strcmp((char *)stream->wrapper->wops->label, "plainfile")) {
#ifndef PHP_WIN32
struct stat stat;
fstat(fd, &stat);
if (!S_ISFIFO(stat.st_mode))
#endif
{
php_error_docref(NULL, E_WARNING, "invalid resource passed, this plain files are not supported");
return -1;
}
}
return fd;
}
fd = -1;
#if PHP_VERSION_ID < 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
} else if (php_sockets_le_socket_ptr && (socket = (php_socket *) zend_fetch_resource_ex(ptr, NULL, php_sockets_le_socket_ptr()))) {
fd = socket->bsd_socket;
#endif
} else {
php_error_docref(NULL, E_WARNING, "unhandled resource type detected.");
fd = -1;
}
#if PHP_VERSION_ID >= 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
} else if (socket_ce && Z_TYPE_P(ptr) == IS_OBJECT && Z_OBJCE_P(ptr) == socket_ce && (socket = (php_socket *) ((char *)(Z_OBJ_P(ptr)) - XtOffsetOf(php_socket, std)))) {
fd = socket->bsd_socket;
#endif
}
return fd;
}
static php_socket_t php_uv_zval_to_fd(zval *ptr)
{
php_socket_t fd = -1;
php_stream *stream;
#if !defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
php_socket *socket;
#endif
/* TODO: is this correct on windows platform? */
if (Z_TYPE_P(ptr) == IS_RESOURCE) {
if ((stream = (php_stream *) zend_fetch_resource_ex(ptr, NULL, php_file_le_stream()))) {
if (php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void *) &fd, 1) != SUCCESS || fd < 0) {
fd = -1;
}
#if PHP_VERSION_ID < 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
} else if (php_sockets_le_socket_ptr && (socket = (php_socket *) zend_fetch_resource_ex(ptr, NULL, php_sockets_le_socket_ptr()))) {
fd = socket->bsd_socket;
#endif
} else {
php_error_docref(NULL, E_WARNING, "unhandled resource type detected.");
fd = -1;
}
} else if (Z_TYPE_P(ptr) == IS_LONG) {
fd = Z_LVAL_P(ptr);
if (fd < 0) {
fd = -1;
}
{
/* make sure that a valid resource handle was passed - issue #36 */
int err = uv_guess_handle((uv_file) fd);
if (err == UV_UNKNOWN_HANDLE) {
php_error_docref(NULL, E_WARNING, "invalid resource type detected");
fd = -1;
}
}
#if PHP_VERSION_ID >= 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
} else if (socket_ce && Z_TYPE_P(ptr) == IS_OBJECT && Z_OBJCE_P(ptr) == socket_ce && (socket = (php_socket *) ((char *)(Z_OBJ_P(ptr)) - XtOffsetOf(php_socket, std)))) {
fd = socket->bsd_socket;
#endif
}
return fd;
}
static const char* php_uv_strerror(long error_code)
{
/* Note: uv_strerror doesn't use assert. we don't need check value here */
return uv_strerror(error_code);
}
static php_uv_cb_t* php_uv_cb_init_dynamic(php_uv_t *uv, zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
php_uv_cb_t *cb = emalloc(sizeof(php_uv_cb_t));
memcpy(&cb->fci, fci, sizeof(zend_fcall_info));
memcpy(&cb->fcc, fcc, sizeof(zend_fcall_info_cache));
if (ZEND_FCI_INITIALIZED(*fci)) {
Z_TRY_ADDREF(cb->fci.function_name);
if (fci->object) {
GC_ADDREF(cb->fci.object);
}
}
return cb;
}
static void php_uv_cb_init(php_uv_cb_t **result, php_uv_t *uv, zend_fcall_info *fci, zend_fcall_info_cache *fcc, enum php_uv_callback_type type)
{
php_uv_cb_t *cb;
if (uv->callback[type] == NULL) {
cb = emalloc(sizeof(php_uv_cb_t));
} else {
cb = uv->callback[type];
if (Z_TYPE(cb->fci.function_name) != IS_UNDEF) {
zval_dtor(&cb->fci.function_name);
}
if (fci->object) {
OBJ_RELEASE(fci->object);
}
}
memcpy(&cb->fci, fci, sizeof(zend_fcall_info));
memcpy(&cb->fcc, fcc, sizeof(zend_fcall_info_cache));
if (ZEND_FCI_INITIALIZED(*fci)) {
Z_TRY_ADDREF(cb->fci.function_name);
if (fci->object) {
GC_ADDREF(cb->fci.object);
}
}
uv->callback[type] = cb;
}
static void php_uv_lock_init(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
{
php_uv_lock_t *lock = NULL;
int error = 0;
switch (lock_type) {
case IS_UV_RWLOCK:
case IS_UV_RWLOCK_WR:
case IS_UV_RWLOCK_RD:
{
PHP_UV_INIT_LOCK(lock, IS_UV_RWLOCK);
error = uv_rwlock_init(PHP_UV_LOCK_RWLOCK_P(lock));
}
break;
case IS_UV_MUTEX:
{
PHP_UV_INIT_LOCK(lock, IS_UV_MUTEX);
error = uv_mutex_init(PHP_UV_LOCK_MUTEX_P(lock));
}
break;
case IS_UV_SEMAPHORE:
{
zend_long val = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"l", &val) == FAILURE) {
return;
}
PHP_UV_INIT_LOCK(lock, IS_UV_SEMAPHORE);
error = uv_sem_init(PHP_UV_LOCK_SEM_P(lock), (int) val);
}
break;
default:
php_error_docref(NULL, E_ERROR, "unexpected type");
break;
}
if (error == 0) {
RETURN_OBJ(&lock->std);
} else {
OBJ_RELEASE(&lock->std);
RETURN_FALSE;
}
}
static void php_uv_lock_lock(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
{
php_uv_lock_t *lock;
ZEND_PARSE_PARAMETERS_START(1, 1)
UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
ZEND_PARSE_PARAMETERS_END();
switch (lock_type) {
case IS_UV_RWLOCK:
case IS_UV_RWLOCK_RD:
{
if (lock->locked == 0x01) {
zend_error(E_WARNING, "Cannot acquire a read lock while holding a write lock");
RETURN_FALSE;
}
uv_rwlock_rdlock(PHP_UV_LOCK_RWLOCK_P(lock));
if (!lock->locked++) {
lock->locked = 0x02;
}
}
break;
case IS_UV_RWLOCK_WR:
{
if (lock->locked) {
zend_error(E_WARNING, "Cannot acquire a write lock when already holding a lock");
RETURN_FALSE;
}
uv_rwlock_wrlock(PHP_UV_LOCK_RWLOCK_P(lock));
lock->locked = 0x01;
}
break;
case IS_UV_MUTEX:
{
uv_mutex_lock(PHP_UV_LOCK_MUTEX_P(lock));
lock->locked = 0x01;
}
break;
case IS_UV_SEMAPHORE:
{
uv_sem_post(PHP_UV_LOCK_SEM_P(lock));
}
break;
default:
php_error_docref(NULL, E_ERROR, "unexpected type");
break;
}
}
static void php_uv_lock_unlock(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
{
php_uv_lock_t *lock;
ZEND_PARSE_PARAMETERS_START(1, 1)
UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
ZEND_PARSE_PARAMETERS_END();
switch (lock_type) {
case IS_UV_RWLOCK:
case IS_UV_RWLOCK_RD:
{
if (lock->locked > 0x01) {
uv_rwlock_rdunlock(PHP_UV_LOCK_RWLOCK_P(lock));
if (--lock->locked == 0x01) {
lock->locked = 0x00;
}
}
}
break;
case IS_UV_RWLOCK_WR:
{
if (lock->locked == 0x01) {
uv_rwlock_wrunlock(PHP_UV_LOCK_RWLOCK_P(lock));
lock->locked = 0x00;
}
}
break;
case IS_UV_MUTEX:
{
if (lock->locked == 0x01) {
uv_mutex_unlock(PHP_UV_LOCK_MUTEX_P(lock));
lock->locked = 0x00;
}
}
break;
case IS_UV_SEMAPHORE:
{
uv_sem_wait(PHP_UV_LOCK_SEM_P(lock));
}
break;
default:
php_error_docref(NULL, E_ERROR, "unexpected type");
break;
}
}
static void php_uv_lock_trylock(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
{
php_uv_lock_t *lock;
int error = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
ZEND_PARSE_PARAMETERS_END();
switch(lock_type) {
case IS_UV_RWLOCK:
case IS_UV_RWLOCK_RD:
{
if (lock->locked == 0x01) {
zend_error(E_WARNING, "Cannot acquire a read lock while holding a write lock");
RETURN_FALSE;
}
error = uv_rwlock_tryrdlock(PHP_UV_LOCK_RWLOCK_P(lock));
if (error == 0) {
if (!lock->locked++) {
lock->locked = 0x02;
}
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
break;
case IS_UV_RWLOCK_WR:
{
if (lock->locked) {
zend_error(E_WARNING, "Cannot acquire a write lock when already holding a lock");
RETURN_FALSE;
}
error = uv_rwlock_trywrlock(PHP_UV_LOCK_RWLOCK_P(lock));
if (error == 0) {
lock->locked = 0x01;
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
break;
case IS_UV_MUTEX:
{
error = uv_mutex_trylock(PHP_UV_LOCK_MUTEX_P(lock));
if (error == 0) {
lock->locked = 0x01;
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
break;
case IS_UV_SEMAPHORE:
{
error = uv_sem_trywait(PHP_UV_LOCK_SEM_P(lock));
RETURN_LONG(error);
}
default:
php_error_docref(NULL, E_ERROR, "unexpected type");
break;
}
}
static void php_uv_fs_common(uv_fs_type fs_type, INTERNAL_FUNCTION_PARAMETERS)
{
int error = 0;
php_uv_loop_t *loop;
php_uv_t *uv;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
php_uv_cb_t *cb;
#define PHP_UV_FS_PARSE_PARAMETERS_EX(num, params, required_cb) \
ZEND_PARSE_PARAMETERS_START(1 + num + required_cb, 2 + num) \
UV_PARAM_OBJ(loop, php_uv_loop_t, uv_loop_ce) \
params \
if (!required_cb) { \
Z_PARAM_OPTIONAL \
} \
Z_PARAM_FUNC_EX(fci, fcc, 1, 0) \
ZEND_PARSE_PARAMETERS_END()
#define PHP_UV_FS_PARSE_PARAMETERS(num, params) PHP_UV_FS_PARSE_PARAMETERS_EX(num, params, 0)
#define PHP_UV_FS_SETUP() \
PHP_UV_INIT_UV(uv, uv_fs_ce); \
PHP_UV_FETCH_UV_DEFAULT_LOOP(loop); \
php_uv_cb_init(&cb, uv, &fci, &fcc, PHP_UV_FS_CB);
#define PHP_UV_FS_SETUP_AND_EXECUTE(command, ...) \
PHP_UV_FS_SETUP(); \
PHP_UV_FS_ASYNC(loop, command, __VA_ARGS__);
switch (fs_type) {
case UV_FS_SYMLINK:
{
zend_string *from, *to;
zend_long flags;
PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_STR(from) Z_PARAM_STR(to) Z_PARAM_LONG(flags));
PHP_UV_FS_SETUP_AND_EXECUTE(symlink, from->val, to->val, flags);
break;
}
case UV_FS_LINK:
{
zend_string *from, *to;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(from) Z_PARAM_STR(to));
PHP_UV_FS_SETUP_AND_EXECUTE(link, from->val, to->val);
break;
}
case UV_FS_CHMOD:
{
zend_long mode;
zend_string *path;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(path) Z_PARAM_LONG(mode));
PHP_UV_FS_SETUP_AND_EXECUTE(chmod, path->val, mode);
break;
}
case UV_FS_FCHMOD:
{
zval *zstream;
zend_long mode;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(mode));
PHP_UV_FS_SETUP();
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, fchmod, fd, mode);
break;
}
case UV_FS_RENAME:
{
zend_string *from, *to;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(from) Z_PARAM_STR(to));
PHP_UV_FS_SETUP_AND_EXECUTE(rename, from->val, to->val);
break;
}
case UV_FS_UNLINK:
{
zend_string *path;
PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_STR(path));
PHP_UV_FS_SETUP_AND_EXECUTE(unlink, path->val);
break;
}
case UV_FS_RMDIR:
{
zend_string *path;
PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_STR(path));
PHP_UV_FS_SETUP_AND_EXECUTE(rmdir, path->val);
break;
}
case UV_FS_MKDIR:
{
zend_string *path;
zend_long mode;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(path) Z_PARAM_LONG(mode));
PHP_UV_FS_SETUP_AND_EXECUTE(mkdir, path->val, mode);
break;
}
case UV_FS_FTRUNCATE:
{
zval *zstream = NULL;
zend_long offset = 0;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(offset));
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, ftruncate, fd, offset);
break;
}
case UV_FS_FDATASYNC:
{
zval *zstream = NULL;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, fdatasync, fd);
break;
}
case UV_FS_FSYNC:
{
zval *zstream = NULL;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, fsync, fd);
break;
}
case UV_FS_CLOSE:
{
zval *zstream = NULL;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, close, fd);
break;
}
case UV_FS_CHOWN:
{
zend_long uid, gid;
zend_string *path;
PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_STR(path) Z_PARAM_LONG(uid) Z_PARAM_LONG(gid));
PHP_UV_FS_SETUP_AND_EXECUTE(chown, path->val, uid, gid);
break;
}
case UV_FS_FCHOWN:
{
zval *zstream = NULL;
zend_long uid, gid;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(uid) Z_PARAM_LONG(gid));
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, fchown, fd, uid, gid);
break;
}
case UV_FS_OPEN:
{
zend_string *path;
zend_long flag, mode;
PHP_UV_FS_PARSE_PARAMETERS_EX(3, Z_PARAM_STR(path) Z_PARAM_LONG(flag) Z_PARAM_LONG(mode), 1);
PHP_UV_FS_SETUP_AND_EXECUTE(open, path->val, flag, mode);
break;
}
case UV_FS_SCANDIR:
{
zend_string *path;
zend_long flags = 0;
ZEND_PARSE_PARAMETERS_START(3, 4)
UV_PARAM_OBJ(loop, php_uv_loop_t, uv_loop_ce)
Z_PARAM_STR(path)
Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(flags)
ZEND_PARSE_PARAMETERS_END();
PHP_UV_FS_SETUP_AND_EXECUTE(scandir, path->val, flags);
break;
}
case UV_FS_LSTAT:
{
zend_string *path;
PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_STR(path), 1);
PHP_UV_FS_SETUP_AND_EXECUTE(lstat, path->val);
break;
}
case UV_FS_FSTAT:
{
zval *zstream = NULL;
unsigned long fd;
PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_RESOURCE(zstream), 1);
PHP_UV_FS_SETUP()
PHP_UV_ZVAL_TO_FD(fd, zstream);
uv->fs_fd = *zstream;
Z_ADDREF(uv->fs_fd);
PHP_UV_FS_ASYNC(loop, fstat, fd);