| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 1 | #include <alloca.h> | 
|  | 2 | #include <errno.h> | 
| Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 3 | #include <sys/socket.h> | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 4 | #include <sys/types.h> | 
|  | 5 | #include <pthread.h> | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 6 | #include <string.h> | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 7 |  | 
|  | 8 | #define LOG_TAG "SocketClient" | 
|  | 9 | #include <cutils/log.h> | 
|  | 10 |  | 
|  | 11 | #include <sysutils/SocketClient.h> | 
|  | 12 |  | 
| Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 13 | SocketClient::SocketClient(int socket) | 
|  | 14 | : mSocket(socket) | 
|  | 15 | , mPid(-1) | 
|  | 16 | , mUid(-1) | 
|  | 17 | , mGid(-1) | 
| Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 18 | , mRefCount(1) | 
| Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 19 | { | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 20 | pthread_mutex_init(&mWriteMutex, NULL); | 
| Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 21 | pthread_mutex_init(&mRefCountMutex, NULL); | 
| Kenny Root | 30abb72 | 2010-09-14 14:26:12 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | struct ucred creds; | 
|  | 24 | socklen_t szCreds = sizeof(creds); | 
|  | 25 | memset(&creds, 0, szCreds); | 
|  | 26 |  | 
|  | 27 | int err = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds); | 
|  | 28 | if (err == 0) { | 
|  | 29 | mPid = creds.pid; | 
|  | 30 | mUid = creds.uid; | 
|  | 31 | mGid = creds.gid; | 
|  | 32 | } | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 33 | } | 
|  | 34 |  | 
| San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 35 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 36 | char *buf; | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 37 | const char* arg; | 
|  | 38 | const char* fmt; | 
|  | 39 | char tmp[1]; | 
|  | 40 | int  len; | 
| San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame] | 41 |  | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 42 | if (addErrno) { | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 43 | fmt = "%.3d %s (%s)"; | 
|  | 44 | arg = strerror(errno); | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 45 | } else { | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 46 | fmt = "%.3d %s"; | 
|  | 47 | arg = NULL; | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 48 | } | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 49 | /* Measure length of required buffer */ | 
|  | 50 | len = snprintf(tmp, sizeof tmp, fmt, code, msg, arg); | 
|  | 51 | /* Allocate in the stack, then write to it */ | 
|  | 52 | buf = (char*)alloca(len+1); | 
|  | 53 | snprintf(buf, len+1, fmt, code, msg, arg); | 
|  | 54 | /* Send the zero-terminated message */ | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 55 | return sendMsg(buf); | 
|  | 56 | } | 
|  | 57 |  | 
| San Mehat | db01754 | 2009-05-20 15:27:14 -0700 | [diff] [blame] | 58 | int SocketClient::sendMsg(const char *msg) { | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 59 | if (mSocket < 0) { | 
|  | 60 | errno = EHOSTUNREACH; | 
|  | 61 | return -1; | 
|  | 62 | } | 
|  | 63 |  | 
| San Mehat | c73a3a5 | 2009-06-15 14:06:03 -0700 | [diff] [blame] | 64 | // Send the message including null character | 
| Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 65 | if (sendData(msg, strlen(msg) + 1) != 0) { | 
|  | 66 | SLOGW("Unable to send msg '%s'", msg); | 
|  | 67 | return -1; | 
|  | 68 | } | 
|  | 69 | return 0; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | int SocketClient::sendData(const void* data, int len) { | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 73 | int rc = 0; | 
| Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 74 | const char *p = (const char*) data; | 
|  | 75 | int brtw = len; | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 76 |  | 
| Brad Fitzpatrick | 16ae478 | 2010-11-02 10:55:52 -0700 | [diff] [blame] | 77 | if (len == 0) { | 
|  | 78 | return 0; | 
|  | 79 | } | 
|  | 80 |  | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 81 | pthread_mutex_lock(&mWriteMutex); | 
| Brad Fitzpatrick | 8c5669f | 2010-10-27 10:23:16 -0700 | [diff] [blame] | 82 | while (brtw > 0) { | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 83 | rc = write(mSocket, p, brtw); | 
|  | 84 | if (rc > 0) { | 
|  | 85 | p += rc; | 
|  | 86 | brtw -= rc; | 
|  | 87 | continue; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | if (rc < 0 && errno == EINTR) | 
|  | 91 | continue; | 
|  | 92 |  | 
|  | 93 | pthread_mutex_unlock(&mWriteMutex); | 
|  | 94 | if (rc == 0) { | 
| San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 95 | SLOGW("0 length write :("); | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 96 | errno = EIO; | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 97 | } else { | 
|  | 98 | SLOGW("write error (%s)", strerror(errno)); | 
| San Mehat | d768066 | 2009-05-12 11:16:59 -0700 | [diff] [blame] | 99 | } | 
| David 'Digit' Turner | af61509 | 2011-01-17 02:34:15 +0100 | [diff] [blame] | 100 | return -1; | 
| San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 101 | } | 
|  | 102 | pthread_mutex_unlock(&mWriteMutex); | 
|  | 103 | return 0; | 
|  | 104 | } | 
| Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | void SocketClient::incRef() { | 
| Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 107 | pthread_mutex_lock(&mRefCountMutex); | 
|  | 108 | mRefCount++; | 
|  | 109 | pthread_mutex_unlock(&mRefCountMutex); | 
| Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Brad Fitzpatrick | 4be4e69 | 2011-03-17 17:14:46 -0700 | [diff] [blame] | 112 | bool SocketClient::decRef() { | 
|  | 113 | bool deleteSelf = false; | 
|  | 114 | pthread_mutex_lock(&mRefCountMutex); | 
|  | 115 | mRefCount--; | 
|  | 116 | if (mRefCount == 0) { | 
|  | 117 | deleteSelf = true; | 
|  | 118 | } else if (mRefCount < 0) { | 
|  | 119 | SLOGE("SocketClient refcount went negative!"); | 
|  | 120 | } | 
|  | 121 | pthread_mutex_unlock(&mRefCountMutex); | 
|  | 122 | if (deleteSelf) { | 
|  | 123 | delete this; | 
|  | 124 | } | 
|  | 125 | return deleteSelf; | 
| Brad Fitzpatrick | 648ebad | 2011-03-17 15:41:20 -0700 | [diff] [blame] | 126 | } |