1 /* assuan-io-pth.c - Pth version of assua-io.c.
2 * Copyright (C) 2002, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
34 #ifdef HAVE_W32_SYSTEM
37 # include <sys/wait.h>
41 #include "assuan-defs.h"
45 #ifndef HAVE_W32_SYSTEM
47 _assuan_waitpid (pid_t pid
, int *status
, int options
)
49 return pth_waitpid (pid
, status
, options
);
55 _assuan_simple_read (assuan_context_t ctx
, void *buffer
, size_t size
)
57 /* Fixme: For W32 we should better not cast the HANDLE type to int.
58 However, this requires changes in w32pth too. */
61 if (_assuan_io_hooks
.read_hook
62 && _assuan_io_hooks
.read_hook (ctx
, ctx
->inbound
.fd
,
63 buffer
, size
, &retval
) == 1)
66 return _assuan_io_read (ctx
->inbound
.fd
, buffer
, size
);
70 _assuan_simple_write (assuan_context_t ctx
, const void *buffer
, size_t size
)
74 if (_assuan_io_hooks
.write_hook
75 && _assuan_io_hooks
.write_hook (ctx
, ctx
->outbound
.fd
,
76 buffer
, size
, &retval
) == 1)
78 return _assuan_io_write (ctx
->outbound
.fd
, buffer
, size
);
82 _assuan_io_read (assuan_fd_t fd
, void *buffer
, size_t size
)
86 if (_assuan_io_hooks
.read_hook
87 && _assuan_io_hooks
.read_hook (NULL
, fd
, buffer
, size
, &retval
) == 1)
89 return pth_read ((int)fd
, buffer
, size
);
93 _assuan_io_write (assuan_fd_t fd
, const void *buffer
, size_t size
)
97 if (_assuan_io_hooks
.write_hook
98 && _assuan_io_hooks
.write_hook (NULL
, fd
, buffer
, size
, &retval
) == 1)
100 return pth_write ((int)fd
, buffer
, size
);
104 #ifdef HAVE_W32_SYSTEM
106 _assuan_simple_sendmsg (assuan_context_t ctx
, void *msg
)
109 _assuan_simple_sendmsg (assuan_context_t ctx
, struct msghdr
*msg
)
112 #if defined(HAVE_W32_SYSTEM)
113 return _assuan_error (ASSUAN_Not_Implemented
);
115 /* Pth does not provide a sendmsg function. Thus we implement it here. */
117 int fd
= ctx
->outbound
.fd
;
120 fdmode
= pth_fdmode (fd
, PTH_FDMODE_POLL
);
121 if (fdmode
== PTH_FDMODE_ERROR
)
126 if (fdmode
== PTH_FDMODE_BLOCK
)
132 while ( (ret
= pth_select (fd
+1, NULL
, &fds
, NULL
, NULL
)) < 0
139 while ((ret
= sendmsg (fd
, msg
, 0)) == -1 && errno
== EINTR
)
145 #ifdef HAVE_W32_SYSTEM
147 _assuan_simple_recvmsg (assuan_context_t ctx
, void *msg
)
150 _assuan_simple_recvmsg (assuan_context_t ctx
, struct msghdr
*msg
)
153 #if defined(HAVE_W32_SYSTEM)
154 return _assuan_error (ASSUAN_Not_Implemented
);
156 /* Pth does not provide a recvmsg function. Thus we implement it here. */
158 int fd
= ctx
->inbound
.fd
;
161 fdmode
= pth_fdmode (fd
, PTH_FDMODE_POLL
);
162 if (fdmode
== PTH_FDMODE_ERROR
)
167 if (fdmode
== PTH_FDMODE_BLOCK
)
173 while ( (ret
= pth_select (fd
+1, &fds
, NULL
, NULL
, NULL
)) < 0
180 while ((ret
= recvmsg (fd
, msg
, 0)) == -1 && errno
== EINTR
)
188 _assuan_usleep (unsigned int usec
)