]> granicus.if.org Git - apache/commitdiff
Simplify ap_read_pid and make it more portable.
authorJustin Erenkrantz <jerenkrantz@apache.org>
Wed, 29 May 2002 04:39:07 +0000 (04:39 +0000)
committerJustin Erenkrantz <jerenkrantz@apache.org>
Wed, 29 May 2002 04:39:07 +0000 (04:39 +0000)
- Switch to using apr_file_read_full()
- Stop checking for \n (non-portable)
- Error if we read the entire buffer or the first digit isn't a number

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@95338 13f79535-47bb-0310-9956-ffa450edef68

server/log.c

index 33d427cbc39941d1d1c290d1ac3b1700efd1ed4b..fe24b11aa6356c10d0ffd3ebc78b5087ecd35645 100644 (file)
@@ -634,7 +634,7 @@ AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
     apr_status_t rv;
     const char *fname;
     char *buf, *endptr;
-    apr_size_t bytes_wanted, bytes_read;
+    apr_size_t bytes_read;
 
     if (!filename) {
         return APR_EGENERAL;
@@ -652,25 +652,23 @@ AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
         return rv;
     }
 
-    bytes_wanted = BUFFER_SIZE;
-    endptr = buf = apr_palloc(p, BUFFER_SIZE);
-    do {
-        bytes_read = bytes_wanted;
-        rv = apr_file_read(pid_file, endptr, &bytes_read);
-        if (rv != APR_SUCCESS && rv != APR_EOF) {
-            return rv;
-        }
-        bytes_wanted -= bytes_read;
-        endptr += bytes_read; 
+    /* Ensure null-termination, so that strtol doesn't go crazy. */
+    buf = apr_palloc(p, BUFFER_SIZE);
+    buf[BUFFER_SIZE - 1] = '\0';
+
+    rv = apr_file_read_full(pid_file, buf, BUFFER_SIZE - 1, &bytes_read);
+    if (rv != APR_SUCCESS && rv != APR_EOF) {
+        return rv;
     }
-    while (bytes_wanted > 0 && rv != APR_EOF);
 
-    *mypid = strtol(buf, &endptr, 10);
-    /* We only know for sure that the beginning part is the pid. */
-    if (*buf == '\0' || *endptr != '\n') {
+    /* If we fill the buffer, we're probably reading a corrupt pid file.
+     * To be nice, let's also ensure the first char is a digit. */
+    if (bytes_read == BUFFER_SIZE - 1 || !apr_isdigit(*buf)) {
         return APR_EGENERAL;
     }
 
+    *mypid = strtol(buf, &endptr, 10);
+
     apr_file_close(pid_file);
     return APR_SUCCESS;
 }