blob: 722dcb29150a79fe80cbe5fc9b05ae69c6eb879d [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
Xianzhu Wang45202462011-09-29 12:59:55 +080013SocketClient::SocketClient(int socket, bool owned)
Kenny Root30abb722010-09-14 14:26:12 -070014 : mSocket(socket)
Xianzhu Wang45202462011-09-29 12:59:55 +080015 , mSocketOwned(owned)
Kenny Root30abb722010-09-14 14:26:12 -070016 , mPid(-1)
17 , mUid(-1)
18 , mGid(-1)
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070019 , mRefCount(1)
Kenny Root30abb722010-09-14 14:26:12 -070020{
San Mehatfa644ff2009-05-08 11:15:53 -070021 pthread_mutex_init(&mWriteMutex, NULL);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -070022 pthread_mutex_init(&mRefCountMutex, NULL);
Kenny Root30abb722010-09-14 14:26:12 -070023
24 struct ucred creds;
25 socklen_t szCreds = sizeof(creds);
26 memset(&creds, 0, szCreds);
27
28 int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
29 if (err == 0) {
30 mPid = creds.pid;
31 mUid = creds.uid;
32 mGid = creds.gid;
33 }
San Mehatfa644ff2009-05-08 11:15:53 -070034}
35
Xianzhu Wang45202462011-09-29 12:59:55 +080036SocketClient::~SocketClient()
37{
38 if (mSocketOwned) {
39 close(mSocket);
40 }
41}
42
San Mehatdb017542009-05-20 15:27:14 -070043int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
San Mehatd7680662009-05-12 11:16:59 -070044 char *buf;
David 'Digit' Turneraf615092011-01-17 02:34:15 +010045 const char* arg;
46 const char* fmt;
47 char tmp[1];
48 int len;
San Mehat03f0d272009-05-26 15:18:25 -070049
San Mehatd7680662009-05-12 11:16:59 -070050 if (addErrno) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010051 fmt = "%.3d %s (%s)";
52 arg = strerror(errno);
San Mehatd7680662009-05-12 11:16:59 -070053 } else {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010054 fmt = "%.3d %s";
55 arg = NULL;
San Mehatd7680662009-05-12 11:16:59 -070056 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +010057 /* Measure length of required buffer */
58 len = snprintf(tmp, sizeof tmp, fmt, code, msg, arg);
59 /* Allocate in the stack, then write to it */
60 buf = (char*)alloca(len+1);
61 snprintf(buf, len+1, fmt, code, msg, arg);
62 /* Send the zero-terminated message */
San Mehatd7680662009-05-12 11:16:59 -070063 return sendMsg(buf);
64}
65
San Mehatdb017542009-05-20 15:27:14 -070066int SocketClient::sendMsg(const char *msg) {
San Mehatfa644ff2009-05-08 11:15:53 -070067 if (mSocket < 0) {
68 errno = EHOSTUNREACH;
69 return -1;
70 }
71
San Mehatc73a3a52009-06-15 14:06:03 -070072 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070073 if (sendData(msg, strlen(msg) + 1) != 0) {
74 SLOGW("Unable to send msg '%s'", msg);
75 return -1;
76 }
77 return 0;
78}
79
80int SocketClient::sendData(const void* data, int len) {
San Mehatd7680662009-05-12 11:16:59 -070081 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070082 const char *p = (const char*) data;
83 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -070084
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -070085 if (len == 0) {
86 return 0;
87 }
88
San Mehatfa644ff2009-05-08 11:15:53 -070089 pthread_mutex_lock(&mWriteMutex);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070090 while (brtw > 0) {
David 'Digit' Turneraf615092011-01-17 02:34:15 +010091 rc = write(mSocket, p, brtw);
92 if (rc > 0) {
93 p += rc;
94 brtw -= rc;
95 continue;
96 }
97
98 if (rc < 0 && errno == EINTR)
99 continue;
100
101 pthread_mutex_unlock(&mWriteMutex);
102 if (rc == 0) {
San Mehat7e8529a2010-03-25 09:31:42 -0700103 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -0700104 errno = EIO;
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100105 } else {
106 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -0700107 }
David 'Digit' Turneraf615092011-01-17 02:34:15 +0100108 return -1;
San Mehatfa644ff2009-05-08 11:15:53 -0700109 }
110 pthread_mutex_unlock(&mWriteMutex);
111 return 0;
112}
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700113
114void SocketClient::incRef() {
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700115 pthread_mutex_lock(&mRefCountMutex);
116 mRefCount++;
117 pthread_mutex_unlock(&mRefCountMutex);
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700118}
119
Brad Fitzpatrick4be4e692011-03-17 17:14:46 -0700120bool SocketClient::decRef() {
121 bool deleteSelf = false;
122 pthread_mutex_lock(&mRefCountMutex);
123 mRefCount--;
124 if (mRefCount == 0) {
125 deleteSelf = true;
126 } else if (mRefCount < 0) {
127 SLOGE("SocketClient refcount went negative!");
128 }
129 pthread_mutex_unlock(&mRefCountMutex);
130 if (deleteSelf) {
131 delete this;
132 }
133 return deleteSelf;
Brad Fitzpatrick648ebad2011-03-17 15:41:20 -0700134}