]> granicus.if.org Git - apache/commitdiff
add ap_log_pid() for reading an Apache pid file
authorJeff Trawick <trawick@apache.org>
Thu, 23 May 2002 12:19:09 +0000 (12:19 +0000)
committerJeff Trawick <trawick@apache.org>
Thu, 23 May 2002 12:19:09 +0000 (12:19 +0000)
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
server/log.c

index 8eb1d96a5eb8bf138f25a07f88f712f3ff4d20ab..e8dd1375e90b196fa17147a8b520f6228176e5b3 100644 (file)
@@ -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;
 
 /**
index 34aa7a328d1347338a0fba6cf10fdc933b3c20c1..33d427cbc39941d1d1c290d1ac3b1700efd1ed4b 100644 (file)
@@ -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)
 {