@@ -301,6 +301,7 @@ def register
301
301
end
302
302
303
303
def start
304
+ disable_nss_modules
304
305
@checker = LeakChecker . new
305
306
end
306
307
@@ -316,4 +317,61 @@ def after(state)
316
317
end
317
318
end
318
319
end
320
+
321
+ private
322
+
323
+ # This function is intended to disable all NSS modules when ruby is compiled
324
+ # against glibc. NSS modules allow the system administrator to load custom
325
+ # shared objects into all processes using glibc, and use them to customise
326
+ # the behaviour of username, groupname, hostname, etc lookups. This is
327
+ # normally configured in the file /etc/nsswitch.conf.
328
+ # These modules often do things like open cache files or connect to system
329
+ # daemons like sssd or dbus, which of course means they have open file
330
+ # descriptors of their own. This can cause the leak-checking functionality
331
+ # in this file to report that such descriptors have been leaked, and fail
332
+ # the test suite.
333
+ # This function uses glibc's __nss_configure_lookup function to override any
334
+ # configuration in /etc/nsswitch.conf, and just use the built in files/dns
335
+ # name lookup functionality (which is of course perfectly sufficient for
336
+ # running ruby/spec).
337
+ def disable_nss_modules
338
+ begin
339
+ require 'fiddle'
340
+ rescue LoadError
341
+ # Make sure it's possible to run the test suite on a ruby implementation
342
+ # which does not (yet?) have Fiddle.
343
+ return
344
+ end
345
+
346
+ begin
347
+ libc = Fiddle . dlopen ( nil )
348
+ # Older versions of fiddle don't have Fiddle::Type (and instead rely on Fiddle::TYPE_)
349
+ # Even older versions of fiddle don't have CONST_STRING,
350
+ string_type = defined? ( Fiddle ::TYPE_CONST_STRING ) ? Fiddle ::TYPE_CONST_STRING : Fiddle ::TYPE_VOIDP
351
+ nss_configure_lookup = Fiddle ::Function . new (
352
+ libc [ '__nss_configure_lookup' ] ,
353
+ [ string_type , string_type ] ,
354
+ Fiddle ::TYPE_INT
355
+ )
356
+ rescue Fiddle ::DLError
357
+ # We're not running with glibc - no need to do this.
358
+ return
359
+ end
360
+
361
+ nss_configure_lookup . call 'passwd' , 'files'
362
+ nss_configure_lookup . call 'shadow' , 'files'
363
+ nss_configure_lookup . call 'group' , 'files'
364
+ nss_configure_lookup . call 'hosts' , 'files dns'
365
+ nss_configure_lookup . call 'services' , 'files'
366
+ nss_configure_lookup . call 'netgroup' , 'files'
367
+ nss_configure_lookup . call 'automount' , 'files'
368
+ nss_configure_lookup . call 'aliases' , 'files'
369
+ nss_configure_lookup . call 'ethers' , 'files'
370
+ nss_configure_lookup . call 'gshadow' , 'files'
371
+ nss_configure_lookup . call 'initgroups' , 'files'
372
+ nss_configure_lookup . call 'networks' , 'files dns'
373
+ nss_configure_lookup . call 'protocols' , 'files'
374
+ nss_configure_lookup . call 'publickey' , 'files'
375
+ nss_configure_lookup . call 'rpc' , 'files'
376
+ end
319
377
end
0 commit comments