imsd: List answers IOError for a store it could not read, and a scan's read error no longer ends the daemon
LoadStoredSms had no way to report a failure. A store directory that could not be opened read as an empty store, and a record that could not be opened read as a corrupt file and was skipped: Messaging.List answered success with texts missing. A consumer that forgets a deleted text's id once List no longer carries it forgets it too early then, and imports the text again when a later List has it; and a text that arrived while no app ran stays unseen until another List (issue #5). The scans also walked the directory with the iterator's throwing increment and tested each entry with is_regular_file() in its throwing form, and nothing catches that at the start's sweep, in List's handler or in the run loop: a read error there ended the process — in the run loop from the count for the `stored` journal line, with a text stored and its delivery report unsent. The three scans now go through opendir, readdir, stat and read. What a scan cannot read is a StoreFault, logged by the scan that met it; List's read and the count hand theirs back as well. List answers org.freedesktop.DBus.Error.IOError with the system's reason, the error Delete gives for a removal that failed: no list, so a consumer takes nothing for deleted and asks again. A store directory that does not exist is still an empty list, and a file that reads and is no record is still skipped with its line. The sweep logs the fault and lets the daemon start; the `stored` line goes without its count. The journal names what could not be read: `sms store: cannot read <path>: <reason>`. The Messaging interface is frozen as of 1.2.0 (AGENTS.md §8.4): no member, signature, key, state word or error name changes; List gains a failure answer under an error name the interface has. Tested in dev mode on a private session bus, on the workstation's build of this commit (the fp6 journal's 2026-11-20-issues-2-5 capture, list-io): IOError with "Permission denied" for the store directory at mode 000 and at mode 400 and for a record at mode 000, and with "Not a directory" for a file in the directory's place; an empty list with no directory; both records listed beside a file that is no record, a directory and a dangling link named like records; a start with the directory at mode 000 logs the sweep's fault and the daemon answers. On the dev phone, with the binary of this range's last commit (the capture's phone-form6): an empty list with the store directory moved away, IOError with "Not a directory" with a file in its place, the fault in the journal for each call it refused. Not benched: a read that fails part-way, a file removed between the directory's read and its own, a process out of descriptors, the `stored` line without its count.
This commit is contained in:
parent
9333621c66
commit
710f884575
3 changed files with 187 additions and 30 deletions
|
|
@ -29,6 +29,7 @@ its subscriber set on the main thread too.
|
|||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <gio/gio.h>
|
||||
#include <glib-unix.h>
|
||||
|
|
@ -40,6 +41,7 @@ its subscriber set on the main thread too.
|
|||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -1033,7 +1035,12 @@ void EmitSignalTo(const std::string& destination, const char* name, GVariant* pa
|
|||
// A record is written whole to a .tmp beside it, fsynced, linked to its
|
||||
// name with link(2) — which refuses a name already taken — and the .tmp
|
||||
// removed, the directory fsynced after it, so a listing never sees a
|
||||
// half-written one and nothing is replaced.
|
||||
// half-written one and nothing is replaced. The three scans — the start's
|
||||
// sweep, List's read and the count for the journal — go through
|
||||
// opendir(3), readdir(3), stat(2) and read(2), whose errors come back as
|
||||
// values. What a scan cannot read is a StoreFault, logged by the scan that
|
||||
// met it;
|
||||
// List's read and the count hand theirs back as well.
|
||||
std::string SmsStoreDir() { return std::format("{}/sms", StateDir()); }
|
||||
|
||||
bool ValidSmsId(std::string_view id) {
|
||||
|
|
@ -1045,18 +1052,104 @@ bool ValidSmsId(std::string_view id) {
|
|||
|
||||
std::string SmsRecordPath(std::string_view id) { return std::format("{}/{}.msg", SmsStoreDir(), id); }
|
||||
|
||||
// What kept a scan of the store from reading it whole: where it stopped
|
||||
// and the system's error. Empty when nothing did.
|
||||
struct StoreFault {
|
||||
std::string path;
|
||||
int err = 0;
|
||||
explicit operator bool() const { return err != 0; }
|
||||
std::string Reason() const { return std::generic_category().message(err); }
|
||||
};
|
||||
|
||||
void LogStoreFault(const StoreFault& f) { Log(std::format("sms store: cannot read {}: {}", f.path, f.Reason())); }
|
||||
|
||||
// The names in the store directory. A directory that is not there holds
|
||||
// none and is no fault: the first record stored makes it. Any other
|
||||
// failure to open it, or to read it to its end, is — and the names read
|
||||
// until then are not handed over.
|
||||
StoreFault SmsStoreNames(std::vector<std::string>& names) {
|
||||
const std::string dir = SmsStoreDir();
|
||||
DIR* d = opendir(dir.c_str());
|
||||
if (!d) {
|
||||
const int err = errno; // read before the fault copies the path: an allocation may change it
|
||||
return err == ENOENT ? StoreFault{} : StoreFault{dir, err};
|
||||
}
|
||||
StoreFault fault;
|
||||
for (;;) {
|
||||
errno = 0; // readdir(3): a null with errno unchanged is the end
|
||||
const dirent* e = readdir(d);
|
||||
if (!e) {
|
||||
if (const int err = errno; err != 0) fault = {dir, err};
|
||||
break;
|
||||
}
|
||||
names.emplace_back(e->d_name);
|
||||
}
|
||||
closedir(d);
|
||||
if (fault) names.clear();
|
||||
return fault;
|
||||
}
|
||||
|
||||
// A store file's name by its ending: `<something>.msg` a record,
|
||||
// `<something>.tmp` a writer's temporary file.
|
||||
bool HasStoreSuffix(std::string_view name, std::string_view suffix) { return name.size() > suffix.size() && name.ends_with(suffix); }
|
||||
|
||||
// Whether `path` is a regular file, a symbolic link to one included. One
|
||||
// that is gone since the directory was read is not, and no fault; any
|
||||
// other failure of stat(2) is the fault.
|
||||
bool IsStoreFile(const std::string& path, StoreFault& fault) {
|
||||
struct stat st {};
|
||||
if (stat(path.c_str(), &st) != 0) {
|
||||
if (const int err = errno; err != ENOENT) fault = {path, err};
|
||||
return false;
|
||||
}
|
||||
return S_ISREG(st.st_mode);
|
||||
}
|
||||
|
||||
// A store file's whole content into `text`. False when the file is gone
|
||||
// since the directory was read (no fault) or could not be opened or read
|
||||
// to its end (the fault).
|
||||
bool ReadStoreFile(const std::string& path, std::string& text, StoreFault& fault) {
|
||||
const int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
if (const int err = errno; err != ENOENT) fault = {path, err};
|
||||
return false;
|
||||
}
|
||||
bool whole = false;
|
||||
char buf[4096];
|
||||
for (;;) {
|
||||
const ssize_t n = read(fd, buf, sizeof buf);
|
||||
if (n > 0) { text.append(buf, static_cast<std::size_t>(n)); continue; }
|
||||
const int err = n < 0 ? errno : 0;
|
||||
if (err == EINTR) continue;
|
||||
if (err != 0) fault = {path, err};
|
||||
else whole = true;
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
return whole;
|
||||
}
|
||||
|
||||
// The .tmp files a crash left (between the open and the link, or between
|
||||
// the link and the unlink, when one is a record's second name): unlinked
|
||||
// at the engine's start, before it takes its first message; the count is
|
||||
// of those the unlink removed. A record whose second name goes keeps its
|
||||
// inode.
|
||||
// inode. A store that cannot be read is logged and left: the daemon
|
||||
// starts all the same.
|
||||
int SweepSmsStore() {
|
||||
std::error_code ec;
|
||||
std::vector<std::string> names;
|
||||
StoreFault fault = SmsStoreNames(names);
|
||||
int n = 0;
|
||||
for (const auto& e : std::filesystem::directory_iterator(SmsStoreDir(), ec)) {
|
||||
if (!e.is_regular_file() || e.path().extension() != ".tmp") continue;
|
||||
if (unlink(e.path().c_str()) == 0) n++;
|
||||
for (const std::string& name : names) {
|
||||
if (!HasStoreSuffix(name, ".tmp")) continue;
|
||||
const std::string path = std::format("{}/{}", SmsStoreDir(), name);
|
||||
StoreFault here;
|
||||
if (!IsStoreFile(path, here)) {
|
||||
if (here && !fault) fault = here;
|
||||
continue;
|
||||
}
|
||||
if (unlink(path.c_str()) == 0) n++;
|
||||
}
|
||||
if (fault) LogStoreFault(fault);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
@ -1107,25 +1200,42 @@ StoreResult StoreReceivedSms(const imsd::engine::StoredSms& r, std::string& why)
|
|||
return StoreResult::Stored;
|
||||
}
|
||||
|
||||
// The stored messages, oldest first: the `.msg` files, one that is no
|
||||
// record skipped (logged when `log`).
|
||||
std::vector<imsd::engine::StoredSms> LoadStoredSms(bool log = true) {
|
||||
std::vector<imsd::engine::StoredSms> out;
|
||||
std::error_code ec;
|
||||
for (const auto& e : std::filesystem::directory_iterator(SmsStoreDir(), ec)) {
|
||||
if (!e.is_regular_file() || e.path().extension() != ".msg") continue;
|
||||
std::ifstream in(e.path(), std::ios::binary);
|
||||
std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
// The stored messages, oldest first: the `.msg` files, one that reads and
|
||||
// is no record skipped (logged when `log`) — one corrupt file does not
|
||||
// close the store. The scan stops at the first thing it cannot read — the
|
||||
// directory, a file's type, a file — with that as `fault`, logged: the
|
||||
// records are then not the store's, and a caller must not hand them over
|
||||
// as if they were.
|
||||
struct StoreRead {
|
||||
std::vector<imsd::engine::StoredSms> records;
|
||||
StoreFault fault;
|
||||
};
|
||||
StoreRead LoadStoredSms(bool log = true) {
|
||||
StoreRead out;
|
||||
std::vector<std::string> names;
|
||||
out.fault = SmsStoreNames(names);
|
||||
for (const std::string& name : names) {
|
||||
if (out.fault) break;
|
||||
if (!HasStoreSuffix(name, ".msg")) continue;
|
||||
const std::string path = std::format("{}/{}", SmsStoreDir(), name);
|
||||
std::string text;
|
||||
if (!IsStoreFile(path, out.fault) || !ReadStoreFile(path, text, out.fault)) continue;
|
||||
auto r = imsd::engine::ParseStoredSms(text);
|
||||
if (!r) { if (log) Log(std::format("sms store: {} is no record; skipped", e.path().string())); continue; }
|
||||
out.push_back(std::move(*r));
|
||||
if (!r) { if (log) Log(std::format("sms store: {} is no record; skipped", path)); continue; }
|
||||
out.records.push_back(std::move(*r));
|
||||
}
|
||||
std::sort(out.begin(), out.end(), imsd::engine::StoredSmsBefore);
|
||||
if (out.fault) LogStoreFault(out.fault);
|
||||
std::sort(out.records.begin(), out.records.end(), imsd::engine::StoredSmsBefore);
|
||||
return out;
|
||||
}
|
||||
|
||||
// The records in the store (what List would hand over), not the files.
|
||||
std::size_t StoredSmsCount() { return LoadStoredSms(false).size(); }
|
||||
// The records in the store (what List would hand over), not the files;
|
||||
// none when the store could not be read.
|
||||
std::optional<std::size_t> StoredSmsCount() {
|
||||
const StoreRead store = LoadStoredSms(false);
|
||||
if (store.fault) return std::nullopt;
|
||||
return store.records.size();
|
||||
}
|
||||
|
||||
// a received short message as the Messaging interface's a{sv} (§8.4)
|
||||
GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt, const std::string& to, const std::string& simOperator) {
|
||||
|
|
@ -2975,8 +3085,13 @@ private:
|
|||
}
|
||||
Log(std::format("sms {}: from {} via {}, {}{}", ev->uni, m.from, m.sc, m.data.empty() ? std::format("{} bytes of text", m.text.size()) : std::format("{} bytes of 8-bit data", m.data.size()),
|
||||
m.segments > 1 ? std::format(" in {} segments", m.segments) : std::string()));
|
||||
if (stored == StoreResult::Stored) Log(std::format("sms {}: stored ({} kept until an app deletes them)", ev->uni, StoredSmsCount()));
|
||||
else Log(std::format("sms {}: NOT stored — {}; the bus signal is its only delivery", ev->uni, stored == StoreResult::Exists ? "a thousand names taken" : why));
|
||||
if (stored == StoreResult::Stored) {
|
||||
// a store that cannot be read back costs this line its count (the scan has logged why), never the signal or the delivery report
|
||||
const auto kept = StoredSmsCount();
|
||||
Log(kept ? std::format("sms {}: stored ({} kept until an app deletes them)", ev->uni, *kept) : std::format("sms {}: stored", ev->uni));
|
||||
} else {
|
||||
Log(std::format("sms {}: NOT stored — {}; the bus signal is its only delivery", ev->uni, stored == StoreResult::Exists ? "a thousand names taken" : why));
|
||||
}
|
||||
PostEvent(std::move(ev));
|
||||
}
|
||||
void PostSmsState(const std::string& id, std::string_view state, std::string_view reason) {
|
||||
|
|
@ -3374,9 +3489,15 @@ void HandleMessagingMethod(GDBusConnection* conn, const gchar*, const gchar*, co
|
|||
return;
|
||||
}
|
||||
if (m == "List") {
|
||||
const StoreRead store = LoadStoredSms();
|
||||
if (store.fault) {
|
||||
// not a shorter list: a consumer takes a record List leaves out for one it may forget
|
||||
g_dbus_method_invocation_return_dbus_error(inv, "org.freedesktop.DBus.Error.IOError", store.fault.Reason().c_str());
|
||||
return;
|
||||
}
|
||||
GVariantBuilder b;
|
||||
g_variant_builder_init(&b, G_VARIANT_TYPE("a(sa{sv})"));
|
||||
for (const auto& r : LoadStoredSms()) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt, r.to, r.simOperator));
|
||||
for (const auto& r : store.records) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt, r.to, r.simOperator));
|
||||
g_dbus_method_invocation_return_value(inv, g_variant_new("(a(sa{sv}))", &b));
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue