blob: c9c7417f1472aadda607dc534d7cd8f0f76ab05a [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;
San Mehat03f0d272009-05-26 15:18:25 -070035
San Mehatd7680662009-05-12 11:16:59 -070036 if (addErrno) {
37 buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8);
38 sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno));
39 } else {
40 buf = (char *) alloca(strlen(msg) + strlen("XXX "));
41 sprintf(buf, "%.3d %s", code, msg);
42 }
43 return sendMsg(buf);
44}
45
San Mehatdb017542009-05-20 15:27:14 -070046int SocketClient::sendMsg(const char *msg) {
San Mehatfa644ff2009-05-08 11:15:53 -070047 if (mSocket < 0) {
48 errno = EHOSTUNREACH;
49 return -1;
50 }
51
San Mehatc73a3a52009-06-15 14:06:03 -070052 // Send the message including null character
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070053 if (sendData(msg, strlen(msg) + 1) != 0) {
54 SLOGW("Unable to send msg '%s'", msg);
55 return -1;
56 }
57 return 0;
58}
59
60int SocketClient::sendData(const void* data, int len) {
San Mehatd7680662009-05-12 11:16:59 -070061 int rc = 0;
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070062 const char *p = (const char*) data;
63 int brtw = len;
San Mehatd7680662009-05-12 11:16:59 -070064
Brad Fitzpatrick16ae4782010-11-02 10:55:52 -070065 if (len == 0) {
66 return 0;
67 }
68
San Mehatfa644ff2009-05-08 11:15:53 -070069 pthread_mutex_lock(&mWriteMutex);
Brad Fitzpatrick8c5669f2010-10-27 10:23:16 -070070 while (brtw > 0) {
71 if ((rc = write(mSocket, p, brtw)) < 0) {
72 SLOGW("write error (%s)", strerror(errno));
San Mehatd7680662009-05-12 11:16:59 -070073 pthread_mutex_unlock(&mWriteMutex);
74 return -1;
75 } else if (!rc) {
San Mehat7e8529a2010-03-25 09:31:42 -070076 SLOGW("0 length write :(");
San Mehatd7680662009-05-12 11:16:59 -070077 errno = EIO;
78 pthread_mutex_unlock(&mWriteMutex);
79 return -1;
80 }
81 p += rc;
82 brtw -= rc;
San Mehatfa644ff2009-05-08 11:15:53 -070083 }
84 pthread_mutex_unlock(&mWriteMutex);
85 return 0;
86}