2 Copyright (C) 2006-2021 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
29 #ifdef HAVE_LINUX_SOCKIOS_H
30 #include <linux/sockios.h>
33 #ifdef HAVE_SYS_FILIO_H
34 #include <sys/filio.h>
37 #include "pwmd-error.h"
39 #include "util-misc.h"
41 #include "util-string.h"
47 send_status (assuan_context_t ctx
, status_msg_t which
, const char *fmt
, ...)
49 const char *line
= NULL
;
50 char buf
[ASSUAN_LINELENGTH
+ 1];
51 const char *status
= NULL
;
53 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
62 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
70 case STATUS_REHANDSHAKE
:
71 status
= "REHANDSHAKE";
84 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
89 MUTEX_LOCK (&cn_mutex
);
90 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
92 MUTEX_UNLOCK (&cn_mutex
);
97 line
= _("Waiting for lock");
108 case STATUS_KEEPALIVE
:
109 status
= "KEEPALIVE";
123 log_write ("%s %s", status
, line
? line
: "");
128 if (client
&& client
->thd
->remote
&& which
== STATUS_KEEPALIVE
)
132 #ifdef HAVE_DECL_SIOCOUTQ
133 if (ioctl (client
->thd
->fd
, SIOCOUTQ
, &buffered
) == -1)
134 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
135 pwmd_strerror (gpg_error_from_errno (errno
)));
136 #elif defined (HAVE_DECL_FIONWRITE)
137 if (ioctl (client
->thd
->fd
, FIONWRITE
, &buffered
) == -1)
138 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
139 pwmd_strerror (gpg_error_from_errno (errno
)));
140 #elif !defined (HAVE_WINLIKE_SYSTEM)
144 socklen_t len
= sizeof(int);
146 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDBUF
, &sndbuf
,
148 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
, __LINE__
,
149 pwmd_strerror (gpg_error_from_errno (errno
)));
155 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
157 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
,
158 __LINE__
, pwmd_strerror (gpg_error_from_errno (errno
)));
162 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
164 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
166 pwmd_strerror (gpg_error_from_errno (errno
)));
169 struct pollfd fds
[1];
172 fds
[0].fd
= client
->thd
->fd
;
173 fds
[0].events
= POLLOUT
;
175 buffered
= client
->thd
->last_buffer_size
+ 1;
176 n
= poll (fds
, 1, 0);
178 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
180 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
182 pwmd_strerror (gpg_error_from_errno (errno
)));
183 if (n
> 0 && (fds
[0].revents
& POLLOUT
))
192 int interval
= config_get_integer ("global", "keepalive_interval");
193 int timeout
= config_get_integer ("global", "tls_timeout");
195 if (buffered
< client
->thd
->last_buffer_size
)
196 client
->thd
->buffer_timeout
= 0;
198 client
->thd
->last_buffer_size
= buffered
;
200 if (++client
->thd
->buffer_timeout
* interval
>= timeout
)
201 rc
= gpg_error (GPG_ERR_ETIMEDOUT
);
204 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
209 rc
= assuan_write_status (ctx
, status
, line
);
212 if (client
&& client
->thd
->remote
&& which
!= STATUS_KEEPALIVE
)
213 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
220 do_send_status_all (status_msg_t s
, const char *line
, pthread_t
*not_tid
)
222 MUTEX_LOCK (&cn_mutex
);
224 int t
= slist_length (cn_thread_list
);
226 pthread_cleanup_push (release_mutex_cb
, &cn_mutex
);
230 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
231 struct status_msg_s
*msg
, *p
;
236 if (not_tid
&& pthread_equal (*not_tid
, thd
->tid
))
239 if (thd
->state
== CLIENT_STATE_UNKNOWN
240 || thd
->state
== CLIENT_STATE_DISCON
)
243 MUTEX_LOCK (&thd
->status_mutex
);
244 if (s
== STATUS_STATE
&& !thd
->cl
->client_state
)
246 MUTEX_UNLOCK (&thd
->status_mutex
);
250 pthread_cleanup_push (release_mutex_cb
, &thd
->status_mutex
);
252 for (p
= thd
->msg_queue
; p
; p
= p
->next
)
261 if (match
&& s
!= STATUS_STATE
)
264 p
->line
= line
? str_dup (line
) : NULL
;
266 if (!thd
->wrote_status
)
268 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
270 rc
= gpg_error_from_syserror ();
272 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
276 thd
->wrote_status
= 1;
280 msg
= xcalloc (1, sizeof (struct status_msg_s
));
282 msg
->line
= line
? str_dup (line
) : NULL
;
284 for (p
= thd
->msg_queue
; p
&& p
->next
; p
= p
->next
);
286 thd
->msg_queue
= msg
;
290 if (!thd
->wrote_status
)
292 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
294 rc
= gpg_error_from_syserror ();
296 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
300 thd
->wrote_status
= 1;
303 pthread_cleanup_pop (1);
306 pthread_cleanup_pop (1);
310 send_status_all_not_self (status_msg_t s
, const char *fmt
, ...)
313 pthread_t tid
= pthread_self ();
320 str_vasprintf (&line
, fmt
, ap
);
324 pthread_cleanup_push (xfree
, line
);
325 do_send_status_all (s
, line
, &tid
);
326 pthread_cleanup_pop (1);
330 send_status_all (status_msg_t s
, const char *fmt
, ...)
339 str_vasprintf (&line
, fmt
, ap
);
343 pthread_cleanup_push (xfree
, line
);
344 do_send_status_all (s
, line
, NULL
);
345 pthread_cleanup_pop (1);