From 049ca9f0830304bfb0907071804c2bc40942c86e Mon Sep 17 00:00:00 2001 From: Peng Huang Date: Wed, 12 Aug 2009 07:43:59 +0800 Subject: [PATCH] Check if the daemon is alive in ibus_get_socket_address --- src/ibusshare.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ibusshare.c b/src/ibusshare.c index ca505f6..2fb7cf7 100644 --- a/src/ibusshare.c +++ b/src/ibusshare.c @@ -179,6 +179,7 @@ const gchar * ibus_get_address (void) { const gchar *address = NULL; + pid_t pid = -1; static gchar buffer[1024]; FILE *pf; @@ -189,25 +190,40 @@ ibus_get_address (void) /* read address from ~/.config/ibus/bus/soketfile */ pf = fopen (ibus_get_socket_path (), "r"); - if (pf == NULL) + if (pf == NULL) { return NULL; + } while (!feof (pf)) { gchar *p = buffer; if (fgets (buffer, sizeof (buffer), pf) == NULL) break; + + /* skip comment line */ if (p[0] == '#') continue; - if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) != 0) + /* parse IBUS_ADDRESS */ + if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) { + address = p + sizeof ("IBUS_ADDRESS=") - 1; + for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++); + if (*p == '\n') + *p = '\0'; continue; - address = p + sizeof ("IBUS_ADDRESS=") - 1; - for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++); - if (*p == '\n') - *p = '\0'; - break; - } + } + /* parse IBUS_DAEMON_PID */ + if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) { + pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1); + continue; + } + + } fclose (pf); + + if (pid == -1 || kill (pid, 0) != 0) { + return NULL; + } + return address; } -- 2.7.4