From 6fba81114cd47135d6796f172616fa7b0e5662cb Mon Sep 17 00:00:00 2001 From: Jeff Trawick Date: Thu, 23 May 2002 12:19:09 +0000 Subject: [PATCH] add ap_log_pid() for reading an Apache pid file Submitted by: Justin Erenkrantz git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@95235 13f79535-47bb-0310-9956-ffa450edef68 --- include/http_log.h | 8 ++++++++ server/log.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/http_log.h b/include/http_log.h index 8eb1d96a5e..e8dd1375e9 100644 --- a/include/http_log.h +++ b/include/http_log.h @@ -240,6 +240,14 @@ AP_DECLARE(void) ap_error_log2stderr(server_rec *s); */ AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *fname); +/** + * Retrieve the pid from a pidfile. + * @param p The pool to use for logging + * @param filename The name of the file containing the pid + * @param mypid Pointer to pid_t (valid only if return APR_SUCCESS) + */ +AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename, pid_t *mypid); + typedef struct piped_log piped_log; /** diff --git a/server/log.c b/server/log.c index 34aa7a328d..33d427cbc3 100644 --- a/server/log.c +++ b/server/log.c @@ -626,6 +626,55 @@ AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename) saved_pid = mypid; } +AP_DECLARE(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename, + pid_t *mypid) +{ + const int BUFFER_SIZE = sizeof(long) * 3 + 2; /* see apr_ltoa */ + apr_file_t *pid_file = NULL; + apr_status_t rv; + const char *fname; + char *buf, *endptr; + apr_size_t bytes_wanted, bytes_read; + + if (!filename) { + return APR_EGENERAL; + } + + fname = ap_server_root_relative(p, filename); + if (!fname) { + ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, APR_EBADPATH, + NULL, "Invalid PID file path %s, ignoring.", filename); + return APR_EGENERAL; + } + + rv = apr_file_open(&pid_file, fname, APR_READ, APR_OS_DEFAULT, p); + if (rv != APR_SUCCESS) { + 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; + } + 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') { + return APR_EGENERAL; + } + + apr_file_close(pid_file); + return APR_SUCCESS; +} + AP_DECLARE(void) ap_log_assert(const char *szExp, const char *szFile, int nLine) { -- 2.50.1