From 1d4f883090c6e081041d8909256a393727a2ecf1 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 30 Jan 2015 11:49:20 +0100 Subject: [PATCH] Move sdb_get_homedir() from tools/sysdb to utils/os. --- src/include/utils/os.h | 12 ++++++++++++ src/tools/sysdb/main.c | 38 ++++---------------------------------- src/utils/os.c | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/include/utils/os.h b/src/include/utils/os.h index 8ffca4b..f4decde 100644 --- a/src/include/utils/os.h +++ b/src/include/utils/os.h @@ -35,6 +35,18 @@ extern "C" { #endif +/* + * sdb_get_homedir: + * Returns the home directory of the current user. The buffer to hold the + * return value is allocated dynamically and has to be freed by the caller. + * + * Returns: + * - the current user's home directory on success + * - NULL else + */ +char * +sdb_get_homedir(void); + /* * sysdb_mkdir_all: * Recursively create the directory 'pathname' (similar to 'mkdir -p' on the diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c index 20f37ae..b22e11f 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -84,38 +84,6 @@ # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock" #endif -static const char * -get_homedir(void) -{ - char *username = sdb_get_current_user(); - - struct passwd pw_entry; - struct passwd *result = NULL; - - /* needs to be static because we return a pointer into this buffer - * to the caller */ - static char buf[1024]; - - int status; - - if (username) { - memset(&pw_entry, 0, sizeof(pw_entry)); - status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result); - } - else - status = -1; - - if (status || (! result)) { - char errbuf[1024]; - sdb_log(SDB_LOG_WARNING, "Failed to determine home directory " - "for user %s: %s", username, - sdb_strerror(errno, errbuf, sizeof(errbuf))); - free(username); - return NULL; - } - free(username); - return result->pw_dir; -} /* get_homedir */ static void exit_usage(char *name, int status) @@ -214,7 +182,7 @@ main(int argc, char **argv) { const char *host = NULL; - const char *homedir; + char *homedir; char hist_file[1024] = ""; sdb_input_t input = SDB_INPUT_INIT; @@ -313,10 +281,12 @@ main(int argc, char **argv) using_history(); - if ((homedir = get_homedir())) { + if ((homedir = sdb_get_homedir())) { snprintf(hist_file, sizeof(hist_file) - 1, "%s/.sysdb_history", homedir); hist_file[sizeof(hist_file) - 1] = '\0'; + free(homedir); + homedir = NULL; errno = 0; if (read_history(hist_file) && (errno != ENOENT)) { diff --git a/src/utils/os.c b/src/utils/os.c index 4afc466..e975f96 100644 --- a/src/utils/os.c +++ b/src/utils/os.c @@ -53,6 +53,37 @@ * public API */ +char * +sdb_get_homedir(void) +{ + char *username = sdb_get_current_user(); + + struct passwd pw_entry; + struct passwd *result = NULL; + + char buf[4096]; + + int status; + + if (username) { + memset(&pw_entry, 0, sizeof(pw_entry)); + status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result); + } + else + status = -1; + + if (status || (! result)) { + char errbuf[1024]; + sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory " + "for user %s: %s", username, + sdb_strerror(errno, errbuf, sizeof(errbuf))); + free(username); + return NULL; + } + free(username); + return strdup(result->pw_dir); +} /* sdb_get_homedir */ + int sdb_mkdir_all(const char *pathname, mode_t mode) { @@ -152,7 +183,7 @@ sdb_get_current_user(void) uid_t uid; - char buf[1024]; + char buf[4096]; int status; uid = geteuid(); -- 2.30.2