Fixed a segfault during an SSH connection failure.
[libpwmd.git] / assuan / assuan-io-pth.c
blob6064ec43b9d5c512ffb4620377bbcc237a53afd5
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/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #if HAVE_SYS_UIO_H
30 # include <sys/uio.h>
31 #endif
32 #include <unistd.h>
33 #include <errno.h>
34 #ifdef HAVE_W32_SYSTEM
35 # include <windows.h>
36 #else
37 # include <sys/wait.h>
38 #endif
39 #include <pth.h>
41 #include "assuan-defs.h"
45 #ifndef HAVE_W32_SYSTEM
46 pid_t
47 _assuan_waitpid (pid_t pid, int *status, int options)
49 return pth_waitpid (pid, status, options);
51 #endif
54 ssize_t
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. */
59 ssize_t retval;
61 if (_assuan_io_hooks.read_hook
62 && _assuan_io_hooks.read_hook (ctx, ctx->inbound.fd,
63 buffer, size, &retval) == 1)
64 return retval;
66 return _assuan_io_read (ctx->inbound.fd, buffer, size);
69 ssize_t
70 _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size)
72 ssize_t retval;
74 if (_assuan_io_hooks.write_hook
75 && _assuan_io_hooks.write_hook (ctx, ctx->outbound.fd,
76 buffer, size, &retval) == 1)
77 return retval;
78 return _assuan_io_write (ctx->outbound.fd, buffer, size);
81 ssize_t
82 _assuan_io_read (assuan_fd_t fd, void *buffer, size_t size)
84 ssize_t retval;
86 if (_assuan_io_hooks.read_hook
87 && _assuan_io_hooks.read_hook (NULL, fd, buffer, size, &retval) == 1)
88 return retval;
89 return pth_read ((int)fd, buffer, size);
92 ssize_t
93 _assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size)
95 ssize_t retval;
97 if (_assuan_io_hooks.write_hook
98 && _assuan_io_hooks.write_hook (NULL, fd, buffer, size, &retval) == 1)
99 return retval;
100 return pth_write ((int)fd, buffer, size);
104 #ifdef HAVE_W32_SYSTEM
106 _assuan_simple_sendmsg (assuan_context_t ctx, void *msg)
107 #else
108 ssize_t
109 _assuan_simple_sendmsg (assuan_context_t ctx, struct msghdr *msg)
110 #endif
112 #if defined(HAVE_W32_SYSTEM)
113 return _assuan_error (ASSUAN_Not_Implemented);
114 #else
115 /* Pth does not provide a sendmsg function. Thus we implement it here. */
116 int ret;
117 int fd = ctx->outbound.fd;
118 int fdmode;
120 fdmode = pth_fdmode (fd, PTH_FDMODE_POLL);
121 if (fdmode == PTH_FDMODE_ERROR)
123 errno = EBADF;
124 return -1;
126 if (fdmode == PTH_FDMODE_BLOCK)
128 fd_set fds;
130 FD_ZERO (&fds);
131 FD_SET (fd, &fds);
132 while ( (ret = pth_select (fd+1, NULL, &fds, NULL, NULL)) < 0
133 && errno == EINTR)
135 if (ret < 0)
136 return -1;
139 while ((ret = sendmsg (fd, msg, 0)) == -1 && errno == EINTR)
141 return ret;
142 #endif
145 #ifdef HAVE_W32_SYSTEM
147 _assuan_simple_recvmsg (assuan_context_t ctx, void *msg)
148 #else
149 ssize_t
150 _assuan_simple_recvmsg (assuan_context_t ctx, struct msghdr *msg)
151 #endif
153 #if defined(HAVE_W32_SYSTEM)
154 return _assuan_error (ASSUAN_Not_Implemented);
155 #else
156 /* Pth does not provide a recvmsg function. Thus we implement it here. */
157 int ret;
158 int fd = ctx->inbound.fd;
159 int fdmode;
161 fdmode = pth_fdmode (fd, PTH_FDMODE_POLL);
162 if (fdmode == PTH_FDMODE_ERROR)
164 errno = EBADF;
165 return -1;
167 if (fdmode == PTH_FDMODE_BLOCK)
169 fd_set fds;
171 FD_ZERO (&fds);
172 FD_SET (fd, &fds);
173 while ( (ret = pth_select (fd+1, &fds, NULL, NULL, NULL)) < 0
174 && errno == EINTR)
176 if (ret < 0)
177 return -1;
180 while ((ret = recvmsg (fd, msg, 0)) == -1 && errno == EINTR)
182 return ret;
183 #endif
187 void
188 _assuan_usleep (unsigned int usec)
190 pth_usleep (usec);