Re: Fighting for the power. [syslogd]

From: Chuck Swiger <cswiger_at_mac.com>
Date: Mon, 11 May 2009 14:37:26 -0700
On May 11, 2009, at 1:13 PM, Sam Leffler wrote:
>> You can use -l flag to syslogd to create additional logging sockets  
>> under your chroot'ed filesystem tree, similar to the way named &  
>> ntpd uses it; see syslogd_precmd() in /etc/rc.d/syslogd....
>>
>
> Blech, thanks.  That might be a stopgap solution but it still  
> doesn't allow syslogd to be started earlier which is what is needed  
> for many/most setups.

Something that's imperfect but available today is much more usable  
than the perfect solution tomorrow.  :-)

> syslogd has mountcritremote as REQUIRE to handle diskless setups but  
> IMO this should be (at least) configurable.  But rather than argue  
> this nonsense I think the better solution is to do what I suggest so  
> syslogd can be started very early and capture everything available  
> (e.g. your suggestion still loses msgs logged while setting up nfs  
> mounts).

True.  I'm not sure I'd really want to set up a machine such that  
critical filesystems depend upon wireless networking to be up before  
they can be mounted, but if you've really got something which needs to  
run before syslogd is allowed to run, then that something needs to be  
able to do it's own logging.  Fortunately, it's not terribly hard to  
write some wrappers which will log either via syslog or to a file (or  
both, if needed):

/* initialize logging to a file, via syslog, or both... */
void
init_logging()
{
     setlogfacility(syslog_level);

     if (enable_syslog)
         logmask |= LF_SYSLOG;
     else
         logmask &= ~LF_SYSLOG;

     if (logfile)
         logmask |= LF_LOGFILE;
     else
         logmask &= ~LF_LOGFILE;

     if (logmask & LF_SYSLOG) {
	openlog("myprogram", LOG_PID|LOG_CONS, fac);
         }

         if (debug) {
             syslog((fac | LOG_NOTICE), "logging subsystem started...");
             setlogmask(LOG_UPTO(LOG_DEBUG));
         } else {
             if (verbose > 1)
                 setlogmask(LOG_UPTO(LOG_DEBUG));
             else
                 setlogmask(LOG_UPTO(LOG_INFO));
         }
     }

     if ((logmask & LF_LOGFILE) && logfile) {
         if ((ofp = fopen(logfile, "a+")) == NULL) {
             fprintf(stderr, "Fatal Error: unable to open and append  
to logfile '%s'.\n", logfile);
             terminate(EX_CANTCREAT);
         }
     } else if (debug) {
         ofp = stdout;
     }
}

/* output a message to syslog and/or the logfile/stdout */
void
logdebug(char *message) {
     if (message == NULL)
         message = strerror(errno);
     if (logmask & LF_SYSLOG)
         syslog((fac | LOG_DEBUG), "%s", message);
     if (ofp) {
         fputs(message, ofp);
         fflush(ofp);
     }
}

[ ...repeat for loginfo(), logwarn(), etc... ]

The above is triggered off of getopt() parsing flags and setting  
enable_syslog and/or logfile variables, but you could check whether a  
syslogd socket is available to scribble to or not, and decide for  
yourself to do your own logging to a file or not.

Come to think of it, are you familiar with sending LOG_CONS to  
openlog()?  Perhaps that would be a reasonable compromise, as you  
wouldn't need to move over to logging via a wrapper rather than  
calling syslog() directly....

Regards,
-- 
-Chuck
Received on Mon May 11 2009 - 19:37:27 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:47 UTC