xref: /aoo42x/main/sal/osl/unx/pipe.c (revision c2209740)
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 
38*c2209740Struckman #define PIPENAMEMASK	"%s/OSL_PIPE_%s"
39*c2209740Struckman #define SECPIPENAMEMASK	"%s/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 */
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 
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;
1185691909dSDamjan 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 
125cdf0e10cSrcweir void __osl_destroyPipeImpl(oslPipe pImpl)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	if (pImpl != NULL)
128cdf0e10cSrcweir 		free(pImpl);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /*****************************************************************************/
133cdf0e10cSrcweir /* osl_createPipe  */
134cdf0e10cSrcweir /*****************************************************************************/
135cdf0e10cSrcweir oslPipe SAL_CALL osl_createPipe(rtl_uString *ustrPipeName, oslPipeOptions Options, oslSecurity Security)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     oslPipe pPipe=0;
138509a48ffSpfg     rtl_String* strPipeName=NULL;
139509a48ffSpfg     sal_Char* pszPipeName=NULL;
140cdf0e10cSrcweir 
141509a48ffSpfg     if ( ustrPipeName != NULL )
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 
151509a48ffSpfg         if ( strPipeName != NULL )
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             rtl_string_release(strPipeName);
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return pPipe;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
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 
168*c2209740Struckman 	sal_Char  	 name[PATH_MAX + 1];
169*c2209740Struckman 	const sal_Char 	 *pPath;
170cdf0e10cSrcweir 	oslPipe  pPipe;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	if (access(PIPEDEFAULTPATH, R_OK|W_OK) == 0)
173*c2209740Struckman 	{
174*c2209740Struckman 		pPath = PIPEDEFAULTPATH;
175*c2209740Struckman 	}
176cdf0e10cSrcweir 	else
177*c2209740Struckman 	{
178*c2209740Struckman 		pPath = PIPEALTERNATEPATH;
179*c2209740Struckman 	}
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	if (Security)
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		sal_Char Ident[256];
184cdf0e10cSrcweir 
185*c2209740Struckman 		Ident[0] = '\0';
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		OSL_VERIFY(osl_psz_getUserIdent(Security, Ident, sizeof(Ident)));
188cdf0e10cSrcweir 
189*c2209740Struckman 		snprintf(name, sizeof(name), SECPIPENAMEMASK, pPath, Ident, pszPipeName);
190cdf0e10cSrcweir 	}
191cdf0e10cSrcweir 	else
192cdf0e10cSrcweir 	{
193*c2209740Struckman 		snprintf(name, sizeof(name), PIPENAMEMASK, pPath, pszPipeName);
194cdf0e10cSrcweir 	}
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	/* alloc memory */
198cdf0e10cSrcweir 	pPipe= __osl_createPipeImpl();
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	/* create socket */
201cdf0e10cSrcweir 	pPipe->m_Socket = socket(AF_UNIX, SOCK_STREAM, 0);
202cdf0e10cSrcweir 	if ( pPipe->m_Socket < 0 )
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		OSL_TRACE("osl_createPipe socket failed. Errno: %d; %s\n",errno, strerror(errno));
205cdf0e10cSrcweir 		__osl_destroyPipeImpl(pPipe);
206cdf0e10cSrcweir 		return NULL;
207cdf0e10cSrcweir 	}
208cdf0e10cSrcweir 
209cdf0e10cSrcweir /*    OSL_TRACE("osl_createPipe : new Pipe on fd %i\n",pPipe->m_Socket);*/
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	/* set close-on-exec flag */
212cdf0e10cSrcweir 	if ((Flags = fcntl(pPipe->m_Socket, F_GETFD, 0)) != -1)
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		Flags |= FD_CLOEXEC;
215cdf0e10cSrcweir 		if (fcntl(pPipe->m_Socket, F_SETFD, Flags) == -1)
216cdf0e10cSrcweir 		{
217cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed changing socket flags. Errno: %d; %s\n",errno,strerror(errno));
218cdf0e10cSrcweir 		}
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	memset(&addr, 0, sizeof(addr));
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     OSL_TRACE("osl_createPipe : Pipe Name '%s'",name);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	addr.sun_family = AF_UNIX;
226cdf0e10cSrcweir 	strncpy(addr.sun_path, name, sizeof(addr.sun_path));
227cdf0e10cSrcweir #if defined(FREEBSD)
228cdf0e10cSrcweir 	len = SUN_LEN(&addr);
229cdf0e10cSrcweir #else
230cdf0e10cSrcweir 	len = sizeof(addr);
231cdf0e10cSrcweir #endif
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	if ( Options & osl_Pipe_CREATE )
234cdf0e10cSrcweir 	{
235cdf0e10cSrcweir 		struct stat status;
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		/* check if there exists an orphan filesystem entry */
238cdf0e10cSrcweir 		if ( ( stat(name, &status) == 0) &&
239cdf0e10cSrcweir 			 ( S_ISSOCK(status.st_mode) || S_ISFIFO(status.st_mode) ) )
240cdf0e10cSrcweir 		{
241cdf0e10cSrcweir 			if ( connect(pPipe->m_Socket,(struct sockaddr *)&addr,len) >= 0 )
242cdf0e10cSrcweir 			{
243cdf0e10cSrcweir 				OSL_TRACE("osl_createPipe : Pipe already in use. Errno: %d; %s\n",errno,strerror(errno));
244cdf0e10cSrcweir 				close (pPipe->m_Socket);
245cdf0e10cSrcweir 				__osl_destroyPipeImpl(pPipe);
246cdf0e10cSrcweir 				return NULL;
247cdf0e10cSrcweir 			}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 			unlink(name);
250cdf0e10cSrcweir 		}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 		/* ok, fs clean */
253cdf0e10cSrcweir 		if ( bind(pPipe->m_Socket, (struct sockaddr *)&addr, len) < 0 )
254cdf0e10cSrcweir 		{
255cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe : failed to bind socket. Errno: %d; %s\n",errno,strerror(errno));
256cdf0e10cSrcweir 			close (pPipe->m_Socket);
257cdf0e10cSrcweir 			__osl_destroyPipeImpl(pPipe);
258cdf0e10cSrcweir 			return NULL;
259cdf0e10cSrcweir 		}
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 		/*	Only give access to all if no security handle was specified, otherwise security
262cdf0e10cSrcweir 			depends on umask */
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 		if ( !Security )
265cdf0e10cSrcweir 			chmod(name,S_IRWXU | S_IRWXG |S_IRWXO);
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 		strncpy(pPipe->m_Name, name, sizeof(pPipe->m_Name));
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		if ( listen(pPipe->m_Socket, 5) < 0 )
271cdf0e10cSrcweir 		{
272cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed to listen. Errno: %d; %s\n",errno,strerror(errno));
273cdf0e10cSrcweir 			unlink(name);	/* remove filesystem entry */
274cdf0e10cSrcweir 			close (pPipe->m_Socket);
275cdf0e10cSrcweir 			__osl_destroyPipeImpl(pPipe);
276cdf0e10cSrcweir 			return NULL;
277cdf0e10cSrcweir 		}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 		return (pPipe);
280cdf0e10cSrcweir 	}
281cdf0e10cSrcweir 	else
282cdf0e10cSrcweir 	{   /* osl_pipe_OPEN */
283cdf0e10cSrcweir 		if ( access(name, F_OK) != -1 )
284cdf0e10cSrcweir 		{
285cdf0e10cSrcweir 			if ( connect( pPipe->m_Socket, (struct sockaddr *)&addr, len) >= 0 )
286cdf0e10cSrcweir 			{
287cdf0e10cSrcweir 				return (pPipe);
288cdf0e10cSrcweir 			}
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 			OSL_TRACE("osl_createPipe failed to connect. Errno: %d; %s\n",errno,strerror(errno));
291cdf0e10cSrcweir 		}
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 		close (pPipe->m_Socket);
294cdf0e10cSrcweir 		__osl_destroyPipeImpl(pPipe);
295cdf0e10cSrcweir 		return NULL;
296cdf0e10cSrcweir 	}
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir void SAL_CALL osl_acquirePipe( oslPipe pPipe )
300cdf0e10cSrcweir {
301cdf0e10cSrcweir 	osl_incrementInterlockedCount( &(pPipe->m_nRefCount) );
302cdf0e10cSrcweir }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir void SAL_CALL osl_releasePipe( oslPipe pPipe )
305cdf0e10cSrcweir {
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	if( 0 == pPipe )
308cdf0e10cSrcweir 		return;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 	if( 0 == osl_decrementInterlockedCount( &(pPipe->m_nRefCount) ) )
311cdf0e10cSrcweir 	{
312cdf0e10cSrcweir 		if( ! pPipe->m_bClosed )
313cdf0e10cSrcweir 			osl_closePipe( pPipe );
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 		__osl_destroyPipeImpl( pPipe );
316cdf0e10cSrcweir 	}
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir void SAL_CALL osl_closePipe( oslPipe pPipe )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir     int nRet;
3225691909dSDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
323cdf0e10cSrcweir     size_t	   len;
324cdf0e10cSrcweir 	struct sockaddr_un addr;
325cdf0e10cSrcweir     int fd;
326cdf0e10cSrcweir #endif
327cdf0e10cSrcweir     int ConnFD;
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 	if( ! pPipe )
330cdf0e10cSrcweir 	{
331cdf0e10cSrcweir 		return;
332cdf0e10cSrcweir 	}
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 	if( pPipe->m_bClosed )
335cdf0e10cSrcweir 	{
336cdf0e10cSrcweir 		return;
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     ConnFD = pPipe->m_Socket;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 	/*
3425691909dSDamjan Jovanovic 	  Thread does not return from accept on some operating systems, so
343cdf0e10cSrcweir 	  connect to the accepting pipe
344cdf0e10cSrcweir 	 */
3455691909dSDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
346cdf0e10cSrcweir     if ( pPipe->m_bIsAccepting )
347cdf0e10cSrcweir     {
348cdf0e10cSrcweir         pPipe->m_bIsInShutdown = sal_True;
349cdf0e10cSrcweir         pPipe->m_Socket = -1;
350cdf0e10cSrcweir         fd = socket(AF_UNIX, SOCK_STREAM, 0);
351cdf0e10cSrcweir         memset(&addr, 0, sizeof(addr));
352cdf0e10cSrcweir 
353cdf0e10cSrcweir         OSL_TRACE("osl_destroyPipe : Pipe Name '%s'",pPipe->m_Name);
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         addr.sun_family = AF_UNIX;
356cdf0e10cSrcweir         strncpy(addr.sun_path, pPipe->m_Name, sizeof(addr.sun_path));
3575691909dSDamjan Jovanovic #if defined(FREEBSD)
3585691909dSDamjan Jovanovic         len = SUN_LEN(&addr);
3595691909dSDamjan Jovanovic #else
3605691909dSDamjan Jovanovic         len = sizeof(addr);
3615691909dSDamjan Jovanovic #endif
362cdf0e10cSrcweir 
363cdf0e10cSrcweir         nRet = connect( fd, (struct sockaddr *)&addr, len);
364cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
365cdf0e10cSrcweir         if ( nRet < 0 )
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             perror("connect in osl_destroyPipe");
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */
370cdf0e10cSrcweir         close(fd);
371cdf0e10cSrcweir     }
3725691909dSDamjan Jovanovic #endif /* CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT */
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 	nRet = shutdown(ConnFD, 2);
376cdf0e10cSrcweir     if ( nRet < 0 )
377cdf0e10cSrcweir     {
378cdf0e10cSrcweir         OSL_TRACE("shutdown in destroyPipe failed : '%s'\n",strerror(errno));
379cdf0e10cSrcweir     }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 	nRet = close(ConnFD);
382cdf0e10cSrcweir     if ( nRet < 0 )
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir         OSL_TRACE("close in destroyPipe failed : '%s'\n",strerror(errno));
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir 	/* remove filesystem entry */
387cdf0e10cSrcweir 	if ( strlen(pPipe->m_Name) > 0 )
388cdf0e10cSrcweir 	{
389cdf0e10cSrcweir 		unlink(pPipe->m_Name);
390cdf0e10cSrcweir 	}
391cdf0e10cSrcweir 	pPipe->m_bClosed = sal_True;
392cdf0e10cSrcweir 
393cdf0e10cSrcweir /*      OSL_TRACE("Out osl_destroyPipe");     */
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 
397cdf0e10cSrcweir /*****************************************************************************/
398cdf0e10cSrcweir /* osl_acceptPipe  */
399cdf0e10cSrcweir /*****************************************************************************/
400cdf0e10cSrcweir oslPipe SAL_CALL osl_acceptPipe(oslPipe pPipe)
401cdf0e10cSrcweir {
402cdf0e10cSrcweir 	int     s, flags;
403cdf0e10cSrcweir 	oslPipe pAcceptedPipe;
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
406cdf0e10cSrcweir 	if ( pPipe == 0 )
407cdf0e10cSrcweir 	{
408cdf0e10cSrcweir 		return NULL;
409cdf0e10cSrcweir 	}
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 	OSL_ASSERT(strlen(pPipe->m_Name) > 0);
412cdf0e10cSrcweir 
4135691909dSDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
414cdf0e10cSrcweir     pPipe->m_bIsAccepting = sal_True;
415cdf0e10cSrcweir #endif
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     s = accept(pPipe->m_Socket, NULL, NULL);
418cdf0e10cSrcweir 
4195691909dSDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
420cdf0e10cSrcweir     pPipe->m_bIsAccepting = sal_False;
421cdf0e10cSrcweir #endif
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     if (s < 0)
424cdf0e10cSrcweir 	{
425cdf0e10cSrcweir         OSL_TRACE("osl_acceptPipe : accept error '%s'", strerror(errno));
426cdf0e10cSrcweir 		return NULL;
427cdf0e10cSrcweir 	}
428cdf0e10cSrcweir 
4295691909dSDamjan Jovanovic #if CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT
430cdf0e10cSrcweir     if ( pPipe->m_bIsInShutdown  )
431cdf0e10cSrcweir     {
432cdf0e10cSrcweir         close(s);
433cdf0e10cSrcweir         return NULL;
434cdf0e10cSrcweir     }
4355691909dSDamjan Jovanovic #endif /* CLOSESOCKET_DOESNT_WAKE_UP_ACCEPT */
436cdf0e10cSrcweir     else
437cdf0e10cSrcweir 	{
438cdf0e10cSrcweir 		/* alloc memory */
439cdf0e10cSrcweir 		pAcceptedPipe= __osl_createPipeImpl();
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 		OSL_ASSERT(pAcceptedPipe);
442cdf0e10cSrcweir 		if(pAcceptedPipe==NULL)
443cdf0e10cSrcweir 		{
444cdf0e10cSrcweir 			close(s);
445cdf0e10cSrcweir 			return NULL;
446cdf0e10cSrcweir 		}
447cdf0e10cSrcweir 
448cdf0e10cSrcweir 		/* set close-on-exec flag */
449cdf0e10cSrcweir 		if (!((flags = fcntl(s, F_GETFD, 0)) < 0))
450cdf0e10cSrcweir 		{
451cdf0e10cSrcweir 			flags |= FD_CLOEXEC;
452cdf0e10cSrcweir 			if (fcntl(s, F_SETFD, flags) < 0)
453cdf0e10cSrcweir 			{
454cdf0e10cSrcweir 				OSL_TRACE("osl_acceptPipe: error changing socket flags. "
455cdf0e10cSrcweir 						  "Errno: %d; %s",errno,strerror(errno));
456cdf0e10cSrcweir 			}
457cdf0e10cSrcweir 		}
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 		pAcceptedPipe->m_Socket = s;
460cdf0e10cSrcweir 	}
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 	return pAcceptedPipe;
463cdf0e10cSrcweir }
464cdf0e10cSrcweir 
465cdf0e10cSrcweir /*****************************************************************************/
466cdf0e10cSrcweir /* osl_receivePipe  */
467cdf0e10cSrcweir /*****************************************************************************/
468cdf0e10cSrcweir sal_Int32 SAL_CALL osl_receivePipe(oslPipe pPipe,
469cdf0e10cSrcweir 					    void* pBuffer,
470cdf0e10cSrcweir 					    sal_Int32 BytesToRead)
471cdf0e10cSrcweir {
472cdf0e10cSrcweir     int nRet = 0;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	if ( pPipe == 0 )
477cdf0e10cSrcweir 	{
478cdf0e10cSrcweir         OSL_TRACE("osl_receivePipe : Invalid socket");
479cdf0e10cSrcweir         errno=EINVAL;
480cdf0e10cSrcweir 		return -1;
481cdf0e10cSrcweir 	}
482cdf0e10cSrcweir 
483cdf0e10cSrcweir     nRet = recv(pPipe->m_Socket,
484cdf0e10cSrcweir   				(sal_Char*)pBuffer,
485cdf0e10cSrcweir   				BytesToRead, 0);
486cdf0e10cSrcweir 
487cdf0e10cSrcweir     if ( nRet < 0 )
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         OSL_TRACE("osl_receivePipe failed : %i '%s'",nRet,strerror(errno));
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir   	return nRet;
493cdf0e10cSrcweir }
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 
496cdf0e10cSrcweir /*****************************************************************************/
497cdf0e10cSrcweir /* osl_sendPipe  */
498cdf0e10cSrcweir /*****************************************************************************/
499cdf0e10cSrcweir sal_Int32 SAL_CALL osl_sendPipe(oslPipe pPipe,
500cdf0e10cSrcweir 				       const void* pBuffer,
501cdf0e10cSrcweir 				       sal_Int32 BytesToSend)
502cdf0e10cSrcweir {
503cdf0e10cSrcweir     int nRet=0;
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 	if ( pPipe == 0 )
508cdf0e10cSrcweir 	{
509cdf0e10cSrcweir         OSL_TRACE("osl_sendPipe : Invalid socket");
510cdf0e10cSrcweir         errno=EINVAL;
511cdf0e10cSrcweir 		return -1;
512cdf0e10cSrcweir 	}
513cdf0e10cSrcweir 
514cdf0e10cSrcweir     nRet = send(pPipe->m_Socket,
515cdf0e10cSrcweir   				(sal_Char*)pBuffer,
516cdf0e10cSrcweir   				BytesToSend, 0);
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 
519cdf0e10cSrcweir     if ( nRet <= 0 )
520cdf0e10cSrcweir     {
521cdf0e10cSrcweir         OSL_TRACE("osl_sendPipe failed : %i '%s'",nRet,strerror(errno));
522cdf0e10cSrcweir     }
523cdf0e10cSrcweir 
524cdf0e10cSrcweir  	return nRet;
525cdf0e10cSrcweir }
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 
528cdf0e10cSrcweir /*****************************************************************************/
529cdf0e10cSrcweir /* osl_getLastPipeError  */
530cdf0e10cSrcweir /*****************************************************************************/
531cdf0e10cSrcweir oslPipeError SAL_CALL osl_getLastPipeError(oslPipe pPipe)
532cdf0e10cSrcweir {
533cdf0e10cSrcweir     (void) pPipe; /* unused */
534cdf0e10cSrcweir 	return ERROR_FROM_NATIVE(errno);
535cdf0e10cSrcweir }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir 
538cdf0e10cSrcweir sal_Int32 SAL_CALL osl_writePipe( oslPipe pPipe, const void *pBuffer , sal_Int32 n )
539cdf0e10cSrcweir {
54086e1cf34SPedro Giffuni 	/* loop until all desired bytes were send or an error occurred */
541cdf0e10cSrcweir 	sal_Int32 BytesSend= 0;
542cdf0e10cSrcweir 	sal_Int32 BytesToSend= n;
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 	OSL_ASSERT(pPipe);
545cdf0e10cSrcweir 	while (BytesToSend > 0)
546cdf0e10cSrcweir 	{
547cdf0e10cSrcweir 		sal_Int32 RetVal;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		RetVal= osl_sendPipe(pPipe, pBuffer, BytesToSend);
550cdf0e10cSrcweir 
55186e1cf34SPedro Giffuni 		/* error occurred? */
552cdf0e10cSrcweir 		if(RetVal <= 0)
553cdf0e10cSrcweir 		{
554cdf0e10cSrcweir 			break;
555cdf0e10cSrcweir 		}
556cdf0e10cSrcweir 
557cdf0e10cSrcweir 		BytesToSend -= RetVal;
558cdf0e10cSrcweir 		BytesSend += RetVal;
559cdf0e10cSrcweir 		pBuffer= (sal_Char*)pBuffer + RetVal;
560cdf0e10cSrcweir 	}
561cdf0e10cSrcweir 
562cdf0e10cSrcweir 	return BytesSend;
563cdf0e10cSrcweir }
564cdf0e10cSrcweir 
565cdf0e10cSrcweir sal_Int32 SAL_CALL osl_readPipe( oslPipe pPipe, void *pBuffer , sal_Int32 n )
566cdf0e10cSrcweir {
56786e1cf34SPedro Giffuni 	/* loop until all desired bytes were read or an error occurred */
568cdf0e10cSrcweir 	sal_Int32 BytesRead= 0;
569cdf0e10cSrcweir 	sal_Int32 BytesToRead= n;
570cdf0e10cSrcweir 
571cdf0e10cSrcweir 	OSL_ASSERT( pPipe );
572cdf0e10cSrcweir 	while (BytesToRead > 0)
573cdf0e10cSrcweir 	{
574cdf0e10cSrcweir 		sal_Int32 RetVal;
575cdf0e10cSrcweir 		RetVal= osl_receivePipe(pPipe, pBuffer, BytesToRead);
576cdf0e10cSrcweir 
57786e1cf34SPedro Giffuni 		/* error occurred? */
578cdf0e10cSrcweir 		if(RetVal <= 0)
579cdf0e10cSrcweir 		{
580cdf0e10cSrcweir 			break;
581cdf0e10cSrcweir 		}
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 		BytesToRead -= RetVal;
584cdf0e10cSrcweir 		BytesRead += RetVal;
585cdf0e10cSrcweir 		pBuffer= (sal_Char*)pBuffer + RetVal;
586cdf0e10cSrcweir 	}
587cdf0e10cSrcweir 	return BytesRead;
588cdf0e10cSrcweir }
589cdf0e10cSrcweir 
590cdf0e10cSrcweir 
591