forked from cms-sw/cmsdist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0005-Remove-unnecessary-global-variable-and-lock.patch
199 lines (185 loc) · 5.22 KB
/
0005-Remove-unnecessary-global-variable-and-lock.patch
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
From 9651eb210234ae6fd048f74f9bc4d29989c79f2f Mon Sep 17 00:00:00 2001
From: Philippe Canal <[email protected]>
Date: Fri, 18 Oct 2013 04:43:11 -0500
Subject: [PATCH] Remove unnecessary global variable and lock
gUtmpContents was used only as a data passing mechanism
between related routines and its content had a short lifetime.
This avoids having to put a lock in TUnixSystem::SetDisplay
and (trivially) avoid introducing a possible dead-lock with the lock
being taken as part of 'Warning'.
Conflicts:
core/unix/inc/TUnixSystem.h
---
core/unix/inc/TUnixSystem.h | 3 -
core/unix/src/TUnixSystem.cxx | 129 +++++++++++++++++++++-------------------
2 files changed, 68 insertions(+), 64 deletions(-)
diff --git a/core/unix/inc/TUnixSystem.h b/core/unix/inc/TUnixSystem.h
index 9e8814e..0cc6e4a 100644
--- a/core/unix/inc/TUnixSystem.h
+++ b/core/unix/inc/TUnixSystem.h
@@ -78,9 +78,6 @@ protected:
static void UnixDynListSymbols(const char *lib, const char *re = "");
static void UnixDynListLibs(const char *lib = "");
- static void *SearchUtmpEntry(int nentries, const char *tty);
- static int ReadUtmpFile();
-
public:
TUnixSystem();
virtual ~TUnixSystem();
diff --git a/core/unix/src/TUnixSystem.cxx b/core/unix/src/TUnixSystem.cxx
index 0fc491b8..979a0df 100644
--- a/core/unix/src/TUnixSystem.cxx
+++ b/core/unix/src/TUnixSystem.cxx
@@ -304,8 +304,72 @@ enum {
// End FPE handling includes
-static STRUCT_UTMP *gUtmpContents;
+struct TUtmpContent {
+ STRUCT_UTMP *fUtmpContents;
+ UInt_t fEntries; // Number of entries in utmp file.
+
+ TUtmpContent() : fUtmpContents(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 (ue->ut_name[0] && !strncmp(tty, ue->ut_line, sizeof(ue->ut_line)))
+ return ue;
+ ue++;
+ }
+ return 0;
+ }
+
+ 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;
+
+ fstat(fileno(utmp), &file_stats);
+ 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 = 0;
+ return 0;
+ }
+};
const char *kServerPath = "/tmp";
const char *kProtocolName = "tcp";
@@ -618,9 +682,10 @@ void TUnixSystem::SetDisplay()
if (tty) {
tty += 5; // remove "/dev/"
- R__LOCKGUARD2(gSystemMutex);
+ TUtmpContent utmp;
+ utmp.ReadUtmpFile();
- STRUCT_UTMP *utmp_entry = (STRUCT_UTMP *)SearchUtmpEntry(ReadUtmpFile(), tty);
+ STRUCT_UTMP *utmp_entry = utmp.SearchUtmpEntry(tty);
if (utmp_entry) {
if (utmp_entry->ut_host[0]) {
if (strchr(utmp_entry->ut_host, ':')) {
@@ -649,7 +714,6 @@ void TUnixSystem::SetDisplay()
}
#endif
}
- free(gUtmpContents);
}
}
}
@@ -4892,63 +4956,6 @@ void TUnixSystem::UnixDynUnload(const char *lib)
#endif
}
-//______________________________________________________________________________
-int TUnixSystem::ReadUtmpFile()
-{
- // Read utmp file. Returns number of entries in utmp file.
-
- FILE *utmp;
- struct stat file_stats;
- size_t n_read, size;
-
- R__LOCKGUARD2(gSystemMutex);
-
- gUtmpContents = 0;
-
- utmp = fopen(UTMP_FILE, "r");
- if (!utmp)
- return 0;
-
- fstat(fileno(utmp), &file_stats);
- size = file_stats.st_size;
- if (size <= 0) {
- fclose(utmp);
- return 0;
- }
-
- gUtmpContents = (STRUCT_UTMP *) malloc(size);
- if (!gUtmpContents) {
- fclose(utmp);
- return 0;
- }
-
- n_read = fread(gUtmpContents, 1, size, utmp);
- if (!ferror(utmp)) {
- if (fclose(utmp) != EOF && n_read == size)
- return size / sizeof(STRUCT_UTMP);
- } else
- fclose(utmp);
-
- free(gUtmpContents);
- gUtmpContents = 0;
- return 0;
-}
-
-//______________________________________________________________________________
-void *TUnixSystem::SearchUtmpEntry(int n, const char *tty)
-{
- // Look for utmp entry which is connected to terminal tty.
-
- STRUCT_UTMP *ue = gUtmpContents;
-
- while (n--) {
- if (ue->ut_name[0] && !strncmp(tty, ue->ut_line, sizeof(ue->ut_line)))
- return ue;
- ue++;
- }
- return 0;
-}
-
//---- System, CPU and Memory info ---------------------------------------------
#if defined(R__MACOSX)
--
1.7.4.1