• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <alloca.h>
2 #include <errno.h>
3 #include <sys/socket.h>
4 #include <sys/types.h>
5 #include <pthread.h>
6 #include <string.h>
7 
8 #define LOG_TAG "SocketClient"
9 #include <cutils/log.h>
10 
11 #include <sysutils/SocketClient.h>
12 
SocketClient(int socket)13 SocketClient::SocketClient(int socket)
14         : mSocket(socket)
15         , mPid(-1)
16         , mUid(-1)
17         , mGid(-1)
18 {
19     pthread_mutex_init(&mWriteMutex, NULL);
20 
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     }
31 }
32 
sendMsg(int code,const char * msg,bool addErrno)33 int SocketClient::sendMsg(int code, const char *msg, bool addErrno) {
34     char *buf;
35 
36     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 
sendMsg(const char * msg)46 int SocketClient::sendMsg(const char *msg) {
47     if (mSocket < 0) {
48         errno = EHOSTUNREACH;
49         return -1;
50     }
51 
52     // Send the message including null character
53     int rc = 0;
54     const char *p = msg;
55     int brtw = strlen(msg) + 1;
56 
57     pthread_mutex_lock(&mWriteMutex);
58     while(brtw) {
59         if ((rc = write(mSocket,p, brtw)) < 0) {
60             SLOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
61             pthread_mutex_unlock(&mWriteMutex);
62             return -1;
63         } else if (!rc) {
64             SLOGW("0 length write :(");
65             errno = EIO;
66             pthread_mutex_unlock(&mWriteMutex);
67             return -1;
68         }
69         p += rc;
70         brtw -= rc;
71     }
72     pthread_mutex_unlock(&mWriteMutex);
73     return 0;
74 }
75