1 /* objomsr.c
2  * Implementation of the omsr (omodStringRequest) object.
3  *
4  * File begun on 2007-07-27 by RGerhards
5  *
6  * Copyright 2007-2012 Rainer Gerhards and Adiscon GmbH.
7  *
8  * This file is part of the rsyslog runtime library.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *       http://www.apache.org/licenses/LICENSE-2.0
15  *       -or-
16  *       see COPYING.ASL20 in the source distribution
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24 #include "config.h"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30 
31 #include "rsyslog.h"
32 #include "objomsr.h"
33 
34 
35 /* destructor
36  */
OMSRdestruct(omodStringRequest_t * pThis)37 rsRetVal OMSRdestruct(omodStringRequest_t *pThis)
38 {
39 	int i;
40 
41 	assert(pThis != NULL);
42 	/* free the strings */
43 	if(pThis->ppTplName != NULL) {
44 		for(i = 0 ; i < pThis->iNumEntries ; ++i) {
45 			free(pThis->ppTplName[i]);
46 		}
47 		free(pThis->ppTplName);
48 	}
49 	if(pThis->piTplOpts != NULL)
50 		free(pThis->piTplOpts);
51 	free(pThis);
52 
53 	return RS_RET_OK;
54 }
55 
56 
57 /* constructor
58  */
OMSRconstruct(omodStringRequest_t ** ppThis,int iNumEntries)59 rsRetVal OMSRconstruct(omodStringRequest_t **ppThis, int iNumEntries)
60 {
61 	omodStringRequest_t *pThis = NULL;
62 	DEFiRet;
63 
64 	assert(ppThis != NULL);
65 	assert(iNumEntries >= 0);
66 	if(iNumEntries > CONF_OMOD_NUMSTRINGS_MAXSIZE) {
67 		ABORT_FINALIZE(RS_RET_MAX_OMSR_REACHED);
68 	}
69 	CHKmalloc(pThis = calloc(1, sizeof(omodStringRequest_t)));
70 
71 	/* got the structure, so fill it */
72 	if(iNumEntries > 0) {
73 		pThis->iNumEntries = iNumEntries;
74 		/* allocate string for template name array. The individual strings will be
75 		 * allocated as the code progresses (we do not yet know the string sizes)
76 		 */
77 		CHKmalloc(pThis->ppTplName = calloc(iNumEntries, sizeof(uchar*)));
78 
79 	/* allocate the template options array. */
80 		CHKmalloc(pThis->piTplOpts = calloc(iNumEntries, sizeof(int)));
81 	}
82 
83 finalize_it:
84 	if(iRet != RS_RET_OK) {
85 		if(pThis != NULL) {
86 			OMSRdestruct(pThis);
87 			pThis = NULL;
88 		}
89 	}
90 	*ppThis = pThis;
91 	RETiRet;
92 }
93 
94 /* set a template name and option to the object. Index must be given. The pTplName must be
95  * pointing to memory that can be freed. If in doubt, the caller must strdup() the value.
96  */
OMSRsetEntry(omodStringRequest_t * pThis,int iEntry,uchar * pTplName,int iTplOpts)97 rsRetVal OMSRsetEntry(omodStringRequest_t *pThis, int iEntry, uchar *pTplName, int iTplOpts)
98 {
99 	assert(pThis != NULL);
100 	assert(iEntry < pThis->iNumEntries);
101 
102 	if(pThis->ppTplName[iEntry] != NULL)
103 		free(pThis->ppTplName[iEntry]);
104 	pThis->ppTplName[iEntry] = pTplName;
105 	pThis->piTplOpts[iEntry] = iTplOpts;
106 
107 	return RS_RET_OK;
108 }
109 
110 
111 /* get number of entries for this object
112  */
OMSRgetEntryCount(omodStringRequest_t * pThis)113 int OMSRgetEntryCount(omodStringRequest_t *pThis)
114 {
115 	assert(pThis != NULL);
116 	return pThis->iNumEntries;
117 }
118 
119 
120 /* return data for a specific entry. All data returned is
121  * read-only and lasts only as long as the object lives. If the caller
122  * needs it for an extended period of time, the caller must copy the
123  * strings. Please note that the string pointer may be NULL, which is the
124  * case when it was never set.
125  */
OMSRgetEntry(omodStringRequest_t * pThis,int iEntry,uchar ** ppTplName,int * piTplOpts)126 int OMSRgetEntry(omodStringRequest_t *pThis, int iEntry, uchar **ppTplName, int *piTplOpts)
127 {
128 	assert(pThis != NULL);
129 	assert(ppTplName != NULL);
130 	assert(piTplOpts != NULL);
131 	assert(iEntry < pThis->iNumEntries);
132 
133 	*ppTplName = pThis->ppTplName[iEntry];
134 	*piTplOpts = pThis->piTplOpts[iEntry];
135 
136 	return RS_RET_OK;
137 }
138 
139 
140 /* return the full set of template options that are supported by this version of
141  * OMSR. They are returned in an unsigned long value. The caller can mask that
142  * value to check on the option he is interested in.
143  * Note that this interface was added in 4.1.6, so a plugin must obtain a pointer
144  * to this interface via queryHostEtryPt().
145  * rgerhards, 2009-04-03
146  */
147 rsRetVal
OMSRgetSupportedTplOpts(unsigned long * pOpts)148 OMSRgetSupportedTplOpts(unsigned long *pOpts)
149 {
150 	DEFiRet;
151 	assert(pOpts != NULL);
152 	*pOpts = OMSR_RQD_TPL_OPT_SQL | OMSR_TPL_AS_ARRAY | OMSR_TPL_AS_MSG
153 		 | OMSR_TPL_AS_JSON;
154 	RETiRet;
155 }
156 
157 /* vim:set ai:
158  */
159