0004-server-log.c-ap_log_pid-Use-a-temporary-file-then-re.patch 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. From 5b95d256387b45fbe33f7ee7890ae35afdd5c371 Mon Sep 17 00:00:00 2001
  2. From: Joe Orton <jorton@apache.org>
  3. Date: Fri, 13 Mar 2020 14:34:18 +0000
  4. Subject: [PATCH] * server/log.c (ap_log_pid): Use a temporary file, then
  5. rename once successfully written; also add error checking. Avoids startup
  6. failures if a previous httpd invocation crashed while writing the pidfile.
  7. Submitted by: Nicolas Carrier <carrier.nicolas0 gmail.com>, jorton
  8. Github: closes #100, closes #69
  9. PR: 63140
  10. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1875153 13f79535-47bb-0310-9956-ffa450edef68
  11. Signed-off-by: Nicolas Carrier <nicolas.carrier@orolia.com>
  12. ---
  13. server/log.c | 33 ++++++++++++++++++++++++++-------
  14. 1 file changed, 26 insertions(+), 7 deletions(-)
  15. diff --git a/server/log.c b/server/log.c
  16. index f0bde6e4b8..8d54b4e057 100644
  17. --- a/server/log.c
  18. +++ b/server/log.c
  19. @@ -1598,6 +1598,9 @@ AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
  20. pid_t mypid;
  21. apr_status_t rv;
  22. const char *fname;
  23. + char *temp_fname;
  24. + apr_fileperms_t perms;
  25. + char pidstr[64];
  26. if (!filename) {
  27. return;
  28. @@ -1626,19 +1629,35 @@ AP_DECLARE(void) ap_log_pid(apr_pool_t *p, const char *filename)
  29. fname);
  30. }
  31. - if ((rv = apr_file_open(&pid_file, fname,
  32. - APR_WRITE | APR_CREATE | APR_TRUNCATE,
  33. - APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
  34. - != APR_SUCCESS) {
  35. + temp_fname = apr_pstrcat(p, fname, ".XXXXXX", NULL);
  36. + rv = apr_file_mktemp(&pid_file, temp_fname,
  37. + APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE, p);
  38. + if (rv != APR_SUCCESS) {
  39. ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00099)
  40. - "could not create %s", fname);
  41. + "could not create %s", temp_fname);
  42. ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00100)
  43. "%s: could not log pid to file %s",
  44. ap_server_argv0, fname);
  45. exit(1);
  46. }
  47. - apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
  48. - apr_file_close(pid_file);
  49. +
  50. + apr_snprintf(pidstr, sizeof pidstr, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
  51. +
  52. + perms = APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD;
  53. + rv = apr_file_perms_set(temp_fname, perms);
  54. + if (rv == APR_SUCCESS)
  55. + rv = apr_file_write_full(pid_file, pidstr, strlen(pidstr), NULL);
  56. + if (rv == APR_SUCCESS)
  57. + rv = apr_file_close(pid_file);
  58. + if (rv == APR_SUCCESS)
  59. + rv = apr_file_rename(temp_fname, fname, p);
  60. + if (rv != APR_SUCCESS) {
  61. + ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(10231)
  62. + "%s: Failed creating pid file %s",
  63. + ap_server_argv0, temp_fname);
  64. + exit(1);
  65. + }
  66. +
  67. saved_pid = mypid;
  68. }
  69. --
  70. 2.25.2