1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

util: add getusername_malloc(), get_shell(), get_home_dir()

This commit is contained in:
Lennart Poettering 2012-07-16 12:15:22 +02:00
parent 49f43d5f91
commit 7c5f152aca
2 changed files with 128 additions and 11 deletions

View File

@ -3078,26 +3078,22 @@ bool hostname_is_set(void) {
return !isempty(u.nodename) && !streq(u.nodename, "(none)");
}
char* getlogname_malloc(void) {
uid_t uid;
static char *lookup_uid(uid_t uid) {
long bufsize;
char *buf, *name;
struct passwd pwbuf, *pw = NULL;
struct stat st;
if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
uid = st.st_uid;
else
uid = getuid();
/* Shortcut things to avoid NSS lookups */
if (uid == 0)
return strdup("root");
if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0)
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize <= 0)
bufsize = 4096;
if (!(buf = malloc(bufsize)))
buf = malloc(bufsize);
if (!buf)
return NULL;
if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) {
@ -3114,6 +3110,28 @@ char* getlogname_malloc(void) {
return name;
}
char* getlogname_malloc(void) {
uid_t uid;
struct stat st;
if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
uid = st.st_uid;
else
uid = getuid();
return lookup_uid(uid);
}
char *getusername_malloc(void) {
const char *e;
e = getenv("USER");
if (e)
return strdup(e);
return lookup_uid(getuid());
}
int getttyname_malloc(int fd, char **r) {
char path[PATH_MAX], *c;
int k;
@ -5875,3 +5893,97 @@ int make_console_stdio(void) {
return 0;
}
int get_home_dir(char **_h) {
char *h;
const char *e;
uid_t u;
struct passwd *p;
assert(_h);
/* Take the user specified one */
e = getenv("HOME");
if (e) {
h = strdup(e);
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid();
if (u == 0) {
h = strdup("/root");
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
/* Check the database... */
errno = 0;
p = getpwuid(u);
if (!p)
return errno ? -errno : -ENOENT;
if (!path_is_absolute(p->pw_dir))
return -EINVAL;
h = strdup(p->pw_dir);
if (!h)
return -ENOMEM;
*_h = h;
return 0;
}
int get_shell(char **_sh) {
char *sh;
const char *e;
uid_t u;
struct passwd *p;
assert(_sh);
/* Take the user specified one */
e = getenv("SHELL");
if (e) {
sh = strdup(e);
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid();
if (u == 0) {
sh = strdup("/bin/sh");
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}
/* Check the database... */
errno = 0;
p = getpwuid(u);
if (!p)
return errno ? -errno : -ESRCH;
if (!path_is_absolute(p->pw_shell))
return -EINVAL;
sh = strdup(p->pw_shell);
if (!sh)
return -ENOMEM;
*_sh = sh;
return 0;
}

View File

@ -345,9 +345,11 @@ void rename_process(const char name[8]);
void sigset_add_many(sigset_t *ss, ...);
char* gethostname_malloc(void);
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* getlogname_malloc(void);
char* getusername_malloc(void);
int getttyname_malloc(int fd, char **r);
int getttyname_harder(int fd, char **r);
@ -527,4 +529,7 @@ bool in_initrd(void);
void warn_melody(void);
int get_shell(char **ret);
int get_home_dir(char **ret);
#endif