From 556ef90e040ffa7176e7e4b698fdda5f1151bc5c Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 11 Feb 2016 10:33:43 +0100 Subject: [PATCH] Show actual / maximum number of open files in speed notification Refs #311 --- .../eu/faircode/netguard/SinkholeService.java | 24 +++---------------- app/src/main/jni/netguard/netguard.c | 20 ++++++++++++++-- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 24c6aa7b..8e11497f 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -146,7 +146,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS private native void jni_stop(int tun, boolean clear); - private native int[] jni_get_session_count(); + private native int[] jni_get_stats(); private static native void jni_pcap(String name); @@ -635,27 +635,9 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS // Show session/file count if (filter && loglevel <= Log.WARN) { - int[] count = jni_get_session_count(); + int[] count = jni_get_stats(); remoteViews.setTextViewText(R.id.tvSessions, count[0] + "/" + count[1] + "/" + count[2]); - - File proc = new File("/proc/sys/fs/file-nr"); - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(proc)); - String line = br.readLine(); - String[] filenr = line.split("\\s+"); - if (filenr.length == 3) { - int perc = Integer.parseInt(filenr[0]) * 100 / Integer.parseInt(filenr[2]); - remoteViews.setTextViewText(R.id.tvFiles, perc + "%"); - } - } catch (Throwable ignored) { - } finally { - if (br != null) - try { - br.close(); - } catch (IOException ignored) { - } - } + remoteViews.setTextViewText(R.id.tvFiles, count[3] + "/" + count[4]); } // Show notification diff --git a/app/src/main/jni/netguard/netguard.c b/app/src/main/jni/netguard/netguard.c index 2d582aa6..0f6d3664 100644 --- a/app/src/main/jni/netguard/netguard.c +++ b/app/src/main/jni/netguard/netguard.c @@ -174,12 +174,14 @@ Java_eu_faircode_netguard_SinkholeService_jni_1stop( log_android(ANDROID_LOG_WARN, "Not running thread %x", t); } +#include + JNIEXPORT jintArray JNICALL -Java_eu_faircode_netguard_SinkholeService_jni_1get_1session_1count(JNIEnv *env, jobject instance) { +Java_eu_faircode_netguard_SinkholeService_jni_1get_1stats(JNIEnv *env, jobject instance) { if (pthread_mutex_lock(&lock)) log_android(ANDROID_LOG_ERROR, "pthread_mutex_lock failed"); - jintArray jarray = (*env)->NewIntArray(env, 3); + jintArray jarray = (*env)->NewIntArray(env, 5); jint *jcount = (*env)->GetIntArrayElements(env, jarray, NULL); jcount[0] = get_icmp_sessions(); jcount[1] = get_udp_sessions(); @@ -188,6 +190,20 @@ Java_eu_faircode_netguard_SinkholeService_jni_1get_1session_1count(JNIEnv *env, if (pthread_mutex_unlock(&lock)) log_android(ANDROID_LOG_ERROR, "pthread_mutex_unlock failed"); + jcount[3] = 0; + DIR *d = opendir("/proc/self/fd"); + if (d) { + struct dirent *dir; + while ((dir = readdir(d)) != NULL) + if (dir->d_type != DT_DIR) + jcount[3]++; + closedir(d); + } + + struct rlimit rlim; + memset(&rlim, 0, sizeof(struct rlimit)); + getrlimit(RLIMIT_NOFILE, &rlim); + jcount[4] = rlim.rlim_cur; (*env)->ReleaseIntArrayElements(env, jarray, jcount, NULL); return jarray;