mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-03 09:01:50 -05:00 
			
		
		
		
	Rework pa_machine_id() a bit
Guarantee this function never fails, use POSIX gethostid as last resort. Add comments.
This commit is contained in:
		
							parent
							
								
									5f931138d2
								
							
						
					
					
						commit
						99d5ec6c93
					
				
					 1 changed files with 27 additions and 10 deletions
				
			
		| 
						 | 
					@ -2391,18 +2391,29 @@ char *pa_machine_id(void) {
 | 
				
			||||||
    FILE *f;
 | 
					    FILE *f;
 | 
				
			||||||
    size_t l;
 | 
					    size_t l;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 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"))) {
 | 
					    if ((f = fopen(PA_MACHINE_ID, "r"))) {
 | 
				
			||||||
        char ln[34] = "", *r;
 | 
					        char ln[34] = "", *r;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        r = fgets(ln, sizeof(ln)-1, f);
 | 
					        r = fgets(ln, sizeof(ln)-1, f);
 | 
				
			||||||
        fclose(f);
 | 
					        fclose(f);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (r)
 | 
					        pa_strip_nl(ln);
 | 
				
			||||||
            return pa_xstrdup(pa_strip_nl(ln));
 | 
					
 | 
				
			||||||
 | 
					        if (ln[0])
 | 
				
			||||||
 | 
					            return pa_xstrdup(ln);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    l = 100;
 | 
					    /* The we fall back to the host name. It supposed to be somewhat
 | 
				
			||||||
 | 
					     * unique, at least in a network, but may change. */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    l = 100;
 | 
				
			||||||
    for (;;) {
 | 
					    for (;;) {
 | 
				
			||||||
        char *c;
 | 
					        char *c;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2410,17 +2421,18 @@ char *pa_machine_id(void) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!pa_get_host_name(c, l)) {
 | 
					        if (!pa_get_host_name(c, l)) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (errno == EINVAL || errno == ENAMETOOLONG) {
 | 
					            if (errno != EINVAL && errno != ENAMETOOLONG)
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        } else if (strlen(c) < l-1) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (*c == 0) {
 | 
				
			||||||
                pa_xfree(c);
 | 
					                pa_xfree(c);
 | 
				
			||||||
                l *= 2;
 | 
					                break;
 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return NULL;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (strlen(c) < l-1)
 | 
					 | 
				
			||||||
            return c;
 | 
					            return c;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* Hmm, the hostname is as long the space we offered the
 | 
					        /* Hmm, the hostname is as long the space we offered the
 | 
				
			||||||
         * function, we cannot know if it fully fit in, so let's play
 | 
					         * function, we cannot know if it fully fit in, so let's play
 | 
				
			||||||
| 
						 | 
					@ -2429,4 +2441,9 @@ char *pa_machine_id(void) {
 | 
				
			||||||
        pa_xfree(c);
 | 
					        pa_xfree(c);
 | 
				
			||||||
        l *= 2;
 | 
					        l *= 2;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* If no hostname was set we use the POSIX hostid. It's usually
 | 
				
			||||||
 | 
					     * the IPv4 address.  Mit not be that stable. */
 | 
				
			||||||
 | 
					    return pa_sprintf_malloc("%08lx", (unsigned long) gethostid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue