summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2009-06-12 11:47:00 +0200
committerRainer Gerhards <rgerhards@adiscon.com>2009-06-12 11:47:00 +0200
commite3d9843c85b1dfddabc937ac6ccb4057d626bf03 (patch)
tree734265f50321802e047a29b984391b619e7bd85f /runtime
parent1af948107e6e520788e374adccf4986bf07e92f5 (diff)
downloadrsyslog-e3d9843c85b1dfddabc937ac6ccb4057d626bf03.tar.gz
rsyslog-e3d9843c85b1dfddabc937ac6ccb4057d626bf03.tar.xz
rsyslog-e3d9843c85b1dfddabc937ac6ccb4057d626bf03.zip
re-enabled pipe, tty and console in omfile
... by moving code to stream.c. Thanks to the new design, new cases are not really needed, resulting in cleaner code. I also did a cleanup of header file usage as a side-activity.
Diffstat (limited to 'runtime')
-rw-r--r--runtime/conf.h4
-rw-r--r--runtime/rsyslog.h13
-rw-r--r--runtime/stream.c162
-rw-r--r--runtime/stream.h5
4 files changed, 127 insertions, 57 deletions
diff --git a/runtime/conf.h b/runtime/conf.h
index f29de1ba..25b887be 100644
--- a/runtime/conf.h
+++ b/runtime/conf.h
@@ -51,5 +51,9 @@ PROTOTYPEObj(conf);
extern EHostnameCmpMode eDfltHostnameCmpMode;
extern cstr_t *pDfltHostnameCmp;
extern cstr_t *pDfltProgNameCmp;
+/* TODO: the following 2 need to go in conf obj interface... */
+rsRetVal cflineParseTemplateName(uchar** pp, omodStringRequest_t *pOMSR, int iEntry, int iTplOpts, uchar *dfltTplName);
+rsRetVal cflineParseFileName(uchar* p, uchar *pFileName, omodStringRequest_t *pOMSR, int iEntry, int iTplOpts, uchar *pszTpl);
+
#endif /* #ifndef INCLUDED_CONF_H */
diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h
index 92942c70..28f7bce9 100644
--- a/runtime/rsyslog.h
+++ b/runtime/rsyslog.h
@@ -99,6 +99,7 @@ typedef struct strmLstnPortList_s strmLstnPortList_t; // TODO: rename?
typedef long long int64;
typedef long long unsigned uint64;
typedef int64 number_t; /* type to use for numbers - TODO: maybe an autoconf option? */
+typedef short bool;
#ifdef __hpux
typedef unsigned int u_int32_t; /* TODO: is this correct? */
@@ -124,6 +125,18 @@ typedef enum {
FIOP_EREREGEX = 5 /* matches a ERE regular expression? */
} fiop_t;
+/* file types (omfile & stream) */
+typedef enum {
+ eTypeFILE,
+ eTypeTTY,
+ eTypeCONSOLE,
+ eTypePIPE
+} filetype_t;
+
+
+#ifndef _PATH_CONSOLE
+#define _PATH_CONSOLE "/dev/console"
+#endif
/* The error codes below are orginally "borrowed" from
* liblogging. As such, we reserve values up to -2999
diff --git a/runtime/stream.c b/runtime/stream.c
index 5355a1a5..c8672aa2 100644
--- a/runtime/stream.c
+++ b/runtime/stream.c
@@ -166,35 +166,14 @@ finalize_it:
* strm instance object.
*/
-/* open a strm file
- * It is OK to call this function when the stream is already open. In that
- * case, it returns immediately with RS_RET_OK
+/* do the physical open() call on a file.
*/
-static rsRetVal strmOpenFile(strm_t *pThis)
+static rsRetVal
+doPhysOpen(strm_t *pThis)
{
- DEFiRet;
int iFlags;
-
- ASSERT(pThis != NULL);
-
- if(pThis->fd != -1)
- ABORT_FINALIZE(RS_RET_OK);
-
- if(pThis->pszFName == NULL)
- ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING);
-
- if(pThis->sType == STREAMTYPE_FILE_CIRCULAR) {
- CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir,
- pThis->pszFName, pThis->lenFName, pThis->iCurrFNum, pThis->iFileNumDigits));
- } else {
- if(pThis->pszDir == NULL) {
- if((pThis->pszCurrFName = (uchar*) strdup((char*) pThis->pszFName)) == NULL)
- ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
- } else {
- CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir,
- pThis->pszFName, pThis->lenFName, -1, 0));
- }
- }
+ DEFiRet;
+ ISOBJ_TYPE_assert(pThis, strm);
/* compute which flags we need to provide to open */
switch(pThis->tOperationsMode) {
@@ -222,8 +201,51 @@ static rsRetVal strmOpenFile(strm_t *pThis)
ABORT_FINALIZE(RS_RET_FILE_NOT_FOUND);
else
ABORT_FINALIZE(RS_RET_IO_ERROR);
+ } else {
+ if(!ustrcmp(pThis->pszCurrFName, UCHAR_CONSTANT(_PATH_CONSOLE)) || isatty(pThis->fd)) {
+ DBGPRINTF("file %d is a tty-type file\n", pThis->fd);
+ pThis->bIsTTY = 1;
+ } else {
+ pThis->bIsTTY = 0;
+ }
}
+finalize_it:
+ RETiRet;
+}
+
+
+/* open a strm file
+ * It is OK to call this function when the stream is already open. In that
+ * case, it returns immediately with RS_RET_OK
+ */
+static rsRetVal strmOpenFile(strm_t *pThis)
+{
+ DEFiRet;
+
+ ASSERT(pThis != NULL);
+
+ if(pThis->fd != -1)
+ ABORT_FINALIZE(RS_RET_OK);
+
+ if(pThis->pszFName == NULL)
+ ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING);
+
+ if(pThis->sType == STREAMTYPE_FILE_CIRCULAR) {
+ CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir,
+ pThis->pszFName, pThis->lenFName, pThis->iCurrFNum, pThis->iFileNumDigits));
+ } else {
+ if(pThis->pszDir == NULL) {
+ if((pThis->pszCurrFName = (uchar*) strdup((char*) pThis->pszFName)) == NULL)
+ ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
+ } else {
+ CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir,
+ pThis->pszFName, pThis->lenFName, -1, 0));
+ }
+ }
+
+ CHKiRet(doPhysOpen(pThis));
+
pThis->iCurrOffs = 0;
if(pThis->tOperationsMode == STREAMMODE_WRITE_APPEND) {
/* we need to obtain the current offset */
@@ -232,8 +254,8 @@ static rsRetVal strmOpenFile(strm_t *pThis)
pThis->iCurrOffs = offset;
}
- dbgoprint((obj_t*) pThis, "opened file '%s' for %s (0x%x) as %d\n", pThis->pszCurrFName,
- (pThis->tOperationsMode == STREAMMODE_READ) ? "READ" : "WRITE", iFlags, pThis->fd);
+ dbgoprint((obj_t*) pThis, "opened file '%s' for %s as %d\n", pThis->pszCurrFName,
+ (pThis->tOperationsMode == STREAMMODE_READ) ? "READ" : "WRITE", pThis->fd);
finalize_it:
RETiRet;
@@ -255,7 +277,7 @@ static rsRetVal strmCloseFile(strm_t *pThis)
if(pThis->tOperationsMode != STREAMMODE_READ)
strmFlush(pThis);
- close(pThis->fd); // TODO: error check
+ close(pThis->fd);
pThis->fd = -1;
if(pThis->fdDir != -1) {
@@ -265,7 +287,13 @@ static rsRetVal strmCloseFile(strm_t *pThis)
}
if(pThis->bDeleteOnClose) {
- unlink((char*) pThis->pszCurrFName); // TODO: check returncode
+ if(unlink((char*) pThis->pszCurrFName) == -1) {
+ char errStr[1024];
+ int err = errno;
+ rs_strerror_r(err, errStr, sizeof(errStr));
+ DBGPRINTF("error %d unlinking '%s' - ignored: %s\n",
+ errno, pThis->pszCurrFName, errStr);
+ }
}
pThis->iCurrOffs = 0; /* we are back at begin of file */
@@ -544,13 +572,12 @@ static rsRetVal strmConstructFinalize(strm_t *pThis)
}
/* if we are aset to sync, we must obtain a file handle to the directory for fsync() purposes */
- if(pThis->bSync) {
- pThis->fdDir = open((char*)pThis->pszDir, O_RDONLY);
+ if(pThis->bSync && !pThis->bIsTTY) {
+ pThis->fdDir = open((char*)pThis->pszDir, O_RDONLY | O_CLOEXEC | O_NOCTTY);
if(pThis->fdDir == -1) {
char errStr[1024];
int err = errno;
rs_strerror_r(err, errStr, sizeof(errStr));
- // TODO: log an error message? think so...
DBGPRINTF("error %d opening directory file for fsync() use - fsync for directory disabled: %s\n",
errno, errStr);
}
@@ -606,6 +633,29 @@ finalize_it:
}
+/* try to recover a tty after a write error. This may have happend
+ * due to vhangup(), and, if so, we can simply re-open it.
+ */
+#ifdef linux
+# define ERR_TTYHUP EIO
+#else
+# define ERR_TTYHUP EBADF
+#endif
+static rsRetVal
+tryTTYRecover(strm_t *pThis, int err)
+{
+ DEFiRet;
+ ISOBJ_TYPE_assert(pThis, strm);
+ if(err == ERR_TTYHUP) {
+ close(pThis->fd);
+ CHKiRet(doPhysOpen(pThis));
+ }
+
+finalize_it:
+ RETiRet;
+}
+#undef ER_TTYHUP
+
/* issue write() api calls until either the buffer is completely
* written or an error occured (it may happen that multiple writes
@@ -614,29 +664,36 @@ finalize_it:
* rgerhards, 2009-06-08
*/
static rsRetVal
-doWriteCall(int fd, uchar *pBuf, size_t *pLenBuf)
+doWriteCall(strm_t *pThis, uchar *pBuf, size_t *pLenBuf)
{
ssize_t lenBuf;
ssize_t iTotalWritten;
ssize_t iWritten;
char *pWriteBuf;
DEFiRet;
+ ISOBJ_TYPE_assert(pThis, strm);
lenBuf = *pLenBuf;
pWriteBuf = (char*) pBuf;
iTotalWritten = 0;
do {
- iWritten = write(fd, pWriteBuf, lenBuf);
+ iWritten = write(pThis->fd, pWriteBuf, lenBuf);
if(iWritten < 0) {
char errStr[1024];
int err = errno;
rs_strerror_r(err, errStr, sizeof(errStr));
- DBGPRINTF("log file (%d) write error %d: %s\n", fd, err, errStr);
+ DBGPRINTF("log file (%d) write error %d: %s\n", pThis->fd, err, errStr);
if(err == EINTR) {
/*NO ERROR, just continue */;
} else {
- ABORT_FINALIZE(RS_RET_IO_ERROR);
- // TODO: cover more error cases!
+ if(pThis->bIsTTY) {
+ CHKiRet(tryTTYRecover(pThis, err));
+ } else {
+ ABORT_FINALIZE(RS_RET_IO_ERROR);
+ /* Would it make sense to cover more error cases? So far, I
+ * do not see good reason to do so.
+ */
+ }
}
}
/* advance buffer to next write position */
@@ -653,7 +710,10 @@ finalize_it:
/* sync the file to disk, so that any unwritten data is persisted. This
* also syncs the directory and thus makes sure that the file survives
- * fatal failure. -- rgerhards, 2009-06-08
+ * fatal failure. Note that we do NOT return an error status if the
+ * sync fails. Doing so would probably cause more trouble than it
+ * is worth (read: data loss may occur where we otherwise might not
+ * have it). -- rgerhards, 2009-06-08
*/
static rsRetVal
syncFile(strm_t *pThis)
@@ -661,6 +721,9 @@ syncFile(strm_t *pThis)
int ret;
DEFiRet;
+ if(pThis->bIsTTY)
+ FINALIZE; /* TTYs can not be synced */
+
DBGPRINTF("syncing file %d\n", pThis->fd);
ret = fdatasync(pThis->fd);
if(ret != 0) {
@@ -670,19 +733,20 @@ syncFile(strm_t *pThis)
DBGPRINTF("sync failed for file %d with error (%d): %s - ignoring\n",
pThis->fd, err, errStr);
}
- // TODO: check error!
if(pThis->fdDir != -1) {
ret = fsync(pThis->fdDir);
-dbgprintf("sync on dir (fd %d) requested, return code %d\n", pThis->fdDir, ret);
}
+finalize_it:
RETiRet;
}
/* physically write to the output file. the provided data is ready for
* writing (e.g. zipped if we are requested to do that).
+ * Note that if the write() API fails, we do not reset any pointers, but return
+ * an error code. That means we may redo work in the next iteration.
* rgerhards, 2009-06-04
*/
static rsRetVal
@@ -690,24 +754,15 @@ strmPhysWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf)
{
size_t iWritten;
DEFiRet;
-
- ASSERT(pThis != NULL);
+ ISOBJ_TYPE_assert(pThis, strm);
if(pThis->fd == -1)
CHKiRet(strmOpenFile(pThis));
iWritten = lenBuf;
- CHKiRet(doWriteCall(pThis->fd, pBuf, &iWritten));
+ CHKiRet(doWriteCall(pThis, pBuf, &iWritten));
dbgoprint((obj_t*) pThis, "file %d write wrote %d bytes\n", pThis->fd, (int) iWritten);
- /* Now indicate buffer empty again. We do this in any case, because there
- * is no way we could react more intelligently to an error during write.
- * This MUST be done BEFORE strCheckNextOutputFile(), otherwise we have an
- * endless loop. We reset the buffer pointer also in finalize_it - this is
- * necessary if we run into problems. Not resetting it would again cause an
- * endless loop. So it is better to loose some data (which also justifies
- * duplicating that code, too...) -- rgerhards, 2008-01-10
- */
pThis->iBufPtr = 0;
pThis->iCurrOffs += iWritten;
/* update user counter, if provided */
@@ -725,8 +780,6 @@ strmPhysWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf)
}
finalize_it:
- pThis->iBufPtr = 0; /* see comment above */
-
RETiRet;
}
@@ -995,7 +1048,6 @@ strmSetFName(strm_t *pThis, uchar *pszName, size_t iLenName)
ASSERT(pThis != NULL);
ASSERT(pszName != NULL);
-dbgprintf("XXX: strm setFname: '%s'\n", pszName);
if(iLenName < 1)
ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING);
diff --git a/runtime/stream.h b/runtime/stream.h
index a06e089f..021c4792 100644
--- a/runtime/stream.h
+++ b/runtime/stream.h
@@ -99,7 +99,7 @@ typedef struct strm_s {
int64 iMaxFileSize;/* maximum size a file may grow to */
int iMaxFiles; /* maximum number of files if a circular mode is in use */
int iFileNumDigits;/* min number of digits to use in file number (only in circular mode) */
- int bDeleteOnClose; /* set to 1 to auto-delete on close -- be careful with that setting! */
+ bool bDeleteOnClose; /* set to 1 to auto-delete on close -- be careful with that setting! */
int64 iCurrOffs;/* current offset */
int64 *pUsrWCntr; /* NULL or a user-provided counter that receives the nbr of bytes written since the last CntrSet() */
/* dynamic properties, valid only during file open, not to be persistet */
@@ -115,12 +115,13 @@ typedef struct strm_s {
size_t iBufPtrMax; /* current max Ptr in Buffer (if partial read!) */
size_t iBufPtr; /* pointer into current buffer */
int iUngetC; /* char set via UngetChar() call or -1 if none set */
- int bInRecord; /* if 1, indicates that we are currently writing a not-yet complete record */
+ bool bInRecord; /* if 1, indicates that we are currently writing a not-yet complete record */
int iZipLevel; /* zip level (0..9). If 0, zip is completely disabled */
Bytef *pZipBuf;
/* support for omfile size-limiting commands, special counters, NOT persisted! */
off_t iSizeLimit; /* file size limit, 0 = no limit */
uchar *pszSizeLimitCmd; /* command to carry out when size limit is reached */
+ bool bIsTTY; /* is this a tty file? */
} strm_t;
/* interfaces */