From: Justin Erenkrantz Date: Wed, 29 May 2002 04:39:07 +0000 (+0000) Subject: Simplify ap_read_pid and make it more portable. X-Git-Tag: 2.0.37~216 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=37b3fdc9d109e4e8ecf27638427e7f1489c6a801;p=apache Simplify ap_read_pid and make it more portable. - 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 --- diff --git a/server/log.c b/server/log.c index 33d427cbc3..fe24b11aa6 100644 --- a/server/log.c +++ b/server/log.c @@ -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; }