Skip to contentSkip to conversation

package/apache: atomic creation of pid file.

PATCH Superseded Nicolas Carrier 2 participants 2 messages Sep 20, 2019 – Sep 21, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 package/apache: atomic creation of pid file.

The original pattern for creating the pid file was:
open_create(pid_file)
write(pid_file, pid)
close(pid_file)

But if a power outage occurs between open_create and write, the file will be empty and httpd will refuse to start afterwards unless the corrupt pid file is removed.

This patch uses the pattern:
open_create(temp_pid_file)
write(temp_pid_file)
close(temp_pid_file)
rename(temp_pid_file, pid_file)
which is guaranteed to be atomic, provided that temp_pid_file and pid_file are located in the same file system, which this patch does by creating a temporary file name with the pattern:
pid_file_name + random_suffix

This patch has been submitted upstream, but did receive no answer at all:
https://bz.apache.org/bugzilla/show_bug.cgi?id=63140

---
 package/apache/0003-corrupt-pid-file.patch | 51 ++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 package/apache/0003-corrupt-pid-file.patch
package/apache/0003-corrupt-pid-file.patch new file +51 -0
new file mode 100644
index 0000000000..8208bc1ce5
@@ -0,0 +1,51 @@
+diff -ur httpd-2.4.35.ref/server/log.c httpd-2.4.35/server/log.c
+--- httpd-2.4.35.ref/server/log.c	2018-06-25 15:24:16.000000000 +0200
++++ httpd-2.4.35/server/log.c	2019-02-04 10:11:22.313157132 +0100
+@@ -1598,6 +1598,8 @@
+     pid_t mypid;
+     apr_status_t rv;
+     const char *fname;
++    char *temp_fname;
++    apr_fileperms_t perms;
+ 
+     if (!filename) {
+         return;
+@@ -1609,6 +1611,10 @@
+                      ap_server_conf, APLOGNO(00097) "Invalid PID file path %s, ignoring.", filename);
+         return;
+     }
++    temp_fname = apr_psprintf(p, "%s.XXXXXX", filename);
++    if (!temp_fname) {
++        return;
++    }
+ 
+     mypid = getpid();
+     if (mypid != saved_pid
+@@ -1626,19 +1632,23 @@
+                       fname);
+     }
+ 
+-    if ((rv = apr_file_open(&pid_file, fname,
+-                            APR_WRITE | APR_CREATE | APR_TRUNCATE,
+-                            APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD, p))
++    if ((rv = apr_file_mktemp(&pid_file, temp_fname,
++                              APR_WRITE | APR_CREATE | APR_TRUNCATE, p))
+         != APR_SUCCESS) {
+         ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00099)
+-                     "could not create %s", fname);
++                     "could not create %s", temp_fname);
+         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(00100)
+                      "%s: could not log pid to file %s",
+                      ap_server_argv0, fname);
+         exit(1);
+     }
++
++    perms = APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD;
++    apr_file_perms_set(temp_fname, perms);
++
+     apr_file_printf(pid_file, "%" APR_PID_T_FMT APR_EOL_STR, mypid);
+     apr_file_close(pid_file);
++    apr_file_rename(temp_fname, fname, p);
+     saved_pid = mypid;
+ }
+ 
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 package/apache: atomic creation of pid file.

Quoted 19 lines · On Fri, 20 Sep 2019 15:04:36 +0000 Nicolas Carrier…

The original pattern for creating the pid file was:
open_create(pid_file)
write(pid_file, pid)
close(pid_file)

But if a power outage occurs between open_create and write, the file will be empty and httpd will refuse to start afterwards unless the corrupt pid file is removed.

This patch uses the pattern:
open_create(temp_pid_file)
write(temp_pid_file)
close(temp_pid_file)
rename(temp_pid_file, pid_file)
which is guaranteed to be atomic, provided that temp_pid_file and pid_file are located in the same file system, which this patch does by creating a temporary file name with the pattern:
pid_file_name + random_suffix

Quoted 4 lines

This patch has been submitted upstream, but did receive no answer at all:
https://bz.apache.org/bugzilla/show_bug.cgi?id=63140

We're not really fond of having patches like this that haven't been reviewed/blessed by upstream. But I see you submitted since months ago and got no feedback.

package/apache/0003-corrupt-pid-file.patch

Quoted 11 lines
---
 package/apache/0003-corrupt-pid-file.patch | 51 ++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 package/apache/0003-corrupt-pid-file.patch
package/apache/0003-corrupt-pid-file.patch new file +0 -0
new file mode 100644
index 0000000000..8208bc1ce5
@@ -0,0 +1,51 @@

In any case, we need a description + Signed-off-by line in the patch itself, and since Apache is available in a Git repo
(https://github.com/apache/httpd), we want the patch to be generated by git format-patch.

Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com