eal: fix race condition in IPC request
[dpdk.git] / lib / librte_eal / common / eal_common_proc.c
index caa8774..da7930f 100644 (file)
@@ -444,13 +444,13 @@ send_msg(const char *dst_path, struct rte_mp_msg *msg, int type)
        if (snd < 0) {
                rte_errno = errno;
                /* Check if it caused by peer process exits */
-               if (errno == -ECONNREFUSED) {
+               if (errno == ECONNREFUSED) {
                        /* We don't unlink the primary's socket here */
                        if (rte_eal_process_type() == RTE_PROC_PRIMARY)
                                unlink_socket_by_path(dst_path);
                        return 0;
                }
-               if (errno == -ENOBUFS) {
+               if (errno == ENOBUFS) {
                        RTE_LOG(ERR, EAL, "Peer cannot receive message %s\n",
                                dst_path);
                        return 0;
@@ -489,10 +489,14 @@ mp_send(struct rte_mp_msg *msg, const char *peer, int type)
                return -1;
        }
        while ((ent = readdir(mp_dir))) {
+               char path[PATH_MAX];
+
                if (fnmatch(mp_filter, ent->d_name, 0) != 0)
                        continue;
 
-               if (send_msg(ent->d_name, msg, type) < 0)
+               snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
+                        ent->d_name);
+               if (send_msg(path, msg, type) < 0)
                        ret = -1;
        }
 
@@ -557,10 +561,10 @@ mp_request_one(const char *dst, struct rte_mp_msg *req,
        exist = find_sync_request(dst, req->name);
        if (!exist)
                TAILQ_INSERT_TAIL(&sync_requests.requests, &sync_req, next);
-       pthread_mutex_unlock(&sync_requests.lock);
        if (exist) {
                RTE_LOG(ERR, EAL, "A pending request %s:%s\n", dst, req->name);
                rte_errno = EEXIST;
+               pthread_mutex_unlock(&sync_requests.lock);
                return -1;
        }
 
@@ -574,7 +578,6 @@ mp_request_one(const char *dst, struct rte_mp_msg *req,
 
        reply->nb_sent++;
 
-       pthread_mutex_lock(&sync_requests.lock);
        do {
                pthread_cond_timedwait(&sync_req.cond, &sync_requests.lock, ts);
                /* Check spurious wakeups */
@@ -583,7 +586,7 @@ mp_request_one(const char *dst, struct rte_mp_msg *req,
                /* Check if time is out */
                if (gettimeofday(&now, NULL) < 0)
                        break;
-               if (now.tv_sec < ts->tv_sec)
+               if (ts->tv_sec < now.tv_sec)
                        break;
                else if (now.tv_sec == ts->tv_sec &&
                         now.tv_usec * 1000 < ts->tv_nsec)
@@ -654,10 +657,15 @@ rte_mp_request(struct rte_mp_msg *req, struct rte_mp_reply *reply,
        }
 
        while ((ent = readdir(mp_dir))) {
+               char path[PATH_MAX];
+
                if (fnmatch(mp_filter, ent->d_name, 0) != 0)
                        continue;
 
-               if (mp_request_one(ent->d_name, req, reply, &end))
+               snprintf(path, sizeof(path), "%s/%s", mp_dir_path,
+                        ent->d_name);
+
+               if (mp_request_one(path, req, reply, &end))
                        ret = -1;
        }