2 * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the GNU
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/socket.h>
31 #define _(String) gettext((String))
40 static int logfacility=LOG_DAEMON;
41 static int logpriority=LOG_NOTICE;
46 regex_t **regList = NULL;
48 static int logEntries = 0;
49 struct logInfo *logData = NULL;
51 void readConfiguration(char *fname) {
58 if ((fd=open(fname,O_RDONLY))==-1) return;
59 if (fstat(fd,&sbuf)) {
63 data=malloc(sbuf.st_size+1);
64 if (read(fd,data,sbuf.st_size)!=sbuf.st_size) {
70 data[sbuf.st_size] = '\0';
71 while ((line=getLine(&data))) {
72 if (line[0]=='#') continue;
73 if (!strncmp(line,"ignore ",7)) {
74 regexp = malloc(sizeof(regex_t));
75 if (!regcomp(regexp,line+7,REG_EXTENDED|REG_NOSUB|REG_NEWLINE)) {
76 regList = realloc(regList,(num+2) * sizeof(regex_t *));
77 regList[num] = regexp;
78 regList[num+1] = NULL;
82 if (!strncmp(line,"facility ",9)) {
84 if ((lfac == 0) && strcmp(line+9,"0")) {
88 for (x=0;facilitynames[x].c_name;x++) {
89 if (!strcmp(line+9,facilitynames[x].c_name)) {
90 lfac = facilitynames[x].c_val;
96 if (!strncmp(line,"priority ",9)) {
98 if ((lpri == 0) && strcmp(line+9,"0")) {
102 for (x=0;prioritynames[x].c_name;x++) {
103 if (!strcmp(line+9,prioritynames[x].c_name)) {
104 lpri = prioritynames[x].c_val;
111 if (lfac!=-1) logfacility=lfac;
112 if (lpri!=-1) logpriority=lpri;
115 char *getLine(char **data) {
116 /* Get one line from data */
117 /* Anything up to a carraige return (\r) or a backspace (\b) is discarded. */
118 /* If this really bothers you, mail me and I might make it configurable. */
119 /* It's here to avoid confilcts with fsck's progress bar. */
122 if (!*data) return NULL;
124 while (*x && (*x != '\n')) {
125 while (*x && (*x != '\n') && (*x != '\r') && (*x != '\b')) x++;
126 if (*x && (*x=='\r' || *x =='\b')) {
138 memcpy(y,*data,x-*data);
147 memcpy(y,*data,x-*data-1);
152 char **toArray(char *line, int *num) {
153 /* Converts a long string into an array of lines. */
160 while ((tmpline=getLine(&line))) {
162 lines = (char **) malloc(sizeof(char *));
164 lines = (char **) realloc(lines, (*num+1)*sizeof(char *));
165 lines[*num] = tmpline;
175 if ( (pid = fork()) == -1 ) {
183 DDEBUG("minilogd returned %d!\n",WEXITSTATUS(rc));
184 return WEXITSTATUS(rc);
191 fd=open("/dev/null",O_RDWR);
197 execlp("minilogd","minilogd",NULL);
205 struct sockaddr_un addr;
207 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
211 bzero(&addr,sizeof(addr));
212 addr.sun_family = AF_LOCAL;
213 strncpy(addr.sun_path,_PATH_LOG,sizeof(addr.sun_path)-1);
215 if (connect(s,(struct sockaddr *) &addr,sizeof(addr))<0) {
216 if (errno == EPROTOTYPE || errno == ECONNREFUSED) {
217 DDEBUG("connect failed (EPROTOTYPE), trying stream\n");
219 s = socket(AF_LOCAL, SOCK_STREAM, 0);
220 if (connect(s,(struct sockaddr *) &addr, sizeof(addr)) < 0) {
221 DDEBUG("connect failed: %s\n",strerror(errno));
229 DDEBUG("connect failed: %s\n",strerror(errno));
237 int logLine(struct logInfo *logEnt) {
238 /* Logs a line... somewhere. */
242 /* Don't log empty or null lines */
243 if (!logEnt->line || !strcmp(logEnt->line,"\n")) return 0;
246 if ( ((stat(_PATH_LOG,&statbuf)==-1) || trySocket())
249 DDEBUG("starting daemon failed, pooling entry %d\n",logEntries);
250 logData=realloc(logData,(logEntries+1)*sizeof(struct logInfo));
251 logData[logEntries]= (*logEnt);
255 for (x=0;x<logEntries;x++) {
256 DDEBUG("flushing log entry %d =%s=\n",x,logData[x].line);
257 openlog(logData[x].cmd,0,logData[x].fac);
258 syslog(logData[x].pri,"%s",logData[x].line);
264 DDEBUG("logging =%s= via syslog\n",logEnt->line);
265 openlog(logEnt->cmd,0,logEnt->fac);
266 syslog(logEnt->pri,"%s",logEnt->line);
272 int logEvent(char *cmd, int eventtype,char *string) {
273 char *eventtable [] = {
274 _("%s babbles incoherently"),
277 _("%s cancelled at user request"),
278 _("%s failed due to a failed dependency"),
279 /* insert more here */
283 struct logInfo logentry;
286 logentry.cmd = strdup(basename(cmd));
287 if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') &&
288 ( logentry.cmd[1] >= '0' && logentry.cmd[1] <= '9' ) &&
289 ( logentry.cmd[2] >= '0' && logentry.cmd[2] <= '9' ) )
292 logentry.cmd = strdup(_("(none)"));
294 string = strdup(cmd);
296 while (eventtable[x] && x<eventtype) x++;
297 if (!(eventtable[x])) x=0;
299 len=strlen(eventtable[x])+strlen(string);
300 logentry.line=malloc(len);
301 snprintf(logentry.line,len,eventtable[x],string);
303 logentry.pri = logpriority;
304 logentry.fac = logfacility;
306 return logLine(&logentry);
309 int logString(char *cmd, char *string) {
310 struct logInfo logentry;
313 logentry.cmd = strdup(basename(cmd));
314 if ((logentry.cmd[0] =='K' || logentry.cmd[0] == 'S') &&
315 ( logentry.cmd[1] >= '0' && logentry.cmd[1] <= 0x39 ) &&
316 ( logentry.cmd[2] >= '0' && logentry.cmd[2] <= 0x39 ) )
319 logentry.cmd = strdup(_(""));
320 logentry.line = strdup(string);
321 logentry.pri = logpriority;
322 logentry.fac = logfacility;
324 return logLine(&logentry);
327 int processArgs(int argc, char **argv, int silent) {
332 char *logstring=NULL;
333 char *fac=NULL,*pri=NULL;
334 int lfac=-1, lpri=-1;
337 struct poptOption optTable[] = {
339 { "conf", 0, POPT_ARG_STRING, &conffile, 0,
340 "configuration file (default: /etc/initlog.conf)", NULL
342 { "name", 'n', POPT_ARG_STRING, &cmdname, 0,
343 "name of service being logged", NULL
345 { "event", 'e', POPT_ARG_INT, &cmdevent, 0,
346 "event being logged (see man page)", NULL
348 { "cmd", 'c', POPT_ARG_STRING, &cmd, 0,
349 "command to run, logging output", NULL
351 { "debug", 'd', POPT_ARG_NONE, &debug, 0,
352 "print lots of verbose debugging info", NULL
354 { "run", 'r', POPT_ARG_STRING, &cmd, 3,
355 "command to run, accepting input on open fd", NULL
357 { "string", 's', POPT_ARG_STRING, &logstring, 0,
358 "string to log", NULL
360 { "facility", 'f', POPT_ARG_STRING, &fac, 1,
361 "facility to log at (default: 'local7')", NULL
363 { "priority", 'p', POPT_ARG_STRING, &pri, 2,
364 "priority to log at (default: 'notice')", NULL
366 { "quiet", 'q', POPT_ARG_NONE, &quiet, 0,
367 "suppress stdout/stderr", NULL
372 context = poptGetContext("initlog", argc, argv, optTable, 0);
374 while ((rc = poptGetNextOpt(context)) > 0) {
378 if ((lfac == 0) && strcmp(fac,"0")) {
382 for (x=0;facilitynames[x].c_name;x++) {
383 if (!strcmp(fac,facilitynames[x].c_name)) {
384 lfac = facilitynames[x].c_val;
392 if ((lpri == 0) && strcmp(pri,"0")) {
396 for (x=0;prioritynames[x].c_name;x++) {
397 if (!strcmp(pri,prioritynames[x].c_name)) {
398 lpri = prioritynames[x].c_val;
414 fprintf(stderr, "%s: %s\n",
415 poptBadOption(context, POPT_BADOPTION_NOALIAS),
420 if ( (cmd && logstring) || (cmd && cmdname) ) {
422 fprintf(stderr, _("--cmd and --run are incompatible with --string or --name\n"));
425 if ( cmdname && (!logstring && !cmdevent)) {
427 fprintf(stderr, _("--name requires one of --event or --string\n"));
430 if (cmdevent && cmd) {
432 fprintf(stderr, _("--cmd and --run are incompatible with --event\n"));
436 readConfiguration(conffile);
438 readConfiguration("/etc/initlog.conf");
441 while (isspace(*cmd)) cmd++;
443 if (lpri!=-1) logpriority=lpri;
444 if (lfac!=-1) logfacility=lfac;
446 logEvent(cmdname,cmdevent,logstring);
447 } else if (logstring) {
448 logString(cmdname,logstring);
449 } else if ( cmd && *cmd) {
450 return(runCommand(cmd,reexec,quiet,debug));
453 fprintf(stderr,"nothing to do!\n");
459 int main(int argc, char **argv) {
461 setlocale(LC_ALL,"");
462 bindtextdomain("initlog","/etc/locale");
463 textdomain("initlog");
464 exit(processArgs(argc,argv,0));