} else if (!strcmp(argv[0], "poll")) {
pthread_mutex_lock(&client_mutex);
+ if (client)
+ fclose(client);
client = f;
pthread_mutex_unlock(&client_mutex);
.msg_controllen = sizeof(cmsg_buffer),
};
FILE *f =NULL;
+ struct cmsghdr *cmsg;
conn = accept(sock, NULL, NULL);
if (conn < 0) {
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);
}
}