OSDN Git Service

sens: simplify the server dispatch routine
authorOctavian Purdila <octavian.purdila@intel.com>
Mon, 24 Nov 2014 11:33:53 +0000 (13:33 +0200)
committerAdriana Reus <adriana.reus@intel.com>
Wed, 3 Dec 2014 13:56:06 +0000 (15:56 +0200)
Simplify the server dispatch routine so that it supports only one
command per connection. This also fixes the issue of not being able to
issue commands after poll.

Change-Id: I02932ca7c3aad0c6f46f80f547e8e515c085e763
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
sens.c

diff --git a/sens.c b/sens.c
index a24cc60..efb5811 100644 (file)
--- a/sens.c
+++ b/sens.c
@@ -297,6 +297,8 @@ static int dispatch_cmd(char *cmd, FILE *f)
        } else if (!strcmp(argv[0], "poll")) {
 
                pthread_mutex_lock(&client_mutex);
+               if (client)
+                       fclose(client);
                client = f;
                pthread_mutex_unlock(&client_mutex);
 
@@ -359,6 +361,7 @@ static int start_server(void)
                        .msg_controllen = sizeof(cmsg_buffer),
                };
                FILE *f =NULL;
+               struct cmsghdr *cmsg;
 
                conn = accept(sock, NULL, NULL);
                if (conn < 0) {
@@ -366,49 +369,47 @@ static int start_server(void)
                        continue;
                }
 
-               while (1) {
-                       struct cmsghdr *cmsg;
+               err = recvmsg(conn, &msg, 0);
+               if (err < 0) {
+                       ALOGE("error in recvmsg: %s", strerror(errno));
+                       close(conn);
+                       continue;
+               }
 
-                       err = recvmsg(conn, &msg, 0);
-                       if (err < 0) {
-                               ALOGE("error in recvmsg: %s", strerror(errno));
-                               break;
-                       }
+               if (err == 0)
+                       continue;
 
-                       if (err == 0)
+               for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+                    cmsg = CMSG_NXTHDR(&msg,cmsg)) {
+                       if (cmsg->cmsg_level == SOL_SOCKET
+                           && cmsg->cmsg_type == SCM_RIGHTS) {
+                               int *fd = (int *)CMSG_DATA(cmsg);
+                               f = fdopen(*fd, "w");
                                break;
-
-                       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
-                            cmsg = CMSG_NXTHDR(&msg,cmsg)) {
-                               if (cmsg->cmsg_level == SOL_SOCKET
-                                   && cmsg->cmsg_type == SCM_RIGHTS) {
-                                       int *fd = (int *)CMSG_DATA(cmsg);
-                                       f = fdopen(*fd, "w");
-                                       break;
-                               }
                        }
+               }
 
-                       if (data_buff[err - 1] != 0) {
-                               ALOGE("command is not NULL terminated\n");
-                               break;
-                       }
+               if (data_buff[err - 1] != 0) {
+                       ALOGE("command is not NULL terminated\n");
+                       close(conn);
+                       continue;
+               }
 
-                       err = dispatch_cmd(data_buff, f);
-                       if (err < 0) {
-                               ALOGE("error dispatching command: %d", err);
-                               break;
-                       }
+               err = dispatch_cmd(data_buff, f);
+               if (err < 0) {
+                       ALOGE("error dispatching command: %d", err);
+                       close(conn);
+                       continue;
+               }
 
-                       /* send ack */
-                       if (!err)
-                               write(conn, data_buff, 1);
+               /* send ack */
+               if (!err) {
+                       write(conn, data_buff, 1);
+                       fclose(f);
                }
 
-               pthread_mutex_lock(&client_mutex);
-               client = NULL;
-               pthread_mutex_unlock(&client_mutex);
 
-               fclose(f); close(conn);
+               close(conn);
        }
 }