trousers: Fix double close() when no tcsd

Fix a second (wrong) call to close() with the same file
descriptor, which was happening in case when connecting to
tcsd failed.

The problematic codepath was: send_init() calls get_socket(),
and get_socket() calls connect() (which fails) and close() and
exits with returning the closed descriptor in |sd|, and then
send_init() calls close() with that same |sd| again.

The consequence of this bug was that the program that uses the
Trousers library could start misbehaving, if due to this bug a
file descriptor that belongs to some other component of the
program was wrongly closed by Trousers.

BUG=chromium:973948, chromium:945489
TEST=manual: add one more TYPE_IO thread into cryptohomed, run it under VM and check that it doesn't crash on startup (try 1000 attempts)

Change-Id: I4513cf92ac6342e6583172737b76cb51f6a91382
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/trousers/+/1659248
Tested-by: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: Wei-Cheng Xiao <garryxiao@chromium.org>
Reviewed-by: Andrey Pronin <apronin@chromium.org>
1 file changed