From 039abb69c2d92da91eadcfc30189183ed85bc026 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Tue, 17 Dec 2024 05:56:59 +1100 Subject: [PATCH] Code changes to support FreeBSD * Code changes to support FreeBSD --- src/main.d | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- src/monitor.d | 20 ++++++++++++------- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/main.d b/src/main.d index 2bf126860..e106d7cbd 100644 --- a/src/main.d +++ b/src/main.d @@ -867,11 +867,9 @@ int main(string[] cliArgs) { performFileSystemMonitoring = true; // What are the current values for the platform we are running on - // Max number of open files /proc/sys/fs/file-max - string maxOpenFiles = strip(readText("/proc/sys/fs/file-max")); + string maxOpenFiles = strip(getMaxOpenFiles()); // What is the currently configured maximum inotify watches that can be used - // /proc/sys/fs/inotify/max_user_watches - string maxInotifyWatches = strip(readText("/proc/sys/fs/inotify/max_user_watches")); + string maxInotifyWatches = strip(getMaxInotifyWatches()); // Start the monitor process addLogEntry("OneDrive synchronisation interval (seconds): " ~ to!string(appConfig.getValueLong("monitor_interval"))); @@ -1238,6 +1236,54 @@ int main(string[] cliArgs) { } } +// Retrieves the maximum number of open files allowed by the system +string getMaxOpenFiles() { + version (Linux) { + try { + // Read max open files from procfs on Linux + return strip(readText("/proc/sys/fs/file-max")); + } catch (Exception e) { + return "Unknown (Error reading /proc/sys/fs/file-max)"; + } + } else version (FreeBSD) { + try { + // Read max open files using sysctl on FreeBSD + return strip(executeShell("sysctl -n kern.maxfiles").output); + } catch (Exception e) { + return "Unknown (sysctl error)"; + } + } else version (OpenBSD) { + try { + // Read max open files using sysctl on OpenBSD + return strip(executeShell("sysctl -n kern.maxfiles").output); + } catch (Exception e) { + return "Unknown (sysctl error)"; + } + } else { + return "Unsupported platform"; + } +} + +// Retrieves the maximum inotify watches allowed (Linux) or a placeholder for other platforms +string getMaxInotifyWatches() { + version (Linux) { + try { + // Read max inotify watches from procfs on Linux + return strip(readText("/proc/sys/fs/inotify/max_user_watches")); + } catch (Exception e) { + return "Unknown (Error reading /proc/sys/fs/inotify/max_user_watches)"; + } + } else version (FreeBSD) { + // FreeBSD uses kqueue instead of inotify, no direct equivalent + return "N/A (uses kqueue)"; + } else version (OpenBSD) { + // OpenBSD uses kqueue instead of inotify, no direct equivalent + return "N/A (uses kqueue)"; + } else { + return "Unsupported platform"; + } +} + // Print error message when --sync or --monitor has not been used and no valid 'no-sync' operation was requested void printMissingOperationalSwitchesError() { // notify the user that --sync or --monitor were missing diff --git a/src/monitor.d b/src/monitor.d index f5db7a741..3ac8bbcc6 100644 --- a/src/monitor.d +++ b/src/monitor.d @@ -57,13 +57,19 @@ class MonitorBackgroundWorker { int wd = inotify_add_watch(fd, toStringz(pathname), mask); if (wd < 0) { if (errno() == ENOSPC) { - // Get the current value - ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches"))); - addLogEntry("The user limit on the total number of inotify watches has been reached."); - addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches)); - addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value."); - addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:"); - addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2))); + version (Linux) { + // Read max inotify watches from procfs on Linux + ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches"))); + addLogEntry("The user limit on the total number of inotify watches has been reached."); + addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches)); + addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value."); + addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:"); + addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2))); + } else { + // some other platform + addLogEntry("The user limit on the total number of inotify watches has been reached."); + addLogEntry("Please seek support from your distribution on how to increase the max number of inotify watches to at least double your existing value."); + } } if (errno() == 13) { if (verboseLogging) {addLogEntry("WARNING: inotify_add_watch failed - permission denied: " ~ pathname, ["verbose"]);}