| [2603] | 1 | From 7eefa90ac1422825db6f1bbbe4e66f1336fca531 Mon Sep 17 00:00:00 2001 | 
|---|
|  | 2 | From: Alexander Chernyakhovsky <achernya@mit.edu> | 
|---|
|  | 3 | Date: Thu, 28 Aug 2014 22:51:21 -0400 | 
|---|
|  | 4 | Subject: [PATCH] Redirect stderr to systemd-journald | 
|---|
|  | 5 |  | 
|---|
|  | 6 | Scripts provides the "logview" facility for users to be able to see | 
|---|
|  | 7 | the error logs from their applications. However, this facility | 
|---|
|  | 8 | requires running the moral equivalent of grep $USER error_log. Not all | 
|---|
|  | 9 | error messages contain the username, and therefore, the logview | 
|---|
|  | 10 | facility is unreliable at best. | 
|---|
|  | 11 |  | 
|---|
|  | 12 | Additionally, the error_log contains an interleaving of all errors, | 
|---|
|  | 13 | which makes it difficult for system administrators to help withs | 
|---|
|  | 14 | upport requests in which an Internal Server Error has been | 
|---|
|  | 15 | experienced. | 
|---|
|  | 16 |  | 
|---|
|  | 17 | Since systemd-journald supports per-user journals, replace stderr, | 
|---|
|  | 18 | which is provided by Apache, with a file descriptor pointing to the | 
|---|
|  | 19 | journal. Assuming that journald is configured to split the log on | 
|---|
|  | 20 | UIDs, this will allow journalctl --user to show each individual user | 
|---|
|  | 21 | their error logs. | 
|---|
|  | 22 | --- | 
|---|
|  | 23 | support/Makefile.in |  7 ++++++- | 
|---|
|  | 24 | support/suexec.c    | 21 +++++++++++++++++++++ | 
|---|
|  | 25 | 2 files changed, 27 insertions(+), 1 deletion(-) | 
|---|
|  | 26 |  | 
|---|
|  | 27 | diff --git a/support/Makefile.in b/support/Makefile.in | 
|---|
|  | 28 | index 745d86c..4014c1f 100644 | 
|---|
|  | 29 | --- a/support/Makefile.in | 
|---|
|  | 30 | +++ b/support/Makefile.in | 
|---|
|  | 31 | @@ -73,8 +73,13 @@ checkgid: $(checkgid_OBJECTS) | 
|---|
|  | 32 | $(LINK) $(checkgid_LTFLAGS) $(checkgid_OBJECTS) $(PROGRAM_LDADD) | 
|---|
|  | 33 |  | 
|---|
|  | 34 | suexec_OBJECTS = suexec.lo | 
|---|
|  | 35 | +suexec_LDADD = "-lsystemd-journal -lsystemd-id128" | 
|---|
|  | 36 | +suexec.lo: suexec.c | 
|---|
|  | 37 | +       $(LIBTOOL) --mode=compile $(CC) $(ab_CFLAGS) $(ALL_CFLAGS) $(ALL_CPPFLAGS) \ | 
|---|
|  | 38 | +           $(ALL_INCLUDES) $(PICFLAGS) $(LTCFLAGS) -DSCRIPTS_HAVE_SYSTEMD_JOURNAL \ | 
|---|
|  | 39 | +           -c $< && touch $@ | 
|---|
|  | 40 | suexec: $(suexec_OBJECTS) | 
|---|
|  | 41 | -       $(LINK) $(suexec_OBJECTS) | 
|---|
|  | 42 | +       $(LINK) $(suexec_OBJECTS) $(suexec_LDADD) | 
|---|
|  | 43 |  | 
|---|
|  | 44 | htcacheclean_OBJECTS = htcacheclean.lo | 
|---|
|  | 45 | htcacheclean: $(htcacheclean_OBJECTS) | 
|---|
|  | 46 | diff --git a/support/suexec.c b/support/suexec.c | 
|---|
|  | 47 | index 3a4d802..4fe4c44 100644 | 
|---|
|  | 48 | --- a/support/suexec.c | 
|---|
|  | 49 | +++ b/support/suexec.c | 
|---|
|  | 50 | @@ -61,6 +61,11 @@ | 
|---|
|  | 51 | #include <grp.h> | 
|---|
|  | 52 | #endif | 
|---|
|  | 53 |  | 
|---|
|  | 54 | +#ifdef SCRIPTS_HAVE_SYSTEMD_JOURNAL | 
|---|
|  | 55 | +#include <systemd/sd-journal.h> | 
|---|
|  | 56 | +#include <systemd/sd-daemon.h> | 
|---|
|  | 57 | +#endif | 
|---|
|  | 58 | + | 
|---|
|  | 59 | #ifdef AP_LOG_SYSLOG | 
|---|
|  | 60 | #include <syslog.h> | 
|---|
|  | 61 | #endif | 
|---|
|  | 62 | @@ -769,6 +774,22 @@ TRUSTED_DIRECTORY: | 
|---|
|  | 63 | umask(AP_SUEXEC_UMASK); | 
|---|
|  | 64 | #endif /* AP_SUEXEC_UMASK */ | 
|---|
|  | 65 |  | 
|---|
|  | 66 | +#ifdef SCRIPTS_HAVE_SYSTEMD_JOURNAL | 
|---|
|  | 67 | +    int fd = sd_journal_stream_fd("CGI Script", LOG_NOTICE, 0); | 
|---|
|  | 68 | +    if (fd < 0) { | 
|---|
|  | 69 | +       log_err("unable to open systemd-journald file descriptor\n"); | 
|---|
|  | 70 | +       exit(254); | 
|---|
|  | 71 | +    } | 
|---|
|  | 72 | +    if (dup2(fd, STDERR_FILENO) < 0) { | 
|---|
|  | 73 | +       log_err("unable to make journald file descriptor available as stderr\n"); | 
|---|
|  | 74 | +       exit(253); | 
|---|
|  | 75 | +    } | 
|---|
|  | 76 | +    if (close(fd) < 0) { | 
|---|
|  | 77 | +       log_err("unable to close journald file descriptor copy\n"); | 
|---|
|  | 78 | +       exit(252); | 
|---|
|  | 79 | +    } | 
|---|
|  | 80 | +#endif /* SCRIPTS_HAVE_SYSTEMD_JOURNAL */ | 
|---|
|  | 81 | + | 
|---|
|  | 82 | /* Be sure to close the log file so the CGI can't mess with it. */ | 
|---|
|  | 83 | #ifdef AP_LOG_SYSLOG | 
|---|
|  | 84 | if (log_open) { | 
|---|
|  | 85 | -- | 
|---|
|  | 86 | 1.8.5.2 (Apple Git-48) | 
|---|
|  | 87 |  | 
|---|