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