327
330
if(pCache[iEntry] == NULL)
330
dbgprintf("Removed entry %d for file '%s' from dynaCache.\n", iEntry,
333
DBGPRINTF("Removed entry %d for file '%s' from dynaCache.\n", iEntry,
331
334
pCache[iEntry]->pName == NULL ? "[OPEN FAILED]" : (char*)pCache[iEntry]->pName);
332
335
/* if the name is NULL, this is an improperly initilized entry which
333
336
* needs to be discarded. In this case, neither the file is to be closed
355
/* This function frees all dynamic file name cache entries and closes the
356
* relevant files. Part of Shutdown and HUP processing.
357
* rgerhards, 2008-10-23
359
static inline void dynaFileFreeCacheEntries(instanceData *pData)
362
ASSERT(pData != NULL);
365
for(i = 0 ; i < pData->iCurrCacheSize ; ++i) {
366
dynaFileDelCacheEntry(pData->dynCache, i, 1);
352
372
/* This function frees the dynamic file name cache.
354
374
static void dynaFileFreeCache(instanceData *pData)
357
376
ASSERT(pData != NULL);
360
for(i = 0 ; i < pData->iCurrCacheSize ; ++i) {
361
dynaFileDelCacheEntry(pData->dynCache, i, 1);
379
dynaFileFreeCacheEntries(pData);
364
380
if(pData->dynCache != NULL)
365
381
d_free(pData->dynCache);
370
/* This is a shared code for both static and dynamic files.
386
/* This is now shared code for all types of files. It simply prepares
387
* file access, which, among others, means the the file wil be opened
388
* and any directories in between will be created (based on config, of
389
* course). -- rgerhards, 2008-10-22
390
* changed to iRet interface - 2009-03-19
372
static void prepareFile(instanceData *pData, uchar *newFileName)
393
prepareFile(instanceData *pData, uchar *newFileName)
396
if(pData->fileType == eTypePIPE) {
397
pData->fd = open((char*) pData->f_fname, O_RDWR|O_NONBLOCK|O_CLOEXEC);
398
FINALIZE; /* we are done in this case */
374
401
if(access((char*)newFileName, F_OK) == 0) {
375
402
/* file already exists */
376
pData->fd = open((char*) newFileName, O_WRONLY|O_APPEND|O_CREAT|O_NOCTTY,
403
pData->fd = open((char*) newFileName, O_WRONLY|O_APPEND|O_CREAT|O_NOCTTY|O_CLOEXEC,
377
404
pData->fCreateMode);
380
407
/* file does not exist, create it (and eventually parent directories */
381
408
if(pData->bCreateDirs) {
382
/* we fist need to create parent dirs if they are missing
409
/* We first need to create parent dirs if they are missing.
383
410
* We do not report any errors here ourselfs but let the code
384
411
* fall through to error handler below.
386
413
if(makeFileParentDirs(newFileName, strlen((char*)newFileName),
387
414
pData->fDirCreateMode, pData->dirUID,
388
415
pData->dirGID, pData->bFailOnChown) != 0) {
389
return; /* we give up */
416
ABORT_FINALIZE(RS_RET_ERR); /* we give up */
392
419
/* no matter if we needed to create directories or not, we now try to create
393
420
* the file. -- rgerhards, 2008-12-18 (based on patch from William Tisater)
395
pData->fd = open((char*) newFileName, O_WRONLY|O_APPEND|O_CREAT|O_NOCTTY,
422
pData->fd = open((char*) newFileName, O_WRONLY|O_APPEND|O_CREAT|O_NOCTTY|O_CLOEXEC,
396
423
pData->fCreateMode);
397
424
if(pData->fd != -1) {
398
425
/* check and set uid/gid */
399
426
if(pData->fileUID != (uid_t)-1 || pData->fileGID != (gid_t) -1) {
400
427
/* we need to set owner/group */
401
if(fchown(pData->fd, pData->fileUID,
402
pData->fileGID) != 0) {
428
if(fchown(pData->fd, pData->fileUID, pData->fileGID) != 0) {
403
429
if(pData->bFailOnChown) {
404
430
int eSave = errno;
405
431
close(pData->fd);
486
526
/* we need to allocate memory for the cache structure */
487
527
pCache[iFirstFree] = (dynaFileCacheEntry*) calloc(1, sizeof(dynaFileCacheEntry));
488
528
if(pCache[iFirstFree] == NULL) {
489
dbgprintf("prepareDynfile(): could not alloc mem, discarding this request\n");
529
DBGPRINTF("prepareDynfile(): could not alloc mem, discarding this request\n");
494
534
/* Ok, we finally can open the file */
495
prepareFile(pData, newFileName);
535
prepareFile(pData, newFileName); /* ignore exact error, we check fd below */
497
537
/* file is either open now or an error state set */
498
538
if(pData->fd == -1) {
500
540
* message. Otherwise, we could run into a never-ending loop. The bad
501
541
* news is that we also lose errors on startup messages, but so it is.
503
if(iMsgOpts & INTERNAL_MSG)
504
dbgprintf("Could not open dynaFile, discarding message\n");
543
if(iMsgOpts & INTERNAL_MSG) {
544
DBGPRINTF("Could not open dynaFile, discarding message\n");
506
546
errmsg.LogError(0, NO_ERRCODE, "Could not open dynamic file '%s' - discarding message", (char*)newFileName);
507
548
dynaFileDelCacheEntry(pCache, iFirstFree, 1);
508
549
pData->iCurrElt = -1;
513
554
pCache[iFirstFree]->pName = (uchar*)strdup((char*)newFileName); /* TODO: check for NULL (very unlikely) */
514
555
pCache[iFirstFree]->lastUsed = time(NULL);
515
556
pData->iCurrElt = iFirstFree;
516
dbgprintf("Added new entry %d for file cache, file '%s'.\n",
517
iFirstFree, newFileName);
557
DBGPRINTF("Added new entry %d for file cache, file '%s'.\n", iFirstFree, newFileName);
537
580
if(pData->bDynamicName) {
538
581
if(prepareDynFile(pData, ppString[1], iMsgOpts) != 0)
539
ABORT_FINALIZE(RS_RET_ERR);
582
ABORT_FINALIZE(RS_RET_SUSPENDED); /* whatever the failure was, we need to retry */
585
if(pData->fd == -1) {
587
iRetLocal = prepareFile(pData, pData->f_fname);
588
if((iRetLocal != RS_RET_OK) || (pData->fd == -1))
589
ABORT_FINALIZE(RS_RET_SUSPENDED); /* whatever the failure was, we need to retry */
542
592
/* create the message based on format specified */
575
if (write(pData->fd, ppString[0], strlen((char*)ppString[0])) < 0) {
625
iLenWritten = write(pData->fd, ppString[0], strlen((char*)ppString[0]));
626
//dbgprintf("lenwritten: %d\n", iLenWritten);
627
if(iLenWritten < 0) {
578
/* If a named pipe is full, just ignore it for now
580
if (pData->fileType == eTypePIPE && e == EAGAIN)
581
ABORT_FINALIZE(RS_RET_OK);
583
/* If the filesystem is filled up, just ignore
584
* it for now and continue writing when possible
585
* based on patch for sysklogd by Martin Schulze on 2007-05-24
587
if (pData->fileType == eTypeFILE && e == ENOSPC)
588
ABORT_FINALIZE(RS_RET_OK);
590
(void) close(pData->fd);
592
* Check for EBADF on TTY's due to vhangup()
630
rs_strerror_r(errno, errStr, sizeof(errStr));
631
DBGPRINTF("log file (%d) write error %d: %s\n", pData->fd, e, errStr);
633
/* If a named pipe is full, we suspend this action for a while */
634
if(pData->fileType == eTypePIPE && e == EAGAIN)
635
ABORT_FINALIZE(RS_RET_SUSPENDED);
638
pData->fd = -1; /* tell that fd is no longer open! */
639
if(pData->bDynamicName && pData->iCurrElt != -1) {
640
/* in this case, we need to invalidate the name in the cache, too
641
* otherwise, an invalid fd may show up if we had a file name change.
642
* rgerhards, 2009-03-19
644
pData->dynCache[pData->iCurrElt]->fd = -1;
646
/* Check for EBADF on TTY's due to vhangup()
593
647
* Linux uses EIO instead (mrn 12 May 96)
595
if ((pData->fileType == eTypeTTY || pData->fileType == eTypeCONSOLE)
649
if((pData->fileType == eTypeTTY || pData->fileType == eTypeCONSOLE)
601
pData->fd = open((char*) pData->f_fname, O_WRONLY|O_APPEND|O_NOCTTY);
656
pData->fd = open((char*) pData->f_fname, O_WRONLY|O_APPEND|O_NOCTTY|O_CLOEXEC);
602
657
if (pData->fd < 0) {
603
iRet = RS_RET_DISABLE_ACTION;
658
iRet = RS_RET_SUSPENDED;
604
659
errmsg.LogError(0, NO_ERRCODE, "%s", pData->f_fname);
610
iRet = RS_RET_DISABLE_ACTION;
665
iRet = RS_RET_SUSPENDED;
612
667
errmsg.LogError(0, NO_ERRCODE, "%s", pData->f_fname);
643
698
CODESTARTdoAction
644
dbgprintf(" (%s)\n", pData->f_fname);
645
/* pData->fd == -1 is an indicator that the we couldn't
646
* open the file at startup. For dynaFiles, this is ok,
647
* all others are doomed.
649
if(pData->bDynamicName || (pData->fd != -1))
650
iRet = writeFile(ppString, iMsgOpts, pData);
699
DBGPRINTF(" (%s)\n", pData->f_fname);
700
iRet = writeFile(ppString, iMsgOpts, pData);
764
813
pData->dirUID = dirUID;
765
814
pData->dirGID = dirGID;
767
if(pData->fileType == eTypePIPE) {
768
pData->fd = open((char*) pData->f_fname, O_RDWR|O_NONBLOCK);
770
prepareFile(pData, pData->f_fname);
816
/* at this stage, we ignore the return value of prepareFile, this is taken
817
* care of in later steps. -- rgerhards, 2009-03-19
819
prepareFile(pData, pData->f_fname);
773
if ( pData->fd < 0 ){
775
dbgprintf("Error opening log file: %s\n", pData->f_fname);
776
errmsg.LogError(0, NO_ERRCODE, "%s", pData->f_fname);
823
DBGPRINTF("Error opening log file: %s\n", pData->f_fname);
824
errmsg.LogError(0, RS_RET_NO_FILE_ACCESS, "Could no open output file '%s'", pData->f_fname);
779
if (isatty(pData->fd)) {
780
pData->fileType = eTypeTTY;
783
if (strcmp((char*) p, _PATH_CONSOLE) == 0)
827
if(strcmp((char*) p, _PATH_CONSOLE) == 0)
784
828
pData->fileType = eTypeCONSOLE;