#include <dlfcn.h>
#include <pthread.h>
#include <errno.h>
+#include <signal.h>
#include <hardware/sensors.h>
#include <utils/Log.h>
}
}
-static void *run_sensors_thread(void *arg)
+static void sig_pipe(int sig)
{
+ client = NULL;
+}
+
+static void *run_sensors_thread(void *arg __attribute((unused)))
+{
+
+ signal(SIGPIPE, sig_pipe);
+
switch (dev->version) {
case SENSORS_DEVICE_API_VERSION_0_1:
default:
#define CLIENT_ERR(f, fmt...) \
{ if (f) { fprintf(f, fmt); fprintf(f, "\n"); } ALOGE(fmt); }
-static int dispatch_cmd(char *cmd, int cmd_len, FILE *f)
+static int dispatch_cmd(char *cmd, FILE *f)
{
char *argv[16], *tmp;
int argc = 0, handle;
} 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;
- }
}
+ }
- err = dispatch_cmd(data_buff, err, f);
- if (err < 0) {
- ALOGE("error dispatching command: %d", err);
- break;
- }
+ if (data_buff[err - 1] != 0) {
+ ALOGE("command is not NULL terminated\n");
+ close(conn);
+ continue;
+ }
- /* send ack */
- if (!err)
- write(conn, data_buff, 1);
+ err = dispatch_cmd(data_buff, f);
+ if (err < 0) {
+ ALOGE("error dispatching command: %d", err);
+ close(conn);
+ continue;
}
- pthread_mutex_lock(&client_mutex);
- client = NULL;
- pthread_mutex_unlock(&client_mutex);
+ /* send ack */
+ if (!err) {
+ write(conn, data_buff, 1);
+ fclose(f);
+ }
- fclose(f); close(conn);
+ close(conn);
}
}