ibus: fall back from unreadable machine id

This commit is contained in:
2026-08-14 02:10:16 +09:00
parent e44fc005da
commit 6c0a533c4e
4 changed files with 72 additions and 14 deletions

41
ibus.c
View File

@@ -55,24 +55,37 @@ unlinkaddr(void)
addrowned = 0;
}
static void
machineidfiles(char *buf, int sz, char **path, int npath)
{
int fd, i, n;
for(i = 0; i < npath; i++){
fd = open(path[i], 0);
if(fd < 0)
continue;
n = read(fd, buf, sz - 1);
close(fd);
if(n <= 0)
continue;
buf[n] = '\0';
while(n > 0 && (buf[n-1] == '\n' || buf[n-1] == '\r'))
buf[--n] = '\0';
if(n > 0)
return;
}
buf[0] = '\0';
}
static void
machineid(char *buf, int sz)
{
int fd, n;
char *path[] = {
"/etc/machine-id",
"/var/lib/dbus/machine-id",
};
fd = open("/etc/machine-id", 0);
if(fd < 0)
fd = open("/var/lib/dbus/machine-id", 0);
if(fd < 0){
buf[0] = '\0';
return;
}
n = read(fd, buf, sz - 1);
close(fd);
if(n < 0) n = 0;
buf[n] = '\0';
while(n > 0 && (buf[n-1] == '\n' || buf[n-1] == '\r'))
buf[--n] = '\0';
machineidfiles(buf, sz, path, nelem(path));
}
static void