On 11/06/2012 09:45 PM, Sumit Bose wrote:
Hi,

+
+errno_t sss_br_lock_file(int fd, size_t start, size_t len,
+                         int retries, useconds_t wait)
+{
+    int ret;
+    struct flock lock;
+    int retries_left;
+
+    lock.l_type = F_WRLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = start;
+    lock.l_len = len;
+    lock.l_pid = 0;
+
+    for (retries_left = retries; retries_left > 0; retries_left--) {
+        ret = fcntl(fd, F_SETLK, &lock);
+        if (ret == -1) {
+            ret = errno;
+            if (ret == EACCES || ret == EAGAIN || ret == EINTR) {
+                DEBUG(SSSDBG_TRACE_FUNC,
+                      ("Failed to lock file. Retries left: %d\n",
+                       retries_left - 1));
+
+                if ((ret == EACCES || ret == EAGAIN) && (retries_left <= 1)) {
+                    /* File is locked by someone else. Return EACCESS
+                     * if this is the last try. */
+                    return EACCES;
+                }
+
+                if (retries_left - 1 > 0) {
+                    ret = usleep(wait);
+                    if (ret == -1) {
+                        DEBUG(SSSDBG_MINOR_FAILURE,
+                              ("usleep() failed -> ignoring\n"));
+                    }
+                }
+            } else {
+                /* Error occurred */
+                DEBUG(SSSDBG_CRIT_FAILURE,
+                      ("Unable to lock file.\n"));
+                return ret;
+            }
+        } else if (ret == 0) {
+            /* File successfuly locked */
+            break;
+        }
+    }
+    if (retries_left == 0) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to lock file.\n"));
+        return ret;

if retries is 0, ret is uninitialized. Can you fix this? All credits
goes to gcc, I've just seen the compiler warning.

bye,
Sumit

+    }
+
+    return EOK;
+}
_______________________________________________
sssd-devel mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/sssd-devel


Good catch. Having a value of 0 or <= 0 makes no sense here. I'll send a patch that returns EINVAL in such situation in a new thread.

Thanks
Michal
_______________________________________________
sssd-devel mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/sssd-devel

Reply via email to