aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/wpa_ctrl.c
diff options
context:
space:
mode:
authorJouni Malinen <j@w1.fi>2009-01-31 20:22:09 (GMT)
committerJouni Malinen <j@w1.fi>2009-01-31 20:22:09 (GMT)
commit6e488ff03c5af513f7768364f3cdd890ef98b0bd (patch)
tree78cda16ff6a007add7390948176e87a71598cd10 /src/common/wpa_ctrl.c
parent1c5aeef0c28c113a3643f6a50d0508f6f65c431a (diff)
downloadhostap-6e488ff03c5af513f7768364f3cdd890ef98b0bd.zip
hostap-6e488ff03c5af513f7768364f3cdd890ef98b0bd.tar.gz
hostap-6e488ff03c5af513f7768364f3cdd890ef98b0bd.tar.bz2
Remove orphaned wpa_cli control socket on EADDRINUSE
If the bind() on /tmp/wpa_ctrl_<pid>_<in-proc-counter> fails with EADDRINUSE, there is an existing socket file with the name we are trying to create. Since getpid() is unique, there cannot be another process using that socket and we can just unlink the file and try again. This can speed up client connection if wpa_cli is killed without allowing it to clean up the socket file. [Bug 288]
Diffstat (limited to 'src/common/wpa_ctrl.c')
-rw-r--r--src/common/wpa_ctrl.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/common/wpa_ctrl.c b/src/common/wpa_ctrl.c
index e411602..2b4e3aa 100644
--- a/src/common/wpa_ctrl.c
+++ b/src/common/wpa_ctrl.c
@@ -64,6 +64,7 @@ struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
static int counter = 0;
int ret;
size_t res;
+ int tries = 0;
ctrl = os_malloc(sizeof(*ctrl));
if (ctrl == NULL)
@@ -77,15 +78,28 @@ struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
}
ctrl->local.sun_family = AF_UNIX;
+ counter++;
+try_again:
ret = os_snprintf(ctrl->local.sun_path, sizeof(ctrl->local.sun_path),
- "/tmp/wpa_ctrl_%d-%d", getpid(), counter++);
+ "/tmp/wpa_ctrl_%d-%d", getpid(), counter);
if (ret < 0 || (size_t) ret >= sizeof(ctrl->local.sun_path)) {
close(ctrl->s);
os_free(ctrl);
return NULL;
}
+ tries++;
if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
sizeof(ctrl->local)) < 0) {
+ if (errno == EADDRINUSE && tries < 2) {
+ /*
+ * getpid() returns unique identifier for this instance
+ * of wpa_ctrl, so the existing socket file must have
+ * been left by unclean termination of an earlier run.
+ * Remove the file and try again.
+ */
+ unlink(ctrl->local.sun_path);
+ goto try_again;
+ }
close(ctrl->s);
os_free(ctrl);
return NULL;