summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2020-06-08 02:57:33 +0200
committerGravatar Martin Szulecki2020-06-08 02:57:33 +0200
commitea94e4c05360872cb9050b3f39299778d8621c20 (patch)
tree2499fa72e296db0ccc84aed15c52db7353a42b00 /src
parent6bbe87b6f6c5ef388839ebf64a7d38fcd91468bf (diff)
downloadusbmuxd-ea94e4c05360872cb9050b3f39299778d8621c20.tar.gz
usbmuxd-ea94e4c05360872cb9050b3f39299778d8621c20.tar.bz2
Log client process name alongside pid if possible on accept and disconnect
This helps identifing a problematic client software much quicker.
Diffstat (limited to 'src')
-rw-r--r--src/client.c49
1 files changed, 44 insertions, 5 deletions
diff --git a/src/client.c b/src/client.c
index 85d0c8b..1588c38 100644
--- a/src/client.c
+++ b/src/client.c
@@ -78,6 +78,27 @@ static struct collection client_list;
78pthread_mutex_t client_list_mutex; 78pthread_mutex_t client_list_mutex;
79static uint32_t client_number = 0; 79static uint32_t client_number = 0;
80 80
81#ifdef SO_PEERCRED
82static char* _get_process_name_by_pid(const int pid)
83{
84 char* name = (char*)calloc(1024, sizeof(char));
85 if(name) {
86 sprintf(name, "/proc/%d/cmdline", pid);
87 FILE* f = fopen(name, "r");
88 if(f) {
89 size_t size;
90 size = fread(name, sizeof(char), 1024, f);
91 if(size > 0) {
92 if('\n' == name[size-1])
93 name[size-1]='\0';
94 }
95 fclose(f);
96 }
97 }
98 return name;
99}
100#endif
101
81/** 102/**
82 * Receive raw data from the client socket. 103 * Receive raw data from the client socket.
83 * 104 *
@@ -212,23 +233,41 @@ int client_accept(int listenfd)
212 if (log_level >= LL_INFO) { 233 if (log_level >= LL_INFO) {
213 struct ucred cr; 234 struct ucred cr;
214 len = sizeof(struct ucred); 235 len = sizeof(struct ucred);
215 getsockopt(cfd, SOL_SOCKET, SO_PEERCRED, &cr, &len); 236 getsockopt(client->fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
216 237
217 if (getpid() == cr.pid) { 238 if (getpid() == cr.pid) {
218 usbmuxd_log(LL_INFO, "New client on fd %d (self)", client->fd); 239 usbmuxd_log(LL_INFO, "Client %d accepted: %s[%d]", client->fd, PACKAGE_NAME, cr.pid);
219 } else { 240 } else {
220 usbmuxd_log(LL_INFO, "New client on fd %d (pid %d)", client->fd, cr.pid); 241 char* process_name = _get_process_name_by_pid(cr.pid);
242 usbmuxd_log(LL_INFO, "Client %d accepted: %s[%d]", client->fd, process_name, cr.pid);
243 free(process_name);
221 } 244 }
222 } 245 }
223#else 246#else
224 usbmuxd_log(LL_INFO, "New client on fd %d", client->fd); 247 usbmuxd_log(LL_INFO, "Client %d accepted", client->fd);
225#endif 248#endif
226 return client->fd; 249 return client->fd;
227} 250}
228 251
229void client_close(struct mux_client *client) 252void client_close(struct mux_client *client)
230{ 253{
231 usbmuxd_log(LL_INFO, "Disconnecting client fd %d", client->fd); 254#ifdef SO_PEERCRED
255 if (log_level >= LL_INFO) {
256 struct ucred cr;
257 socklen_t len = sizeof(struct ucred);
258 getsockopt(client->fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
259
260 if (getpid() == cr.pid) {
261 usbmuxd_log(LL_INFO, "Client %d is going to be disconnected: %s[%d]", client->fd, PACKAGE_NAME, cr.pid);
262 } else {
263 char* process_name = _get_process_name_by_pid(cr.pid);
264 usbmuxd_log(LL_INFO, "Client %d is going to be disconnected: %s[%d]", client->fd, process_name, cr.pid);
265 free(process_name);
266 }
267 }
268#else
269 usbmuxd_log(LL_INFO, "Client %d is going to be disconnected", client->fd);
270#endif
232 if(client->state == CLIENT_CONNECTING1 || client->state == CLIENT_CONNECTING2) { 271 if(client->state == CLIENT_CONNECTING1 || client->state == CLIENT_CONNECTING2) {
233 usbmuxd_log(LL_INFO, "Client died mid-connect, aborting device %d connection", client->connect_device); 272 usbmuxd_log(LL_INFO, "Client died mid-connect, aborting device %d connection", client->connect_device);
234 client->state = CLIENT_DEAD; 273 client->state = CLIENT_DEAD;