Fix potential memory leak in Agent
Fix memory leak in IO when getting EPIPE
Lock around ttyname() because it's not thread safe

Also check the return value to verify that the call actually succeeds.
Lock around crypt() because it's not thread safe

Linux provides crypt_r, but this is not a standard and common extension.
Therefore we lock around it as the easiest solution.
Use gmtime_r since it's thread safe and reentrant
Use getpwnam_r over getpwnam

The latter is not thread safe, so using the first makes sure this works
properly.
Synchronized around Etc.passwd

This uses non thread safe methods. Sadly Ruby exposes the underying
methods directly as well, so people using those have to be aware of
their thread unsafeness, but we can't do anything about it.
Use strerror_r over strerror for thread safety

strerror is not reentrant and not guaranteed to be thread safe, so we
should use strerror_r instead.