소스 검색

Made x11idle more robust

* UTILITIES/x11idle.c (org-clock-idle-time): Added multiple checks to
  functions return values to prevent segfault. Also "fixed" return codes
  to fail unless the value could be printed, in which case the program
  succeeds.

TINYCHANGE
Nicolas Calderon Asselin 13 년 전
부모
커밋
49994a4c3e
1개의 변경된 파일15개의 추가작업 그리고 4개의 파일을 삭제
  1. 15 4
      UTILITIES/x11idle.c

+ 15 - 4
UTILITIES/x11idle.c

@@ -8,14 +8,25 @@
  * path
  */
 main() {
+    Status querry = 0;
     XScreenSaverInfo *info = XScreenSaverAllocInfo();
+    //open the display specified by the DISPLAY environment variable
     Display *display = XOpenDisplay(0);
 
-    //check that X11 is running or else you get a segafult/coredump
-    if (display != NULL) {
-	XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+    //display could be null if there is no X server running
+    if (info == NULL || display == NULL) {
+    return -1;
     }
-    XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+
+    //X11 is running, retrieve and print idle time
+	querry = XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+
+    if (querry == 0) {
+    return -1;
+    }
+
+    //idle time was retrieved successfully, print it
     printf("%u\n", info->idle);
     return 0;
 }
+