os: don't ever try to mess with stderr / fd 2

1. We've got some ancient code here that's trying to open stderr (fd 2) itself,
   in case it cannot write there for strange reason. POSIX defines the three
   standard streams (and associated fd's) to be available on program startup
   (when main() is reached). One needs a sledgehammer for breaking a system
   so much that this doesn't work anymore - even calling a program directly
   from /etc/inittab does provide them.

2. The current implementation is not POSIX conformant - it should use freopen(),
   and it leaks FILE structure.

3. stderr is set to buffered mode, quite the opposite of POSIX - it states
   stderr shall NOT be buffered. Simple and obvious reason: not risking vital
   error information getting lost.

4. Placing The logfile in /usr/adm - an ancient, pre-FHS, directory that rarely
   exists on modern systems. That's even hardcoded, instead of derived from
   build-time given installation pathes.

Conculusio: obsolete and broken, thus removing it.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-05-14 15:16:55 +02:00
parent 6a01f488c8
commit ed289f734b

View File

@ -73,10 +73,6 @@ SOFTWARE.
#include <sys/resource.h> #include <sys/resource.h>
#endif #endif
#ifndef ADMPATH
#define ADMPATH "/usr/adm/X%smsgs"
#endif
/* The actual user defined max number of clients */ /* The actual user defined max number of clients */
int LimitClients = LIMITCLIENTS; int LimitClients = LIMITCLIENTS;
@ -152,10 +148,6 @@ void
OsInit(void) OsInit(void)
{ {
static Bool been_here = FALSE; static Bool been_here = FALSE;
#ifndef XQUARTZ
static const char *devnull = "/dev/null";
char fname[PATH_MAX];
#endif
if (!been_here) { if (!been_here) {
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
@ -216,42 +208,6 @@ OsInit(void)
} }
#endif #endif
#if !defined(XQUARTZ) /* STDIN is already /dev/null and STDOUT/STDERR is managed by console_redirect.c */
/*
* If a write of zero bytes to stderr returns non-zero, i.e. -1,
* then writing to stderr failed, and we'll write somewhere else
* instead. (Apparently this never happens in the Real World.)
*/
if (write(2, fname, 0) == -1) {
FILE *err;
if (strlen(display) + strlen(ADMPATH) + 1 < sizeof fname)
snprintf(fname, sizeof(fname), ADMPATH, display);
else
strcpy(fname, devnull);
/*
* uses stdio to avoid os dependencies here,
* a real os would use
* open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
*/
if (!(err = fopen(fname, "a+")))
err = fopen(devnull, "w");
if (err && (fileno(err) != 2)) {
dup2(fileno(err), 2);
fclose(err);
}
#if defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
{
static char buf[BUFSIZ];
setvbuf(stderr, buf, _IOLBF, BUFSIZ);
}
#else
setlinebuf(stderr);
#endif
}
#endif /* !XQUARTZ */
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
if (getpgrp() == 0) if (getpgrp() == 0)
setpgid(0, 0); setpgid(0, 0);