From 5b3e441d20dca3bb56433ef43b27e2617e2030a5 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 9 Jun 2010 09:11:48 -0400
Subject: [PATCH] Fix misuse of errno in find_uid.c

---
 src/util/find_uid.c |   43 ++++++++++++++++++++++++++-----------------
 1 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/src/util/find_uid.c b/src/util/find_uid.c
index 952aeea4abd932790e07f36ecd30a0b8a83e45b7..8fab5dc8c4e57583a322ae10ff3d01bfce0dc56a 100644
--- a/src/util/find_uid.c
+++ b/src/util/find_uid.c
@@ -64,6 +64,7 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
     char *e;
     char *endptr;
     long num=0;
+    errno_t error;
 
     ret = snprintf(path, PATHLEN, "/proc/%d/status", pid);
     if (ret < 0) {
@@ -76,13 +77,14 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
 
     ret = lstat(path, &stat_buf);
     if (ret == -1) {
-        if (errno == ENOENT) {
+        error = errno;
+        if (error == ENOENT) {
             DEBUG(7, ("Proc file [%s] is not available anymore, continuing.\n",
                       path));
             return EOK;
         }
-        DEBUG(1, ("lstat failed [%d][%s].\n", errno, strerror(errno)));
-        return errno;
+        DEBUG(1, ("lstat failed [%d][%s].\n", error, strerror(error)));
+        return error;
     }
 
     if (!S_ISREG(stat_buf.st_mode)) {
@@ -92,28 +94,31 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
 
     fd = open(path, O_RDONLY);
     if (fd == -1) {
-        if (errno == ENOENT) {
+        error = errno;
+        if (error == ENOENT) {
             DEBUG(7, ("Proc file [%s] is not available anymore, continuing.\n",
                       path));
             return EOK;
         }
-        DEBUG(1, ("open failed [%d][%s].\n", errno, strerror(errno)));
-        return errno;
+        DEBUG(1, ("open failed [%d][%s].\n", error, strerror(error)));
+        return error;
     }
 
     while ((ret = read(fd, buf, BUFSIZE)) != 0) {
         if (ret == -1) {
-            if (errno == EINTR || errno == EAGAIN) {
+            error = errno;
+            if (error == EINTR || error == EAGAIN) {
                 continue;
             }
-            DEBUG(1, ("read failed [%d][%s].\n", errno, strerror(errno)));
-            return errno;
+            DEBUG(1, ("read failed [%d][%s].\n", error, strerror(error)));
+            return error;
         }
     }
 
     ret = close(fd);
     if (ret == -1) {
-        DEBUG(1, ("close failed [%d][%s].\n", errno, strerror(errno)));
+        error = errno;
+        DEBUG(1, ("close failed [%d][%s].\n", error, strerror(error)));
     }
 
     p = strstr(buf, "\nUid:\t");
@@ -126,10 +131,12 @@ static errno_t get_uid_from_pid(const pid_t pid, uid_t *uid)
         } else {
             *e = '\0';
         }
+        errno = 0;
         num = strtol(p, &endptr, 10);
-        if(errno == ERANGE) {
-            DEBUG(1, ("strtol failed [%s].\n", strerror(errno)));
-            return errno;
+        error = errno;
+        if(error == ERANGE) {
+            DEBUG(1, ("strtol failed [%s].\n", strerror(error)));
+            return error;
         }
         if (*endptr != '\0') {
             DEBUG(1, ("uid contains extra characters\n"));
@@ -155,12 +162,14 @@ static errno_t name_to_pid(const char *name, pid_t *pid)
 {
     long num;
     char *endptr;
+    errno_t error;
 
     errno = 0;
     num = strtol(name, &endptr, 10);
-    if(errno == ERANGE) {
+    error = errno;
+    if(error == ERANGE) {
         perror("strtol");
-        return errno;
+        return error;
     }
 
     if (*endptr != '\0') {
@@ -197,8 +206,8 @@ static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid)
 
     proc_dir = opendir("/proc");
     if (proc_dir == NULL) {
+        ret = errno;
         DEBUG(1, ("Cannot open proc dir.\n"));
-        ret = errno;
         goto done;
     };
 
@@ -240,8 +249,8 @@ static errno_t get_active_uid_linux(hash_table_t *table, uid_t search_uid)
         errno = 0;
     }
     if (errno != 0 && dirent == NULL) {
+        ret = errno;
         DEBUG(1 ,("readdir failed.\n"));
-        ret = errno;
         goto done;
     }
 
-- 
1.7.0.1

