1 /* nssel.c
2  *
3  * The io waiter is a helper object enabling us to wait on a set of streams to become
4  * ready for IO - this is modelled after select(). We need this, because
5  * stream drivers may have different concepts. Consequently,
6  * the structure must contain nsd_t's from the same stream driver type
7  * only. This is implemented as a singly-linked list where every
8  * new element is added at the top of the list.
9  *
10  * Work on this module begun 2008-04-22 by Rainer Gerhards.
11  *
12  * Copyright 2008-2014 Adiscon GmbH.
13  *
14  * This file is part of the rsyslog runtime library.
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License");
17  * you may not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  *       http://www.apache.org/licenses/LICENSE-2.0
21  *       -or-
22  *       see COPYING.ASL20 in the source distribution
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30 #include "config.h"
31 
32 #include "rsyslog.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 
39 #include "rsyslog.h"
40 #include "obj.h"
41 #include "module-template.h"
42 #include "netstrm.h"
43 #include "nssel.h"
44 
45 /* static data */
46 DEFobjStaticHelpers
DEFobjCurrIf(glbl)47 DEFobjCurrIf(glbl)
48 
49 
50 /* load our low-level driver. This must be done before any
51  * driver-specific functions (allmost all...) can be carried
52  * out. Note that the driver's .ifIsLoaded is correctly
53  * initialized by calloc() and we depend on that. Please note that
54  * we do some name-mangeling. We know that each nsd driver also needs
55  * a nssel driver. So we simply append "sel" to the nsd driver name: This,
56  * of course, means that the driver name must match these rules, but that
57  * shouldn't be a real problem.
58  * WARNING: this code is mostly identical to similar code in
59  * netstrms.c - TODO: abstract it and move it to some common place.
60  * rgerhards, 2008-04-28
61  */
62 static rsRetVal
63 loadDrvr(nssel_t *pThis)
64 {
65 	DEFiRet;
66 	uchar *pBaseDrvrName;
67 	uchar szDrvrName[48]; /* 48 shall be large enough */
68 
69 	pBaseDrvrName = pThis->pBaseDrvrName;
70 	if(pBaseDrvrName == NULL) /* if no drvr name is set, use system default */
71 		pBaseDrvrName = glbl.GetDfltNetstrmDrvr();
72 	if(snprintf((char*)szDrvrName, sizeof(szDrvrName), "lmnsdsel_%s", pBaseDrvrName) == sizeof(szDrvrName))
73 		ABORT_FINALIZE(RS_RET_DRVRNAME_TOO_LONG);
74 	CHKmalloc(pThis->pDrvrName = (uchar*) strdup((char*)szDrvrName));
75 
76 	pThis->Drvr.ifVersion = nsdCURR_IF_VERSION;
77 	/* The pDrvrName+2 below is a hack to obtain the object name. It
78 	 * safes us to have yet another variable with the name without "lm" in
79 	 * front of it. If we change the module load interface, we may re-think
80 	 * about this hack, but for the time being it is efficient and clean
81 	 * enough. -- rgerhards, 2008-04-18
82 	 */
83 	CHKiRet(obj.UseObj(__FILE__, szDrvrName+2, DONT_LOAD_LIB, (void*) &pThis->Drvr));
84 
85 finalize_it:
86 	if(iRet != RS_RET_OK) {
87 		if(pThis->pDrvrName != NULL) {
88 			free(pThis->pDrvrName);
89 			pThis->pDrvrName = NULL;
90 		}
91 	}
92 	RETiRet;
93 }
94 
95 
96 /* Standard-Constructor */
97 BEGINobjConstruct(nssel) /* be sure to specify the object type also in END macro! */
98 ENDobjConstruct(nssel)
99 
100 
101 /* destructor for the nssel object */
102 BEGINobjDestruct(nssel) /* be sure to specify the object type also in END and CODESTART macros! */
103 CODESTARTobjDestruct(nssel)
104 	if(pThis->pDrvrData != NULL)
105 		pThis->Drvr.Destruct(&pThis->pDrvrData);
106 
107 	/* and now we must release our driver, if we got one. We use the presence of
108 	 * a driver name string as load indicator (because we also need that string
109 	 * to release the driver
110 	 */
111 	free(pThis->pBaseDrvrName);
112 	if(pThis->pDrvrName != NULL) {
113 		obj.ReleaseObj(__FILE__, pThis->pDrvrName+2, DONT_LOAD_LIB, (void*) &pThis->Drvr);
114 		free(pThis->pDrvrName);
115 	}
ENDobjDestruct(nssel)116 ENDobjDestruct(nssel)
117 
118 
119 /* ConstructionFinalizer */
120 static rsRetVal
121 ConstructFinalize(nssel_t *pThis)
122 {
123 	DEFiRet;
124 	ISOBJ_TYPE_assert(pThis, nssel);
125 	CHKiRet(loadDrvr(pThis));
126 	CHKiRet(pThis->Drvr.Construct(&pThis->pDrvrData));
127 finalize_it:
128 	RETiRet;
129 }
130 
131 
132 /* set the base driver name. If the driver name
133  * is set to NULL, the previously set name is deleted but
134  * no name set again (which results in the system default being
135  * used)-- rgerhards, 2008-05-05
136  */
137 static rsRetVal
SetDrvrName(nssel_t * pThis,uchar * pszName)138 SetDrvrName(nssel_t *pThis, uchar *pszName)
139 {
140 	DEFiRet;
141 	ISOBJ_TYPE_assert(pThis, nssel);
142 	if(pThis->pBaseDrvrName != NULL) {
143 		free(pThis->pBaseDrvrName);
144 		pThis->pBaseDrvrName = NULL;
145 	}
146 
147 	if(pszName != NULL) {
148 		CHKmalloc(pThis->pBaseDrvrName = (uchar*) strdup((char*) pszName));
149 	}
150 finalize_it:
151 	RETiRet;
152 }
153 
154 
155 /* Add a stream object to the current select() set.
156  * Note that a single stream may have multiple "sockets" if
157  * it is a listener. If so, all of them are begin added.
158  */
159 static rsRetVal
Add(nssel_t * pThis,netstrm_t * pStrm,nsdsel_waitOp_t waitOp)160 Add(nssel_t *pThis, netstrm_t *pStrm, nsdsel_waitOp_t waitOp)
161 {
162 	DEFiRet;
163 
164 	ISOBJ_TYPE_assert(pThis, nssel);
165 	ISOBJ_TYPE_assert(pStrm, netstrm);
166 
167 	CHKiRet(pThis->Drvr.Add(pThis->pDrvrData, pStrm->pDrvrData, waitOp));
168 
169 finalize_it:
170 	RETiRet;
171 }
172 
173 
174 /* wait for IO to happen on one of our netstreams. iNumReady has
175  * the number of ready "sockets" after the call. This function blocks
176  * until some are ready. EAGAIN is retried.
177  */
178 static rsRetVal
Wait(nssel_t * pThis,int * piNumReady)179 Wait(nssel_t *pThis, int *piNumReady)
180 {
181 	DEFiRet;
182 	ISOBJ_TYPE_assert(pThis, nssel);
183 	assert(piNumReady != NULL);
184 	iRet = pThis->Drvr.Select(pThis->pDrvrData, piNumReady);
185 	RETiRet;
186 }
187 
188 
189 /* Check if a stream is ready for IO. *piNumReady contains the remaining number
190  * of ready streams. Note that this function may say the stream is not ready
191  * but still decrement *piNumReady. This can happen when (e.g. with TLS) the low
192  * level driver requires some IO which is hidden from the upper layer point of view.
193  * rgerhards, 2008-04-23
194  */
195 static rsRetVal
IsReady(nssel_t * pThis,netstrm_t * pStrm,nsdsel_waitOp_t waitOp,int * pbIsReady,int * piNumReady)196 IsReady(nssel_t *pThis, netstrm_t *pStrm, nsdsel_waitOp_t waitOp, int *pbIsReady,
197 int __attribute__((unused)) *piNumReady)
198 {
199 	DEFiRet;
200 	ISOBJ_TYPE_assert(pThis, nssel);
201 	ISOBJ_TYPE_assert(pStrm, netstrm);
202 	assert(pbIsReady != NULL);
203 	assert(piNumReady != NULL);
204 	iRet = pThis->Drvr.IsReady(pThis->pDrvrData, pStrm->pDrvrData, waitOp, pbIsReady);
205 	RETiRet;
206 }
207 
208 
209 /* queryInterface function */
210 BEGINobjQueryInterface(nssel)
211 CODESTARTobjQueryInterface(nssel)
212 	if(pIf->ifVersion != nsselCURR_IF_VERSION) {/* check for current version, increment on each change */
213 		ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
214 	}
215 
216 	/* ok, we have the right interface, so let's fill it
217 	 * Please note that we may also do some backwards-compatibility
218 	 * work here (if we can support an older interface version - that,
219 	 * of course, also affects the "if" above).
220 	 */
221 	pIf->Construct = nsselConstruct;
222 	pIf->ConstructFinalize = ConstructFinalize;
223 	pIf->Destruct = nsselDestruct;
224 	pIf->SetDrvrName = SetDrvrName;
225 	pIf->Add = Add;
226 	pIf->Wait = Wait;
227 	pIf->IsReady = IsReady;
228 finalize_it:
229 ENDobjQueryInterface(nssel)
230 
231 
232 /* exit our class
233  */
234 BEGINObjClassExit(nssel, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
235 CODESTARTObjClassExit(nssel)
236 	/* release objects we no longer need */
237 	objRelease(glbl, CORE_COMPONENT);
238 ENDObjClassExit(nssel)
239 
240 
241 /* Initialize the nssel class. Must be called as the very first method
242  * before anything else is called inside this class.
243  * rgerhards, 2008-02-19
244  */
245 BEGINObjClassInit(nssel, 1, OBJ_IS_CORE_MODULE) /* class, version */
246 	/* request objects we use */
247 	DBGPRINTF("doing nsselClassInit\n");
248 	CHKiRet(objUse(glbl, CORE_COMPONENT));
249 
250 	/* set our own handlers */
251 ENDObjClassInit(nssel)
252 /* vi:set ai:
253  */
254