Skip to content

Commit 198030f

Browse files
committed
Restart monitor view when files are deleted.
When cvd create `--daemon=false` runs, the monitor view can open the log files from the previous run. The newly running device deletes the old log files and starts writing new ones, but the monitor holds references to the old files, and waits forever for them to update. This change adds delete event monitoring to the inotify watches, and reads through through the inotify event list to scan for delete events. Delete events trigger resetting the monitor view state so it can re-open the log files and create new watches. Bug: b/526626064 Test: launch device, exit, launch new device
1 parent ba558ef commit 198030f

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

  • base/cvd/cuttlefish/host/commands/cvd/cli/commands/monitor

base/cvd/cuttlefish/host/commands/cvd/cli/commands/monitor/monitor.cc

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void UpdateFileAndWatch(const SharedFD& inotify_fd, const std::string& path,
5454
}
5555
}
5656
if (fd->IsOpen() && watch == -1) {
57-
watch = inotify_fd->InotifyAddWatch(path, IN_MODIFY);
57+
watch = inotify_fd->InotifyAddWatch(path, IN_DELETE_SELF | IN_MODIFY);
5858
}
5959
}
6060

@@ -95,7 +95,8 @@ Result<void> MonitorLogs(const LocalInstance& instance, SharedFD stop_eventfd) {
9595

9696
const std::string logs_dir =
9797
absl::StrCat(instance.InstanceDirectory(), "/logs");
98-
const int dir_watch = inotify_fd->InotifyAddWatch(logs_dir, IN_CREATE);
98+
const int dir_watch =
99+
inotify_fd->InotifyAddWatch(logs_dir, IN_CREATE | IN_DELETE);
99100

100101
int kernel_watch = -1;
101102
int launcher_watch = -1;
@@ -112,16 +113,17 @@ Result<void> MonitorLogs(const LocalInstance& instance, SharedFD stop_eventfd) {
112113
}
113114
launcher_fd = SharedFD::Open(launcher_log, O_RDONLY);
114115
if (launcher_fd->IsOpen()) {
115-
launcher_watch = inotify_fd->InotifyAddWatch(launcher_log, IN_MODIFY);
116+
launcher_watch = inotify_fd->InotifyAddWatch(
117+
launcher_log, IN_DELETE_SELF | IN_MODIFY);
116118
using_assemble_log = false;
117119
}
118120
} else if (!launcher_fd->IsOpen()) {
119121
if (FileExists(assemble_log)) {
120122
launcher_fd = SharedFD::Open(assemble_log, O_RDONLY);
121123
if (launcher_fd->IsOpen()) {
122124
if (launcher_watch == -1) {
123-
launcher_watch =
124-
inotify_fd->InotifyAddWatch(assemble_log, IN_MODIFY);
125+
launcher_watch = inotify_fd->InotifyAddWatch(
126+
assemble_log, IN_DELETE_SELF | IN_MODIFY);
125127
}
126128
}
127129
}
@@ -212,6 +214,29 @@ Result<void> MonitorLogs(const LocalInstance& instance, SharedFD stop_eventfd) {
212214
__attribute__((aligned(__alignof__(struct inotify_event))));
213215
ssize_t read_res = 0;
214216
while ((read_res = inotify_fd->Read(buf, sizeof(buf))) > 0) {
217+
char* ptr = buf;
218+
while (ptr < buf + read_res) {
219+
inotify_event* event = reinterpret_cast<inotify_event*>(ptr);
220+
if (event->mask & (IN_DELETE | IN_DELETE_SELF | IN_IGNORED)) {
221+
using_assemble_log = true;
222+
if (launcher_watch != -1) {
223+
inotify_fd->InotifyRmWatch(launcher_watch);
224+
launcher_watch = -1;
225+
}
226+
launcher_fd = SharedFD();
227+
if (kernel_watch != -1) {
228+
inotify_fd->InotifyRmWatch(kernel_watch);
229+
kernel_watch = -1;
230+
}
231+
kernel_fd = SharedFD();
232+
if (logcat_watch != -1) {
233+
inotify_fd->InotifyRmWatch(logcat_watch);
234+
logcat_watch = -1;
235+
}
236+
logcat_fd = SharedFD();
237+
}
238+
ptr += sizeof(inotify_event) + event->len;
239+
}
215240
}
216241
CF_EXPECT(read_res != 0,
217242
"Unexpected End-of-File reading inotify descriptor");

0 commit comments

Comments
 (0)