src/shared: import many code cleanups from upstream

Signed-off-by: Anthony G. Basile <blueness@gentoo.org>