diff options
author | Kenny Root | 2010-09-14 16:26:12 -0500 |
---|---|---|
committer | Kenny Root | 2010-09-14 16:35:10 -0500 |
commit | 30abb7234de2a9caa1add4b00a189436f0b24560 (patch) | |
tree | 7920d9c39658ef8a7dc10e3ba825857597953912 | |
parent | 40c2b7cbedff612ce8bc7a51589a952cc1d047b6 (diff) | |
download | platform-system-core-30abb7234de2a9caa1add4b00a189436f0b24560.tar.gz platform-system-core-30abb7234de2a9caa1add4b00a189436f0b24560.tar.xz platform-system-core-30abb7234de2a9caa1add4b00a189436f0b24560.zip |
Fetch peer credentials for local sockets
Fetch the PID, UID, and GID of the remote side of a local socket
connection in case any users of this library class want to check it.
Change-Id: Ia3230e6bc68ab6f93160df9f5996d2bf744b872c
-rw-r--r-- | include/sysutils/SocketClient.h | 13 | ||||
-rw-r--r-- | libsysutils/src/SocketClient.cpp | 20 |
2 files changed, 31 insertions, 2 deletions
diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h index 469dd9d08..e7fb17756 100644 --- a/include/sysutils/SocketClient.h +++ b/include/sysutils/SocketClient.h | |||
@@ -4,16 +4,29 @@ | |||
4 | #include "../../../frameworks/base/include/utils/List.h" | 4 | #include "../../../frameworks/base/include/utils/List.h" |
5 | 5 | ||
6 | #include <pthread.h> | 6 | #include <pthread.h> |
7 | #include <sys/types.h> | ||
7 | 8 | ||
8 | class SocketClient { | 9 | class SocketClient { |
9 | int mSocket; | 10 | int mSocket; |
10 | pthread_mutex_t mWriteMutex; | 11 | pthread_mutex_t mWriteMutex; |
11 | 12 | ||
13 | /* Peer process ID */ | ||
14 | pid_t mPid; | ||
15 | |||
16 | /* Peer user ID */ | ||
17 | uid_t mUid; | ||
18 | |||
19 | /* Peer group ID */ | ||
20 | gid_t mGid; | ||
21 | |||
12 | public: | 22 | public: |
13 | SocketClient(int sock); | 23 | SocketClient(int sock); |
14 | virtual ~SocketClient() {} | 24 | virtual ~SocketClient() {} |
15 | 25 | ||
16 | int getSocket() { return mSocket; } | 26 | int getSocket() { return mSocket; } |
27 | pid_t getPid() const { return mPid; } | ||
28 | uid_t getUid() const { return mUid; } | ||
29 | gid_t getGid() const { return mGid; } | ||
17 | 30 | ||
18 | int sendMsg(int code, const char *msg, bool addErrno); | 31 | int sendMsg(int code, const char *msg, bool addErrno); |
19 | int sendMsg(const char *msg); | 32 | int sendMsg(const char *msg); |
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index e9ae23a90..8e5f1545a 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | #include <alloca.h> | 1 | #include <alloca.h> |
2 | #include <errno.h> | 2 | #include <errno.h> |
3 | #include <sys/socket.h> | ||
3 | #include <sys/types.h> | 4 | #include <sys/types.h> |
4 | #include <pthread.h> | 5 | #include <pthread.h> |
5 | #include <string.h> | 6 | #include <string.h> |
@@ -9,9 +10,24 @@ | |||
9 | 10 | ||
10 | #include <sysutils/SocketClient.h> | 11 | #include <sysutils/SocketClient.h> |
11 | 12 | ||
12 | SocketClient::SocketClient(int socket) { | 13 | SocketClient::SocketClient(int socket) |
13 | mSocket = socket; | 14 | : mSocket(socket) |
15 | , mPid(-1) | ||
16 | , mUid(-1) | ||
17 | , mGid(-1) | ||
18 | { | ||
14 | pthread_mutex_init(&mWriteMutex, NULL); | 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 | } | ||
15 | } | 31 | } |
16 | 32 | ||
17 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { | 33 | int SocketClient::sendMsg(int code, const char *msg, bool addErrno) { |