From a3839aa69f643c34022dc456368126fe17b036fb Mon Sep 17 00:00:00 2001 From: Martin Stiborsky Date: Mon, 5 Jan 2015 11:46:11 +0100 Subject: [PATCH 1/4] getting current user with whoami like function rather than from env variable --- i3lock.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/i3lock.c b/i3lock.c index 71b02ee..fd32f97 100644 --- a/i3lock.c +++ b/i3lock.c @@ -8,6 +8,7 @@ */ #include #include +#include #include #include #include @@ -658,8 +659,18 @@ static void raise_loop(xcb_window_t window) { } } +char* whoami(void) { + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); + if (pw) { + return pw->pw_name; + } else { + errx(EXIT_FAILURE, "Username not known!\n"); + } +} + int main(int argc, char *argv[]) { - char *username; + char *username = whoami(); char *image_path = NULL; int ret; struct pam_conv conv = {conv_callback, NULL}; @@ -684,9 +695,6 @@ int main(int argc, char *argv[]) { {NULL, no_argument, NULL, 0} }; - if ((username = getenv("USER")) == NULL) - errx(EXIT_FAILURE, "USER environment variable not set, please set it.\n"); - char *optstring = "hvnbdc:p:ui:teI:f"; while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) { switch (o) { From 7d52029ae3a8bb21951106fe8e3e88156a57859b Mon Sep 17 00:00:00 2001 From: Martin Stiborsky Date: Mon, 5 Jan 2015 19:54:48 +0100 Subject: [PATCH 2/4] removed the whoami function, replaced with an inline getpwuid call --- i3lock.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/i3lock.c b/i3lock.c index fd32f97..bead884 100644 --- a/i3lock.c +++ b/i3lock.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -659,18 +660,8 @@ static void raise_loop(xcb_window_t window) { } } -char* whoami(void) { - uid_t uid = geteuid(); - struct passwd *pw = getpwuid(uid); - if (pw) { - return pw->pw_name; - } else { - errx(EXIT_FAILURE, "Username not known!\n"); - } -} - int main(int argc, char *argv[]) { - char *username = whoami(); + char *username = getpwuid(getuid())->pw_name; char *image_path = NULL; int ret; struct pam_conv conv = {conv_callback, NULL}; @@ -695,6 +686,9 @@ int main(int argc, char *argv[]) { {NULL, no_argument, NULL, 0} }; + if (username == NULL) + err(EXIT_FAILURE, "getpwuid() failed"); + char *optstring = "hvnbdc:p:ui:teI:f"; while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) { switch (o) { From 6ffe86ca12c0a1b532532284aebe4ce417323cdb Mon Sep 17 00:00:00 2001 From: Martin Stiborsky Date: Mon, 5 Jan 2015 22:54:32 +0100 Subject: [PATCH 3/4] null check for getpwuid and pw_name --- i3lock.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/i3lock.c b/i3lock.c index bead884..4f9d5d4 100644 --- a/i3lock.c +++ b/i3lock.c @@ -661,7 +661,8 @@ static void raise_loop(xcb_window_t window) { } int main(int argc, char *argv[]) { - char *username = getpwuid(getuid())->pw_name; + struct passwd *pw = getpwuid(getuid()); + char *username; char *image_path = NULL; int ret; struct pam_conv conv = {conv_callback, NULL}; @@ -686,8 +687,10 @@ int main(int argc, char *argv[]) { {NULL, no_argument, NULL, 0} }; - if (username == NULL) + if (pw == NULL) err(EXIT_FAILURE, "getpwuid() failed"); + if ((username = pw->pw_name) == NULL) + errx(EXIT_FAILURE, "pw->pw_name is NULL.\n"); char *optstring = "hvnbdc:p:ui:teI:f"; while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) { From 665ce3e215b9b47a251b4433fe776d42072d9e9b Mon Sep 17 00:00:00 2001 From: Martin Stiborsky Date: Tue, 6 Jan 2015 00:02:23 +0100 Subject: [PATCH 4/4] getpwuid call moved --- i3lock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i3lock.c b/i3lock.c index 4f9d5d4..942781a 100644 --- a/i3lock.c +++ b/i3lock.c @@ -661,7 +661,7 @@ static void raise_loop(xcb_window_t window) { } int main(int argc, char *argv[]) { - struct passwd *pw = getpwuid(getuid()); + struct passwd *pw; char *username; char *image_path = NULL; int ret; @@ -687,7 +687,7 @@ int main(int argc, char *argv[]) { {NULL, no_argument, NULL, 0} }; - if (pw == NULL) + if ((pw = getpwuid(getuid())) == NULL) err(EXIT_FAILURE, "getpwuid() failed"); if ((username = pw->pw_name) == NULL) errx(EXIT_FAILURE, "pw->pw_name is NULL.\n");