From: Yong Shen <yong.s...@linaro.org>

Signed-off-by: Yong Shen <yong.s...@linaro.org>
---
 clocks.c |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 1 deletions(-)

diff --git a/clocks.c b/clocks.c
index dc5c03b..25588f7 100644
--- a/clocks.c
+++ b/clocks.c
@@ -16,12 +16,18 @@
 #include <stdio.h>
 #include <mntent.h>
 #include <sys/stat.h>
+#include <sys/inotify.h>
+#include <poll.h>
 
 #include "powerdebug.h"
 #include "clocks.h"
 
 #define MAX_LINES 120
 
+static int inotify_fd;
+static int inotify_wd;
+static struct pollfd fds;
+
 static char clk_dir_path[PATH_MAX];
 static int  bold[MAX_LINES];
 static char clock_lines[MAX_LINES][128];
@@ -58,11 +64,56 @@ int clock_init(void)
        if (locate_debugfs(clk_dir_path))
                return -1;
 
+       inotify_fd = inotify_init();
+       if ( inotify_fd < 0 ) {
+               fprintf(stderr, "inotify_init error.\n" );
+               return -1;
+       }
+
+       inotify_wd = inotify_add_watch(inotify_fd, clk_dir_path, 
+               IN_ALL_EVENTS );
+
+       fds.fd = inotify_fd;
+       fds.events = POLLIN;
+
        sprintf(clk_dir_path, "%s/clock", clk_dir_path);
 
        return access(clk_dir_path, F_OK);
 }
 
+#define EVENT_SIZE  ( sizeof (struct inotify_event) )
+#define BUF_LEN     ( 10 * ( EVENT_SIZE + 16 ) )
+
+static int debugfs_changed(void)
+{
+       int length, i = 0;
+       char buffer[BUF_LEN];
+
+       if (inotify_fd <= 0) {
+               return 1;
+       }
+
+       poll(&fds, 1, 1);
+       if (fds.revents != POLLIN) {
+               return 0;
+       }
+
+       length = read(inotify_fd, buffer, BUF_LEN);
+
+       if (length < 0)
+               return 0;
+
+       while (i < length) {
+               struct inotify_event *event = (struct inotify_event *) 
&buffer[i];
+               if (event->mask & IN_ALL_EVENTS)
+                       return 1;
+
+               i += EVENT_SIZE + event->len;
+       }
+
+       return 0;
+}
+
 static int file_read_from_format(char *file, int *value, const char *format)
 {
        FILE *f;
@@ -379,7 +430,8 @@ static int clk_number_recursive(char *clk_path)
 
 static int get_clk_number(char *clk_path)
 {
-       if ((max_clk_num != 0)) /* no nodes have been added */
+       /* no nodes have been added */
+       if ((max_clk_num != 0) && (!debugfs_changed()))
                return max_clk_num;
        else {
                max_clk_num = 0;
@@ -429,6 +481,8 @@ static void release_all_clk_info_mem(void)
 void clock_finalize(void)
 {
        release_all_clk_info_mem();
+       inotify_rm_watch(inotify_fd, inotify_wd);
+       close(inotify_fd);
 }
 
 
-- 
1.7.0.4


_______________________________________________
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev

Reply via email to