-
Notifications
You must be signed in to change notification settings - Fork 3k
/
bif.c
5809 lines (4990 loc) · 156 KB
/
bif.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%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h> /* offsetof() */
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#define WANT_NONBLOCKING
#include "sys.h"
#include "erl_vm.h"
#include "erl_sys_driver.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "bif.h"
#include "big.h"
#include "dist.h"
#include "erl_version.h"
#include "erl_binary.h"
#include "beam_bp.h"
#include "erl_db_util.h"
#include "register.h"
#include "erl_thr_progress.h"
#define ERTS_PTAB_WANT_BIF_IMPL__
#include "erl_ptab.h"
#include "erl_bits.h"
#include "erl_bif_unique.h"
#include "erl_map.h"
#include "erl_msacc.h"
#include "erl_proc_sig_queue.h"
#include "erl_fun.h"
#include "ryu.h"
#include "jit/beam_asm.h"
#include "erl_global_literals.h"
#include "beam_load.h"
Export *erts_await_result;
static Export await_exit_trap;
static Export* flush_monitor_messages_trap = NULL;
static Export* set_cpu_topology_trap = NULL;
static Export* await_port_send_result_trap = NULL;
Export* erts_format_cpu_topology_trap = NULL;
#ifndef DEBUG
static
#endif
Export dsend_continue_trap_export;
Export *erts_convert_time_unit_trap = NULL;
static Export *await_msacc_mod_trap = NULL;
static erts_atomic32_t msacc;
static Export *system_flag_scheduler_wall_time_trap;
static Export *await_sched_wall_time_mod_trap;
static erts_atomic32_t sched_wall_time;
#define DECL_AM(S) Eterm AM_ ## S = am_atom_put(#S, sizeof(#S) - 1)
/*
* The BIF's now follow, see the Erlang Manual for a description of what
* each individual BIF does.
*/
BIF_RETTYPE spawn_3(BIF_ALIST_3)
{
ErlSpawnOpts so;
Eterm pid;
ERTS_SET_DEFAULT_SPAWN_OPTS(&so);
pid = erl_create_process(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else {
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, pid, ERTS_MODIFIED_TIMING_DELAY);
}
BIF_RET(pid);
}
}
/**********************************************************************/
/* Utility to add a new link between processes p and another internal
* process (rpid). Process p must be the currently executing process.
*/
/* create a link to the process */
BIF_RETTYPE link_1(BIF_ALIST_1)
{
if (ERTS_IS_P_TRACED_FL(BIF_P, F_TRACE_PROCS)) {
trace_proc(BIF_P, ERTS_PROC_LOCK_MAIN, BIF_P, am_link, BIF_ARG_1);
}
/* check that the pid or port which is our argument is OK */
if (is_internal_pid(BIF_ARG_1)) {
int created;
ErtsLink *lnk, *rlnk;
if (BIF_P->common.id == BIF_ARG_1)
BIF_RET(am_true);
if (!erts_proc_lookup(BIF_ARG_1))
goto res_no_proc;
lnk = erts_link_internal_tree_lookup_create(&ERTS_P_LINKS(BIF_P),
&created,
ERTS_LNK_TYPE_PROC,
BIF_ARG_1);
if (!created) {
ErtsILink *ilnk = (ErtsILink *) lnk;
if (!ilnk->unlinking)
BIF_RET(am_true);
ilnk->unlinking = 0;
}
rlnk = erts_link_internal_create(ERTS_LNK_TYPE_PROC, BIF_P->common.id);
if (erts_proc_sig_send_link(&BIF_P->common, BIF_P->common.id,
BIF_ARG_1, rlnk)) {
BIF_RET(am_true);
}
erts_link_tree_delete(&ERTS_P_LINKS(BIF_P), lnk);
erts_link_internal_release(lnk);
erts_link_internal_release(rlnk);
goto res_no_proc;
}
if (is_internal_port(BIF_ARG_1)) {
int created;
ErtsLink *lnk, *rlnk;
Eterm ref;
Eterm *refp;
Port *prt = erts_port_lookup(BIF_ARG_1,
(erts_port_synchronous_ops
? ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP
: ERTS_PORT_SFLGS_INVALID_LOOKUP));
if (!prt)
goto res_no_proc;
lnk = erts_link_internal_tree_lookup_create(&ERTS_P_LINKS(BIF_P),
&created,
ERTS_LNK_TYPE_PORT,
BIF_ARG_1);
if (!created) {
ErtsILink *ilnk = (ErtsILink *) lnk;
if (!ilnk->unlinking)
BIF_RET(am_true);
ilnk->unlinking = 0;
}
rlnk = erts_link_internal_create(ERTS_LNK_TYPE_PROC, BIF_P->common.id);
refp = erts_port_synchronous_ops ? &ref : NULL;
switch (erts_port_link(BIF_P, prt, rlnk, refp)) {
case ERTS_PORT_OP_BADARG:
erts_link_internal_release(rlnk);
erts_link_tree_delete(&ERTS_P_LINKS(BIF_P), lnk);
erts_link_internal_release(lnk);
goto res_no_proc;
case ERTS_PORT_OP_DROPPED:
case ERTS_PORT_OP_SCHEDULED:
if (refp) {
ASSERT(is_internal_ordinary_ref(ref));
BIF_TRAP3(await_port_send_result_trap, BIF_P, ref, am_true, am_true);
}
default:
break;
}
BIF_RET(am_true);
}
else if (is_external_port(BIF_ARG_1)
&& external_port_dist_entry(BIF_ARG_1) == erts_this_dist_entry) {
goto res_no_proc;
}
if (is_external_pid(BIF_ARG_1)) {
ErtsELink *elnk, *relnk, *pelnk;
int created, replace;
DistEntry *dep;
ErtsLink *lnk, *rlnk;
int code;
ErtsDSigSendContext ctx;
dep = external_pid_dist_entry(BIF_ARG_1);
if (dep == erts_this_dist_entry)
goto res_no_proc;
lnk = erts_link_external_tree_lookup_create(&ERTS_P_LINKS(BIF_P),
&created,
ERTS_LNK_TYPE_DIST_PROC,
BIF_P->common.id,
BIF_ARG_1);
elnk = erts_link_to_elink(lnk);
if (created) {
pelnk = NULL;
relnk = NULL;
rlnk = NULL;
}
else {
if (!elnk->unlinking)
BIF_RET(am_true); /* Already present... */
/*
* We need to replace the link if the connection has changed.
* Prepare a link...
*/
pelnk = (ErtsELink *) erts_link_external_create(ERTS_LNK_TYPE_DIST_PROC,
BIF_P->common.id,
BIF_ARG_1);
ASSERT(eq(pelnk->ld.proc.other.item, BIF_ARG_1));
ASSERT(pelnk->ld.dist.other.item == BIF_P->common.id);
/* Release pelnk if not used as replacement... */
relnk = pelnk;
rlnk = &pelnk->ld.proc;
}
replace = 0;
ASSERT(eq(elnk->ld.proc.other.item, BIF_ARG_1));
ASSERT(elnk->ld.dist.other.item == BIF_P->common.id);
code = erts_dsig_prepare(&ctx, dep, BIF_P,
ERTS_PROC_LOCK_MAIN,
ERTS_DSP_RLOCK, 0, 1, 1);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
if (created || elnk->unlinking) {
if (elnk->unlinking) {
/*
* Currently unlinking an old link from an old connection; replace
* old link with the prepared one...
*/
relnk = NULL;
rlnk = lnk;
elnk = pelnk;
replace = !0;
}
erts_link_set_dead_dist(&elnk->ld.dist, dep->sysname);
}
erts_proc_sig_send_link_exit_noconnection(&elnk->ld.dist);
break;
case ERTS_DSIG_PREP_PENDING:
case ERTS_DSIG_PREP_CONNECTED: {
/*
* We have a connection (or a pending connection).
* Setup link and enqueue link signal.
*/
if (created
|| (elnk->unlinking
&& elnk->dist->connection_id != ctx.connection_id)) {
int inserted;
if (!created) {
/*
* Currently unlinking an old link from an old connection; replace
* old link with the prepared one...
*/
rlnk = lnk;
if (erts_link_dist_delete(&elnk->ld.dist))
relnk = elnk;
else
relnk = NULL;
elnk = pelnk;
replace = !0;
}
inserted = erts_link_dist_insert(&elnk->ld.dist, dep->mld);
ASSERT(inserted); (void)inserted;
}
erts_de_runlock(dep);
code = erts_dsig_send_link(&ctx, BIF_P->common.id, BIF_ARG_1);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
ASSERT(code == ERTS_DSIG_SEND_OK);
break;
}
default:
ERTS_ASSERT(! "Invalid dsig prepare result");
}
if (replace) {
ASSERT(pelnk);
erts_link_tree_replace(&ERTS_P_LINKS(BIF_P), rlnk, &pelnk->ld.proc);
}
if (relnk)
erts_link_release_both(&relnk->ld);
else if (rlnk)
erts_link_release(rlnk);
elnk->unlinking = 0;
BIF_RET(am_true);
}
BIF_ERROR(BIF_P, BADARG);
res_no_proc:
if (BIF_P->flags & F_TRAP_EXIT) {
ErtsProcLocks locks = ERTS_PROC_LOCK_MAIN;
erts_deliver_exit_message(BIF_ARG_1, BIF_P, &locks, am_noproc, NIL);
erts_proc_unlock(BIF_P, ~ERTS_PROC_LOCK_MAIN & locks);
BIF_RET(am_true);
}
else {
/*
* This behaviour is *really* sad but link/1 has
* behaved like this for ages (and this behaviour is
* actually documented)... :'-(
*
* The proper behavior would have been to
* send calling process an exit signal..
*/
BIF_ERROR(BIF_P, EXC_NOPROC);
}
}
static Eterm
demonitor(Process *c_p, Eterm ref, Eterm *multip)
{
ErtsMonitor *mon; /* The monitor entry to delete */
*multip = am_false;
if (is_not_internal_ref(ref)) {
if (is_external_ref(ref)
&& (erts_this_dist_entry
== external_ref_dist_entry(ref))) {
return am_false;
}
return am_badarg; /* Not monitored by this monitor's ref */
}
mon = erts_monitor_tree_lookup(ERTS_P_MONITORS(c_p), ref);
if (!mon)
return am_false;
if (!erts_monitor_is_origin(mon))
return am_badarg;
if (mon->type == ERTS_MON_TYPE_ALIAS)
return am_false; /* Not a monitor (may have been...) */
switch (mon->flags & ERTS_ML_STATE_ALIAS_MASK) {
case ERTS_ML_STATE_ALIAS_UNALIAS: {
ErtsMonitorData *amdp = erts_monitor_create(ERTS_MON_TYPE_ALIAS,
ref,
c_p->common.id,
NIL,
NIL,
THE_NON_VALUE);
amdp->origin.flags = mon->flags & ERTS_ML_STATE_ALIAS_MASK;
mon->flags &= ~ERTS_ML_STATE_ALIAS_MASK;
erts_monitor_tree_replace(&ERTS_P_MONITORS(c_p), mon, &amdp->origin);
break;
}
case ERTS_ML_STATE_ALIAS_ONCE:
case ERTS_ML_STATE_ALIAS_DEMONITOR:
/* fall through... */
default:
erts_monitor_tree_delete(&ERTS_P_MONITORS(c_p), mon);
break;
}
switch (mon->type) {
case ERTS_MON_TYPE_TIME_OFFSET:
*multip = am_true;
erts_demonitor_time_offset(mon);
return am_true;
case ERTS_MON_TYPE_PORT: {
Port *prt;
ASSERT(is_internal_port(mon->other.item));
prt = erts_port_lookup(mon->other.item, ERTS_PORT_SFLGS_DEAD);
if (!prt || erts_port_demonitor(c_p, prt, mon) == ERTS_PORT_OP_DROPPED)
erts_monitor_release(mon);
return am_true;
}
case ERTS_MON_TYPE_DIST_PORT: {
ASSERT(is_external_port(mon->other.item));
ASSERT(external_pid_dist_entry(mon->other.item)
== erts_this_dist_entry);
erts_monitor_release(mon);
return am_true;
}
case ERTS_MON_TYPE_PROC:
erts_proc_sig_send_demonitor(&c_p->common, c_p->common.id, 0, mon);
return am_true;
case ERTS_MON_TYPE_DIST_PROC: {
ErtsMonitorData *mdp = erts_monitor_to_data(mon);
Eterm to = mon->other.item;
DistEntry *dep;
int code = ERTS_DSIG_SEND_OK;
int deleted;
ErtsDSigSendContext ctx;
if (mon->flags & ERTS_ML_FLG_SPAWN_PENDING) {
/*
* Not allowed to remove this until spawn
* operation has succeeded; restore monitor...
*/
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), mon);
return am_false;
}
ASSERT(is_external_pid(to) || is_node_name_atom(to));
if (is_external_pid(to))
dep = external_pid_dist_entry(to);
else /* Monitoring a name at node to */
dep = erts_sysname_to_connected_dist_entry(to);
if (!dep || dep == erts_this_dist_entry) {
erts_monitor_release(mon);
return am_false;
}
code = erts_dsig_prepare(&ctx, dep, c_p, ERTS_PROC_LOCK_MAIN,
ERTS_DSP_RLOCK, 0, 1, 0);
deleted = erts_monitor_dist_delete(&mdp->u.target);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
/*
* In the smp case this is possible if the node goes
* down just before the call to demonitor.
*/
break;
case ERTS_DSIG_PREP_PENDING:
case ERTS_DSIG_PREP_CONNECTED: {
Eterm watched;
erts_de_runlock(dep);
if (mon->flags & ERTS_ML_FLG_NAME)
watched = ((ErtsMonitorDataExtended *) mdp)->u.name;
else
watched = to;
/*
* Soft (no force) send, use ->data in dist slot
* monitor list since in case of monitor name
* the atom is stored there. Yield if necessary.
*/
code = erts_dsig_send_demonitor(&ctx, c_p->common.id,
watched, mdp->ref);
break;
}
default:
ERTS_INTERNAL_ERROR("invalid result from erts_dsig_prepare()");
break;
}
if (deleted)
erts_monitor_release(&mdp->u.target);
erts_monitor_release(mon);
return code == ERTS_DSIG_SEND_YIELD ? am_yield : am_true;
}
default:
ERTS_INTERNAL_ERROR("Unexpected monitor type");
return am_false;
}
}
BIF_RETTYPE demonitor_1(BIF_ALIST_1)
{
Eterm multi;
switch (demonitor(BIF_P, BIF_ARG_1, &multi)) {
case am_false:
case am_true:
BIF_RET(am_true);
case am_yield:
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
case am_badarg:
default:
BIF_ERROR(BIF_P, BADARG);
}
}
BIF_RETTYPE demonitor_2(BIF_ALIST_2)
{
BIF_RETTYPE res;
Eterm multi = am_false;
int info = 0;
int flush = 0;
Eterm list = BIF_ARG_2;
while (is_list(list)) {
Eterm* consp = list_val(list);
switch (CAR(consp)) {
case am_flush:
flush = 1;
break;
case am_info:
info = 1;
break;
default:
goto badarg;
}
list = CDR(consp);
}
if (is_not_nil(list))
goto badarg;
res = am_true;
switch (demonitor(BIF_P, BIF_ARG_1, &multi)) {
case am_false:
if (info)
res = am_false;
if (flush) {
flush_messages:
BIF_TRAP3(flush_monitor_messages_trap, BIF_P,
BIF_ARG_1, multi, res);
}
/* Fall through... */
case am_true:
if (multi == am_true && flush)
goto flush_messages;
BIF_RET(res);
case am_yield:
/* return true after yield... */
if (flush) {
ERTS_VBUMP_ALL_REDS(BIF_P);
goto flush_messages;
}
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
case am_badarg:
default:
break;
}
badarg:
BIF_ERROR(BIF_P, BADARG);
}
Uint16
erts_monitor_opts(Eterm opts, Eterm *tag)
{
Uint16 add_oflags = 0;
*tag = THE_NON_VALUE;
while (is_list(opts)) {
Eterm *tpl, *cons, opt;
cons = list_val(opts);
opt = CAR(cons);
if (is_not_tuple(opt))
return (Uint16) ~0;
tpl = tuple_val(opt);
switch (arityval(tpl[0])) {
case 2:
switch (tpl[1]) {
case am_alias:
add_oflags &= ~ERTS_ML_STATE_ALIAS_MASK;
switch (tpl[2]) {
case am_explicit_unalias:
add_oflags |= ERTS_ML_STATE_ALIAS_UNALIAS;
break;
case am_demonitor:
add_oflags |= ERTS_ML_STATE_ALIAS_DEMONITOR;
break;
case am_reply_demonitor:
add_oflags |= ERTS_ML_STATE_ALIAS_ONCE;
break;
default:
return (Uint16) ~0;
}
break;
case am_tag:
*tag = tpl[2];
break;
default:
return (Uint16) ~0;
}
break;
default:
return (Uint16) ~0;
}
opts = CDR(cons);
}
if (is_not_nil(opts))
return (Uint16) ~0;
return add_oflags;
}
static BIF_RETTYPE monitor(Process *c_p, Eterm type, Eterm target,
Uint16 add_oflags, Eterm tag)
{
Eterm ref, id, name;
ErtsMonitorData *mdp;
BIF_RETTYPE ret_val;
ref = ((add_oflags & ERTS_ML_STATE_ALIAS_MASK)
? erts_make_pid_ref(c_p)
: erts_make_ref(c_p));
ERTS_BIF_PREP_RET(ret_val, ref); /* Prepare for success... */
if (type == am_process) {
DistEntry *dep;
int byname;
if (is_internal_pid(target)) {
name = NIL;
id = target;
local_process:
if (id != c_p->common.id) {
mdp = erts_monitor_create(ERTS_MON_TYPE_PROC,
ref, c_p->common.id,
id, name, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p),
&mdp->origin);
if (is_not_internal_pid(id)
|| !erts_proc_sig_send_monitor(&c_p->common, c_p->common.id,
&mdp->u.target, id)) {
erts_proc_sig_send_monitor_down(NULL, id,
&mdp->u.target,
am_noproc);
}
}
goto done;
}
if (is_atom(target)) {
local_named_process:
name = target;
id = erts_whereis_name_to_id(c_p, target);
goto local_process;
}
if (is_external_pid(target)) {
ErtsDSigSendContext ctx;
int code;
dep = external_pid_dist_entry(target);
if (dep == erts_this_dist_entry) {
mdp = erts_monitor_create(ERTS_MON_TYPE_DIST_PROC, ref,
c_p->common.id, target,
NIL, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), &mdp->origin);
erts_proc_sig_send_monitor_down(NULL, target,
&mdp->u.target, am_noproc);
goto done;
}
id = target;
name = NIL;
byname = 0;
remote_process:
mdp = erts_monitor_create(ERTS_MON_TYPE_DIST_PROC, ref,
c_p->common.id, id, name, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), &mdp->origin);
code = erts_dsig_prepare(&ctx, dep,
c_p, ERTS_PROC_LOCK_MAIN,
ERTS_DSP_RLOCK, 0, 1, 1);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
erts_monitor_set_dead_dist(&mdp->u.target, dep->sysname);
erts_proc_sig_send_monitor_down(NULL, id,
&mdp->u.target,
am_noconnection);
code = ERTS_DSIG_SEND_OK;
break;
case ERTS_DSIG_PREP_PENDING:
case ERTS_DSIG_PREP_CONNECTED: {
int inserted = erts_monitor_dist_insert(&mdp->u.target, dep->mld);
ASSERT(inserted); (void)inserted;
erts_de_runlock(dep);
code = erts_dsig_send_monitor(&ctx, c_p->common.id, target, ref);
break;
}
default:
ERTS_ASSERT(! "Invalid dsig prepare result");
code = ERTS_DSIG_SEND_OK;
break;
}
if (byname)
erts_deref_dist_entry(dep);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_PREP_YIELD_RETURN(ret_val, c_p, ref);
goto done;
}
if (is_tuple(target)) {
Eterm *tpl = tuple_val(target);
if (arityval(tpl[0]) != 2)
goto badarg;
if (is_not_atom(tpl[1]) || is_not_atom(tpl[2]))
goto badarg;
if (tpl[2] != am_Noname && !erts_is_this_node_alive())
goto badarg;
target = tpl[1];
dep = erts_find_or_insert_dist_entry(tpl[2]);
if (dep == erts_this_dist_entry) {
erts_deref_dist_entry(dep);
goto local_named_process;
}
id = dep->sysname;
name = target;
byname = 1;
goto remote_process;
}
/* badarg... */
}
else if (type == am_port) {
if (is_internal_port(target)) {
Port *prt;
name = NIL;
id = target;
local_port:
mdp = erts_monitor_create(ERTS_MON_TYPE_PORT, ref,
c_p->common.id, id,
name, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), &mdp->origin);
prt = erts_port_lookup(id, ERTS_PORT_SFLGS_INVALID_LOOKUP);
if (!prt || erts_port_monitor(c_p, prt, &mdp->u.target) == ERTS_PORT_OP_DROPPED) {
erts_proc_sig_send_monitor_down(prt ? &prt->common : NULL, id,
&mdp->u.target, am_noproc);
}
goto done;
}
if (is_atom(target)) {
local_named_port:
name = target;
id = erts_whereis_name_to_id(c_p, target);
goto local_port;
}
if (is_external_port(target)) {
if (erts_this_dist_entry == external_port_dist_entry(target)) {
mdp = erts_monitor_create(ERTS_MON_TYPE_DIST_PORT, ref,
c_p->common.id, target,
NIL, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), &mdp->origin);
erts_proc_sig_send_monitor_down(NULL, target, &mdp->u.target, am_noproc);
goto done;
}
goto badarg;
}
if (is_tuple(target)) {
Eterm *tpl = tuple_val(target);
if (arityval(tpl[0]) != 2)
goto badarg;
if (is_not_atom(tpl[1]) || is_not_atom(tpl[2]))
goto badarg;
if (tpl[2] == erts_this_dist_entry->sysname) {
target = tpl[1];
goto local_named_port;
}
}
/* badarg... */
}
else if (type == am_time_offset) {
if (target != am_clock_service)
goto badarg;
mdp = erts_monitor_create(ERTS_MON_TYPE_TIME_OFFSET,
ref, c_p->common.id,
am_clock_service, NIL, tag);
mdp->origin.flags |= add_oflags;
erts_monitor_tree_insert(&ERTS_P_MONITORS(c_p), &mdp->origin);
erts_monitor_time_offset(&mdp->u.target);
goto done;
} else {
c_p->fvalue = am_badtype;
BIF_ERROR(c_p, BADARG | EXF_HAS_EXT_INFO);
}
badarg:
ERTS_BIF_PREP_ERROR(ret_val, c_p, BADARG);
done:
return ret_val;
}
BIF_RETTYPE monitor_2(BIF_ALIST_2)
{
return monitor(BIF_P, BIF_ARG_1, BIF_ARG_2, (Uint16) 0, THE_NON_VALUE);
}
BIF_RETTYPE monitor_3(BIF_ALIST_3)
{
Eterm tag;
Uint16 oflags = erts_monitor_opts(BIF_ARG_3, &tag);
if (oflags == (Uint16) ~0) {
BIF_P->fvalue = am_badopt;
BIF_ERROR(BIF_P, BADARG | EXF_HAS_EXT_INFO);
}
return monitor(BIF_P, BIF_ARG_1, BIF_ARG_2, oflags, tag);
}
/**********************************************************************/
/* this is a combination of the spawn and link BIFs */
BIF_RETTYPE spawn_link_3(BIF_ALIST_3)
{
ErlSpawnOpts so;
Eterm pid;
Eterm tmp_heap[2];
ERTS_SET_DEFAULT_SPAWN_OPTS(&so);
so.flags |= SPO_LINK;
so.opts = CONS(&tmp_heap[0], am_link, so.opts);
pid = erl_create_process(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else {
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, pid, ERTS_MODIFIED_TIMING_DELAY);
}
BIF_RET(pid);
}
}
/**********************************************************************/
BIF_RETTYPE spawn_opt_4(BIF_ALIST_4)
{
ErlSpawnOpts so;
Eterm pid;
Eterm res;
int opts_error;
/*
* Fail order:
* - Bad types
* - Bad options
*/
opts_error = erts_parse_spawn_opts(&so, BIF_ARG_4, NULL, 0);
if (opts_error) {
Sint arity;
BIF_P->fvalue = am_badopt;
if (is_not_atom(BIF_ARG_1) || is_not_atom(BIF_ARG_2))
BIF_ERROR(BIF_P, BADARG | EXF_HAS_EXT_INFO);
arity = erts_list_length(BIF_ARG_3);
if (arity < 0)
BIF_ERROR(BIF_P, BADARG | EXF_HAS_EXT_INFO);
if (arity > MAX_SMALL)
BIF_ERROR(BIF_P, SYSTEM_LIMIT | EXF_HAS_EXT_INFO);
BIF_ERROR(BIF_P, BADARG | EXF_HAS_EXT_INFO);
}
/*
* Spawn the process.
*/
so.opts = BIF_ARG_4;
so.tag = am_spawn_reply;
pid = erl_create_process(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else if (so.flags & SPO_MONITOR) {
Eterm* hp = HAlloc(BIF_P, 3);
res = TUPLE2(hp, pid, so.mref);
} else {
res = pid;
}
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, res, ERTS_MODIFIED_TIMING_DELAY);
}
else {
BIF_RET(res);
}
}
/**********************************************************************/
BIF_RETTYPE erts_internal_spawn_request_4(BIF_ALIST_4)
{
ErlSpawnOpts so;
Eterm *hp;
Sint arity;
int opts_error;
Eterm tag, tmp, error;
if (!is_atom(BIF_ARG_1))
goto badarg;
if (!is_atom(BIF_ARG_2))
goto badarg;
arity = erts_list_length(BIF_ARG_3);
if (arity < 0)
goto badarg;
/*
* Fail order:
* - Bad types
* - Bad options
*/
opts_error = erts_parse_spawn_opts(&so, BIF_ARG_4, &tag, !0);
if (arity > MAX_SMALL)
goto system_limit;
if (opts_error) {
if (opts_error > 0) {
/* Return `badopt`, meaning that the Options argument
* is not a proper list. */
BIF_RET(am_badopt);
}
goto badopt;
}
hp = HAlloc(BIF_P, 4 + 4 + 2);
/* Make argument list for erts_internal:spawn_init/1 */
tmp = TUPLE3(hp, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3); hp += 4;
tmp = CONS(hp, tmp, NIL); hp += 2;
so.mfa = TUPLE3(hp, BIF_ARG_1, BIF_ARG_2, make_small(arity)); hp += 4;
so.flags |= SPO_ASYNC;
so.mref = THE_NON_VALUE;
so.tag = tag;
so.opts = BIF_ARG_4;
/*
* Spawn the process.
*/
tmp = erl_create_process(BIF_P, am_erts_internal, am_spawn_init, tmp, &so);
if (is_non_value(tmp)) {
switch (so.error_code) {
case SYSTEM_LIMIT:
goto system_limit;
case BADARG:
default:
ERTS_INTERNAL_ERROR("Unexpected error from erl_create_process()");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
ASSERT(is_internal_pid(tmp));
if (ERTS_USE_MODIFIED_TIMING()) {