xref: /aoo41x/main/sal/osl/unx/pipe.c (revision ebd402a9)
1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647f063dSAndrew Rist  * distributed with this work for additional information
6647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647f063dSAndrew Rist  *
11647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647f063dSAndrew Rist  *
13647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist  * software distributed under the License is distributed on an
15647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17647f063dSAndrew Rist  * specific language governing permissions and limitations
18647f063dSAndrew Rist  * under the License.
19647f063dSAndrew Rist  *
20647f063dSAndrew Rist  *************************************************************/
21647f063dSAndrew Rist 
22647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "system.h"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <osl/pipe.h>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir /*#include <osl/signal.h>*/
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir #include <osl/interlck.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "sockimpl.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #define PIPEDEFAULTPATH		"/tmp"
36cdf0e10cSrcweir #define PIPEALTERNATEPATH	"/var/tmp"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define PIPENAMEMASK	"OSL_PIPE_%s"
39cdf0e10cSrcweir #define SECPIPENAMEMASK	"OSL_PIPE_%s_%s"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir sal_Bool SAL_CALL osl_psz_getUserIdent(oslSecurity Security, sal_Char *pszIdent, sal_uInt32 nMax);
42cdf0e10cSrcweir oslPipe SAL_CALL osl_psz_createPipe(const sal_Char *pszPipeName, oslPipeOptions Options, oslSecurity Security);
43cdf0e10cSrcweir 
44cdf0e10cSrcweir /*#define DEBUG_OSL_PIPE*/
45cdf0e10cSrcweir /*#define TRACE_OSL_PIPE*/
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /*****************************************************************************/
49cdf0e10cSrcweir /* enum oslPipeError */
50cdf0e10cSrcweir /*****************************************************************************/
51cdf0e10cSrcweir 
52cdf0e10cSrcweir static struct
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	int            errcode;
55cdf0e10cSrcweir 	oslPipeError   error;
56cdf0e10cSrcweir } PipeError[]= {
57cdf0e10cSrcweir 	{ 0,			   osl_Pipe_E_None		    	},	/* no error */
58cdf0e10cSrcweir 	{ EPROTOTYPE,	   osl_Pipe_E_NoProtocol	    },	/* Protocol wrong type for socket */
59cdf0e10cSrcweir 	{ ENOPROTOOPT,	   osl_Pipe_E_NoProtocol	    },	/* Protocol not available */
60cdf0e10cSrcweir 	{ EPROTONOSUPPORT, osl_Pipe_E_NoProtocol		},	/* Protocol not supported */
61cdf0e10cSrcweir 	{ ESOCKTNOSUPPORT, osl_Pipe_E_NoProtocol 		},	/* Socket type not supported */
62cdf0e10cSrcweir 	{ EPFNOSUPPORT,	   osl_Pipe_E_NoProtocol     	},	/* Protocol family not supported */
63cdf0e10cSrcweir 	{ EAFNOSUPPORT,	   osl_Pipe_E_NoProtocol     	},	/* Address family not supported by */
64cdf0e10cSrcweir 														/* protocol family */
65cdf0e10cSrcweir 	{ ENETRESET,	   osl_Pipe_E_NetworkReset 		},	/* Network dropped connection because */
66cdf0e10cSrcweir 											 			/* of reset */
67cdf0e10cSrcweir 	{ ECONNABORTED,	   osl_Pipe_E_ConnectionAbort 	},	/* Software caused connection abort */
68cdf0e10cSrcweir 	{ ECONNRESET,	   osl_Pipe_E_ConnectionReset 	},	/* Connection reset by peer */
69cdf0e10cSrcweir 	{ ENOBUFS,		   osl_Pipe_E_NoBufferSpace 	},	/* No buffer space available */
70cdf0e10cSrcweir 	{ ETIMEDOUT,	   osl_Pipe_E_TimedOut 			},	/* Connection timed out */
71cdf0e10cSrcweir 	{ ECONNREFUSED,	   osl_Pipe_E_ConnectionRefused	},	/* Connection refused */
72cdf0e10cSrcweir 	{ -1,		   	   osl_Pipe_E_invalidError 		}
73cdf0e10cSrcweir };
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir /* map */
77cdf0e10cSrcweir /* mfe: NOT USED
78cdf0e10cSrcweir    static int osl_NativeFromPipeError(oslPipeError errorCode)
79cdf0e10cSrcweir    {
80cdf0e10cSrcweir    int i = 0;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir    while ((PipeError[i].error != osl_Pipe_E_invalidError) &&
83cdf0e10cSrcweir    (PipeError[i].error != errorCode)) i++;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir    return PipeError[i].errcode;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir    }
88cdf0e10cSrcweir */
89cdf0e10cSrcweir 
90cdf0e10cSrcweir /* reverse map */
osl_PipeErrorFromNative(int nativeType)91cdf0e10cSrcweir static oslPipeError osl_PipeErrorFromNative(int nativeType)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	int i = 0;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	while ((PipeError[i].error != osl_Pipe_E_invalidError) &&
96cdf0e10cSrcweir 		   (PipeError[i].errcode != nativeType)) i++;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	return PipeError[i].error;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
102cdf0e10cSrcweir /* macros */
103cdf0e10cSrcweir #define ERROR_TO_NATIVE(x)		osl_NativeFromPipeError(x)
104cdf0e10cSrcweir #define ERROR_FROM_NATIVE(y)	osl_PipeErrorFromNative(y)
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir /*****************************************************************************/
108cdf0e10cSrcweir /* osl_create/destroy-PipeImpl */
109cdf0e10cSrcweir /*****************************************************************************/
110cdf0e10cSrcweir 
__osl_createPipeImpl()111cdf0e10cSrcweir oslPipe __osl_createPipeImpl()
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	oslPipe pPipeImpl;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	pPipeImpl = (oslPipe)calloc(1, sizeof(struct oslPipeImpl));
116cdf0e10cSrcweir 	pPipeImpl->m_nRefCount =1;
117cdf0e10cSrcweir 	pPipeImpl->m_bClosed = sal_False;
118*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
119cdf0e10cSrcweir 	pPipeImpl->m_bIsInShutdown = sal_False;
120cdf0e10cSrcweir 	pPipeImpl->m_bIsAccepting = sal_False;
121cdf0e10cSrcweir #endif
122cdf0e10cSrcweir 	return pPipeImpl;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
__osl_destroyPipeImpl(oslPipe pImpl)125cdf0e10cSrcweir void __osl_destroyPipeImpl(oslPipe pImpl)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	if (pImpl != NULL)
128cdf0e10cSrcweir 		free(pImpl);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /*****************************************************************************/
133cdf0e10cSrcweir /* osl_createPipe  */
134cdf0e10cSrcweir /*****************************************************************************/
osl_createPipe(rtl_uString * ustrPipeName,oslPipeOptions Options,oslSecurity Security)135cdf0e10cSrcweir oslPipe SAL_CALL osl_createPipe(rtl_uString *ustrPipeName, oslPipeOptions Options, oslSecurity Security)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     oslPipe pPipe=0;
138cdf0e10cSrcweir     rtl_String* strPipeName=0;
139cdf0e10cSrcweir     sal_Char* pszPipeName=0;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     if ( ustrPipeName != 0 )
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         rtl_uString2String( &strPipeName,
144cdf0e10cSrcweir                             rtl_uString_getStr(ustrPipeName),
145cdf0e10cSrcweir                             rtl_uString_getLength(ustrPipeName),
146cdf0e10cSrcweir                             osl_getThreadTextEncoding(),
147cdf0e10cSrcweir                             OUSTRING_TO_OSTRING_CVTFLAGS );
148cdf0e10cSrcweir         pszPipeName = rtl_string_getStr(strPipeName);
149cdf0e10cSrcweir         pPipe = osl_psz_createPipe(pszPipeName, Options, Security);
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         if ( strPipeName != 0 )
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             rtl_string_release(strPipeName);
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return pPipe;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
osl_psz_createPipe(const sal_Char * pszPipeName,oslPipeOptions Options,oslSecurity Security)161cdf0e10cSrcweir oslPipe SAL_CALL osl_psz_createPipe(const sal_Char *pszPipeName, oslPipeOptions Options,
162cdf0e10cSrcweir                        oslSecurity Security)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	int    Flags;
165cdf0e10cSrcweir 	size_t	   len;
166cdf0e10cSrcweir 	struct sockaddr_un addr;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     sal_Char  	 name[PATH_MAX + 1];
169cdf0e10cSrcweir 	oslPipe  pPipe;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	if (access(PIPEDEFAULTPATH, R_OK|W_OK) == 0)
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir 		strncpy(name, PIPEDEFAULTPATH, sizeof(name));
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 	else
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir 		strncpy(name, PIPEALTERNATEPATH, sizeof(name));
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	strncat(name, "/", sizeof(name));
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	if (Security)
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir 		sal_Char Ident[256];
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         Ident[0] = '\0';
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		OSL_VERIFY(osl_psz_getUserIdent(Security, Ident, sizeof(Ident)));
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		snprintf(&name[strlen(name)], sizeof(name), SECPIPENAMEMASK, Ident, pszPipeName);
192cdf0e10cSrcweir 	}
193cdf0e10cSrcweir 	else
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		snprintf(&name[strlen(name)], sizeof(name), PIPENAMEMASK, pszPipeName);
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	/* alloc memory */
200cdf0e10cSrcweir 	pPipe= __osl_createPipeImpl();
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	/* create socket */
203cdf0e10cSrcweir 	pPipe->m_Socket = socket(AF_UNIX, SOCK_STREAM, 0);
204cdf0e10cSrcweir 	if ( pPipe->m_Socket < 0 )
205cdf0e10cSrcweir 	{
206cdf0e10cSrcweir 		OSL_TRACE("osl_createPipe socket failed. Errno: %d; %s\n",errno, strerror(errno));
207cdf0e10cSrcweir 		__osl_destroyPipeImpl(pPipe);
208cdf0e10cSrcweir 		return NULL;
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir /*    OSL_TRACE("osl_createPipe : new Pipe on fd %i\n",pPipe->m_Socket);*/
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	/* set close-on-exec flag */
214cdf0e10cSrcweir 	if ((Flags = fcntl(pPipe->m_Socket, F_GETFD, 0)) != -1)
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		Flags |= FD_CLOEXEC;
217cdf0e10cSrcweir 		if (fcntl(pPipe->m_Socket, F_SETFD, Flags) == -1)
218cdf0e10cSrcweir 		{
219cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed changing socket flags. Errno: %d; %s\n",errno,strerror(errno));
220cdf0e10cSrcweir 		}
221cdf0e10cSrcweir 	}
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	memset(&addr, 0, sizeof(addr));
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     OSL_TRACE("osl_createPipe : Pipe Name '%s'",name);
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	addr.sun_family = AF_UNIX;
228cdf0e10cSrcweir 	strncpy(addr.sun_path, name, sizeof(addr.sun_path));
229cdf0e10cSrcweir #if defined(FREEBSD)
230cdf0e10cSrcweir 	len = SUN_LEN(&addr);
231cdf0e10cSrcweir #else
232cdf0e10cSrcweir 	len = sizeof(addr);
233cdf0e10cSrcweir #endif
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	if ( Options & osl_Pipe_CREATE )
236cdf0e10cSrcweir 	{
237cdf0e10cSrcweir 		struct stat status;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		/* check if there exists an orphan filesystem entry */
240cdf0e10cSrcweir 		if ( ( stat(name, &status) == 0) &&
241cdf0e10cSrcweir 			 ( S_ISSOCK(status.st_mode) || S_ISFIFO(status.st_mode) ) )
242cdf0e10cSrcweir 		{
243cdf0e10cSrcweir 			if ( connect(pPipe->m_Socket,(struct sockaddr *)&addr,len) >= 0 )
244cdf0e10cSrcweir 			{
245cdf0e10cSrcweir 				OSL_TRACE("osl_createPipe : Pipe already in use. Errno: %d; %s\n",errno,strerror(errno));
246cdf0e10cSrcweir 				close (pPipe->m_Socket);
247cdf0e10cSrcweir 				__osl_destroyPipeImpl(pPipe);
248cdf0e10cSrcweir 				return NULL;
249cdf0e10cSrcweir 			}
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 			unlink(name);
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		/* ok, fs clean */
255cdf0e10cSrcweir 		if ( bind(pPipe->m_Socket, (struct sockaddr *)&addr, len) < 0 )
256cdf0e10cSrcweir 		{
257cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe : failed to bind socket. Errno: %d; %s\n",errno,strerror(errno));
258cdf0e10cSrcweir 			close (pPipe->m_Socket);
259cdf0e10cSrcweir 			__osl_destroyPipeImpl(pPipe);
260cdf0e10cSrcweir 			return NULL;
261cdf0e10cSrcweir 		}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		/*	Only give access to all if no security handle was specified, otherwise security
264cdf0e10cSrcweir 			depends on umask */
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 		if ( !Security )
267cdf0e10cSrcweir 			chmod(name,S_IRWXU | S_IRWXG |S_IRWXO);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		strncpy(pPipe->m_Name, name, sizeof(pPipe->m_Name));
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 		if ( listen(pPipe->m_Socket, 5) < 0 )
273cdf0e10cSrcweir 		{
274cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed to listen. Errno: %d; %s\n",errno,strerror(errno));
275cdf0e10cSrcweir 			unlink(name);	/* remove filesystem entry */
276cdf0e10cSrcweir 			close (pPipe->m_Socket);
277cdf0e10cSrcweir 			__osl_destroyPipeImpl(pPipe);
278cdf0e10cSrcweir 			return NULL;
279cdf0e10cSrcweir 		}
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 		return (pPipe);
282cdf0e10cSrcweir 	}
283cdf0e10cSrcweir 	else
284cdf0e10cSrcweir 	{   /* osl_pipe_OPEN */
285cdf0e10cSrcweir 		if ( access(name, F_OK) != -1 )
286cdf0e10cSrcweir 		{
287cdf0e10cSrcweir 			if ( connect( pPipe->m_Socket, (struct sockaddr *)&addr, len) >= 0 )
288cdf0e10cSrcweir 			{
289cdf0e10cSrcweir 				return (pPipe);
290cdf0e10cSrcweir 			}
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed to connect. Errno: %d; %s\n",errno,strerror(errno));
293cdf0e10cSrcweir 		}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 		close (pPipe->m_Socket);
296cdf0e10cSrcweir 		__osl_destroyPipeImpl(pPipe);
297cdf0e10cSrcweir 		return NULL;
298cdf0e10cSrcweir 	}
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
osl_acquirePipe(oslPipe pPipe)301cdf0e10cSrcweir void SAL_CALL osl_acquirePipe( oslPipe pPipe )
302cdf0e10cSrcweir {
303cdf0e10cSrcweir 	osl_incrementInterlockedCount( &(pPipe->m_nRefCount) );
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
osl_releasePipe(oslPipe pPipe)306cdf0e10cSrcweir void SAL_CALL osl_releasePipe( oslPipe pPipe )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 	if( 0 == pPipe )
310cdf0e10cSrcweir 		return;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	if( 0 == osl_decrementInterlockedCount( &(pPipe->m_nRefCount) ) )
313cdf0e10cSrcweir 	{
314cdf0e10cSrcweir 		if( ! pPipe->m_bClosed )
315cdf0e10cSrcweir 			osl_closePipe( pPipe );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 		__osl_destroyPipeImpl( pPipe );
318cdf0e10cSrcweir 	}
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
osl_closePipe(oslPipe pPipe)321cdf0e10cSrcweir void SAL_CALL osl_closePipe( oslPipe pPipe )
322cdf0e10cSrcweir {
323cdf0e10cSrcweir     int nRet;
324*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
325cdf0e10cSrcweir     size_t	   len;
326cdf0e10cSrcweir 	struct sockaddr_un addr;
327cdf0e10cSrcweir     int fd;
328cdf0e10cSrcweir #endif
329cdf0e10cSrcweir     int ConnFD;
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 	if( ! pPipe )
332cdf0e10cSrcweir 	{
333cdf0e10cSrcweir 		return;
334cdf0e10cSrcweir 	}
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 	if( pPipe->m_bClosed )
337cdf0e10cSrcweir 	{
338cdf0e10cSrcweir 		return;
339cdf0e10cSrcweir 	}
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     ConnFD = pPipe->m_Socket;
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	/*
344*ebd402a9SDamjan Jovanovic 	  Thread does not return from accept on some operating systems, so
345cdf0e10cSrcweir 	  connect to the accepting pipe
346cdf0e10cSrcweir 	 */
347*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
348cdf0e10cSrcweir     if ( pPipe->m_bIsAccepting )
349cdf0e10cSrcweir     {
350cdf0e10cSrcweir         pPipe->m_bIsInShutdown = sal_True;
351cdf0e10cSrcweir         pPipe->m_Socket = -1;
352cdf0e10cSrcweir         fd = socket(AF_UNIX, SOCK_STREAM, 0);
353cdf0e10cSrcweir         memset(&addr, 0, sizeof(addr));
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         OSL_TRACE("osl_destroyPipe : Pipe Name '%s'",pPipe->m_Name);
356cdf0e10cSrcweir 
357cdf0e10cSrcweir         addr.sun_family = AF_UNIX;
358cdf0e10cSrcweir         strncpy(addr.sun_path, pPipe->m_Name, sizeof(addr.sun_path));
359*ebd402a9SDamjan Jovanovic #if defined(FREEBSD)
360*ebd402a9SDamjan Jovanovic         len = SUN_LEN(&addr);
361*ebd402a9SDamjan Jovanovic #else
362*ebd402a9SDamjan Jovanovic         len = sizeof(addr);
363*ebd402a9SDamjan Jovanovic #endif
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         nRet = connect( fd, (struct sockaddr *)&addr, len);
366cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
367cdf0e10cSrcweir         if ( nRet < 0 )
368cdf0e10cSrcweir         {
369cdf0e10cSrcweir             perror("connect in osl_destroyPipe");
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
372cdf0e10cSrcweir         close(fd);
373cdf0e10cSrcweir     }
374*ebd402a9SDamjan Jovanovic #endif /* CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT */
375cdf0e10cSrcweir 
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 	nRet = shutdown(ConnFD, 2);
378cdf0e10cSrcweir     if ( nRet < 0 )
379cdf0e10cSrcweir     {
380cdf0e10cSrcweir         OSL_TRACE("shutdown in destroyPipe failed : '%s'\n",strerror(errno));
381cdf0e10cSrcweir     }
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 	nRet = close(ConnFD);
384cdf0e10cSrcweir     if ( nRet < 0 )
385cdf0e10cSrcweir     {
386cdf0e10cSrcweir         OSL_TRACE("close in destroyPipe failed : '%s'\n",strerror(errno));
387cdf0e10cSrcweir     }
388cdf0e10cSrcweir 	/* remove filesystem entry */
389cdf0e10cSrcweir 	if ( strlen(pPipe->m_Name) > 0 )
390cdf0e10cSrcweir 	{
391cdf0e10cSrcweir 		unlink(pPipe->m_Name);
392cdf0e10cSrcweir 	}
393cdf0e10cSrcweir 	pPipe->m_bClosed = sal_True;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir /*      OSL_TRACE("Out osl_destroyPipe");     */
396cdf0e10cSrcweir }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 
399cdf0e10cSrcweir /*****************************************************************************/
400cdf0e10cSrcweir /* osl_acceptPipe  */
401cdf0e10cSrcweir /*****************************************************************************/
osl_acceptPipe(oslPipe pPipe)402cdf0e10cSrcweir oslPipe SAL_CALL osl_acceptPipe(oslPipe pPipe)
403cdf0e10cSrcweir {
404cdf0e10cSrcweir 	int     s, flags;
405cdf0e10cSrcweir 	oslPipe pAcceptedPipe;
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
408cdf0e10cSrcweir 	if ( pPipe == 0 )
409cdf0e10cSrcweir 	{
410cdf0e10cSrcweir 		return NULL;
411cdf0e10cSrcweir 	}
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 	OSL_ASSERT(strlen(pPipe->m_Name) > 0);
414cdf0e10cSrcweir 
415*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
416cdf0e10cSrcweir     pPipe->m_bIsAccepting = sal_True;
417cdf0e10cSrcweir #endif
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     s = accept(pPipe->m_Socket, NULL, NULL);
420cdf0e10cSrcweir 
421*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
422cdf0e10cSrcweir     pPipe->m_bIsAccepting = sal_False;
423cdf0e10cSrcweir #endif
424cdf0e10cSrcweir 
425cdf0e10cSrcweir     if (s < 0)
426cdf0e10cSrcweir 	{
427cdf0e10cSrcweir         OSL_TRACE("osl_acceptPipe : accept error '%s'", strerror(errno));
428cdf0e10cSrcweir 		return NULL;
429cdf0e10cSrcweir 	}
430cdf0e10cSrcweir 
431*ebd402a9SDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
432cdf0e10cSrcweir     if ( pPipe->m_bIsInShutdown  )
433cdf0e10cSrcweir     {
434cdf0e10cSrcweir         close(s);
435cdf0e10cSrcweir         return NULL;
436cdf0e10cSrcweir     }
437*ebd402a9SDamjan Jovanovic #endif /* CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT */
438cdf0e10cSrcweir     else
439cdf0e10cSrcweir 	{
440cdf0e10cSrcweir 		/* alloc memory */
441cdf0e10cSrcweir 		pAcceptedPipe= __osl_createPipeImpl();
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 		OSL_ASSERT(pAcceptedPipe);
444cdf0e10cSrcweir 		if(pAcceptedPipe==NULL)
445cdf0e10cSrcweir 		{
446cdf0e10cSrcweir 			close(s);
447cdf0e10cSrcweir 			return NULL;
448cdf0e10cSrcweir 		}
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 		/* set close-on-exec flag */
451cdf0e10cSrcweir 		if (!((flags = fcntl(s, F_GETFD, 0)) < 0))
452cdf0e10cSrcweir 		{
453cdf0e10cSrcweir 			flags |= FD_CLOEXEC;
454cdf0e10cSrcweir 			if (fcntl(s, F_SETFD, flags) < 0)
455cdf0e10cSrcweir 			{
456cdf0e10cSrcweir 				OSL_TRACE("osl_acceptPipe: error changing socket flags. "
457cdf0e10cSrcweir 						  "Errno: %d; %s",errno,strerror(errno));
458cdf0e10cSrcweir 			}
459cdf0e10cSrcweir 		}
460cdf0e10cSrcweir 
461cdf0e10cSrcweir 		pAcceptedPipe->m_Socket = s;
462cdf0e10cSrcweir 	}
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 	return pAcceptedPipe;
465cdf0e10cSrcweir }
466cdf0e10cSrcweir 
467cdf0e10cSrcweir /*****************************************************************************/
468cdf0e10cSrcweir /* osl_receivePipe  */
469cdf0e10cSrcweir /*****************************************************************************/
osl_receivePipe(oslPipe pPipe,void * pBuffer,sal_Int32 BytesToRead)470cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receivePipe(oslPipe pPipe,
471cdf0e10cSrcweir 					    void* pBuffer,
472cdf0e10cSrcweir 					    sal_Int32 BytesToRead)
473cdf0e10cSrcweir {
474cdf0e10cSrcweir     int nRet = 0;
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 	if ( pPipe == 0 )
479cdf0e10cSrcweir 	{
480cdf0e10cSrcweir         OSL_TRACE("osl_receivePipe : Invalid socket");
481cdf0e10cSrcweir         errno=EINVAL;
482cdf0e10cSrcweir 		return -1;
483cdf0e10cSrcweir 	}
484cdf0e10cSrcweir 
485cdf0e10cSrcweir     nRet = recv(pPipe->m_Socket,
486cdf0e10cSrcweir   				(sal_Char*)pBuffer,
487cdf0e10cSrcweir   				BytesToRead, 0);
488cdf0e10cSrcweir 
489cdf0e10cSrcweir     if ( nRet < 0 )
490cdf0e10cSrcweir     {
491cdf0e10cSrcweir         OSL_TRACE("osl_receivePipe failed : %i '%s'",nRet,strerror(errno));
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir 
494cdf0e10cSrcweir   	return nRet;
495cdf0e10cSrcweir }
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 
498cdf0e10cSrcweir /*****************************************************************************/
499cdf0e10cSrcweir /* osl_sendPipe  */
500cdf0e10cSrcweir /*****************************************************************************/
osl_sendPipe(oslPipe pPipe,const void * pBuffer,sal_Int32 BytesToSend)501cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendPipe(oslPipe pPipe,
502cdf0e10cSrcweir 				       const void* pBuffer,
503cdf0e10cSrcweir 				       sal_Int32 BytesToSend)
504cdf0e10cSrcweir {
505cdf0e10cSrcweir     int nRet=0;
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 	if ( pPipe == 0 )
510cdf0e10cSrcweir 	{
511cdf0e10cSrcweir         OSL_TRACE("osl_sendPipe : Invalid socket");
512cdf0e10cSrcweir         errno=EINVAL;
513cdf0e10cSrcweir 		return -1;
514cdf0e10cSrcweir 	}
515cdf0e10cSrcweir 
516cdf0e10cSrcweir     nRet = send(pPipe->m_Socket,
517cdf0e10cSrcweir   				(sal_Char*)pBuffer,
518cdf0e10cSrcweir   				BytesToSend, 0);
519cdf0e10cSrcweir 
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     if ( nRet <= 0 )
522cdf0e10cSrcweir     {
523cdf0e10cSrcweir         OSL_TRACE("osl_sendPipe failed : %i '%s'",nRet,strerror(errno));
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir  	return nRet;
527cdf0e10cSrcweir }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 
530cdf0e10cSrcweir /*****************************************************************************/
531cdf0e10cSrcweir /* osl_getLastPipeError  */
532cdf0e10cSrcweir /*****************************************************************************/
osl_getLastPipeError(oslPipe pPipe)533cdf0e10cSrcweir oslPipeError SAL_CALL osl_getLastPipeError(oslPipe pPipe)
534cdf0e10cSrcweir {
535cdf0e10cSrcweir     (void) pPipe; /* unused */
536cdf0e10cSrcweir 	return ERROR_FROM_NATIVE(errno);
537cdf0e10cSrcweir }
538cdf0e10cSrcweir 
539cdf0e10cSrcweir 
osl_writePipe(oslPipe pPipe,const void * pBuffer,sal_Int32 n)540cdf0e10cSrcweir sal_Int32 SAL_CALL osl_writePipe( oslPipe pPipe, const void *pBuffer , sal_Int32 n )
541cdf0e10cSrcweir {
542cdf0e10cSrcweir 	/* loop until all desired bytes were send or an error occured */
543cdf0e10cSrcweir 	sal_Int32 BytesSend= 0;
544cdf0e10cSrcweir 	sal_Int32 BytesToSend= n;
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
547cdf0e10cSrcweir 	while (BytesToSend > 0)
548cdf0e10cSrcweir 	{
549cdf0e10cSrcweir 		sal_Int32 RetVal;
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 		RetVal= osl_sendPipe(pPipe, pBuffer, BytesToSend);
552cdf0e10cSrcweir 
553cdf0e10cSrcweir 		/* error occured? */
554cdf0e10cSrcweir 		if(RetVal <= 0)
555cdf0e10cSrcweir 		{
556cdf0e10cSrcweir 			break;
557cdf0e10cSrcweir 		}
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 		BytesToSend -= RetVal;
560cdf0e10cSrcweir 		BytesSend += RetVal;
561cdf0e10cSrcweir 		pBuffer= (sal_Char*)pBuffer + RetVal;
562cdf0e10cSrcweir 	}
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 	return BytesSend;
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
osl_readPipe(oslPipe pPipe,void * pBuffer,sal_Int32 n)567cdf0e10cSrcweir sal_Int32 SAL_CALL osl_readPipe( oslPipe pPipe, void *pBuffer , sal_Int32 n )
568cdf0e10cSrcweir {
569cdf0e10cSrcweir 	/* loop until all desired bytes were read or an error occured */
570cdf0e10cSrcweir 	sal_Int32 BytesRead= 0;
571cdf0e10cSrcweir 	sal_Int32 BytesToRead= n;
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 	OSL_ASSERT( pPipe );
574cdf0e10cSrcweir 	while (BytesToRead > 0)
575cdf0e10cSrcweir 	{
576cdf0e10cSrcweir 		sal_Int32 RetVal;
577cdf0e10cSrcweir 		RetVal= osl_receivePipe(pPipe, pBuffer, BytesToRead);
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 		/* error occured? */
580cdf0e10cSrcweir 		if(RetVal <= 0)
581cdf0e10cSrcweir 		{
582cdf0e10cSrcweir 			break;
583cdf0e10cSrcweir 		}
584cdf0e10cSrcweir 
585cdf0e10cSrcweir 		BytesToRead -= RetVal;
586cdf0e10cSrcweir 		BytesRead += RetVal;
587cdf0e10cSrcweir 		pBuffer= (sal_Char*)pBuffer + RetVal;
588cdf0e10cSrcweir 	}
589cdf0e10cSrcweir 	return BytesRead;
590cdf0e10cSrcweir }
591cdf0e10cSrcweir 
592cdf0e10cSrcweir 
593