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

31
ibus.c
View File

@@ -56,23 +56,36 @@ unlinkaddr(void)
}
static void
machineid(char *buf, int sz)
machineidfiles(char *buf, int sz, char **path, int npath)
{
int fd, n;
int fd, i, n;
fd = open("/etc/machine-id", 0);
for(i = 0; i < npath; i++){
fd = open(path[i], 0);
if(fd < 0)
fd = open("/var/lib/dbus/machine-id", 0);
if(fd < 0){
buf[0] = '\0';
return;
}
continue;
n = read(fd, buf, sz - 1);
close(fd);
if(n < 0) n = 0;
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)
{
char *path[] = {
"/etc/machine-id",
"/var/lib/dbus/machine-id",
};
machineidfiles(buf, sz, path, nelem(path));
}
static void

View File

@@ -24,6 +24,49 @@ struct Ibusfix
int pumpactive;
};
void
ibus_machine_id_fallback(struct ct *t)
{
char root[] = "/tmp/strans-machine-id.XXXXXX";
char primary[128], fallback[128], got[64];
char *path[2];
FILE *fp;
int madeprimary, madefallback;
madeprimary = 0;
madefallback = 0;
if(!CT_CHECK(t, mkdtemp(root) != nil))
return;
if(!CT_CHECK(t, snprintf(primary, sizeof primary, "%s/primary", root)
< (int)sizeof primary) ||
!CT_CHECK(t, snprintf(fallback, sizeof fallback, "%s/fallback", root)
< (int)sizeof fallback))
goto cleanup;
if(!CT_CHECK(t, mkdir(primary, 0700) == 0))
goto cleanup;
madeprimary = 1;
fp = fopen(fallback, "w");
if(!CT_CHECK(t, fp != nil))
goto cleanup;
madefallback = 1;
if(!CT_CHECK(t, fputs("fallback-machine-id\n", fp) >= 0)){
fclose(fp);
goto cleanup;
}
if(!CT_CHECK(t, fclose(fp) == 0))
goto cleanup;
path[0] = primary;
path[1] = fallback;
machineidfiles(got, sizeof got, path, nelem(path));
CT_EQ_STR(t, "fallback-machine-id", got);
cleanup:
if(madefallback)
CT_CHECK(t, unlink(fallback) == 0);
if(madeprimary)
CT_CHECK(t, rmdir(primary) == 0);
CT_CHECK(t, rmdir(root) == 0);
}
static void
enginepump(void *arg)
{

View File

@@ -71,5 +71,6 @@ void ipc_response_max_and_drain(struct ct*);
void ipc_response_fragmented_and_truncated(struct ct*);
void ipc_broken_peer_send(struct ct*);
void server_connection_ownership(struct ct*);
void ibus_machine_id_fallback(struct ct*);
void ibus_context_lifecycle(struct ct*);
void ibus_active_release_lifecycle(struct ct*);

View File

@@ -125,6 +125,7 @@ static const struct ct_test tests[] = {
{ "ipc/response-fragmented-truncated", ipc_response_fragmented_and_truncated },
{ "ipc/broken-peer-send", ipc_broken_peer_send },
{ "server/connection-ownership", server_connection_ownership },
{ "ibus/machine-id-fallback", ibus_machine_id_fallback },
{ "ibus/context-lifecycle", ibus_context_lifecycle },
{ "ibus/active-release-lifecycle", ibus_active_release_lifecycle },
};