@@ -2,9 +2,7 @@ package dockergen
2
2
3
3
import (
4
4
"bufio"
5
- "bytes"
6
5
"fmt"
7
- "io"
8
6
"os"
9
7
"regexp"
10
8
"sync"
@@ -169,43 +167,42 @@ func GetCurrentContainerID(filepaths ...string) (id string) {
169
167
filepaths = []string {"/proc/1/cpuset" , "/proc/self/cgroup" , "/proc/self/mountinfo" }
170
168
}
171
169
172
- var files []io.Reader
173
-
170
+ // We try to match a 64 character hex string starting with the hostname first
174
171
for _ , filepath := range filepaths {
175
172
file , err := os .Open (filepath )
176
173
if err != nil {
177
174
continue
178
175
}
179
176
defer file .Close ()
180
- files = append (files , file )
181
- }
182
-
183
- reader := io .MultiReader (files ... )
184
- var buffer bytes.Buffer
185
- tee := io .TeeReader (reader , & buffer )
186
-
187
- // We try to match a 64 character hex string starting with the hostname first
188
- scanner := bufio .NewScanner (tee )
189
- scanner .Split (bufio .ScanLines )
190
- for scanner .Scan () {
191
- _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
192
- if err == nil {
193
- strLines := string (lines )
194
- if id = matchContainerIDWithHostname (strLines ); len (id ) == 64 {
195
- return
177
+ scanner := bufio .NewScanner (file )
178
+ scanner .Split (bufio .ScanLines )
179
+ for scanner .Scan () {
180
+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
181
+ if err == nil {
182
+ strLines := string (lines )
183
+ if id = matchContainerIDWithHostname (strLines ); len (id ) == 64 {
184
+ return
185
+ }
196
186
}
197
187
}
198
188
}
199
189
200
190
// If we didn't get any ID that matches the hostname, fall back to matching the first 64 character hex string
201
- scanner = bufio .NewScanner (& buffer )
202
- scanner .Split (bufio .ScanLines )
203
- for scanner .Scan () {
204
- _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
205
- if err == nil {
206
- strLines := string (lines )
207
- if id = matchContainerID (strLines ); len (id ) == 64 {
208
- return
191
+ for _ , filepath := range filepaths {
192
+ file , err := os .Open (filepath )
193
+ if err != nil {
194
+ continue
195
+ }
196
+ defer file .Close ()
197
+ scanner := bufio .NewScanner (file )
198
+ scanner .Split (bufio .ScanLines )
199
+ for scanner .Scan () {
200
+ _ , lines , err := bufio .ScanLines ([]byte (scanner .Text ()), true )
201
+ if err == nil {
202
+ strLines := string (lines )
203
+ if id = matchContainerID ("([[:alnum:]]{64})" , strLines ); len (id ) == 64 {
204
+ return
205
+ }
209
206
}
210
207
}
211
208
}
@@ -219,22 +216,21 @@ func matchContainerIDWithHostname(lines string) string {
219
216
220
217
if re .MatchString (hostname ) {
221
218
regex := fmt .Sprintf ("(%s[[:alnum:]]{52})" , hostname )
222
- re := regexp .MustCompilePOSIX (regex )
223
219
224
- if re .MatchString (lines ) {
225
- submatches := re .FindStringSubmatch (string (lines ))
226
- containerID := submatches [1 ]
227
-
228
- return containerID
229
- }
220
+ return matchContainerID (regex , lines )
230
221
}
231
222
return ""
232
223
}
233
224
234
- func matchContainerID (lines string ) string {
235
- regex := "([[:alnum:]]{64})"
236
- re := regexp .MustCompilePOSIX (regex )
225
+ func matchContainerID (regex , lines string ) string {
226
+ // Attempt to detect if we're on a line from a /proc/<pid>/mountinfo file and modify the regexp accordingly
227
+ // https://www.kernel.org/doc/Documentation/filesystems/proc.txt section 3.5
228
+ re := regexp .MustCompilePOSIX ("^[0-9]+ [0-9]+ [0-9]+:[0-9]+ /" )
229
+ if re .MatchString (lines ) {
230
+ regex = fmt .Sprintf ("containers/%v" , regex )
231
+ }
237
232
233
+ re = regexp .MustCompilePOSIX (regex )
238
234
if re .MatchString (lines ) {
239
235
submatches := re .FindStringSubmatch (string (lines ))
240
236
containerID := submatches [1 ]
0 commit comments