-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTUnixSystem.cxx
5356 lines (4623 loc) · 160 KB
/
TUnixSystem.cxx
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
// @(#)root/unix:$Id: 887c618d89c4ed436e4034fc133f468fecad651b $
// Author: Fons Rademakers 15/09/95
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// TUnixSystem //
// //
// Class providing an interface to the UNIX Operating System. //
// //
//////////////////////////////////////////////////////////////////////////
#include "RConfigure.h"
#include <ROOT/RConfig.hxx>
#include <ROOT/FoundationUtils.hxx>
#include "TUnixSystem.h"
#include "TROOT.h"
#include "TError.h"
#include "TOrdCollection.h"
#include "TRegexp.h"
#include "TPRegexp.h"
#include "TException.h"
#include "TEnv.h"
#include "Getline.h"
#include "TInterpreter.h"
#include "TApplication.h"
#include "TObjString.h"
#include "TVirtualMutex.h"
#include "ThreadLocalStorage.h"
#include "TObjArray.h"
#include "snprintf.h"
#include "strlcpy.h"
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <atomic>
//#define G__OLDEXPAND
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#if defined(R__SUN) || defined(R__AIX) || \
defined(R__LINUX) || defined(R__SOLARIS) || \
defined(R__FBSD) || defined(R__OBSD) || \
defined(R__MACOSX) || defined(R__HURD)
#define HAS_DIRENT
#endif
#ifdef HAS_DIRENT
# include <dirent.h>
#else
# include <sys/dir.h>
#endif
#if defined(ULTRIX) || defined(R__SUN)
# include <sgtty.h>
#endif
#if defined(R__AIX) || defined(R__LINUX) || \
defined(R__FBSD) || defined(R__OBSD) || \
defined(R__LYNXOS) || defined(R__MACOSX) || defined(R__HURD)
# include <sys/ioctl.h>
#endif
#if defined(R__AIX) || defined(R__SOLARIS)
# include <sys/select.h>
#endif
#if defined(R__MACOSX)
# include <mach-o/dyld.h>
# include <sys/mount.h>
extern "C" int statfs(const char *file, struct statfs *buffer);
#elif defined(R__LINUX) || defined(R__HURD)
# include <sys/vfs.h>
#elif defined(R__FBSD) || defined(R__OBSD)
# include <sys/param.h>
# include <sys/mount.h>
# ifdef R__FBSD
# include <sys/user.h>
# include <sys/types.h>
# include <sys/param.h>
# include <sys/queue.h>
# include <libprocstat.h>
# include <libutil.h>
# endif
#else
# include <sys/statfs.h>
#endif
#include <utime.h>
#include <syslog.h>
#include <sys/stat.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/param.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#if defined(R__AIX)
# define _XOPEN_EXTENDED_SOURCE
# include <arpa/inet.h>
# undef _XOPEN_EXTENDED_SOURCE
# if !defined(_AIX41) && !defined(_AIX43)
// AIX 3.2 doesn't have it
# define HASNOT_INETATON
# endif
#else
# include <arpa/inet.h>
#endif
#include <sys/un.h>
#include <netdb.h>
#include <fcntl.h>
#if defined(R__SOLARIS)
# include <sys/systeminfo.h>
# include <sys/filio.h>
# include <sys/sockio.h>
# define HASNOT_INETATON
# ifndef INADDR_NONE
# define INADDR_NONE (UInt_t)-1
# endif
#endif
#if defined(R__SOLARIS)
# define HAVE_UTMPX_H
# define UTMP_NO_ADDR
#endif
#if defined(MAC_OS_X_VERSION_10_5)
# define HAVE_UTMPX_H
# define UTMP_NO_ADDR
#endif
#if defined(R__FBSD)
# include <sys/param.h>
# if __FreeBSD_version >= 900007
# define HAVE_UTMPX_H
# endif
#endif
#if defined(R__AIX) || defined(R__FBSD) || \
defined(R__OBSD) || defined(R__LYNXOS) || \
(defined(R__MACOSX) && !defined(MAC_OS_X_VERSION_10_5))
# define UTMP_NO_ADDR
#endif
#if defined(R__LYNXOS)
extern "C" {
extern int putenv(const char *);
extern int inet_aton(const char *, struct in_addr *);
};
#endif
#if defined(R__ARC4_STDLIB)
// do nothing, stdlib.h already included
#elif defined(R__ARC4_BSDLIB)
#include <bsd/stdlib.h>
#elif defined(R__GETRANDOM_CLIB)
#include <sys/random.h>
#endif
#ifdef HAVE_UTMPX_H
#include <utmpx.h>
#define STRUCT_UTMP struct utmpx
#else
#include <utmp.h>
#define STRUCT_UTMP struct utmp
#endif
#if !defined(UTMP_FILE) && defined(_PATH_UTMP) // 4.4BSD
#define UTMP_FILE _PATH_UTMP
#endif
#if defined(UTMPX_FILE) // Solaris, SysVr4
#undef UTMP_FILE
#define UTMP_FILE UTMPX_FILE
#endif
#ifndef UTMP_FILE
#define UTMP_FILE "/etc/utmp"
#endif
// stack trace code
#if (defined(R__LINUX) || defined(R__HURD)) && !defined(R__WINGCC)
# if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1
# define HAVE_BACKTRACE_SYMBOLS_FD
# endif
# define HAVE_DLADDR
#endif
#if defined(R__MACOSX) || defined(R__FBSD)
# define HAVE_BACKTRACE_SYMBOLS_FD
# define HAVE_DLADDR
#endif
#ifdef HAVE_BACKTRACE_SYMBOLS_FD
# include <execinfo.h>
#endif
#ifdef HAVE_DLADDR
# ifndef __USE_GNU
# define __USE_GNU
# endif
# include <dlfcn.h>
#endif
#ifdef HAVE_BACKTRACE_SYMBOLS_FD
// The maximum stack trace depth for systems where we request the
// stack depth separately (currently glibc-based systems).
static const int kMAX_BACKTRACE_DEPTH = 128;
#endif
// FPE handling includes
#if (defined(R__LINUX) && !defined(R__WINGCC))
#include <fenv.h>
#include <sys/prctl.h> // for prctl() function used in StackTrace()
#endif
#if defined(R__MACOSX) && defined(__SSE2__)
#include <xmmintrin.h>
#endif
#if defined(R__MACOSX) && !defined(__SSE2__) && !defined(__xlC__) && \
!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__) && \
!defined(__arm64__)
#include <fenv.h>
#include <signal.h>
#include <ucontext.h>
#include <stdlib.h>
#include <stdio.h>
#include <mach/thread_status.h>
#define fegetenvd(x) asm volatile("mffs %0" : "=f" (x));
#define fesetenvd(x) asm volatile("mtfsf 255,%0" : : "f" (x));
enum {
FE_ENABLE_INEXACT = 0x00000008,
FE_ENABLE_DIVBYZERO = 0x00000010,
FE_ENABLE_UNDERFLOW = 0x00000020,
FE_ENABLE_OVERFLOW = 0x00000040,
FE_ENABLE_INVALID = 0x00000080,
FE_ENABLE_ALL_EXCEPT = 0x000000F8
};
#endif
#if defined(R__MACOSX) && !defined(__SSE2__) && \
(defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__))
#include <fenv.h>
#endif
// End FPE handling includes
namespace {
// Depending on the platform the struct utmp (or utmpx) has either ut_name or ut_user
// which are semantically equivalent. Instead of using preprocessor magic,
// which is bothersome for cxx modules use SFINAE.
template<typename T>
struct ut_name {
template<typename U = T, typename std::enable_if<std::is_member_pointer<decltype(&U::ut_name)>::value, int>::type = 0>
static char getValue(U* ue, int) {
return ue->ut_name[0];
}
template<typename U = T, typename std::enable_if<std::is_member_pointer<decltype(&U::ut_user)>::value, int>::type = 0>
static char getValue(U* ue, long) {
return ue->ut_user[0];
}
};
static char get_ut_name(STRUCT_UTMP *ue) {
// 0 is an integer literal forcing an overload pickup in case both ut_name and ut_user are present.
return ut_name<STRUCT_UTMP>::getValue(ue, 0);
}
}
struct TUtmpContent {
STRUCT_UTMP *fUtmpContents;
UInt_t fEntries; // Number of entries in utmp file.
TUtmpContent() : fUtmpContents(nullptr), fEntries(0) {}
~TUtmpContent() { free(fUtmpContents); }
STRUCT_UTMP *SearchUtmpEntry(const char *tty)
{
// Look for utmp entry which is connected to terminal tty.
STRUCT_UTMP *ue = fUtmpContents;
UInt_t n = fEntries;
while (n--) {
if (get_ut_name(ue) && !strncmp(tty, ue->ut_line, sizeof(ue->ut_line)))
return ue;
ue++;
}
return nullptr;
}
int ReadUtmpFile()
{
// Read utmp file. Returns number of entries in utmp file.
FILE *utmp;
struct stat file_stats;
size_t n_read, size;
fEntries = 0;
R__LOCKGUARD2(gSystemMutex);
utmp = fopen(UTMP_FILE, "r");
if (!utmp)
return 0;
if (fstat(fileno(utmp), &file_stats) == -1) {
fclose(utmp);
return 0;
}
size = file_stats.st_size;
if (size <= 0) {
fclose(utmp);
return 0;
}
fUtmpContents = (STRUCT_UTMP *) malloc(size);
if (!fUtmpContents) {
fclose(utmp);
return 0;
}
n_read = fread(fUtmpContents, 1, size, utmp);
if (!ferror(utmp)) {
if (fclose(utmp) != EOF && n_read == size) {
fEntries = size / sizeof(STRUCT_UTMP);
return fEntries;
}
} else
fclose(utmp);
free(fUtmpContents);
fUtmpContents = nullptr;
return 0;
}
};
const char *kServerPath = "/tmp";
const char *kProtocolName = "tcp";
//------------------- Unix TFdSet ----------------------------------------------
#ifndef HOWMANY
# define HOWMANY(x, y) (((x)+((y)-1))/(y))
#endif
const Int_t kNFDBITS = (sizeof(Long_t) * 8); // 8 bits per byte
#ifdef FD_SETSIZE
const Int_t kFDSETSIZE = FD_SETSIZE; // Linux = 1024 file descriptors
#else
const Int_t kFDSETSIZE = 256; // upto 256 file descriptors
#endif
class TFdSet {
private:
ULong_t fds_bits[HOWMANY(kFDSETSIZE, kNFDBITS)];
public:
TFdSet() { memset(fds_bits, 0, sizeof(fds_bits)); }
TFdSet(const TFdSet &org) { memcpy(fds_bits, org.fds_bits, sizeof(org.fds_bits)); }
TFdSet &operator=(const TFdSet &rhs) { if (this != &rhs) { memcpy(fds_bits, rhs.fds_bits, sizeof(rhs.fds_bits));} return *this; }
void Zero() { memset(fds_bits, 0, sizeof(fds_bits)); }
void Set(Int_t n)
{
if (n >= 0 && n < kFDSETSIZE) {
fds_bits[n/kNFDBITS] |= (1UL << (n % kNFDBITS));
} else {
::Fatal("TFdSet::Set","fd (%d) out of range [0..%d]", n, kFDSETSIZE-1);
}
}
void Clr(Int_t n)
{
if (n >= 0 && n < kFDSETSIZE) {
fds_bits[n/kNFDBITS] &= ~(1UL << (n % kNFDBITS));
} else {
::Fatal("TFdSet::Clr","fd (%d) out of range [0..%d]", n, kFDSETSIZE-1);
}
}
Int_t IsSet(Int_t n)
{
if (n >= 0 && n < kFDSETSIZE) {
return (fds_bits[n/kNFDBITS] & (1UL << (n % kNFDBITS))) != 0;
} else {
::Fatal("TFdSet::IsSet","fd (%d) out of range [0..%d]", n, kFDSETSIZE-1);
return 0;
}
}
ULong_t *GetBits() { return (ULong_t *)fds_bits; }
};
////////////////////////////////////////////////////////////////////////////////
/// Unix signal handler.
static void SigHandler(ESignals sig)
{
if (gSystem)
((TUnixSystem*)gSystem)->DispatchSignals(sig);
}
////////////////////////////////////////////////////////////////////////////////
static const char *GetExePath()
{
TTHREAD_TLS_DECL(TString,exepath);
if (exepath == "") {
#if defined(R__MACOSX)
exepath = _dyld_get_image_name(0);
#elif defined(R__LINUX) || defined(R__SOLARIS) || defined(R__FBSD)
char buf[kMAXPATHLEN]=""; // exe path name
// get the name from the link in /proc
#if defined(R__LINUX)
int ret = readlink("/proc/self/exe", buf, kMAXPATHLEN);
#elif defined(R__SOLARIS)
int ret = readlink("/proc/self/path/a.out", buf, kMAXPATHLEN);
#elif defined(R__FBSD)
procstat* ps = procstat_open_sysctl();
kinfo_proc* kp = kinfo_getproc(getpid());
int ret{0};
if (kp!=NULL) {
procstat_getpathname(ps, kp, buf, sizeof(buf));
}
free(kp);
procstat_close(ps);
exepath = buf;
#endif
if (ret > 0 && ret < kMAXPATHLEN) {
buf[ret] = 0;
exepath = buf;
}
#else
if (!gApplication)
return exepath;
TString p = gApplication->Argv(0);
if (p.BeginsWith("/"))
exepath = p;
else if (p.Contains("/")) {
exepath = gSystem->WorkingDirectory();
exepath += "/";
exepath += p;
} else {
char *exe = gSystem->Which(gSystem->Getenv("PATH"), p, kExecutePermission);
if (exe) {
exepath = exe;
delete [] exe;
}
}
#endif
}
return exepath;
}
#if defined(HAVE_DLADDR) && !defined(R__MACOSX)
////////////////////////////////////////////////////////////////////////////////
static void SetRootSys()
{
#ifdef ROOTPREFIX
if (gSystem->Getenv("ROOTIGNOREPREFIX")) {
#endif
void *addr = (void *)SetRootSys;
Dl_info info;
if (dladdr(addr, &info) && info.dli_fname && info.dli_fname[0]) {
char respath[kMAXPATHLEN];
if (!realpath(info.dli_fname, respath)) {
if (!gSystem->Getenv("ROOTSYS"))
::SysError("TUnixSystem::SetRootSys", "error getting realpath of libCore, please set ROOTSYS in the shell");
} else {
TString rs = gSystem->GetDirName(respath);
gSystem->Setenv("ROOTSYS", gSystem->GetDirName(rs.Data()).Data());
}
}
#ifdef ROOTPREFIX
}
#endif
}
#endif
#if defined(R__MACOSX)
static TString gLinkedDylibs;
////////////////////////////////////////////////////////////////////////////////
static void DylibAdded(const struct mach_header *mh, intptr_t /* vmaddr_slide */)
{
static int i = 0;
static Bool_t gotFirstSo = kFALSE;
static TString linkedDylibs;
// to copy the local linkedDylibs to the global gLinkedDylibs call this
// function with mh==0
if (!mh) {
gLinkedDylibs = linkedDylibs;
return;
}
TString lib = _dyld_get_image_name(i++);
TRegexp sovers = "libCore\\.[0-9]+\\.*[0-9]*\\.*[0-9]*\\.so";
TRegexp dyvers = "libCore\\.[0-9]+\\.*[0-9]*\\.*[0-9]*\\.dylib";
#ifdef ROOTPREFIX
if (gSystem->Getenv("ROOTIGNOREPREFIX")) {
#endif
if (lib.EndsWith("libCore.dylib") || lib.EndsWith("libCore.so") ||
lib.Index(sovers) != kNPOS || lib.Index(dyvers) != kNPOS) {
char respath[kMAXPATHLEN];
if (!realpath(lib, respath)) {
if (!gSystem->Getenv("ROOTSYS"))
::SysError("TUnixSystem::DylibAdded", "error getting realpath of libCore, please set ROOTSYS in the shell");
} else {
TString rs = gSystem->GetDirName(respath);
gSystem->Setenv("ROOTSYS", gSystem->GetDirName(rs.Data()).Data());
}
}
#ifdef ROOTPREFIX
}
#endif
// when libSystem.B.dylib is loaded we have finished loading all dylibs
// explicitly linked against the executable. Additional dylibs
// come when they are explicitly linked against loaded so's, currently
// we are not interested in these
if (lib.EndsWith("/libSystem.B.dylib")) {
gotFirstSo = kTRUE;
if (linkedDylibs.IsNull()) {
// TSystem::GetLibraries() assumes that an empty GetLinkedLibraries()
// means failure to extract the linked libraries. Signal "we did
// manage, but it's empty" by returning a single space.
linkedDylibs = ' ';
}
}
// add all libs loaded before libSystem.B.dylib
if (!gotFirstSo && (lib.EndsWith(".dylib") || lib.EndsWith(".so"))) {
sovers = "\\.[0-9]+\\.*[0-9]*\\.so";
Ssiz_t idx = lib.Index(sovers);
if (idx != kNPOS) {
lib.Remove(idx);
lib += ".so";
}
dyvers = "\\.[0-9]+\\.*[0-9]*\\.dylib";
idx = lib.Index(dyvers);
if (idx != kNPOS) {
lib.Remove(idx);
lib += ".dylib";
}
if (!gSystem->AccessPathName(lib, kReadPermission)) {
if (linkedDylibs.Length())
linkedDylibs += " ";
linkedDylibs += lib;
}
}
}
#endif
ClassImp(TUnixSystem);
////////////////////////////////////////////////////////////////////////////////
TUnixSystem::TUnixSystem() : TSystem("Unix", "Unix System")
{ }
////////////////////////////////////////////////////////////////////////////////
/// Reset to original state.
TUnixSystem::~TUnixSystem()
{
UnixResetSignals();
delete fReadmask;
delete fWritemask;
delete fReadready;
delete fWriteready;
delete fSignals;
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize Unix system interface.
Bool_t TUnixSystem::Init()
{
if (TSystem::Init())
return kTRUE;
fReadmask = new TFdSet;
fWritemask = new TFdSet;
fReadready = new TFdSet;
fWriteready = new TFdSet;
fSignals = new TFdSet;
//--- install default handlers
UnixSignal(kSigChild, SigHandler);
UnixSignal(kSigBus, SigHandler);
UnixSignal(kSigSegmentationViolation, SigHandler);
UnixSignal(kSigIllegalInstruction, SigHandler);
UnixSignal(kSigAbort, SigHandler);
UnixSignal(kSigSystem, SigHandler);
UnixSignal(kSigAlarm, SigHandler);
UnixSignal(kSigUrgent, SigHandler);
UnixSignal(kSigFloatingException, SigHandler);
UnixSignal(kSigWindowChanged, SigHandler);
UnixSignal(kSigUser2, SigHandler);
#if defined(R__MACOSX)
// trap loading of all dylibs to register dylib name,
// sets also ROOTSYS if built without ROOTPREFIX
_dyld_register_func_for_add_image(DylibAdded);
#elif defined(HAVE_DLADDR)
SetRootSys();
#endif
// This is a fallback in case TROOT::GetRootSys() can't determine ROOTSYS
gRootDir = ROOT::FoundationUtils::GetFallbackRootSys().c_str();
return kFALSE;
}
//---- Misc --------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// Set the application name (from command line, argv[0]) and copy it in
/// gProgName. Copy the application pathname in gProgPath.
/// If name is 0 let the system set the actual executable name and path
/// (works on MacOS X and Linux).
void TUnixSystem::SetProgname(const char *name)
{
if (gProgName)
delete [] gProgName;
if (gProgPath)
delete [] gProgPath;
if (!name || !*name) {
name = GetExePath();
gProgName = StrDup(BaseName(name));
gProgPath = StrDup(DirName(name));
} else {
gProgName = StrDup(BaseName(name));
char *w = Which(Getenv("PATH"), gProgName);
gProgPath = StrDup(DirName(w));
delete [] w;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Set DISPLAY environment variable based on utmp entry. Only for UNIX.
void TUnixSystem::SetDisplay()
{
if (!Getenv("DISPLAY")) {
char *tty = ::ttyname(0); // device user is logged in on
if (tty) {
tty += 5; // remove "/dev/"
TUtmpContent utmp;
utmp.ReadUtmpFile();
STRUCT_UTMP *utmp_entry = utmp.SearchUtmpEntry(tty);
if (utmp_entry) {
if (utmp_entry->ut_host[0]) {
TString disp;
for (unsigned n = 0; (n < sizeof(utmp_entry->ut_host)) && utmp_entry->ut_host[n]; n++)
disp.Append(utmp_entry->ut_host[n]);
if (disp.First(':') == kNPOS)
disp.Append(":0.0");
Setenv("DISPLAY", disp.Data());
Warning("SetDisplay", "DISPLAY not set, setting it to %s", disp.Data());
}
#ifndef UTMP_NO_ADDR
else if (utmp_entry->ut_addr) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = 0;
memcpy(&addr.sin_addr, &utmp_entry->ut_addr, sizeof(addr.sin_addr));
memset(&addr.sin_zero[0], 0, sizeof(addr.sin_zero));
struct sockaddr *sa = (struct sockaddr *) &addr; // input
char hbuf[NI_MAXHOST + 4];
if (getnameinfo(sa, sizeof(struct sockaddr), hbuf, sizeof(hbuf), nullptr, 0, NI_NAMEREQD) == 0) {
assert( strlen(hbuf) < NI_MAXHOST );
strlcat(hbuf, ":0.0", sizeof(hbuf));
Setenv("DISPLAY", hbuf);
Warning("SetDisplay", "DISPLAY not set, setting it to %s",
hbuf);
}
}
#endif
}
}
#ifndef R__HAS_COCOA
if (!gROOT->IsBatch() && !getenv("DISPLAY")) {
Error("SetDisplay", "Can't figure out DISPLAY, set it manually\n"
"In case you run a remote ssh session, restart your ssh session with:\n"
"=========> ssh -Y");
}
#endif
}
}
////////////////////////////////////////////////////////////////////////////////
/// Return system error string.
const char *TUnixSystem::GetError()
{
Int_t err = GetErrno();
if (err == 0 && GetLastErrorString() != "")
return GetLastErrorString();
#if defined(R__SOLARIS) || defined (R__LINUX) || defined(R__AIX) || \
defined(R__FBSD) || defined(R__OBSD) || defined(R__HURD)
return strerror(err);
#else
if (err < 0 || err >= sys_nerr)
return Form("errno out of range %d", err);
return sys_errlist[err];
#endif
}
////////////////////////////////////////////////////////////////////////////////
/// Return cryptographic random number
/// Fill provided buffer with random values
/// Returns number of bytes written to buffer or -1 in case of error
Int_t TUnixSystem::GetCryptoRandom(void *buf, Int_t len)
{
#if defined(R__ARC4_STDLIB) || defined(R__ARC4_BSDLIB)
arc4random_buf(buf, len);
return len;
#elif defined(R__GETRANDOM_CLIB)
return getrandom(buf, len, GRND_NONBLOCK);
#elif defined(R__USE_URANDOM)
std::ifstream urandom{"/dev/urandom"};
if (!urandom)
return -1;
urandom.read(reinterpret_cast<char *>(buf), len);
return len;
#else
#error "Reliable cryptographic random function not defined"
return -1;
#endif
}
////////////////////////////////////////////////////////////////////////////////
/// Return the system's host name.
const char *TUnixSystem::HostName()
{
if (fHostname == "") {
char hn[64];
#if defined(R__SOLARIS)
sysinfo(SI_HOSTNAME, hn, sizeof(hn));
#else
gethostname(hn, sizeof(hn));
#endif
fHostname = hn;
}
return (const char *)fHostname;
}
//---- EventLoop ---------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// Add a file handler to the list of system file handlers. Only adds
/// the handler if it is not already in the list of file handlers.
void TUnixSystem::AddFileHandler(TFileHandler *h)
{
R__LOCKGUARD2(gSystemMutex);
TSystem::AddFileHandler(h);
if (h) {
int fd = h->GetFd();
if (h->HasReadInterest()) {
fReadmask->Set(fd);
fMaxrfd = TMath::Max(fMaxrfd, fd);
}
if (h->HasWriteInterest()) {
fWritemask->Set(fd);
fMaxwfd = TMath::Max(fMaxwfd, fd);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Remove a file handler from the list of file handlers. Returns
/// the handler or 0 if the handler was not in the list of file handlers.
TFileHandler *TUnixSystem::RemoveFileHandler(TFileHandler *h)
{
if (!h) return nullptr;
R__LOCKGUARD2(gSystemMutex);
TFileHandler *oh = TSystem::RemoveFileHandler(h);
if (oh) { // found
TFileHandler *th;
TIter next(fFileHandler);
fMaxrfd = -1;
fMaxwfd = -1;
fReadmask->Zero();
fWritemask->Zero();
while ((th = (TFileHandler *) next())) {
int fd = th->GetFd();
if (th->HasReadInterest()) {
fReadmask->Set(fd);
fMaxrfd = TMath::Max(fMaxrfd, fd);
}
if (th->HasWriteInterest()) {
fWritemask->Set(fd);
fMaxwfd = TMath::Max(fMaxwfd, fd);
}
}
}
return oh;
}
////////////////////////////////////////////////////////////////////////////////
/// Add a signal handler to list of system signal handlers. Only adds
/// the handler if it is not already in the list of signal handlers.
void TUnixSystem::AddSignalHandler(TSignalHandler *h)
{
R__LOCKGUARD2(gSystemMutex);
TSystem::AddSignalHandler(h);
UnixSignal(h->GetSignal(), SigHandler);
}
////////////////////////////////////////////////////////////////////////////////
/// Remove a signal handler from list of signal handlers. Returns
/// the handler or 0 if the handler was not in the list of signal handlers.
TSignalHandler *TUnixSystem::RemoveSignalHandler(TSignalHandler *h)
{
if (!h) return nullptr;
R__LOCKGUARD2(gSystemMutex);
TSignalHandler *oh = TSystem::RemoveSignalHandler(h);
Bool_t last = kTRUE;
TSignalHandler *hs;
TIter next(fSignalHandler);
while ((hs = (TSignalHandler*) next())) {
if (hs->GetSignal() == h->GetSignal())
last = kFALSE;
}
if (last)
ResetSignal(h->GetSignal(), kTRUE);
return oh;
}
////////////////////////////////////////////////////////////////////////////////
/// If reset is true reset the signal handler for the specified signal
/// to the default handler, else restore previous behaviour.
void TUnixSystem::ResetSignal(ESignals sig, Bool_t reset)
{
if (reset)
UnixResetSignal(sig);
else
UnixSignal(sig, SigHandler);
}
////////////////////////////////////////////////////////////////////////////////
/// Reset signals handlers to previous behaviour.
void TUnixSystem::ResetSignals()
{
UnixResetSignals();
}
////////////////////////////////////////////////////////////////////////////////
/// If ignore is true ignore the specified signal, else restore previous
/// behaviour.
void TUnixSystem::IgnoreSignal(ESignals sig, Bool_t ignore)
{
UnixIgnoreSignal(sig, ignore);
}
////////////////////////////////////////////////////////////////////////////////
/// When the argument is true the SIGALRM signal handler is set so that
/// interrupted syscalls will not be restarted by the kernel. This is
/// typically used in case one wants to put a timeout on an I/O operation.
/// By default interrupted syscalls will always be restarted (for all
/// signals). This can be controlled for each a-synchronous TTimer via
/// the method TTimer::SetInterruptSyscalls().
void TUnixSystem::SigAlarmInterruptsSyscalls(Bool_t set)
{
UnixSigAlarmInterruptsSyscalls(set);
}
////////////////////////////////////////////////////////////////////////////////
/// Return the bitmap of conditions that trigger a floating point exception.
Int_t TUnixSystem::GetFPEMask()
{
Int_t mask = 0;
#if defined(R__LINUX) && !defined(__powerpc__)
#if defined(__GLIBC__) && (__GLIBC__>2 || __GLIBC__==2 && __GLIBC_MINOR__>=1)
#if __GLIBC_MINOR__>=3
Int_t oldmask = fegetexcept();
#else
fenv_t oldenv;
fegetenv(&oldenv);
fesetenv(&oldenv);
#if __ia64__
Int_t oldmask = ~oldenv;
#else
Int_t oldmask = ~oldenv.__control_word;
#endif
#endif
if (oldmask & FE_INVALID ) mask |= kInvalid;
if (oldmask & FE_DIVBYZERO) mask |= kDivByZero;
if (oldmask & FE_OVERFLOW ) mask |= kOverflow;
if (oldmask & FE_UNDERFLOW) mask |= kUnderflow;
# ifdef FE_INEXACT
if (oldmask & FE_INEXACT ) mask |= kInexact;
# endif
#endif
#endif
#if defined(R__MACOSX) && defined(__SSE2__)
// OS X uses the SSE unit for all FP math by default, not the x87 FP unit
Int_t oldmask = ~_MM_GET_EXCEPTION_MASK();
if (oldmask & _MM_MASK_INVALID ) mask |= kInvalid;
if (oldmask & _MM_MASK_DIV_ZERO ) mask |= kDivByZero;
if (oldmask & _MM_MASK_OVERFLOW ) mask |= kOverflow;
if (oldmask & _MM_MASK_UNDERFLOW) mask |= kUnderflow;
if (oldmask & _MM_MASK_INEXACT ) mask |= kInexact;
#endif
#if defined(R__MACOSX) && !defined(__SSE2__) && \
(defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__))
fenv_t oldenv;
fegetenv(&oldenv);
fesetenv(&oldenv);
#if defined(__arm__)
Int_t oldmask = ~oldenv.__fpscr;
#elif defined(__arm64__)
Int_t oldmask = ~oldenv.__fpcr;
#else
Int_t oldmask = ~oldenv.__control;
#endif
if (oldmask & FE_INVALID ) mask |= kInvalid;
if (oldmask & FE_DIVBYZERO) mask |= kDivByZero;
if (oldmask & FE_OVERFLOW ) mask |= kOverflow;
if (oldmask & FE_UNDERFLOW) mask |= kUnderflow;
if (oldmask & FE_INEXACT ) mask |= kInexact;
#endif
#if defined(R__MACOSX) && !defined(__SSE2__) && !defined(__xlC__) && \
!defined(__i386__) && !defined(__x86_64__) && !defined(__arm__) && \
!defined(__arm64__)
Long64_t oldmask;
fegetenvd(oldmask);
if (oldmask & FE_ENABLE_INVALID ) mask |= kInvalid;
if (oldmask & FE_ENABLE_DIVBYZERO) mask |= kDivByZero;
if (oldmask & FE_ENABLE_OVERFLOW ) mask |= kOverflow;
if (oldmask & FE_ENABLE_UNDERFLOW) mask |= kUnderflow;
if (oldmask & FE_ENABLE_INEXACT ) mask |= kInexact;
#endif
return mask;
}
////////////////////////////////////////////////////////////////////////////////
/// Set which conditions trigger a floating point exception.
/// Return the previous set of conditions.
Int_t TUnixSystem::SetFPEMask(Int_t mask)
{