use getpwuid(geteuid())->pw_name rather than getlogin()

getlogin() can fail when stdin is not a tty on certain configuration
as it needs to find a controling terminal.
This commit is contained in:
Maxime Coste 2015-09-15 13:32:26 +01:00
parent 0338c0f437
commit 85918d9f15
2 changed files with 5 additions and 3 deletions

View File

@ -29,6 +29,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
using namespace Kakoune;
@ -598,7 +599,7 @@ int main(int argc, char* argv[])
if (parser.get_switch("l"))
{
for (auto& file : list_files(format("/tmp/kakoune/{}/", getlogin())))
for (auto& file : list_files(format("/tmp/kakoune/{}/", getpwuid(geteuid())->pw_name)))
write_stdout(format("{}\n", file));
return 0;
}

View File

@ -13,6 +13,7 @@
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <fcntl.h>
@ -411,7 +412,7 @@ static sockaddr_un session_addr(StringView session)
{
sockaddr_un addr;
addr.sun_family = AF_UNIX;
format_to(addr.sun_path, "/tmp/kakoune/{}/{}", getlogin(), session);
format_to(addr.sun_path, "/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, session);
return addr;
}
@ -631,7 +632,7 @@ Server::Server(String session_name)
void Server::close_session()
{
char socket_file[128];
format_to(socket_file, "/tmp/kakoune/{}/{}", getlogin(), m_session);
format_to(socket_file, "/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
unlink(socket_file);
m_listener->close_fd();
m_listener.reset();