ap_log_perror(aplog_mark, aplog_warning, 0, p,
apr_psprintf(p, "pid file %s overwritten -- unclean "
"shutdown of previous apache run?",
fname));
}
if ((rv = apr_file_open(&pid_file, fname,
apr_write | apr_create | apr_truncate,
apr_uread | apr_uwrite | apr_gread | apr_wread, p))
!= apr_success) {
ap_log_error(aplog_mark, aplog_err, rv, null,
"could not create %s", fname);
ap_log_error(aplog_mark, aplog_err, 0, null,
"%s: could not log pid to file %s",
ap_server_argv0, fname);
exit(1);
}
apr_file_printf(pid_file, "%ld" apr_eol_str, (long)mypid);
apr_file_close(pid_file);
saved_pid = mypid;
}
ap_declare(apr_status_t) ap_read_pid(apr_pool_t *p, const char *filename,
pid_t *mypid)
{
const apr_size_t 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_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;
}
/* 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;
}
/* 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;
}
ap_declare(void) ap_log_assert(const char *szexp, const char *szfile,
int nline)
{