core-util: introduce pa_get_host_name_malloc() and pa_get_user_name_malloc()

This commit is contained in:
Lennart Poettering 2009-04-29 01:54:44 +02:00
parent 4abd5fae14
commit a8f0d7ec1e
2 changed files with 49 additions and 19 deletions

View file

@ -2467,31 +2467,29 @@ pa_bool_t pa_in_system_mode(void) {
return !!atoi(e); return !!atoi(e);
} }
char *pa_machine_id(void) { char *pa_get_user_name_malloc(void) {
FILE *f; ssize_t k;
size_t l; char *u;
/* The returned value is supposed be some kind of ascii identifier #ifdef _SC_LOGIN_NAME_MAX
* that is unique and stable across reboots. */ k = (ssize_t) sysconf(_SC_LOGIN_NAME_MAX);
/* First we try the D-Bus UUID, which is the best option we have, if (k <= 0)
* since it fits perfectly our needs and is not as volatile as the #endif
* hostname which might be set from dhcp. */ k = 32;
if ((f = fopen(PA_MACHINE_ID, "r"))) { u = pa_xnew(char, k+1);
char ln[34] = "", *r;
r = fgets(ln, sizeof(ln)-1, f); if (!(pa_get_user_name(u, k))) {
fclose(f); pa_xfree(u);
return NULL;
pa_strip_nl(ln);
if (r && ln[0])
return pa_utf8_filter(ln);
} }
/* The we fall back to the host name. It supposed to be somewhat return u;
* unique, at least in a network, but may change. */ }
char *pa_get_host_name_malloc(void) {
size_t l;
l = 100; l = 100;
for (;;) { for (;;) {
@ -2525,6 +2523,35 @@ char *pa_machine_id(void) {
l *= 2; l *= 2;
} }
return NULL;
}
char *pa_machine_id(void) {
FILE *f;
char *h;
/* The returned value is supposed be some kind of ascii identifier
* that is unique and stable across reboots. */
/* First we try the D-Bus UUID, which is the best option we have,
* since it fits perfectly our needs and is not as volatile as the
* hostname which might be set from dhcp. */
if ((f = fopen(PA_MACHINE_ID, "r"))) {
char ln[34] = "", *r;
r = fgets(ln, sizeof(ln)-1, f);
fclose(f);
pa_strip_nl(ln);
if (r && ln[0])
return pa_utf8_filter(ln);
}
if ((h = pa_get_host_name_malloc()))
return h;
/* If no hostname was set we use the POSIX hostid. It's usually /* If no hostname was set we use the POSIX hostid. It's usually
* the IPv4 address. Might not be that stable. */ * the IPv4 address. Might not be that stable. */
return pa_sprintf_malloc("%08lx", (unsigned long) gethostid); return pa_sprintf_malloc("%08lx", (unsigned long) gethostid);

View file

@ -201,6 +201,9 @@ pa_bool_t pa_in_system_mode(void);
#define pa_streq(a,b) (!strcmp((a),(b))) #define pa_streq(a,b) (!strcmp((a),(b)))
char *pa_get_host_name_malloc(void);
char *pa_get_user_name_malloc(void);
char *pa_machine_id(void); char *pa_machine_id(void);
char *pa_session_id(void); char *pa_session_id(void);
char *pa_uname_string(void); char *pa_uname_string(void);