2 Copyright (C) 2006-2018 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 as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
30 #ifdef HAVE_LINUX_SOCKIOS_H
31 #include <linux/sockios.h>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
38 #include "pwmd-error.h"
40 #include "util-misc.h"
42 #include "util-string.h"
48 send_status (assuan_context_t ctx
, status_msg_t which
, const char *fmt
, ...)
50 const char *line
= NULL
;
51 char buf
[ASSUAN_LINELENGTH
+ 1];
52 const char *status
= NULL
;
54 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
63 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
71 case STATUS_REHANDSHAKE
:
72 status
= "REHANDSHAKE";
85 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
90 MUTEX_LOCK (&cn_mutex
);
91 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
93 MUTEX_UNLOCK (&cn_mutex
);
98 line
= _("Waiting for lock");
109 case STATUS_KEEPALIVE
:
110 status
= "KEEPALIVE";
122 log_write ("%s %s", status
, line
? line
: "");
127 if (client
&& client
->thd
->remote
&& which
== STATUS_KEEPALIVE
)
131 #ifdef HAVE_DECL_SIOCOUTQ
132 if (ioctl (client
->thd
->fd
, SIOCOUTQ
, &buffered
) == -1)
133 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
134 pwmd_strerror (gpg_error_from_errno (errno
)));
135 #elif defined (HAVE_DECL_FIONWRITE)
136 if (ioctl (client
->thd
->fd
, FIONWRITE
, &buffered
) == -1)
137 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
138 pwmd_strerror (gpg_error_from_errno (errno
)));
139 #elif !defined (HAVE_WINLIKE_SYSTEM)
143 socklen_t len
= sizeof(int);
145 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDBUF
, &sndbuf
,
147 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
, __LINE__
,
148 pwmd_strerror (gpg_error_from_errno (errno
)));
154 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
156 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
,
157 __LINE__
, pwmd_strerror (gpg_error_from_errno (errno
)));
161 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
163 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
165 pwmd_strerror (gpg_error_from_errno (errno
)));
168 struct pollfd fds
[1];
171 fds
[0].fd
= client
->thd
->fd
;
172 fds
[0].events
= POLLOUT
;
174 buffered
= client
->thd
->last_buffer_size
+ 1;
175 n
= poll (fds
, 1, 0);
177 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
179 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
181 pwmd_strerror (gpg_error_from_errno (errno
)));
182 if (n
> 0 && (fds
[0].revents
& POLLOUT
))
191 int interval
= config_get_integer ("global", "keepalive_interval");
192 int timeout
= config_get_integer ("global", "tls_timeout");
194 if (buffered
< client
->thd
->last_buffer_size
)
195 client
->thd
->buffer_timeout
= 0;
197 client
->thd
->last_buffer_size
= buffered
;
199 if (++client
->thd
->buffer_timeout
* interval
>= timeout
)
200 rc
= gpg_error (GPG_ERR_ETIMEDOUT
);
203 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
208 rc
= assuan_write_status (ctx
, status
, line
);
211 if (client
&& client
->thd
->remote
&& which
!= STATUS_KEEPALIVE
)
212 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
219 do_send_status_all (status_msg_t s
, const char *line
, pthread_t
*not_tid
)
221 MUTEX_LOCK (&cn_mutex
);
223 int t
= slist_length (cn_thread_list
);
225 pthread_cleanup_push (release_mutex_cb
, &cn_mutex
);
229 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
230 struct status_msg_s
*msg
, *p
;
235 if (not_tid
&& pthread_equal (*not_tid
, thd
->tid
))
238 if (thd
->state
== CLIENT_STATE_UNKNOWN
239 || thd
->state
== CLIENT_STATE_DISCON
)
242 MUTEX_LOCK (&thd
->status_mutex
);
243 if (s
== STATUS_STATE
&& !thd
->cl
->client_state
)
245 MUTEX_UNLOCK (&thd
->status_mutex
);
249 pthread_cleanup_push (release_mutex_cb
, &thd
->status_mutex
);
251 for (p
= thd
->msg_queue
; p
; p
= p
->next
)
260 if (match
&& s
!= STATUS_STATE
)
263 p
->line
= line
? str_dup (line
) : NULL
;
265 if (!thd
->wrote_status
)
267 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
269 rc
= gpg_error_from_syserror ();
271 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
275 thd
->wrote_status
= 1;
279 msg
= xcalloc (1, sizeof (struct status_msg_s
));
281 msg
->line
= line
? str_dup (line
) : NULL
;
283 for (p
= thd
->msg_queue
; p
&& p
->next
; p
= p
->next
);
285 thd
->msg_queue
= msg
;
289 if (!thd
->wrote_status
)
291 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
293 rc
= gpg_error_from_syserror ();
295 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
299 thd
->wrote_status
= 1;
302 pthread_cleanup_pop (1);
305 pthread_cleanup_pop (1);
309 send_status_all_not_self (status_msg_t s
, const char *fmt
, ...)
312 pthread_t tid
= pthread_self ();
319 str_vasprintf (&line
, fmt
, ap
);
323 pthread_cleanup_push (xfree
, line
);
324 do_send_status_all (s
, line
, &tid
);
325 pthread_cleanup_pop (1);
329 send_status_all (status_msg_t s
, const char *fmt
, ...)
338 str_vasprintf (&line
, fmt
, ap
);
342 pthread_cleanup_push (xfree
, line
);
343 do_send_status_all (s
, line
, NULL
);
344 pthread_cleanup_pop (1);