Use a more robust way to get user name

'logname' can fail in some situations.