ibus: fall back from unreadable machine id
This commit is contained in:
41
ibus.c
41
ibus.c
@@ -55,24 +55,37 @@ unlinkaddr(void)
|
|||||||
addrowned = 0;
|
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
|
static void
|
||||||
machineid(char *buf, int sz)
|
machineid(char *buf, int sz)
|
||||||
{
|
{
|
||||||
int fd, n;
|
char *path[] = {
|
||||||
|
"/etc/machine-id",
|
||||||
|
"/var/lib/dbus/machine-id",
|
||||||
|
};
|
||||||
|
|
||||||
fd = open("/etc/machine-id", 0);
|
machineidfiles(buf, sz, path, nelem(path));
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|||||||
@@ -24,6 +24,49 @@ struct Ibusfix
|
|||||||
int pumpactive;
|
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
|
static void
|
||||||
enginepump(void *arg)
|
enginepump(void *arg)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,5 +71,6 @@ void ipc_response_max_and_drain(struct ct*);
|
|||||||
void ipc_response_fragmented_and_truncated(struct ct*);
|
void ipc_response_fragmented_and_truncated(struct ct*);
|
||||||
void ipc_broken_peer_send(struct ct*);
|
void ipc_broken_peer_send(struct ct*);
|
||||||
void server_connection_ownership(struct ct*);
|
void server_connection_ownership(struct ct*);
|
||||||
|
void ibus_machine_id_fallback(struct ct*);
|
||||||
void ibus_context_lifecycle(struct ct*);
|
void ibus_context_lifecycle(struct ct*);
|
||||||
void ibus_active_release_lifecycle(struct ct*);
|
void ibus_active_release_lifecycle(struct ct*);
|
||||||
|
|||||||
@@ -125,6 +125,7 @@ static const struct ct_test tests[] = {
|
|||||||
{ "ipc/response-fragmented-truncated", ipc_response_fragmented_and_truncated },
|
{ "ipc/response-fragmented-truncated", ipc_response_fragmented_and_truncated },
|
||||||
{ "ipc/broken-peer-send", ipc_broken_peer_send },
|
{ "ipc/broken-peer-send", ipc_broken_peer_send },
|
||||||
{ "server/connection-ownership", server_connection_ownership },
|
{ "server/connection-ownership", server_connection_ownership },
|
||||||
|
{ "ibus/machine-id-fallback", ibus_machine_id_fallback },
|
||||||
{ "ibus/context-lifecycle", ibus_context_lifecycle },
|
{ "ibus/context-lifecycle", ibus_context_lifecycle },
|
||||||
{ "ibus/active-release-lifecycle", ibus_active_release_lifecycle },
|
{ "ibus/active-release-lifecycle", ibus_active_release_lifecycle },
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user