-
Notifications
You must be signed in to change notification settings - Fork 3k
/
io.c
7783 lines (6712 loc) · 210 KB
/
io.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2024. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* I/O routines for manipulating ports.
*/
#define ERL_IO_C__
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
/* must be included BEFORE global.h (since it includes erl_driver.h) */
#include "erl_sys_driver.h"
#include "erl_nif.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "dist.h"
#include "big.h"
#include "erl_binary.h"
#include "erl_bits.h"
#include "erl_version.h"
#include "error.h"
#include "erl_async.h"
#define ERTS_WANT_EXTERNAL_TAGS
#include "external.h"
#include "dtrace-wrapper.h"
#include "lttng-wrapper.h"
#include "erl_map.h"
#include "erl_bif_unique.h"
#include "erl_hl_timer.h"
#include "erl_time.h"
#include "erl_io_queue.h"
#include "erl_proc_sig_queue.h"
#include "erl_global_literals.h"
#include "erl_iolist.h"
extern ErlDrvEntry fd_driver_entry;
extern ErlDrvEntry spawn_driver_entry;
#ifndef __WIN32__
extern ErlDrvEntry forker_driver_entry;
#endif
extern ErtsStaticDriver driver_tab[]; /* table of static drivers, only used during initialization */
erts_driver_t *driver_list; /* List of all drivers, static and dynamic. */
erts_rwmtx_t erts_driver_list_lock; /* Mutex for driver list */
static erts_tsd_key_t driver_list_lock_status_key; /*stop recursive locks when calling
driver init */
static erts_tsd_key_t driver_list_last_error_key; /* Save last DDLL error on a
per thread basis (for BC interfaces) */
ErtsPTab erts_port erts_align_attribute(ERTS_CACHE_LINE_SIZE); /* The port table */
const ErlDrvTermData driver_term_nil = (ErlDrvTermData)NIL;
const Port erts_invalid_port = {{ERTS_INVALID_PORT}};
erts_driver_t spawn_driver;
#ifndef __WIN32__
erts_driver_t forker_driver;
#endif
erts_driver_t fd_driver;
int erts_port_synchronous_ops = 0;
int erts_port_schedule_all_ops = 0;
int erts_port_parallelism = 0;
static erts_atomic64_t bytes_in;
static erts_atomic64_t bytes_out;
static void deliver_result(Port *p, Eterm sender, Eterm pid, Eterm res);
static int init_driver(erts_driver_t *, ErlDrvEntry *, DE_Handle *);
static void terminate_port(Port *p);
static void pdl_init(void);
static int driver_failure_term(ErlDrvPort ix, Eterm term, int eof);
static void driver_monitor_lock_pdl(Port *p);
static void driver_monitor_unlock_pdl(Port *p);
#define DRV_MONITOR_LOOKUP_PORT_LOCK_PDL(Port) erts_thr_drvport2port((Port), 1)
#define DRV_MONITOR_LOCK_PDL(Port) driver_monitor_lock_pdl(Port)
#define DRV_MONITOR_UNLOCK_PDL(Port) driver_monitor_unlock_pdl(Port)
#define ERL_SMALL_IO_BIN_LIMIT (4*ERL_ONHEAP_BINARY_LIMIT)
#define SMALL_WRITE_VEC 16
static ERTS_INLINE ErlPortIOQueue*
drvport2ioq(ErlDrvPort drvport)
{
Port *prt = erts_thr_drvport2port(drvport, 0);
if (prt == ERTS_INVALID_ERL_DRV_PORT)
return NULL;
return &prt->ioq;
}
static ERTS_INLINE int
is_port_ioq_empty(Port *pp)
{
int res;
ERTS_LC_ASSERT(erts_lc_is_port_locked(pp));
if (!pp->port_data_lock)
res = (erts_ioq_size(&pp->ioq) == 0);
else {
ErlDrvPDL pdl = pp->port_data_lock;
erts_mtx_lock(&pdl->mtx);
res = (erts_ioq_size(&pp->ioq) == 0);
erts_mtx_unlock(&pdl->mtx);
}
return res;
}
int
erts_is_port_ioq_empty(Port *pp)
{
return is_port_ioq_empty(pp);
}
Uint
erts_port_ioq_size(Port *pp)
{
ErlDrvSizeT res;
ERTS_LC_ASSERT(erts_lc_is_port_locked(pp));
if (!pp->port_data_lock)
res = erts_ioq_size(&pp->ioq);
else {
ErlDrvPDL pdl = pp->port_data_lock;
erts_mtx_lock(&pdl->mtx);
res = erts_ioq_size(&pp->ioq);
erts_mtx_unlock(&pdl->mtx);
}
return (Uint) res;
}
/*
* Line buffered I/O.
*/
typedef struct line_buf_context {
LineBuf **b;
char *buf;
ErlDrvSizeT left;
ErlDrvSizeT retlen;
} LineBufContext;
#define LINEBUF_EMPTY 0
#define LINEBUF_EOL 1
#define LINEBUF_NOEOL 2
#define LINEBUF_ERROR -1
#define LINEBUF_STATE(LBC) ((*(LBC).b)->data[0])
#define LINEBUF_DATA(LBC) (((*(LBC).b)->data) + 1)
#define LINEBUF_DATALEN(LBC) ((LBC).retlen)
#define LINEBUF_INITIAL 100
#ifdef USE_VM_PROBES
#define DTRACE_FORMAT_COMMON_PID_AND_PORT(PID, PORT) \
DTRACE_CHARBUF(process_str, DTRACE_TERM_BUF_SIZE); \
DTRACE_CHARBUF(port_str, DTRACE_TERM_BUF_SIZE); \
\
dtrace_pid_str((PID), process_str); \
dtrace_port_str((PORT), port_str);
#define DTRACE_FORMAT_COMMON_PROC_AND_PORT(PID, PORT) \
DTRACE_CHARBUF(process_str, DTRACE_TERM_BUF_SIZE); \
DTRACE_CHARBUF(port_str, DTRACE_TERM_BUF_SIZE); \
\
dtrace_proc_str((PID), process_str); \
dtrace_port_str((PORT), port_str);
void
dtrace_drvport_str(ErlDrvPort drvport, char *port_buf)
{
Port *port = erts_drvport2port(drvport);
if (port != ERTS_INVALID_ERL_DRV_PORT)
erts_snprintf(port_buf, DTRACE_TERM_BUF_SIZE, "#Port<%lu.%b64u>",
port_channel_no(port->common.id),
port_number(port->common.id));
else
erts_snprintf(port_buf, DTRACE_TERM_BUF_SIZE, "#Port<%lu.INVALID>",
port_channel_no(port->common.id));
}
#endif
static ERTS_INLINE void
kill_port(Port *pp)
{
ERTS_LC_ASSERT(erts_lc_is_port_locked(pp));
delete_all_trace_refs(&pp->common);
erts_ptab_delete_element(&erts_port, &pp->common); /* Time of death */
erts_port_task_free_port(pp);
/* In non-smp case the port structure may have been deallocated now */
}
#ifdef ERTS_ENABLE_LOCK_CHECK
int
erts_lc_is_port_locked(Port *prt)
{
if (!prt)
return 0;
ERTS_LC_ASSERT(prt->lock);
return erts_lc_mtx_is_locked(prt->lock);
}
#endif
static void initq(Port* prt);
#if defined(ERTS_ENABLE_LOCK_CHECK) || defined(ERTS_ENABLE_LOCK_COUNT)
#define ERTS_PORT_INIT_INSTR_NEED_ID 1
#else
#define ERTS_PORT_INIT_INSTR_NEED_ID 0
#endif
static ERTS_INLINE void port_init_instr(Port *prt
#if ERTS_PORT_INIT_INSTR_NEED_ID
, Eterm id
#endif
)
{
#if !ERTS_PORT_INIT_INSTR_NEED_ID
Eterm id = NIL; /* Not used */
#endif
/*
* Stuff that need to be initialized with the port id
* in the instrumented case, but not in the normal case.
*/
ASSERT(prt->drv_ptr && prt->lock);
if (!prt->drv_ptr->lock) {
erts_mtx_init_locked(prt->lock, "port_lock", id, ERTS_LOCK_FLAGS_CATEGORY_IO);
}
erts_port_task_init_sched(&prt->sched, id);
}
#if !ERTS_PORT_INIT_INSTR_NEED_ID
static ERTS_INLINE void port_init_instr_abort(Port *prt)
{
ASSERT(prt->drv_ptr && prt->lock);
if (!prt->drv_ptr->lock) {
erts_mtx_unlock(prt->lock);
erts_mtx_destroy(prt->lock);
}
erts_port_task_fini_sched(&prt->sched);
}
#endif
static void insert_port_struct(void *vprt, Eterm data)
{
Port *prt = (Port *) vprt;
Eterm id = make_internal_port(data);
#if ERTS_PORT_INIT_INSTR_NEED_ID
/*
* This cannot be done earlier in the instrumented
* case since we don't now 'id' until now.
*/
port_init_instr(prt, id);
#endif
prt->common.id = id;
erts_atomic32_init_relb(&prt->state, ERTS_PORT_SFLG_INITIALIZING);
}
#define ERTS_CREATE_PORT_FLAG_PARALLELISM (1 << 0)
static Port *create_port(char *name,
erts_driver_t *driver,
erts_mtx_t *driver_lock,
int create_flags,
Eterm pid,
int *enop)
{
ErtsPortTaskBusyPortQ *busy_port_queue;
Port *prt;
char *p;
size_t port_size, busy_port_queue_size, size;
erts_aint32_t state = ERTS_PORT_SFLG_CONNECTED;
erts_aint32_t x_pts_flgs = 0;
if (!driver_lock) {
/* Align size for mutex following port struct */
port_size = size = ERTS_ALC_DATA_ALIGN_SIZE(sizeof(Port));
size += sizeof(erts_mtx_t);
}
else
port_size = size = ERTS_ALC_DATA_ALIGN_SIZE(sizeof(Port));
#ifdef DEBUG
/* Make sure the debug flags survives until port is freed */
state |= ERTS_PORT_SFLG_PORT_DEBUG;
#endif
busy_port_queue_size
= ((driver->flags & ERL_DRV_FLAG_NO_BUSY_MSGQ)
? 0
: ERTS_ALC_DATA_ALIGN_SIZE(sizeof(ErtsPortTaskBusyPortQ)));
size += busy_port_queue_size;
size += sys_strlen(name) + 1;
p = erts_alloc_fnf(ERTS_ALC_T_PORT, size);
if (!p) {
if (enop)
*enop = ENOMEM;
return NULL;
}
prt = (Port *) p;
p += port_size;
if (!busy_port_queue_size)
busy_port_queue = NULL;
else {
busy_port_queue = (ErtsPortTaskBusyPortQ *) p;
p += busy_port_queue_size;
}
if (driver_lock) {
prt->lock = driver_lock;
erts_mtx_lock(driver_lock);
}
else {
prt->lock = (erts_mtx_t *) p;
p += sizeof(erts_mtx_t);
state |= ERTS_PORT_SFLG_PORT_SPECIFIC_LOCK;
}
{
ErtsRunQueue *runq;
ErtsSchedulerData *esdp = erts_get_scheduler_data();
if (esdp)
runq = erts_get_runq_current(esdp);
else
runq = ERTS_RUNQ_IX(0);
erts_init_runq_port(prt, runq);
}
prt->xports = NULL;
erts_port_task_pre_init_sched(&prt->sched, busy_port_queue);
prt->name = p;
sys_strcpy(p, name);
prt->drv_ptr = driver;
ERTS_P_LINKS(prt) = NULL;
ERTS_P_MONITORS(prt) = NULL;
ERTS_P_LT_MONITORS(prt) = NULL;
prt->linebuf = NULL;
prt->suspended = NULL;
erts_init_port_data(prt);
prt->port_data_lock = NULL;
prt->control_flags = 0;
prt->bytes_in = 0;
prt->bytes_out = 0;
ERTS_PORT_INIT_CONNECTED(prt, pid);
prt->common.u.alive.reg = NULL;
ERTS_PTMR_INIT(prt);
erts_port_task_handle_init(&prt->timeout_task);
erts_atomic_init_nob(&prt->psd, (erts_aint_t) NULL);
prt->async_open_port = NULL;
prt->drv_data = (SWord) 0;
prt->os_pid = -1;
ERTS_CT_ASSERT(offsetof(Port,common) == 0);
#if !ERTS_PORT_INIT_INSTR_NEED_ID
/*
* When 'id' isn't needed (the normal case), it is better to
* do the initialization here avoiding unnecessary contention
* on table...
*/
port_init_instr(prt);
#endif
if (!erts_ptab_new_element(&erts_port,
&prt->common,
(void *) prt,
insert_port_struct)) {
#if !ERTS_PORT_INIT_INSTR_NEED_ID
port_init_instr_abort(prt);
#endif
if (driver_lock)
erts_mtx_unlock(driver_lock);
if (enop)
*enop = 0;
erts_free(ERTS_ALC_T_PORT, prt);
return NULL;
}
ASSERT(prt == (Port *) (erts_ptab_pix2intptr_nob(
&erts_port,
internal_port_index(prt->common.id))));
ERTS_P_ALL_TRACE_FLAGS(prt) = 0;
prt->common.tracee.first_ref = NULL;
if (erts_refc_read(&erts_new_ports_trace_cnt, 0)) {
/* Set new port tracing */
erts_rwmtx_rlock(&erts_trace_session_list_lock);
for (ErtsTraceSession *s = &erts_trace_session_0; s; s = s->next) {
Uint32 trace_flags;
ErtsTracer tracer;
erts_get_new_port_tracing(s, &trace_flags, &tracer);
if (trace_flags) {
ErtsTracerRef *ref = new_tracer_ref(&prt->common, s);
ref->flags = trace_flags;
ref->tracer = tracer;
}
}
erts_rwmtx_runlock(&erts_trace_session_list_lock);
ERTS_P_ALL_TRACE_FLAGS(prt) = erts_sum_all_trace_flags(&prt->common);
}
initq(prt);
ERTS_LC_ASSERT(erts_lc_is_port_locked(prt));
if (erts_port_schedule_all_ops)
x_pts_flgs |= ERTS_PTS_FLG_FORCE_SCHED;
if (create_flags & ERTS_CREATE_PORT_FLAG_PARALLELISM)
x_pts_flgs |= ERTS_PTS_FLG_PARALLELISM;
if (x_pts_flgs)
erts_atomic32_read_bor_nob(&prt->sched.flags, x_pts_flgs);
erts_atomic32_set_relb(&prt->state, state);
return prt;
}
void
erts_port_free(Port *prt)
{
erts_aint32_t state = erts_atomic32_read_nob(&prt->state);
ERTS_LC_ASSERT(state & (ERTS_PORT_SFLG_INITIALIZING
| ERTS_PORT_SFLG_FREE));
ASSERT(state & ERTS_PORT_SFLG_PORT_DEBUG);
ERTS_LC_ASSERT(erts_atomic_read_nob(&prt->common.refc.atmc) == 0);
erts_port_task_fini_sched(&prt->sched);
if (prt->async_open_port) {
erts_free(ERTS_ALC_T_PRTSD, prt->async_open_port);
prt->async_open_port = NULL;
}
ASSERT(prt->lock);
if (state & ERTS_PORT_SFLG_PORT_SPECIFIC_LOCK)
erts_mtx_destroy(prt->lock);
/*
* We cannot dereference a driver using driver
* locking until here in smp case. Otherwise,
* the driver lock may still be in use by others.
*
* In the non-smp case we cannot do it here since
* this function may be called by non-scheduler
* threads. This is done in erts_port_cleanup()
* in the non-smp case.
*/
if (prt->drv_ptr->handle)
erts_ddll_dereference_driver(prt->drv_ptr->handle);
erts_free(ERTS_ALC_T_PORT, prt);
}
/*
** Initialize v_start to point to the small fixed vector.
** Once (reallocated) we never reset the pointer to the small vector
** This is a possible optimisation.
*/
static void initq(Port* prt)
{
ERTS_LC_ASSERT(!prt->port_data_lock);
erts_ioq_init(&prt->ioq, ERTS_ALC_T_IOQ, 1);
}
static void stopq(Port* prt)
{
if (prt->port_data_lock)
driver_pdl_lock(prt->port_data_lock);
erts_ioq_clear(&prt->ioq);
if (prt->port_data_lock) {
driver_pdl_unlock(prt->port_data_lock);
driver_pdl_dec_refc(prt->port_data_lock);
}
}
int
erts_save_suspend_process_on_port(Port *prt, Process *process)
{
int saved;
erts_aint32_t flags;
erts_port_task_sched_lock(&prt->sched);
flags = erts_atomic32_read_nob(&prt->sched.flags);
saved = (flags & ERTS_PTS_FLGS_BUSY) && !(flags & ERTS_PTS_FLG_EXIT);
if (saved)
erts_proclist_store_last(&prt->suspended, erts_proclist_create(process));
erts_port_task_sched_unlock(&prt->sched);
return saved;
}
/*
Opens a driver.
Returns the non-negative port number, if successful.
If there is an error, -1 or -2 or -3 is returned. -2 means that
there is valid error information in *error_number_ptr.
Returning -3 means that an error in the given options was detected
(*error_number_ptr must contain either BADARG or SYSTEM_LIMIT).
The driver start function must obey the same conventions.
*/
Port *
erts_open_driver(erts_driver_t* driver, /* Pointer to driver. */
Eterm pid, /* Current process. */
char* name, /* Driver name. */
SysDriverOpts* opts, /* Options. */
int *error_type_ptr, /* error type */
int *error_number_ptr) /* errno in case of error type -2 */
{
#undef ERTS_OPEN_DRIVER_RET
#define ERTS_OPEN_DRIVER_RET(Prt, EType, ENo) \
do { \
if (error_type_ptr) \
*error_type_ptr = (EType); \
if (error_number_ptr) \
*error_number_ptr = (ENo); \
return (Prt); \
} while (0)
ErlDrvData drv_data = 0;
Port *port;
int error_type, error_number;
int port_errno = 0;
erts_mtx_t *driver_lock = NULL;
int cprt_flgs = 0;
ERTS_CHK_NO_PROC_LOCKS;
erts_rwmtx_rlock(&erts_driver_list_lock);
if (!driver) {
for (driver = driver_list; driver; driver = driver->next) {
if (sys_strcmp(driver->name, name) == 0)
break;
}
if (!driver) {
erts_rwmtx_runlock(&erts_driver_list_lock);
ERTS_OPEN_DRIVER_RET(NULL, -3, BADARG);
}
}
if (driver == &spawn_driver) {
char *p;
erts_driver_t *d;
/*
* Dig out the name of the driver or port program.
*/
if (!(opts->spawn_type & ERTS_SPAWN_EXECUTABLE)) {
/* No spawn driver default */
driver = NULL;
} else {
#ifdef __IOS__
erts_rwmtx_runlock(&erts_driver_list_lock);
ERTS_OPEN_DRIVER_RET(NULL, -3, BADARG);
#endif
}
if (opts->spawn_type != ERTS_SPAWN_EXECUTABLE) {
p = name;
while(*p != '\0' && *p != ' ')
p++;
if (*p == '\0')
p = NULL;
else
*p = '\0';
/*
* Search for a driver having this name. Defaults to spawn_driver
* if not found.
*/
for (d = driver_list; d; d = d->next) {
if (sys_strcmp(d->name, name) == 0 &&
erts_ddll_driver_ok(d->handle)) {
driver = d;
break;
}
}
if (p != NULL)
*p = ' ';
}
}
if (driver == NULL || (driver != &spawn_driver && opts->exit_status)) {
erts_rwmtx_runlock(&erts_driver_list_lock);
ERTS_OPEN_DRIVER_RET(NULL, -3, BADARG);
}
if (opts->port_watermarks_set && driver != &spawn_driver
&& driver != &fd_driver) {
erts_rwmtx_runlock(&erts_driver_list_lock);
ERTS_OPEN_DRIVER_RET(NULL, -3, BADARG);
}
if (opts->msgq_watermarks_set
&& (driver->flags & ERL_DRV_FLAG_NO_BUSY_MSGQ)
&& opts->high_msgq_watermark != ERL_DRV_BUSY_MSGQ_DISABLED) {
erts_rwmtx_runlock(&erts_driver_list_lock);
ERTS_OPEN_DRIVER_RET(NULL, -3, BADARG);
}
driver_lock = driver->lock;
if (driver->handle != NULL) {
erts_ddll_increment_port_count(driver->handle);
erts_ddll_reference_driver(driver->handle);
}
erts_rwmtx_runlock(&erts_driver_list_lock);
/*
* We'll set up the port before calling the start function,
* to allow message sending and setting timers in the start function.
*/
if (opts->parallelism)
cprt_flgs |= ERTS_CREATE_PORT_FLAG_PARALLELISM;
port = create_port(name, driver, driver_lock, cprt_flgs, pid, &port_errno);
if (!port) {
if (driver->handle) {
erts_rwmtx_rlock(&erts_driver_list_lock);
erts_ddll_decrement_port_count(driver->handle);
erts_rwmtx_runlock(&erts_driver_list_lock);
erts_ddll_dereference_driver(driver->handle);
}
if (port_errno)
ERTS_OPEN_DRIVER_RET(NULL, -2, port_errno);
else
ERTS_OPEN_DRIVER_RET(NULL, -3, SYSTEM_LIMIT);
}
if (ERTS_IS_P_TRACED_FL(port, F_TRACE_PORTS)) {
trace_port_open(port,
pid,
erts_atom_put((byte *) port->name,
sys_strlen(port->name),
ERTS_ATOM_ENC_LATIN1,
1));
}
if (opts->msgq_watermarks_set)
erl_drv_busy_msgq_limits(ERTS_Port2ErlDrvPort(port),
&opts->low_msgq_watermark,
&opts->high_msgq_watermark);
error_number = error_type = 0;
if (driver->start) {
ERTS_MSACC_PUSH_STATE_M();
if (ERTS_IS_P_TRACED_FL(port, F_TRACE_SCHED_PORTS)) {
trace_sched_ports_where(port, am_in, am_open);
}
port->caller = pid;
#ifdef USE_VM_PROBES
if (DTRACE_ENABLED(driver_start)) {
DTRACE_FORMAT_COMMON_PID_AND_PORT(pid, port)
DTRACE3(driver_start, process_str, driver->name, port_str);
}
#endif
ERTS_MSACC_SET_STATE_CACHED_M(ERTS_MSACC_STATE_PORT);
#ifdef USE_LTTNG_VM_TRACEPOINTS
if (LTTNG_ENABLED(driver_start)) {
lttng_decl_portbuf(port_str);
lttng_decl_procbuf(proc_str);
lttng_pid_to_str(pid, proc_str);
lttng_port_to_str(port, port_str);
LTTNG3(driver_start, proc_str, driver->name, port_str);
}
#endif
drv_data = (*driver->start)(ERTS_Port2ErlDrvPort(port), name, opts);
if (((SWord) drv_data) == -1)
error_type = -1;
else if (((SWord) drv_data) == -2) {
/*
* We need to save errno quickly after the
* call to the 'start' callback before
* something else modify it.
*/
error_type = -2;
error_number = errno;
}
else if (((SWord) drv_data) == -3) {
error_type = -3;
error_number = BADARG;
}
ERTS_MSACC_POP_STATE_M();
port->caller = NIL;
if (ERTS_IS_P_TRACED_FL(port, F_TRACE_SCHED_PORTS)) {
trace_sched_ports_where(port, am_out, am_open);
}
if (port->xports)
erts_port_handle_xports(port);
ASSERT(!port->xports);
}
if (error_type) {
/*
* Must clean up the port.
*/
erts_cancel_port_timer(port);
stopq(port);
if (port->linebuf != NULL) {
erts_free(ERTS_ALC_T_LINEBUF,
(void *) port->linebuf);
port->linebuf = NULL;
}
if (driver->handle != NULL) {
erts_rwmtx_rlock(&erts_driver_list_lock);
erts_ddll_decrement_port_count(driver->handle);
erts_rwmtx_runlock(&erts_driver_list_lock);
}
kill_port(port);
erts_port_release(port);
ERTS_OPEN_DRIVER_RET(NULL, error_type, error_number);
}
port->drv_data = (UWord) drv_data;
ERTS_OPEN_DRIVER_RET(port, 0, 0);
#undef ERTS_OPEN_DRIVER_RET
}
struct ErtsXPortsList_ {
ErtsXPortsList *next;
Port *port;
};
ERTS_SCHED_PREF_QUICK_ALLOC_IMPL(xports_list, ErtsXPortsList, 50, ERTS_ALC_T_XPORTS_LIST)
/*
* Driver function to create new instances of a driver
* Historical reason: to be used with inet_drv for creating
* accept sockets inorder to avoid a global table.
*/
ErlDrvPort
driver_create_port(ErlDrvPort creator_port_ix, /* Creating port */
ErlDrvTermData pid, /* Owner/Caller */
char* name, /* Driver name */
ErlDrvData drv_data) /* Driver data */
{
int cprt_flgs = 0;
Port *creator_port;
Port* port;
erts_driver_t *driver;
erts_mtx_t *driver_lock = NULL;
ErtsLink *port_lnk, *proc_lnk;
ERTS_CHK_NO_PROC_LOCKS;
/* Need to be called from a scheduler thread */
if (!erts_get_scheduler_id())
return ERTS_INVALID_ERL_DRV_PORT;
creator_port = erts_drvport2port(creator_port_ix);
if (creator_port == ERTS_INVALID_ERL_DRV_PORT)
return ERTS_INVALID_ERL_DRV_PORT;
ERTS_LC_ASSERT(erts_lc_is_port_locked(creator_port));
driver = creator_port->drv_ptr;
erts_rwmtx_rlock(&erts_driver_list_lock);
if (!erts_ddll_driver_ok(driver->handle)) {
erts_rwmtx_runlock(&erts_driver_list_lock);
return ERTS_INVALID_ERL_DRV_PORT;
}
if (driver->handle != NULL) {
erts_ddll_increment_port_count(driver->handle);
erts_ddll_reference_referenced_driver(driver->handle);
}
driver_lock = driver->lock;
erts_rwmtx_runlock(&erts_driver_list_lock);
/* Inherit parallelism flag from parent */
if (ERTS_PTS_FLG_PARALLELISM &
erts_atomic32_read_nob(&creator_port->sched.flags))
cprt_flgs |= ERTS_CREATE_PORT_FLAG_PARALLELISM;
port = create_port(name, driver, driver_lock, cprt_flgs, pid, NULL);
if (!port) {
if (driver->handle) {
erts_rwmtx_rlock(&erts_driver_list_lock);
erts_ddll_decrement_port_count(driver->handle);
erts_rwmtx_runlock(&erts_driver_list_lock);
erts_ddll_dereference_driver(driver->handle);
}
return ERTS_INVALID_ERL_DRV_PORT;
}
ERTS_LC_ASSERT(erts_lc_is_port_locked(port));
port_lnk = erts_link_internal_create(ERTS_LNK_TYPE_PORT, pid);
erts_link_tree_insert(&ERTS_P_LINKS(port), port_lnk);
proc_lnk = erts_link_internal_create(ERTS_LNK_TYPE_PORT, port->common.id);
if (!erts_proc_sig_send_link(&port->common, port->common.id,
pid, proc_lnk)) {
erts_link_tree_delete(&ERTS_P_LINKS(port), port_lnk);
erts_link_internal_release(proc_lnk);
erts_link_internal_release(port_lnk);
if (driver->handle) {
erts_rwmtx_rlock(&erts_driver_list_lock);
erts_ddll_decrement_port_count(driver->handle);
erts_rwmtx_runlock(&erts_driver_list_lock);
}
kill_port(port);
erts_port_release(port);
return ERTS_INVALID_ERL_DRV_PORT;
}
if (!driver_lock) {
ErtsXPortsList *xplp = xports_list_alloc();
xplp->port = port;
xplp->next = creator_port->xports;
creator_port->xports = xplp;
}
port->drv_data = (UWord) drv_data;
return ERTS_Port2ErlDrvPort(port);
}
int erts_port_handle_xports(Port *prt)
{
int reds = 0;
ErtsXPortsList *xplp;
ASSERT(prt);
xplp = prt->xports;
ASSERT(xplp);
while (xplp) {
Port *rprt = xplp->port;
ErtsXPortsList *free_xplp;
erts_aint32_t state;
if (rprt->xports)
reds += erts_port_handle_xports(rprt);
state = erts_atomic32_read_nob(&rprt->state);
if ((state & ERTS_PORT_SFLG_CLOSING) && erts_is_port_ioq_empty(rprt)) {
terminate_port(rprt);
reds += ERTS_PORT_REDS_TERMINATE;
}
erts_port_release(rprt);
free_xplp = xplp;
xplp = xplp->next;
xports_list_free(free_xplp);
reds++;
}
prt->xports = NULL;
return reds;
}
typedef enum {
ERTS_TRY_IMM_DRV_CALL_OK,
ERTS_TRY_IMM_DRV_CALL_BUSY_LOCK,
ERTS_TRY_IMM_DRV_CALL_INVALID_PORT,
ERTS_TRY_IMM_DRV_CALL_INVALID_SCHED_FLAGS
} ErtsTryImmDrvCallResult;
typedef struct {
Process *c_p; /* Currently executing process (unlocked) */
Port *port; /* Port to operate on */
Eterm port_op; /* port operation as an atom */
erts_aint32_t state; /* in: invalid state; out: read state (if read) */
erts_aint32_t sched_flags; /* in: invalid flags; out: read flags (if read) */
int async; /* Asynchronous operation */
int pre_chk_sched_flags; /* Check sched flags before lock? */
int reds_left_in;
} ErtsTryImmDrvCallState;
#define ERTS_INIT_TRY_IMM_DRV_CALL_STATE(C_P, PRT, SFLGS, PTS_FLGS, A, PRT_OP) \
{(C_P), (PRT), (PRT_OP), (SFLGS), (PTS_FLGS), (A), 1, 0}
/*
* Try doing an immediate driver callback call from a process. If
* this fail, the operation should be scheduled in the normal case...
* Returns: ok to do the call, or error (lock busy, does not exist, etc)
*/
static ERTS_INLINE ErtsTryImmDrvCallResult
try_imm_drv_call(ErtsTryImmDrvCallState *sp)
{
unsigned int prof_runnable_ports;
ErtsTryImmDrvCallResult res;
int reds_left_in;
erts_aint32_t act, exp, invalid_state, invalid_sched_flags;
Port *prt = sp->port;
Process *c_p = sp->c_p;
ASSERT(is_atom(sp->port_op));
invalid_sched_flags = ERTS_PTS_FLGS_FORCE_SCHEDULE_OP;
invalid_sched_flags |= sp->sched_flags;
if (sp->async)
invalid_sched_flags |= ERTS_PTS_FLG_PARALLELISM;
if (sp->pre_chk_sched_flags) {
sp->sched_flags = erts_atomic32_read_nob(&prt->sched.flags);
if (sp->sched_flags & invalid_sched_flags)
return ERTS_TRY_IMM_DRV_CALL_INVALID_SCHED_FLAGS;
}
if (erts_port_trylock(prt) == EBUSY)
return ERTS_TRY_IMM_DRV_CALL_BUSY_LOCK;
invalid_state = sp->state;
sp->state = erts_atomic32_read_nob(&prt->state);
if (sp->state & invalid_state) {
res = ERTS_TRY_IMM_DRV_CALL_INVALID_PORT;
goto locked_fail;
}
prof_runnable_ports = erts_system_profile_flags.runnable_ports;
if (prof_runnable_ports)
erts_port_task_sched_lock(&prt->sched);
act = erts_atomic32_read_nob(&prt->sched.flags);
do {
erts_aint32_t new;
if (act & invalid_sched_flags) {
res = ERTS_TRY_IMM_DRV_CALL_INVALID_SCHED_FLAGS;
sp->sched_flags = act;
goto locked_fail;
}
exp = act;
new = act | ERTS_PTS_FLG_EXEC_IMM;
act = erts_atomic32_cmpxchg_mb(&prt->sched.flags, new, exp);
} while (act != exp);
sp->sched_flags = act;
if (!c_p)
reds_left_in = CONTEXT_REDS/10;
else {
if (ERTS_IS_P_TRACED_FL(c_p, F_TRACE_SCHED_PROCS))
trace_sched(c_p, ERTS_PROC_LOCK_MAIN, am_out, F_TRACE_SCHED_PROCS);
/*
* No status lock held while sending runnable
* proc trace messages. It is however not needed
* in this case, since only this thread can send
* such messages for this process until the process
* has been scheduled out.
*/
if (erts_system_profile_flags.runnable_procs
&& erts_system_profile_flags.exclusive)
profile_runnable_proc(c_p, am_inactive);
reds_left_in = ERTS_BIF_REDS_LEFT(c_p);
erts_proc_unlock(c_p, ERTS_PROC_LOCK_MAIN);
ASSERT((c_p->scheduler_data)->current_port == NULL);
(c_p->scheduler_data)->current_port = prt;
}
ASSERT(0 <= reds_left_in && reds_left_in <= CONTEXT_REDS);
sp->reds_left_in = reds_left_in;
prt->reds = CONTEXT_REDS - reds_left_in;