- Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathngx_http_lua_pipe.c
2576 lines (2020 loc) · 72.3 KB
/
ngx_http_lua_pipe.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
/*
* Copyright (C) by OpenResty Inc.
*/
#ifndefDDEBUG
#defineDDEBUG 0
#endif
#include"ddebug.h"
#include"ngx_http_lua_common.h"
#include"ngx_http_lua_input_filters.h"
#include"ngx_http_lua_util.h"
#include"ngx_http_lua_pipe.h"
#if (NGX_HTTP_LUA_HAVE_SIGNALFD)
#include<sys/signalfd.h>
#endif
#ifdefHAVE_NGX_LUA_PIPE
staticngx_rbtree_node_t*ngx_http_lua_pipe_lookup_pid(ngx_rbtree_key_tkey);
#if !(NGX_HTTP_LUA_HAVE_SIGNALFD)
staticvoidngx_http_lua_pipe_sigchld_handler(intsigno, siginfo_t*siginfo,
void*ucontext);
#endif
staticvoidngx_http_lua_pipe_sigchld_event_handler(ngx_event_t*ev);
staticssize_tngx_http_lua_pipe_fd_read(ngx_connection_t*c, u_char*buf,
size_tsize);
staticssize_tngx_http_lua_pipe_fd_write(ngx_connection_t*c, u_char*buf,
size_tsize);
staticvoidngx_http_lua_pipe_close_helper(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx, ngx_event_t*ev);
staticvoidngx_http_lua_pipe_close_stdin(ngx_http_lua_pipe_t*pipe);
staticvoidngx_http_lua_pipe_close_stdout(ngx_http_lua_pipe_t*pipe);
staticvoidngx_http_lua_pipe_close_stderr(ngx_http_lua_pipe_t*pipe);
staticvoidngx_http_lua_pipe_proc_finalize(ngx_http_lua_ffi_pipe_proc_t*proc);
staticngx_int_tngx_http_lua_pipe_get_lua_ctx(ngx_http_request_t*r,
ngx_http_lua_ctx_t**ctx, u_char*errbuf, size_t*errbuf_size);
staticvoidngx_http_lua_pipe_put_error(ngx_http_lua_pipe_ctx_t*pipe_ctx,
u_char*errbuf, size_t*errbuf_size);
staticvoidngx_http_lua_pipe_put_data(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx, u_char**buf, size_t*buf_size);
staticngx_int_tngx_http_lua_pipe_add_input_buffer(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx);
staticngx_int_tngx_http_lua_pipe_read_all(void*data, ssize_tbytes);
staticngx_int_tngx_http_lua_pipe_read_bytes(void*data, ssize_tbytes);
staticngx_int_tngx_http_lua_pipe_read_line(void*data, ssize_tbytes);
staticngx_int_tngx_http_lua_pipe_read_any(void*data, ssize_tbytes);
staticngx_int_tngx_http_lua_pipe_read(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx);
staticngx_int_tngx_http_lua_pipe_init_ctx(
ngx_http_lua_pipe_ctx_t**pipe_ctx_pt, intfd, ngx_pool_t*pool,
u_char*errbuf, size_t*errbuf_size);
staticngx_int_tngx_http_lua_pipe_write(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx);
staticintngx_http_lua_pipe_read_stdout_retval(
ngx_http_lua_ffi_pipe_proc_t*proc, lua_State*L);
staticintngx_http_lua_pipe_read_stderr_retval(
ngx_http_lua_ffi_pipe_proc_t*proc, lua_State*L);
staticintngx_http_lua_pipe_read_retval_helper(
ngx_http_lua_ffi_pipe_proc_t*proc, lua_State*L, intfrom_stderr);
staticintngx_http_lua_pipe_write_retval(ngx_http_lua_ffi_pipe_proc_t*proc,
lua_State*L);
staticintngx_http_lua_pipe_wait_retval(ngx_http_lua_ffi_pipe_proc_t*proc,
lua_State*L);
staticvoidngx_http_lua_pipe_resume_helper(ngx_event_t*ev,
ngx_http_lua_co_ctx_t*wait_co_ctx);
staticvoidngx_http_lua_pipe_resume_read_stdout_handler(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_resume_read_stderr_handler(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_resume_write_handler(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_resume_wait_handler(ngx_event_t*ev);
staticngx_int_tngx_http_lua_pipe_resume(ngx_http_request_t*r);
staticvoidngx_http_lua_pipe_dummy_event_handler(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_clear_event(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_proc_read_stdout_cleanup(void*data);
staticvoidngx_http_lua_pipe_proc_read_stderr_cleanup(void*data);
staticvoidngx_http_lua_pipe_proc_write_cleanup(void*data);
staticvoidngx_http_lua_pipe_proc_wait_cleanup(void*data);
staticvoidngx_http_lua_pipe_reap_pids(ngx_event_t*ev);
staticvoidngx_http_lua_pipe_reap_timer_handler(ngx_event_t*ev);
voidngx_http_lua_ffi_pipe_proc_destroy(
ngx_http_lua_ffi_pipe_proc_t*proc);
staticngx_rbtree_tngx_http_lua_pipe_rbtree;
staticngx_rbtree_node_tngx_http_lua_pipe_proc_sentinel;
staticngx_event_tngx_reap_pid_event;
#if (NGX_HTTP_LUA_HAVE_SIGNALFD)
staticintngx_http_lua_signalfd;
staticstructsignalfd_siginfongx_http_lua_pipe_notification;
#definengx_http_lua_read_sigfd ngx_http_lua_signalfd
#else
staticintngx_http_lua_sigchldfd[2];
staticu_charngx_http_lua_pipe_notification[1];
#definengx_http_lua_read_sigfd ngx_http_lua_sigchldfd[0]
#definengx_http_lua_write_sigfd ngx_http_lua_sigchldfd[1]
#endif
staticngx_connection_t*ngx_http_lua_sigfd_conn=NULL;
/* The below signals are ignored by Nginx.
* We need to reset them for the spawned child processes. */
ngx_http_lua_pipe_signal_tngx_signals[] = {
{ SIGSYS, "SIGSYS" },
{ SIGPIPE, "SIGPIPE" },
{ 0, NULL }
};
enum {
PIPE_ERR_CLOSED=1,
PIPE_ERR_SYSCALL,
PIPE_ERR_NOMEM,
PIPE_ERR_TIMEOUT,
PIPE_ERR_ADD_READ_EV,
PIPE_ERR_ADD_WRITE_EV,
PIPE_ERR_ABORTED,
};
enum {
PIPE_READ_ALL=0,
PIPE_READ_BYTES,
PIPE_READ_LINE,
PIPE_READ_ANY,
};
#defineREASON_EXIT "exit"
#defineREASON_SIGNAL "signal"
#defineREASON_UNKNOWN "unknown"
#defineREASON_RUNNING_CODE 0
#defineREASON_EXIT_CODE 1
#defineREASON_SIGNAL_CODE 2
#defineREASON_UNKNOWN_CODE 3
void
ngx_http_lua_pipe_init(void)
{
ngx_rbtree_init(&ngx_http_lua_pipe_rbtree,
&ngx_http_lua_pipe_proc_sentinel, ngx_rbtree_insert_value);
}
ngx_int_t
ngx_http_lua_pipe_add_signal_handler(ngx_cycle_t*cycle)
{
ngx_event_t*rev;
#if (NGX_HTTP_LUA_HAVE_SIGNALFD)
sigset_tset;
#else
intrc;
structsigactionsa;
#endif
ngx_reap_pid_event.handler=ngx_http_lua_pipe_reap_timer_handler;
ngx_reap_pid_event.log=cycle->log;
ngx_reap_pid_event.data=cycle;
ngx_reap_pid_event.cancelable=1;
if (!ngx_reap_pid_event.timer_set) {
ngx_add_timer(&ngx_reap_pid_event, 1000);
}
#if (NGX_HTTP_LUA_HAVE_SIGNALFD)
if (sigemptyset(&set) !=0) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe init signal set failed");
returnNGX_ERROR;
}
if (sigaddset(&set, SIGCHLD) !=0) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe add SIGCHLD to signal set failed");
returnNGX_ERROR;
}
if (sigprocmask(SIG_BLOCK, &set, NULL) !=0) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe block SIGCHLD failed");
returnNGX_ERROR;
}
ngx_http_lua_signalfd=signalfd(-1, &set, SFD_NONBLOCK|SFD_CLOEXEC);
if (ngx_http_lua_signalfd<0) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe create signalfd instance failed");
returnNGX_ERROR;
}
#else/* !(NGX_HTTP_LUA_HAVE_SIGNALFD) */
# if (NGX_HTTP_LUA_HAVE_PIPE2)
rc=pipe2(ngx_http_lua_sigchldfd, O_NONBLOCK|O_CLOEXEC);
# else
rc=pipe(ngx_http_lua_sigchldfd);
# endif
if (rc==-1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe init SIGCHLD fd failed");
returnNGX_ERROR;
}
# if !(NGX_HTTP_LUA_HAVE_PIPE2)
if (ngx_nonblocking(ngx_http_lua_read_sigfd) ==-1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno, "lua pipe "
ngx_nonblocking_n" SIGCHLD read fd failed");
goto failed;
}
if (ngx_nonblocking(ngx_http_lua_write_sigfd) ==-1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno, "lua pipe "
ngx_nonblocking_n" SIGCHLD write fd failed");
goto failed;
}
/* it's ok not to set the pipe fd with O_CLOEXEC. This requires
* extra syscall */
# endif/* !(NGX_HTTP_LUA_HAVE_PIPE2) */
#endif/* NGX_HTTP_LUA_HAVE_SIGNALFD */
ngx_http_lua_sigfd_conn=ngx_get_connection(ngx_http_lua_read_sigfd,
cycle->log);
if (ngx_http_lua_sigfd_conn==NULL) {
goto failed;
}
ngx_http_lua_sigfd_conn->log=cycle->log;
ngx_http_lua_sigfd_conn->recv=ngx_http_lua_pipe_fd_read;
rev=ngx_http_lua_sigfd_conn->read;
rev->log=ngx_http_lua_sigfd_conn->log;
rev->handler=ngx_http_lua_pipe_sigchld_event_handler;
#ifdefHAVE_SOCKET_CLOEXEC_PATCH
rev->skip_socket_leak_check=1;
#endif
if (ngx_handle_read_event(rev, 0) ==NGX_ERROR) {
goto failed;
}
#if !(NGX_HTTP_LUA_HAVE_SIGNALFD)
ngx_memzero(&sa, sizeof(structsigaction));
sa.sa_sigaction=ngx_http_lua_pipe_sigchld_handler;
sa.sa_flags=SA_SIGINFO;
if (sigemptyset(&sa.sa_mask) !=0) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe init signal mask failed");
goto failed;
}
if (sigaction(SIGCHLD, &sa, NULL) ==-1) {
ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
"lua pipe sigaction(SIGCHLD) failed");
goto failed;
}
#endif
returnNGX_OK;
failed:
if (ngx_http_lua_sigfd_conn!=NULL) {
ngx_close_connection(ngx_http_lua_sigfd_conn);
ngx_http_lua_sigfd_conn=NULL;
}
if (close(ngx_http_lua_read_sigfd) ==-1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"lua pipe close the read sigfd failed");
}
#if !(NGX_HTTP_LUA_HAVE_SIGNALFD)
if (close(ngx_http_lua_write_sigfd) ==-1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"lua pipe close the write sigfd failed");
}
#endif
returnNGX_ERROR;
}
staticngx_rbtree_node_t*
ngx_http_lua_pipe_lookup_pid(ngx_rbtree_key_tkey)
{
ngx_rbtree_node_t*node, *sentinel;
node=ngx_http_lua_pipe_rbtree.root;
sentinel=ngx_http_lua_pipe_rbtree.sentinel;
while (node!=sentinel) {
if (key<node->key) {
node=node->left;
continue;
}
if (key>node->key) {
node=node->right;
continue;
}
returnnode;
}
returnNULL;
}
#if !(NGX_HTTP_LUA_HAVE_SIGNALFD)
staticvoid
ngx_http_lua_pipe_sigchld_handler(intsigno, siginfo_t*siginfo,
void*ucontext)
{
ngx_err_terr, saved_err;
ngx_int_tn;
saved_err=ngx_errno;
for ( ;; ) {
n=write(ngx_http_lua_write_sigfd, ngx_http_lua_pipe_notification,
sizeof(ngx_http_lua_pipe_notification));
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
"lua pipe SIGCHLD fd write siginfo:%p", siginfo);
if (n >= 0) {
break;
}
err=ngx_errno;
if (err!=NGX_EINTR) {
if (err!=NGX_EAGAIN) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, err,
"lua pipe SIGCHLD fd write failed");
}
break;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, err,
"lua pipe SIGCHLD fd write was interrupted");
}
ngx_set_errno(saved_err);
}
#endif
staticvoid
ngx_http_lua_pipe_sigchld_event_handler(ngx_event_t*ev)
{
intn;
ngx_connection_t*c=ev->data;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
"lua pipe reaping children");
for ( ;; ) {
#if (NGX_HTTP_LUA_HAVE_SIGNALFD)
n=c->recv(c, (u_char*) &ngx_http_lua_pipe_notification,
#else
n=c->recv(c, ngx_http_lua_pipe_notification,
#endif
sizeof(ngx_http_lua_pipe_notification));
if (n <= 0) {
if (n==NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe SIGCHLD fd read failed");
}
break;
}
ngx_http_lua_pipe_reap_pids(ev);
}
}
staticvoid
ngx_http_lua_pipe_reap_pids(ngx_event_t*ev)
{
intstatus;
ngx_pid_tpid;
ngx_rbtree_node_t*node;
ngx_http_lua_pipe_node_t*pipe_node;
for ( ;; ) {
pid=waitpid(-1, &status, WNOHANG);
if (pid==0) {
break;
}
if (pid<0) {
if (ngx_errno!=NGX_ECHILD) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe waitpid failed");
}
break;
}
/* This log is ported from Nginx's signal handler since we override
* or block it in this implementation. */
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
"signal %d (SIGCHLD) received from %P",
SIGCHLD, pid);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua pipe SIGCHLD fd read pid:%P status:%d", pid,
status);
node=ngx_http_lua_pipe_lookup_pid(pid);
if (node!=NULL) {
pipe_node= (ngx_http_lua_pipe_node_t*) &node->color;
if (pipe_node->wait_co_ctx!=NULL) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua pipe resume process:%p waiting for %P",
pipe_node->proc, pid);
/*
* We need the extra parentheses around the first argument
* of ngx_post_event() just to work around macro issues in
* nginx cores older than 1.7.12 (exclusive).
*/
ngx_post_event((&pipe_node->wait_co_ctx->sleep),
&ngx_posted_events);
}
/* TODO: we should proactively close and free up the pipe after
* the user consume all the data in the pipe.
*/
pipe_node->proc->pipe->dead=1;
if (WIFSIGNALED(status)) {
pipe_node->status=WTERMSIG(status);
pipe_node->reason_code=REASON_SIGNAL_CODE;
} elseif (WIFEXITED(status)) {
pipe_node->status=WEXITSTATUS(status);
pipe_node->reason_code=REASON_EXIT_CODE;
} else {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"lua pipe unknown exit status %d from "
"process %P", status, pid);
pipe_node->status=status;
pipe_node->reason_code=REASON_UNKNOWN_CODE;
}
}
}
}
staticvoid
ngx_http_lua_pipe_reap_timer_handler(ngx_event_t*ev)
{
ngx_http_lua_pipe_reap_pids(ev);
if (!ngx_exiting) {
ngx_add_timer(&ngx_reap_pid_event, 1000);
ngx_reap_pid_event.timedout=0;
}
}
staticssize_t
ngx_http_lua_pipe_fd_read(ngx_connection_t*c, u_char*buf, size_tsize)
{
ssize_tn;
ngx_err_terr;
ngx_event_t*rev;
rev=c->read;
do {
n=read(c->fd, buf, size);
err=ngx_errno;
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
"read: fd:%d %z of %uz", c->fd, n, size);
if (n==0) {
rev->ready=0;
rev->eof=1;
return0;
}
if (n>0) {
if ((size_t) n<size
&& !(ngx_event_flags&NGX_USE_GREEDY_EVENT))
{
rev->ready=0;
}
returnn;
}
if (err==NGX_EAGAIN||err==NGX_EINTR) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"read() not ready");
n=NGX_AGAIN;
} else {
n=ngx_connection_error(c, err, "read() failed");
break;
}
} while (err==NGX_EINTR);
rev->ready=0;
if (n==NGX_ERROR) {
rev->error=1;
}
returnn;
}
staticssize_t
ngx_http_lua_pipe_fd_write(ngx_connection_t*c, u_char*buf, size_tsize)
{
ssize_tn;
ngx_err_terr;
ngx_event_t*wev;
wev=c->write;
do {
n=write(c->fd, buf, size);
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
"write: fd:%d %z of %uz", c->fd, n, size);
if (n >= 0) {
if ((size_t) n!=size) {
wev->ready=0;
}
returnn;
}
err=ngx_errno;
if (err==NGX_EAGAIN||err==NGX_EINTR) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"write() not ready");
n=NGX_AGAIN;
} elseif (err!=NGX_EPIPE) {
n=ngx_connection_error(c, err, "write() failed");
break;
}
} while (err==NGX_EINTR);
wev->ready=0;
if (n==NGX_ERROR) {
wev->error=1;
}
returnn;
}
#if !(NGX_HTTP_LUA_HAVE_EXECVPE)
staticint
ngx_http_lua_execvpe(constchar*program, char*constargv[],
char*constenvp[])
{
intrc;
char**saved=environ;
environ= (char**) envp;
rc=execvp(program, argv);
environ=saved;
returnrc;
}
#endif
int
ngx_http_lua_ffi_pipe_spawn(ngx_http_request_t*r,
ngx_http_lua_ffi_pipe_proc_t*proc,
constchar*file, constchar**argv, intmerge_stderr, size_tbuffer_size,
constchar**environ, u_char*errbuf, size_t*errbuf_size)
{
intrc;
intin[2];
intout[2];
interr[2];
intstdin_fd, stdout_fd, stderr_fd;
interrlog_fd, temp_errlog_fd;
ngx_pid_tpid;
ssize_tpool_size;
ngx_pool_t*pool;
ngx_uint_ti;
ngx_listening_t*ls;
ngx_http_lua_pipe_t*pp;
ngx_rbtree_node_t*node;
ngx_http_lua_pipe_node_t*pipe_node;
structsigactionsa;
ngx_http_lua_pipe_signal_t*sig;
ngx_pool_cleanup_t*cln;
sigset_tset;
pool_size=ngx_align(NGX_MIN_POOL_SIZE+buffer_size*2,
NGX_POOL_ALIGNMENT);
pool=ngx_create_pool(pool_size, ngx_cycle->log);
if (pool==NULL) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "no memory")
-errbuf;
returnNGX_ERROR;
}
pp=ngx_pcalloc(pool, sizeof(ngx_http_lua_pipe_t)
+ offsetof(ngx_rbtree_node_t, color)
+sizeof(ngx_http_lua_pipe_node_t));
if (pp==NULL) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "no memory")
-errbuf;
goto free_pool;
}
rc=pipe(in);
if (rc==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "pipe failed: %s",
strerror(errno))
-errbuf;
goto free_pool;
}
rc=pipe(out);
if (rc==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "pipe failed: %s",
strerror(errno))
-errbuf;
goto close_in_fd;
}
if (!merge_stderr) {
rc=pipe(err);
if (rc==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size,
"pipe failed: %s", strerror(errno))
-errbuf;
goto close_in_out_fd;
}
}
pid=fork();
if (pid==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "fork failed: %s",
strerror(errno))
-errbuf;
goto close_in_out_err_fd;
}
if (pid==0) {
#if (NGX_HAVE_CPU_AFFINITY)
/* reset the CPU affinity mask */
ngx_uint_tlog_level;
ngx_cpuset_tchild_cpu_affinity;
if (ngx_process==NGX_PROCESS_WORKER
&&ngx_get_cpu_affinity(ngx_worker) !=NULL)
{
CPU_ZERO(&child_cpu_affinity);
for (i=0; i< (ngx_uint_t) ngx_min(ngx_ncpu, CPU_SETSIZE); i++) {
CPU_SET(i, &child_cpu_affinity);
}
log_level=ngx_cycle->log->log_level;
ngx_cycle->log->log_level=NGX_LOG_WARN;
ngx_setaffinity(&child_cpu_affinity, ngx_cycle->log);
ngx_cycle->log->log_level=log_level;
}
#endif
/* reset the handler of ignored signals to the default */
for (sig=ngx_signals; sig->signo!=0; sig++) {
ngx_memzero(&sa, sizeof(structsigaction));
sa.sa_handler=SIG_DFL;
if (sigemptyset(&sa.sa_mask) !=0) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child init signal mask failed");
exit(EXIT_FAILURE);
}
if (sigaction(sig->signo, &sa, NULL) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child reset signal handler for %s "
"failed", sig->signame);
exit(EXIT_FAILURE);
}
}
/* reset signal mask */
if (sigemptyset(&set) !=0) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child init signal set failed");
exit(EXIT_FAILURE);
}
if (sigprocmask(SIG_SETMASK, &set, NULL) !=0) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child reset signal mask failed");
exit(EXIT_FAILURE);
}
/* close listening socket fd */
ls=ngx_cycle->listening.elts;
for (i=0; i<ngx_cycle->listening.nelts; i++) {
if (ls[i].fd!= (ngx_socket_t) -1&&
ngx_close_socket(ls[i].fd) ==-1)
{
ngx_log_error(NGX_LOG_WARN, ngx_cycle->log, ngx_socket_errno,
"lua pipe child "ngx_close_socket_n
" %V failed", &ls[i].addr_text);
}
}
/* close and dup pipefd */
if (close(in[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe child failed to close the in[1] "
"pipe fd");
}
if (close(out[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe child failed to close the out[0] "
"pipe fd");
}
if (ngx_cycle->log->file&&ngx_cycle->log->file->fd==STDERR_FILENO) {
errlog_fd=ngx_cycle->log->file->fd;
temp_errlog_fd=dup(errlog_fd);
if (temp_errlog_fd==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child dup errlog fd failed");
exit(EXIT_FAILURE);
}
if (ngx_cloexec(temp_errlog_fd) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child new errlog fd "ngx_cloexec_n
" failed");
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua pipe child dup old errlog fd %d to new fd %d",
ngx_cycle->log->file->fd, temp_errlog_fd);
ngx_cycle->log->file->fd=temp_errlog_fd;
}
if (dup2(in[0], STDIN_FILENO) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child dup2 stdin failed");
exit(EXIT_FAILURE);
}
if (dup2(out[1], STDOUT_FILENO) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child dup2 stdout failed");
exit(EXIT_FAILURE);
}
if (merge_stderr) {
if (dup2(STDOUT_FILENO, STDERR_FILENO) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child dup2 stderr failed");
exit(EXIT_FAILURE);
}
} else {
if (close(err[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe child failed to close the err[0] "
"pipe fd");
}
if (dup2(err[1], STDERR_FILENO) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child dup2 stderr failed");
exit(EXIT_FAILURE);
}
}
if (close(in[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe failed to close the in[0]");
}
if (close(out[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe failed to close the out[1]");
}
if (!merge_stderr) {
if (close(err[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe failed to close the err[1]");
}
}
if (environ!=NULL) {
#if (NGX_HTTP_LUA_HAVE_EXECVPE)
if (execvpe(file, (char*const*) argv, (char*const*) environ)
#else
if (ngx_http_lua_execvpe(file, (char*const*) argv,
(char*const*) environ)
#endif
==-1)
{
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child execvpe() failed while "
"executing %s", file);
}
} else {
if (execvp(file, (char*const*) argv) ==-1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child execvp() failed while "
"executing %s", file);
}
}
exit(EXIT_FAILURE);
}
/* parent process */
if (close(in[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe: failed to close the in[0] pipe fd");
}
stdin_fd=in[1];
if (ngx_nonblocking(stdin_fd) ==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size,
ngx_nonblocking_n" failed: %s",
strerror(errno))
-errbuf;
goto close_in_out_err_fd;
}
pp->stdin_fd=stdin_fd;
if (close(out[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe: failed to close the out[1] pipe fd");
}
stdout_fd=out[0];
if (ngx_nonblocking(stdout_fd) ==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size,
ngx_nonblocking_n" failed: %s",
strerror(errno))
-errbuf;
goto close_in_out_err_fd;
}
pp->stdout_fd=stdout_fd;
if (!merge_stderr) {
if (close(err[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"lua pipe: failed to close the err[1] pipe fd");
}
stderr_fd=err[0];
if (ngx_nonblocking(stderr_fd) ==-1) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size,
ngx_nonblocking_n" failed: %s",
strerror(errno))
-errbuf;
goto close_in_out_err_fd;
}
pp->stderr_fd=stderr_fd;
}
if (pp->cleanup==NULL) {
cln=ngx_pool_cleanup_add(r->pool, 0);
if (cln==NULL) {
*errbuf_size=ngx_snprintf(errbuf, *errbuf_size, "no memory")
-errbuf;
goto close_in_out_err_fd;
}
cln->handler= (ngx_pool_cleanup_pt) ngx_http_lua_ffi_pipe_proc_destroy;
cln->data=proc;
pp->cleanup=&cln->handler;
pp->r=r;
}
node= (ngx_rbtree_node_t*) (pp+1);
node->key=pid;
pipe_node= (ngx_http_lua_pipe_node_t*) &node->color;
pipe_node->proc=proc;
ngx_rbtree_insert(&ngx_http_lua_pipe_rbtree, node);
pp->node=node;
pp->pool=pool;
pp->merge_stderr=merge_stderr;
pp->buffer_size=buffer_size;
proc->_pid=pid;
proc->pipe=pp;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua pipe spawn process:%p pid:%P merge_stderr:%d "
"buffer_size:%uz", proc, pid, merge_stderr, buffer_size);
returnNGX_OK;
close_in_out_err_fd:
if (!merge_stderr) {
if (close(err[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the err[0] pipe fd");
}
if (close(err[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the err[1] pipe fd");
}
}
close_in_out_fd:
if (close(out[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the out[0] pipe fd");
}
if (close(out[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the out[1] pipe fd");
}
close_in_fd:
if (close(in[0]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the in[0] pipe fd");
}
if (close(in[1]) ==-1) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
"failed to close the in[1] pipe fd");
}
free_pool:
ngx_destroy_pool(pool);
returnNGX_ERROR;
}
staticvoid
ngx_http_lua_pipe_close_helper(ngx_http_lua_pipe_t*pipe,
ngx_http_lua_pipe_ctx_t*pipe_ctx, ngx_event_t*ev)
{
if (ev->handler!=ngx_http_lua_pipe_dummy_event_handler) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua pipe abort blocking operation pipe_ctx:%p ev:%p",
pipe_ctx, ev);
if (pipe->dead) {
pipe_ctx->err_type=PIPE_ERR_CLOSED;