-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ds-ipc-pal-socket.c
1601 lines (1394 loc) · 36.5 KB
/
ds-ipc-pal-socket.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifdef FEATURE_PERFTRACING_STANDALONE_PAL
#define EP_NO_RT_DEPENDENCY
#endif
#include "ds-rt-config.h"
#ifdef ENABLE_PERFTRACING
#define DS_IMPL_IPC_PAL_SOCKET_GETTER_SETTER
#include "ds-ipc-pal-socket.h"
#if defined (DS_IPC_PAL_TCP)
#define DS_IPC_PAL_AF_INET
#define DS_IPC_PAL_AF_INET6
#elif defined (DS_IPC_PAL_UDS)
#define DS_IPC_PAL_AF_UNIX
#else
#error "Unsupported PAL socket configuration"
#endif
#ifndef HOST_WIN32
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/stat.h>
#if __GNUC__
#include <poll.h>
#else
#include <sys/poll.h>
#endif
#ifdef DS_IPC_PAL_TCP
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#endif
#endif
#ifdef HOST_WIN32
#define DS_IPC_INVALID_SOCKET INVALID_SOCKET
#define DS_IPC_SOCKET_ERROR SOCKET_ERROR
#define DS_IPC_SOCKET_ERROR_WOULDBLOCK WSAEWOULDBLOCK
#define DS_IPC_SOCKET_ERROR_TIMEDOUT WSAETIMEDOUT
#define DS_IPC_ERROR_INTERRUPT WSAEINTR
typedef ADDRINFOA ds_ipc_addrinfo_t;
typedef WSAPOLLFD ds_ipc_pollfd_t;
typedef int ds_ipc_mode_t;
#else
#define DS_IPC_INVALID_SOCKET -1
#define DS_IPC_SOCKET_ERROR -1
#define DS_IPC_SOCKET_ERROR_WOULDBLOCK EINPROGRESS
#define DS_IPC_SOCKET_ERROR_TIMEDOUT ETIMEDOUT
#define DS_IPC_ERROR_INTERRUPT EINTR
typedef struct addrinfo ds_ipc_addrinfo_t;
typedef struct pollfd ds_ipc_pollfd_t;
typedef mode_t ds_ipc_mode_t;
#endif
#ifndef EP_NO_RT_DEPENDENCY
#include "ds-rt.h"
#else
#ifdef FEATURE_CORECLR
#include <pal.h>
#include "processdescriptor.h"
#endif
#ifndef ep_return_null_if_nok
#define ep_return_null_if_nok(expr) do { if (!(expr)) return NULL; } while (0)
#endif
#ifndef ep_raise_error_if_nok
#define ep_raise_error_if_nok(expr) do { if (!(expr)) goto ep_on_error; } while (0)
#endif
#ifndef ep_raise_error
#define ep_raise_error() do { goto ep_on_error; } while (0)
#endif
#ifndef ep_exit_error_handler
#define ep_exit_error_handler() do { goto ep_on_exit; } while (0)
#endif
#ifndef EP_ASSERT
#define EP_ASSERT assert
#endif
#ifndef DS_ENTER_BLOCKING_PAL_SECTION
#define DS_ENTER_BLOCKING_PAL_SECTION
#endif
#ifndef DS_EXIT_BLOCKING_PAL_SECTION
#define DS_EXIT_BLOCKING_PAL_SECTION
#endif
#undef ep_rt_object_alloc
#define ep_rt_object_alloc(obj_type) ((obj_type *)calloc(1, sizeof(obj_type)))
static
inline
void
ep_rt_object_free (void *ptr)
{
if (ptr)
free (ptr);
}
#undef ep_rt_object_array_alloc
#define ep_rt_object_array_alloc(obj_type,size) ((obj_type *)calloc (size, sizeof(obj_type)))
static
inline
void
ep_rt_object_array_free (void *ptr)
{
if (ptr)
free (ptr);
}
#endif
static bool _ipc_pal_socket_init = false;
/*
* Forward declares of all static functions.
*/
static
int
ipc_socket_connect (
ds_ipc_socket_t s,
ds_ipc_socket_address_t *address,
int address_len,
uint32_t timeout_ms);
static
bool
ipc_socket_recv (
ds_ipc_socket_t s,
uint8_t * buffer,
ssize_t bytes_to_read,
ssize_t *bytes_read);
static
bool
ipc_socket_send (
ds_ipc_socket_t s,
const uint8_t *buffer,
ssize_t bytes_to_write,
ssize_t *bytes_written);
static
bool
ipc_transport_get_default_name (
ep_char8_t *name,
int32_t name_len);
static
bool
ipc_init_listener (
DiagnosticsIpc *ipc,
ds_ipc_error_callback_func callback);
static
DiagnosticsIpc *
ipc_alloc_uds_address (
DiagnosticsIpc *ipc,
DiagnosticsIpcConnectionMode mode,
const ep_char8_t *ipc_name);
static
DiagnosticsIpc *
ipc_alloc_tcp_address (
DiagnosticsIpc *ipc,
DiagnosticsIpcConnectionMode mode,
const ep_char8_t *ipc_name);
static
void
ipc_stream_free_func (void *object);
static
bool
ipc_stream_read_func (
void *object,
uint8_t *buffer,
uint32_t bytes_to_read,
uint32_t *bytes_read,
uint32_t timeout_ms);
static
bool
ipc_stream_write_func (
void *object,
const uint8_t *buffer,
uint32_t bytes_to_write,
uint32_t *bytes_written,
uint32_t timeout_ms);
static
bool
ipc_stream_flush_func (void *object);
static
bool
ipc_stream_close_func (void *object);
static
DiagnosticsIpcStream *
ipc_stream_alloc (
int client_socket,
DiagnosticsIpcConnectionMode mode);
/*
* DiagnosticsIpc Socket PAL.
*/
static
inline
int
ipc_get_last_error (void)
{
#ifdef HOST_WIN32
return WSAGetLastError ();
#else
return errno;
#endif
}
static
inline
void
ipc_set_last_error (int error)
{
#ifdef HOST_WIN32
WSASetLastError (error);
#else
errno = error;
#endif
}
static
inline
int
ipc_get_last_socket_error (ds_ipc_socket_t s)
{
int opt_value = DS_IPC_SOCKET_ERROR;
int result_getsockopt;
#ifdef HOST_WIN32
int opt_value_len = sizeof (opt_value);
DS_ENTER_BLOCKING_PAL_SECTION;
result_getsockopt = getsockopt (s, SOL_SOCKET, SO_ERROR, (char *)&opt_value, &opt_value_len);
DS_EXIT_BLOCKING_PAL_SECTION;
#else
socklen_t opt_value_len = sizeof (opt_value);
DS_ENTER_BLOCKING_PAL_SECTION;
result_getsockopt = getsockopt (s, SOL_SOCKET, SO_ERROR, &opt_value, &opt_value_len);
DS_EXIT_BLOCKING_PAL_SECTION;
#endif
return result_getsockopt == 0 ? opt_value : result_getsockopt;
}
#ifndef EP_NO_RT_DEPENDENCY
static
inline
bool
ipc_retry_syscall (int result)
{
return result == -1 && ipc_get_last_error () == DS_IPC_ERROR_INTERRUPT && !ep_rt_process_shutdown ();
}
#else
static
inline
bool
ipc_retry_syscall (int result)
{
return result == -1 && ipc_get_last_error () == DS_IPC_ERROR_INTERRUPT;
}
#endif
static
inline
ds_ipc_mode_t
ipc_socket_set_default_umask (void)
{
#if defined(DS_IPC_PAL_AF_UNIX) && (defined(__APPLE__) || defined(__FreeBSD__))
// This will set the default permission bit to 600
return umask (~(S_IRUSR | S_IWUSR));
#else
return 0;
#endif
}
static
inline
void
ipc_socket_reset_umask (ds_ipc_mode_t mode)
{
#if defined(DS_IPC_PAL_AF_UNIX) && (defined(__APPLE__) || defined(__FreeBSD__))
umask (mode);
#endif
}
static
inline
ds_ipc_socket_t
ipc_socket_create_uds (DiagnosticsIpc *ipc)
{
#if defined(DS_IPC_PAL_AF_UNIX)
EP_ASSERT (ipc->server_address_family == AF_UNIX);
ds_ipc_socket_t new_socket = DS_IPC_INVALID_SOCKET;
int socket_type = SOCK_STREAM;
#ifdef SOCK_CLOEXEC
socket_type |= SOCK_CLOEXEC;
#endif // SOCK_CLOEXEC
DS_ENTER_BLOCKING_PAL_SECTION;
new_socket = socket (ipc->server_address_family, socket_type, 0);
#ifndef SOCK_CLOEXEC
if (new_socket != DS_IPC_INVALID_SOCKET)
fcntl (new_socket, F_SETFD, FD_CLOEXEC); // ignore any failures; this is best effort
#endif // SOCK_CLOEXEC
DS_EXIT_BLOCKING_PAL_SECTION;
return new_socket;
#endif // DS_IPC_PAL_AF_UNIX
return DS_IPC_INVALID_SOCKET;
}
static
inline
ds_ipc_socket_t
ipc_socket_create_tcp (DiagnosticsIpc *ipc)
{
#if defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
#if defined(DS_IPC_PAL_AF_INET) && defined(DS_IPC_PAL_AF_INET6)
EP_ASSERT (ipc->server_address_family == AF_INET || ipc->server_address_family == AF_INET6);
#else
EP_ASSERT (ipc->server_address_family == AF_INET);
#endif
ds_ipc_socket_t new_socket = DS_IPC_INVALID_SOCKET;
int socket_type = SOCK_STREAM;
#ifdef SOCK_CLOEXEC
socket_type |= SOCK_CLOEXEC;
#endif // SOCK_CLOEXEC
DS_ENTER_BLOCKING_PAL_SECTION;
new_socket = socket (ipc->server_address_family, socket_type, IPPROTO_TCP);
if (new_socket != DS_IPC_INVALID_SOCKET) {
#ifndef SOCK_CLOEXEC
#ifndef HOST_WIN32
fcntl (new_socket, F_SETFD, FD_CLOEXEC); // ignore any failures; this is best effort
#endif // HOST_WIN32
#endif // SOCK_CLOEXEC
int option_value = 1;
setsockopt (new_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&option_value, sizeof (option_value));
if (ipc->mode == DS_IPC_CONNECTION_MODE_LISTEN) {
setsockopt (new_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&option_value, sizeof (option_value));
#ifdef DS_IPC_PAL_AF_INET6
if (ipc->is_dual_mode) {
option_value = 0;
setsockopt (new_socket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&option_value, sizeof (option_value));
}
}
#endif
}
DS_EXIT_BLOCKING_PAL_SECTION;
return new_socket;
#endif
return DS_IPC_INVALID_SOCKET;
}
static
inline
ds_ipc_socket_t
ipc_socket_create (DiagnosticsIpc *ipc)
{
#if defined(DS_IPC_PAL_AF_UNIX)
return ipc_socket_create_uds (ipc);
#elif defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
return ipc_socket_create_tcp (ipc);
#else
#error "Unknown address family"
#endif
}
static
inline
int
ipc_socket_close (ds_ipc_socket_t s)
{
int result_close;
DS_ENTER_BLOCKING_PAL_SECTION;
#ifdef HOST_WIN32
result_close = closesocket (s);
#else
do {
result_close = close (s);
} while (ipc_retry_syscall (result_close));
#endif
DS_EXIT_BLOCKING_PAL_SECTION;
return result_close;
}
static
inline
int
ipc_socket_set_permission (ds_ipc_socket_t s)
{
#if defined(DS_IPC_PAL_AF_UNIX) && !(defined(__APPLE__) || defined(__FreeBSD__))
int result_fchmod;
DS_ENTER_BLOCKING_PAL_SECTION;
do {
result_fchmod = fchmod (s, S_IRUSR | S_IWUSR);
} while (ipc_retry_syscall (result_fchmod));
DS_EXIT_BLOCKING_PAL_SECTION;
return result_fchmod;
#else
return 0;
#endif
}
static
inline
int
ipc_socket_set_blocking (
ds_ipc_socket_t s,
bool blocking)
{
int result = DS_IPC_SOCKET_ERROR;
DS_ENTER_BLOCKING_PAL_SECTION;
#ifdef HOST_WIN32
u_long blocking_mode = blocking ? 0 : 1;
result = ioctlsocket (s, FIONBIO, &blocking_mode);
#else
result = fcntl (s, F_GETFL, 0);
if (result != -1)
result = fcntl (s, F_SETFL, blocking ? (result & (~O_NONBLOCK)) : (result | (O_NONBLOCK)));
#endif
DS_EXIT_BLOCKING_PAL_SECTION;
return result;
}
static
inline
int
ipc_poll_fds (
ds_ipc_pollfd_t *fds,
size_t nfds,
uint32_t timeout)
{
int result_poll;
DS_ENTER_BLOCKING_PAL_SECTION;
#ifdef HOST_WIN32
result_poll = WSAPoll (fds, (ULONG)nfds, (INT)timeout);
#else
#ifndef EP_NO_RT_DEPENDENCY
int64_t start = 0;
int64_t stop = 0;
bool retry_poll = false;
do {
if (timeout != EP_INFINITE_WAIT)
start = ep_rt_perf_counter_query ();
result_poll = poll (fds, nfds, (int)timeout);
retry_poll = ipc_retry_syscall (result_poll);
if (retry_poll && timeout != EP_INFINITE_WAIT) {
stop = ep_rt_perf_counter_query ();
uint32_t waited_ms = (uint32_t)(((stop - start) * 1000) / ep_rt_perf_frequency_query ());
timeout = (waited_ms < timeout) ? timeout - waited_ms : 0;
}
if (retry_poll && timeout == 0)
result_poll = 0; // Return time out.
} while (retry_poll && timeout != 0);
#else
do {
result_poll = poll (fds, nfds, (int)timeout);
} while (ipc_retry_syscall (result_poll));
#endif
#endif
DS_EXIT_BLOCKING_PAL_SECTION;
return result_poll;
}
static
inline
int
ipc_socket_bind (
ds_ipc_socket_t s,
const ds_ipc_socket_address_t *address,
ds_ipc_socket_len_t address_len)
{
int result_bind;
DS_ENTER_BLOCKING_PAL_SECTION;
result_bind = bind (s, address, address_len);
DS_EXIT_BLOCKING_PAL_SECTION;
return result_bind;
}
static
inline
int
ipc_socket_listen (
ds_ipc_socket_t s,
int backlog)
{
int result_listen;
DS_ENTER_BLOCKING_PAL_SECTION;
result_listen = listen (s, backlog);
DS_EXIT_BLOCKING_PAL_SECTION;
return result_listen;
}
static
inline
ds_ipc_socket_t
ipc_socket_accept (
ds_ipc_socket_t s,
ds_ipc_socket_address_t *address,
ds_ipc_socket_len_t *address_len)
{
ds_ipc_socket_t client_socket;
DS_ENTER_BLOCKING_PAL_SECTION;
do {
#if HAVE_ACCEPT4 && defined(SOCK_CLOEXEC)
client_socket = accept4 (s, address, address_len, SOCK_CLOEXEC);
#else
client_socket = accept (s, address, address_len);
#endif
} while (ipc_retry_syscall (client_socket));
#ifndef HOST_WIN32
#if !HAVE_ACCEPT4 || !defined(SOCK_CLOEXEC)
#if defined(FD_CLOEXEC)
if (client_socket != -1)
{
// ignore any failures; this is best effort
fcntl (client_socket, F_SETFD, FD_CLOEXEC);
}
#endif
#endif
#endif
DS_EXIT_BLOCKING_PAL_SECTION;
return client_socket;
}
static
int
ipc_socket_connect (
ds_ipc_socket_t s,
ds_ipc_socket_address_t *address,
int address_len,
uint32_t timeout_ms)
{
int result_connect;
// We don't expect this to block on Unix Domain Socket. `connect` may block until the
// TCP handshake is complete for TCP/IP sockets. On UDS `connect` will return even if
// the server hasn't called `accept`, so no need to check for timeout or connect error.
#if defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
if (timeout_ms != DS_IPC_TIMEOUT_INFINITE) {
// Set socket to none blocking.
ipc_socket_set_blocking (s, false);
}
#endif
DS_ENTER_BLOCKING_PAL_SECTION;
do {
result_connect = connect (s, address, address_len);
} while (ipc_retry_syscall (result_connect));
DS_EXIT_BLOCKING_PAL_SECTION;
#if defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
if (timeout_ms != DS_IPC_TIMEOUT_INFINITE) {
if (result_connect == DS_IPC_SOCKET_ERROR) {
if (ipc_get_last_error () == DS_IPC_SOCKET_ERROR_WOULDBLOCK) {
ds_ipc_pollfd_t pfd;
pfd.fd = s;
pfd.events = POLLOUT;
int result_poll = ipc_poll_fds (&pfd, 1, timeout_ms);
if (result_poll == 0) {
// timeout
ipc_set_last_error (DS_IPC_SOCKET_ERROR_TIMEDOUT);
result_connect = DS_IPC_SOCKET_ERROR;
} else if (result_poll < 0 || !(pfd.revents & POLLOUT)) {
// error
result_connect = DS_IPC_SOCKET_ERROR;
} else {
// success, check non-blocking connect result.
result_connect = ipc_get_last_socket_error (s);
if (result_connect != 0 && result_connect != DS_IPC_SOCKET_ERROR) {
ipc_set_last_error (result_connect);
result_connect = DS_IPC_SOCKET_ERROR;
}
}
}
}
}
if (timeout_ms != DS_IPC_TIMEOUT_INFINITE) {
// Reset socket to blocking.
int last_error = ipc_get_last_error ();
ipc_socket_set_blocking (s, true);
ipc_set_last_error (last_error);
}
#endif
return result_connect;
}
static
bool
ipc_socket_recv (
ds_ipc_socket_t s,
uint8_t * buffer,
ssize_t bytes_to_read,
ssize_t *bytes_read)
{
uint8_t *buffer_cursor = (uint8_t*)buffer;
ssize_t current_bytes_read = 0;
ssize_t total_bytes_read = 0;
bool continue_recv = true;
DS_ENTER_BLOCKING_PAL_SECTION;
while (continue_recv && bytes_to_read - total_bytes_read > 0) {
current_bytes_read = recv (
s,
(char *)buffer_cursor,
bytes_to_read - total_bytes_read,
0);
if (ipc_retry_syscall (current_bytes_read))
continue;
continue_recv = current_bytes_read > 0;
if (!continue_recv)
break;
total_bytes_read += current_bytes_read;
buffer_cursor += current_bytes_read;
}
DS_EXIT_BLOCKING_PAL_SECTION;
*bytes_read = total_bytes_read;
return continue_recv;
}
static
bool
ipc_socket_send (
ds_ipc_socket_t s,
const uint8_t *buffer,
ssize_t bytes_to_write,
ssize_t *bytes_written)
{
uint8_t *buffer_cursor = (uint8_t*)buffer;
ssize_t current_bytes_written = 0;
ssize_t total_bytes_written = 0;
bool continue_send = true;
DS_ENTER_BLOCKING_PAL_SECTION;
while (continue_send && bytes_to_write - total_bytes_written > 0) {
current_bytes_written = send (
s,
(const char *)buffer_cursor,
bytes_to_write - total_bytes_written,
0);
if (ipc_retry_syscall (current_bytes_written))
continue;
continue_send = current_bytes_written != DS_IPC_SOCKET_ERROR;
if (!continue_send)
break;
total_bytes_written += current_bytes_written;
buffer_cursor += current_bytes_written;
}
DS_EXIT_BLOCKING_PAL_SECTION;
*bytes_written = total_bytes_written;
return continue_send;
}
/*
* DiagnosticsIpc.
*/
static
inline
bool
ipc_transport_get_default_name (
ep_char8_t *name,
int32_t name_len)
{
#ifdef DS_IPC_PAL_AF_UNIX
#ifndef EP_NO_RT_DEPENDENCY
return ds_rt_transport_get_default_name (
name,
name_len,
"dotnet-diagnostic",
ep_rt_current_process_get_id (),
NULL,
"socket");
#elif defined (EP_NO_RT_DEPENDENCY) && defined (FEATURE_CORECLR)
// generate the default socket name in TMP Path
const ProcessDescriptor pd = ProcessDescriptor::FromCurrentProcess();
PAL_GetTransportName(
name_len,
name,
"dotnet-diagnostic",
pd.m_Pid,
pd.m_ApplicationGroupId,
"socket");
return true;
#else
return false;
#endif
#else
return false;
#endif
}
static
bool
ipc_init_listener (
DiagnosticsIpc *ipc,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (ipc != NULL);
EP_ASSERT (ipc->mode == DS_IPC_CONNECTION_MODE_LISTEN);
bool success = false;
ds_ipc_mode_t prev_mode = ipc_socket_set_default_umask ();
ds_ipc_socket_t server_socket;
server_socket = ipc_socket_create (ipc);
if (server_socket == DS_IPC_INVALID_SOCKET) {
if (callback)
callback (strerror (ipc_get_last_error ()), ipc_get_last_error ());
EP_ASSERT (!"Failed to create diagnostics IPC socket.");
ep_raise_error ();
}
int result_set_permission;
result_set_permission = ipc_socket_set_permission (server_socket);
if (result_set_permission == DS_IPC_SOCKET_ERROR) {
if (callback)
callback (strerror (ipc_get_last_error ()), ipc_get_last_error ());
EP_ASSERT (!"Failed to set permissions on diagnostics IPC socket.");
ep_raise_error ();
}
int result_bind;
result_bind = ipc_socket_bind (server_socket, ipc->server_address, ipc->server_address_len);
if (result_bind == DS_IPC_SOCKET_ERROR) {
if (callback)
callback (strerror (ipc_get_last_error ()), ipc_get_last_error ());
int result_close;
result_close = ipc_socket_close (server_socket);
if (result_close == DS_IPC_SOCKET_ERROR) {
if (callback)
callback (strerror (ipc_get_last_error ()), ipc_get_last_error ());
}
ep_raise_error ();
}
ipc->server_socket = server_socket;
success = true;
ep_on_exit:
ipc_socket_reset_umask (prev_mode);
return success;
ep_on_error:
success = false;
ep_exit_error_handler ();
}
static
DiagnosticsIpc *
ipc_alloc_uds_address (
DiagnosticsIpc *ipc,
DiagnosticsIpcConnectionMode mode,
const ep_char8_t *ipc_name)
{
#ifdef DS_IPC_PAL_AF_UNIX
EP_ASSERT (ipc != NULL);
struct sockaddr_un *server_address = ep_rt_object_alloc (struct sockaddr_un);
ep_return_null_if_nok (server_address != NULL);
server_address->sun_family = AF_UNIX;
if (ipc_name) {
int32_t result = snprintf (
server_address->sun_path,
sizeof (server_address->sun_path),
"%s",
ipc_name);
if (result <= 0 || result >= (int32_t)(sizeof (server_address->sun_path)))
server_address->sun_path [0] = '\0';
} else {
// generate the default socket name
ipc_transport_get_default_name (
server_address->sun_path,
sizeof (server_address->sun_path));
}
ipc->server_address = (ds_ipc_socket_address_t *)server_address;
ipc->server_address_len = sizeof (struct sockaddr_un);
ipc->server_address_family = server_address->sun_family;
return ipc;
#else
return NULL;
#endif
}
static
DiagnosticsIpc *
ipc_alloc_tcp_address (
DiagnosticsIpc *ipc,
DiagnosticsIpcConnectionMode mode,
const ep_char8_t *ipc_name)
{
#if defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
EP_ASSERT (ipc != NULL);
ep_return_null_if_nok (ipc_name != NULL);
#ifdef DS_IPC_PAL_AF_INET
struct sockaddr_in *server_address = NULL;
#endif
#ifdef DS_IPC_PAL_AF_INET6
struct sockaddr_in6 *server_address6 = NULL;
#endif
ds_ipc_addrinfo_t hints = {0};
ds_ipc_addrinfo_t *info = NULL;
ep_char8_t *address = NULL;
int32_t port = 0;
int result_getaddrinfo = -1;
const ep_char8_t *host_address = NULL;
const ep_char8_t *host_port = NULL;
address = ep_rt_utf8_string_dup (ipc_name);
ep_raise_error_if_nok (address != NULL);
host_address = address;
host_port = strrchr (address, ':');
if (host_port && host_port != host_address) {
size_t host_address_len = host_port - address;
address [host_address_len] = 0;
port = atoi (host_port + 1);
}
ipc->server_address = NULL;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = (mode == DS_IPC_CONNECTION_MODE_LISTEN) ? AI_PASSIVE : 0;
DS_ENTER_BLOCKING_PAL_SECTION;
if (mode == DS_IPC_CONNECTION_MODE_LISTEN && *host_address == '*') {
#ifdef DS_IPC_PAL_AF_INET6
hints.ai_family = AF_INET6;
result_getaddrinfo = getaddrinfo ("[::]", NULL, &hints, &info);
if (!result_getaddrinfo)
ipc->is_dual_mode = true;
#endif
if (result_getaddrinfo != 0) {
hints.ai_family = AF_INET;
result_getaddrinfo = getaddrinfo ("0.0.0.0", NULL, &hints, &info);
}
} else {
result_getaddrinfo = getaddrinfo (host_address, NULL, &hints, &info);
}
DS_EXIT_BLOCKING_PAL_SECTION;
if (!result_getaddrinfo && info) {
#ifdef DS_IPC_PAL_AF_INET
if (!ipc->server_address && info->ai_family == AF_INET) {
server_address = ep_rt_object_alloc (struct sockaddr_in);
if (server_address) {
server_address->sin_family = (uint8_t) info->ai_family;
server_address->sin_port = htons (port);
server_address->sin_addr = ((struct sockaddr_in*)info->ai_addr)->sin_addr;
ipc->server_address = (ds_ipc_socket_address_t *)server_address;
ipc->server_address_len = sizeof (struct sockaddr_in);
ipc->server_address_family = server_address->sin_family;
server_address = NULL;
}
}
#endif
#ifdef DS_IPC_PAL_AF_INET6
if (!ipc->server_address && info->ai_family == AF_INET6) {
server_address6 = ep_rt_object_alloc (struct sockaddr_in6);
if (server_address6) {
server_address6->sin6_family = (uint8_t) info->ai_family;
server_address6->sin6_port = htons (port);
server_address6->sin6_addr = ((struct sockaddr_in6*)info->ai_addr)->sin6_addr;
ipc->server_address = (ds_ipc_socket_address_t *)server_address6;
ipc->server_address_len = sizeof (struct sockaddr_in6);
ipc->server_address_family = server_address6->sin6_family;
server_address6 = NULL;
}
}
#endif
}
ep_raise_error_if_nok (ipc->server_address != NULL);
ep_on_exit:
if (info)
freeaddrinfo (info);
ep_rt_utf8_string_free (address);
return ipc;
ep_on_error:
#ifdef DS_IPC_PAL_AF_INET
ep_rt_object_free (server_address);
#endif
#ifdef DS_IPC_PAL_AF_INET6
ep_rt_object_free (server_address6);
#endif
ipc = NULL;
ep_exit_error_handler ();
#else
return NULL;
#endif
}
static
inline
DiagnosticsIpc *
ipc_alloc_address (
DiagnosticsIpc *ipc,
DiagnosticsIpcConnectionMode mode,
const ep_char8_t *ipc_name)
{
#ifdef DS_IPC_PAL_AF_UNIX
return ipc_alloc_uds_address (ipc, mode, ipc_name);
#elif defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
return ipc_alloc_tcp_address (ipc, mode, ipc_name);
#else
#error "Unknown address family"
#endif
}
static
inline
void
ipc_free_uds_address (DiagnosticsIpc *ipc)
{
#ifdef DS_IPC_PAL_AF_UNIX
if (ipc->server_address_family == AF_UNIX) {
ep_rt_object_free ((struct sockaddr_un *)ipc->server_address);
}
#endif
}
static
inline
void
ipc_free_tcp_address (DiagnosticsIpc *ipc)
{
#ifdef DS_IPC_PAL_AF_INET
if (ipc->server_address_family == AF_INET) {
ep_rt_object_free ((struct sockaddr_in *)ipc->server_address);
}
#endif
#ifdef DS_IPC_PAL_AF_INET6
if (ipc->server_address_family == AF_INET6) {
ep_rt_object_free ((struct sockaddr_in6 *)ipc->server_address);
}
#endif
}
static
inline
void
ipc_free_address (DiagnosticsIpc *ipc)
{
EP_ASSERT (ipc != NULL);
#ifdef DS_IPC_PAL_AF_UNIX
ipc_free_uds_address (ipc);
#elif defined(DS_IPC_PAL_AF_INET) || defined(DS_IPC_PAL_AF_INET6)
ipc_free_tcp_address (ipc);
#else
#error "Unknown address family"
#endif
}
bool