diff --git a/ibus.c b/ibus.c index 082c460..1ff062b 100644 --- a/ibus.c +++ b/ibus.c @@ -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 diff --git a/tests/ibus_test.c b/tests/ibus_test.c index f55c5c6..5871783 100644 --- a/tests/ibus_test.c +++ b/tests/ibus_test.c @@ -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) { diff --git a/tests/test.h b/tests/test.h index 30d75b7..1cdfce2 100644 --- a/tests/test.h +++ b/tests/test.h @@ -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*); diff --git a/tests/unit_test.c b/tests/unit_test.c index 11a6f29..80258ab 100644 --- a/tests/unit_test.c +++ b/tests/unit_test.c @@ -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 }, };