system/bt
Revision | 223015ecd785a86cc97af3bedbcb311b3ec5d9be (tree) |
---|---|
Zeit | 2017-04-05 19:55:42 |
Autor | Mingbo Zhang <mingboz@code...> |
Commiter | Gerrit - the friendly Code Review server |
uipc: retry to create srv chan if EADDRINUSE error
In very rare cases, socket cannot be closed in time.
Then we will fail to create the same socket immediately.
So need to retry after EADDRINUSE error happens
Change-Id: I1e54ded102d38cd42b3df40d405205a79b08928b
CRs-Fixed: 2029248
@@ -67,6 +67,9 @@ | ||
67 | 67 | |
68 | 68 | #define UIPC_FLUSH_BUFFER_SIZE 1024 |
69 | 69 | |
70 | +#define CHAN_CREATE_WAIT_TIME_MS 30 | |
71 | +#define CHAN_CREATE_RETRY_COUNT 10 | |
72 | + | |
70 | 73 | /***************************************************************************** |
71 | 74 | ** Local type definitions |
72 | 75 | ******************************************************************************/ |
@@ -143,6 +146,7 @@ const char* dump_uipc_event(tUIPC_EVENT event) | ||
143 | 146 | |
144 | 147 | static inline int create_server_socket(const char* name) |
145 | 148 | { |
149 | + int ret; | |
146 | 150 | int s = socket(AF_LOCAL, SOCK_STREAM, 0); |
147 | 151 | if (s < 0) |
148 | 152 | return -1; |
@@ -151,9 +155,10 @@ static inline int create_server_socket(const char* name) | ||
151 | 155 | |
152 | 156 | if(osi_socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0) |
153 | 157 | { |
158 | + ret = (errno == EADDRINUSE ? -EADDRINUSE : -1); | |
154 | 159 | BTIF_TRACE_EVENT("socket failed to create (%s)", strerror(errno)); |
155 | 160 | close(s); |
156 | - return -1; | |
161 | + return ret; | |
157 | 162 | } |
158 | 163 | |
159 | 164 | if(listen(s, 5) < 0) |
@@ -350,6 +355,7 @@ static inline void uipc_wakeup_locked(void) | ||
350 | 355 | static int uipc_setup_server_locked(tUIPC_CH_ID ch_id, char *name, tUIPC_RCV_CBACK *cback) |
351 | 356 | { |
352 | 357 | int fd; |
358 | + int i; | |
353 | 359 | |
354 | 360 | BTIF_TRACE_EVENT("SETUP CHANNEL SERVER %d", ch_id); |
355 | 361 |
@@ -358,7 +364,19 @@ static int uipc_setup_server_locked(tUIPC_CH_ID ch_id, char *name, tUIPC_RCV_CBA | ||
358 | 364 | |
359 | 365 | UIPC_LOCK(); |
360 | 366 | |
361 | - fd = create_server_socket(name); | |
367 | + for (i = 0; i < CHAN_CREATE_RETRY_COUNT; i++) | |
368 | + { | |
369 | + fd = create_server_socket(name); | |
370 | + if (fd == -EADDRINUSE) | |
371 | + { | |
372 | + BTIF_TRACE_ERROR("Address already in use, retry: %d", i); | |
373 | + usleep(CHAN_CREATE_WAIT_TIME_MS * 1000); | |
374 | + } | |
375 | + else | |
376 | + { | |
377 | + break; | |
378 | + } | |
379 | + } | |
362 | 380 | |
363 | 381 | if (fd < 0) |
364 | 382 | { |