blob: a6aed265dacac1b53f193231bec9447839d66a77 [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#include <alloca.h>
2#include <errno.h>
Kenny Root30abb722010-09-14 14:26:12 -07003#include <sys/socket.h>
San Mehatfa644ff2009-05-08 11:15:53 -07004#include <sys/types.h>
5#include <pthread.h>
San Mehatd7680662009-05-12 11:16:59 -07006#include <string.h>
San Mehatfa644ff2009-05-08 11:15:53 -07007
8#define LOG_TAG "SocketClient"
9#include <cutils/log.h>
10
11#include <sysutils/SocketClient.h>
12
Kenny Root30abb722010-09-14 14:26:12 -070013SocketClient::SocketClient(int socket)
14 : mSocket(socket)
15 , mPid(-1)
16 , mUid(-1)
17 , mGid(-1)
18{
San Mehatfa644ff2009-05-08 11:15:53 -070019 pthread_mutex_init(&mWriteMutex, NULL);
Kenny Root30abb722010-09-14 14:26:12 -070020
21 struct ucred creds;
22 socklen_t szCreds = sizeof(creds);
23 memset(&creds, 0, szCreds);
24
25 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
26 if (err == 0) {
27 mPid = creds.pid;
28 mUid = creds.uid;
29 mGid = creds.gid;
30 }
San Mehatfa644ff2009-05-08 11:15:53 -070031}
32
San Mehatdb017542009-05-20 15:27:14 -070033int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
San Mehatd7680662009-05-12 11:16:59 -070034 char *buf;
David 'Digit' Turneraf615092011-01-17 02:34:15 +010035 const char* arg;
36 const char* fmt;
37 char tmp[1];
38 int len;
San Mehat03f0d272009-05-26 15:18:25 -070039
San Mehatd7680662009-05-12 11:16:59 -070040 if (addErrno) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010041 fmt = "%.3d %s (%s)";
42 arg = strerror(errno);
San Mehatd7680662009-05-12 11:16:59 -070043 } else {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010044 fmt = "%.3d %s";
45 arg = NULL;
San Mehatd7680662009-05-12 11:16:59 -070046 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +010047 /* Measure length of required buffer */
48 len = snprintf(tmp, sizeof tmp, fmt, code, msg, arg);
49 /* Allocate in the stack, then write to it */
50 buf = (char*)alloca(len+1);
51 snprintf(buf, len+1, fmt, code, msg, arg);
52 /* Send the zero-terminated message */
San Mehatd7680662009-05-12 11:16:59 -070053 return sendMsg(buf);
54}
55
San Mehatdb017542009-05-20 15:27:14 -070056int SocketClient::sendMsg(const char *msg) {
San Mehatfa644ff2009-05-08 11:15:53 -070057 if (mSocket < 0) {
58 errno = EHOSTUNREACH;
59 return -1;
60 }
61
San Mehatc73a3a52009-06-15 14:06:03 -070062 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070063 if (sendData(msg, strlen(msg) + 1) != 0) {
64 SLOGW("Unable to send msg '%s'", msg);
65 return -1;
66 }
67 return 0;
68}
69
70int SocketClient::sendData(const void* data, int len) {
San Mehatd7680662009-05-12 11:16:59 -070071 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070072 const char *p = (const char*) data;
73 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -070074
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -070075 if (len == 0) {
76 return 0;
77 }
78
San Mehatfa644ff2009-05-08 11:15:53 -070079 pthread_mutex_lock(&mWriteMutex);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070080 while (brtw > 0) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010081 rc = write(mSocket, p, brtw);
82 if (rc > 0) {
83 p += rc;
84 brtw -= rc;
85 continue;
86 }
87
88 if (rc < 0 && errno == EINTR)
89 continue;
90
91 pthread_mutex_unlock(&mWriteMutex);
92 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -070093 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -070094 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +010095 } else {
96 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -070097 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +010098 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -070099 }
100 pthread_mutex_unlock(&mWriteMutex);
101 return 0;
102}