mirror of
https://github.com/X11Libre/xserver.git
synced 2026-10-29 02:31:18 +00:00
os: Cleaned up the log functions and handled return values.
Some checks are pending
Build X servers / xserver-build-ubuntu (push) Waiting to run
Build X servers / drivers-build-ubuntu (push) Waiting to run
Build X servers / xserver-build-mingw32-ubuntu (push) Waiting to run
Build X servers / xserver-build-macos (push) Waiting to run
Build X servers / xserver-build-freebsd (push) Waiting to run
Build X servers / xserver-build-dragonflybsd (push) Waiting to run
Build X servers / xserver-build-netbsd (push) Waiting to run
Build X servers / Release pushed tag (push) Blocked by required conditions
check PRs for merge conflicts / notify (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_timeout (push) Waiting to run
delete old workflow runs / purge_deleted_branch_workflows (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (maint-25.0) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (master) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/AllocColor) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/ReplyNotSwappd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/XvPortNotifyPtr) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/always-pciaccess) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/build-rootless) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/char-subscr) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/clang-tidy) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-api-xfree86) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-api-xfree86-backup) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-grabs) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-os-export) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/client_array) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/conv-warnings) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dix-config) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dixconf-1) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dixgetproperty) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/fmt) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/generations) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/geode) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-build) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-macos-deps) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-releases) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/glx_deferred) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/grouping) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/includes) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/includes-2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/intel-2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/io) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/log_req_len) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/logging) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/logging_submitted_1) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/maint-xnest) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/marshal) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/matrix-test) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/meson) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/misc.h) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/mpbt) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/netbsd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/no-libsystemd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/optional-pci) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/os-unexport) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pciaccess) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pedantic) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pixmap-lifecycle) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/reallocarray) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/requests) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/screenproc) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/screenproc-wrap) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swap0) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swapping) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swapping_new) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/warn-conv) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/win32_2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xcb-marshal) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xf86_platform_bus_newabi) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xf86bigfont) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xi-properties) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-render) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-rootless) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-shm) (push) Waiting to run
Some checks are pending
Build X servers / xserver-build-ubuntu (push) Waiting to run
Build X servers / drivers-build-ubuntu (push) Waiting to run
Build X servers / xserver-build-mingw32-ubuntu (push) Waiting to run
Build X servers / xserver-build-macos (push) Waiting to run
Build X servers / xserver-build-freebsd (push) Waiting to run
Build X servers / xserver-build-dragonflybsd (push) Waiting to run
Build X servers / xserver-build-netbsd (push) Waiting to run
Build X servers / Release pushed tag (push) Blocked by required conditions
check PRs for merge conflicts / notify (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_timeout (push) Waiting to run
delete old workflow runs / purge_deleted_branch_workflows (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (maint-25.0) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (master) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/AllocColor) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/ReplyNotSwappd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/XvPortNotifyPtr) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/always-pciaccess) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/build-rootless) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/char-subscr) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/clang-tidy) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-api-xfree86) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-api-xfree86-backup) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-grabs) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/cleanup-os-export) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/client_array) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/conv-warnings) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dix-config) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dixconf-1) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/dixgetproperty) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/fmt) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/generations) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/geode) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-build) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-macos-deps) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/github-releases) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/glx_deferred) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/grouping) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/includes) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/includes-2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/intel-2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/io) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/log_req_len) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/logging) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/logging_submitted_1) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/maint-xnest) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/marshal) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/matrix-test) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/meson) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/misc.h) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/mpbt) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/netbsd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/no-libsystemd) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/optional-pci) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/os-unexport) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pciaccess) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pedantic) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/pixmap-lifecycle) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/reallocarray) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/requests) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/screenproc) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/screenproc-wrap) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swap0) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swapping) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/swapping_new) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/warn-conv) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/win32_2) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xcb-marshal) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xf86_platform_bus_newabi) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xf86bigfont) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xi-properties) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-render) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-rootless) (push) Waiting to run
delete old workflow runs / delete_old_workflow_runs_branches (wip/xnest-shm) (push) Waiting to run
The old function used int for write() return values, that could overflow. And the return values of the write calls were discarded and not checked. Now if the write() to the log file fails, a warning will be logged to stderr and the log message will be appended after the warning. If writes to stderr fail, a warning will be logged to stdout and the log message will be appended after the warning. If writes to stdout fail, we can't really do anything else and will continue on silently. Also used the length returned by strftime for the next write call to eliminate an extra use of strlen. The -1 was also removed from the buflen argument of strftime because it was unnecessary. Did some small formatting changes too. Signed-off-by: b-aaz <b-aazbsd@proton.me>
This commit is contained in:
parent
d45aeab1b6
commit
a5f87f20b2
1 changed files with 87 additions and 16 deletions
103
os/log.c
103
os/log.c
|
|
@ -83,6 +83,7 @@ OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h> /* for calloc() */
|
||||
#include <string.h> /* for strerror*() */
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <X11/Xfuncproto.h>
|
||||
|
|
@ -227,6 +228,73 @@ static void initSyslog(void) {
|
|||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
LogFailedWriteStdout(const void * buf, size_t len)
|
||||
{
|
||||
if (write(STDOUT_FILENO, buf, len)==-1)
|
||||
{
|
||||
/* We can't write to the logfile, stderr, and stdout; something
|
||||
* bad is probably happening, but we can't really do anything */
|
||||
return;
|
||||
}
|
||||
}
|
||||
static void
|
||||
LogFailedWrite(const void * buf, size_t len)
|
||||
{
|
||||
if (write(STDERR_FILENO, buf, len)==-1)
|
||||
{
|
||||
/* We can't even write to stderr, let's try stdout as a last resort. */
|
||||
{
|
||||
char error[]="Can't write to stderr: ";
|
||||
LogFailedWriteStdout(error,sizeof(error));
|
||||
}
|
||||
#ifndef __MINGW32__
|
||||
char dsc[256]={0};
|
||||
(void) !strerror_r(errno,dsc,sizeof(dsc));
|
||||
#else
|
||||
char * dsc;
|
||||
dsc=strerror(errno);
|
||||
#endif
|
||||
LogFailedWriteStdout(dsc,strlen(dsc));
|
||||
LogFailedWriteStdout("\n",1);
|
||||
{
|
||||
char error[]="Intended to write the following to stderr:\n";
|
||||
LogFailedWriteStdout(error,sizeof(error));
|
||||
}
|
||||
LogFailedWriteStdout(buf,len);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
LogWrite(int fd, const void * buf, size_t len)
|
||||
{
|
||||
if (write(fd, buf, len)==-1)
|
||||
{
|
||||
/* If the write() call fails, we can not log this event to the log file,
|
||||
* but we still have the stderr.
|
||||
*/
|
||||
{
|
||||
char error[]="Can't write to log file: ";
|
||||
LogFailedWrite(error,sizeof(error));
|
||||
}
|
||||
#ifndef __MINGW32__
|
||||
char dsc[256]={0};
|
||||
(void) !strerror_r(errno,dsc,sizeof(dsc));
|
||||
#else
|
||||
char * dsc;
|
||||
dsc=strerror(errno);
|
||||
#endif
|
||||
LogFailedWrite(dsc,strlen(dsc));
|
||||
LogFailedWrite("\n",1);
|
||||
{
|
||||
char error[]="Intended to write the following to log file:\n";
|
||||
LogFailedWrite(error,sizeof(error));
|
||||
}
|
||||
LogFailedWrite(buf,len);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* LogInit is called to start logging to a file. It is also called (with
|
||||
* NULL arguments) when logging to a file is not wanted. It must always be
|
||||
|
|
@ -268,7 +336,7 @@ LogInit(const char *fname, const char *backup)
|
|||
|
||||
/* Flush saved log information. */
|
||||
if (saveBuffer && bufferSize > 0) {
|
||||
(void)!write(logFileFd, saveBuffer, bufferPos);
|
||||
LogWrite(logFileFd, saveBuffer, bufferPos);
|
||||
doLogSync();
|
||||
}
|
||||
}
|
||||
|
|
@ -581,52 +649,55 @@ static void
|
|||
LogSWrite(int verb, const char *buf, size_t len, Bool end_line)
|
||||
{
|
||||
static Bool newline = TRUE;
|
||||
int ret;
|
||||
|
||||
LogSyslogWrite(verb, buf, len, end_line);
|
||||
|
||||
if (verb < 0 || xorgLogVerbosity >= verb)
|
||||
ret = write(2, buf, len);
|
||||
if (verb < 0 || xorgLogVerbosity >= verb) {
|
||||
LogWrite(2, buf, len);
|
||||
}
|
||||
|
||||
if (verb < 0 || xorgLogFileVerbosity >= verb) {
|
||||
if (inSignalContext && logFileFd >= 0) {
|
||||
ret = write(logFileFd, buf, len);
|
||||
if (xorgLogSync)
|
||||
LogWrite(logFileFd, buf, len);
|
||||
if (xorgLogSync){
|
||||
doLogSync();
|
||||
}
|
||||
}
|
||||
else if (!inSignalContext && logFileFd != -1) {
|
||||
if (newline) {
|
||||
time_t t = time(NULL);
|
||||
struct tm tm;
|
||||
char fmt_tm[32];
|
||||
size_t fmt_len;
|
||||
|
||||
localtime_r(&t, &tm);
|
||||
strftime(fmt_tm, sizeof(fmt_tm) - 1, "[%Y-%m-%d %H:%M:%S] ", &tm);
|
||||
(void)!write(logFileFd, fmt_tm, strlen(fmt_tm));
|
||||
fmt_len = strftime(
|
||||
fmt_tm,
|
||||
sizeof(fmt_tm),
|
||||
"[%Y-%m-%d %H:%M:%S] ",
|
||||
&tm);
|
||||
LogWrite(logFileFd, fmt_tm, fmt_len);
|
||||
}
|
||||
newline = end_line;
|
||||
(void)!write(logFileFd, buf, len);
|
||||
if (xorgLogSync)
|
||||
LogWrite(logFileFd, buf, len);
|
||||
if (xorgLogSync) {
|
||||
doLogSync();
|
||||
}
|
||||
}
|
||||
else if (!inSignalContext && needBuffer) {
|
||||
if (len > bufferUnused) {
|
||||
bufferSize += 1024;
|
||||
bufferUnused += 1024;
|
||||
saveBuffer = realloc(saveBuffer, bufferSize);
|
||||
if (!saveBuffer)
|
||||
if (!saveBuffer) {
|
||||
FatalError("realloc() failed while saving log messages\n");
|
||||
}
|
||||
}
|
||||
bufferUnused -= len;
|
||||
memcpy(saveBuffer + bufferPos, buf, len);
|
||||
bufferPos += len;
|
||||
}
|
||||
}
|
||||
|
||||
/* There's no place to log an error message if the log write
|
||||
* fails...
|
||||
*/
|
||||
(void) ret;
|
||||
}
|
||||
|
||||
/* Returns the Message Type string to prepend to a logging message, or NULL
|
||||
|
|
|
|||
Loading…
Reference in a new issue