2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
32 #ifdef HAVE_LINUX_SOCKIOS_H
33 #include <linux/sockios.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
40 #include "pwmd-error.h"
42 #include "util-misc.h"
44 #include "util-string.h"
50 send_status (assuan_context_t ctx
, status_msg_t which
, const char *fmt
, ...)
52 const char *line
= NULL
;
53 char buf
[ASSUAN_LINELENGTH
+ 1];
54 const char *status
= NULL
;
56 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
65 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
82 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
87 MUTEX_LOCK (&cn_mutex
);
88 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
90 MUTEX_UNLOCK (&cn_mutex
);
95 line
= _("Waiting for lock");
106 case STATUS_KEEPALIVE
:
107 status
= "KEEPALIVE";
119 log_write ("%s %s", status
, line
? line
: "");
124 if (client
&& client
->thd
->remote
&& which
== STATUS_KEEPALIVE
)
128 #ifdef HAVE_DECL_SIOCOUTQ
129 if (ioctl (client
->thd
->fd
, SIOCOUTQ
, &buffered
) == -1)
130 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
131 pwmd_strerror (gpg_error_from_errno (errno
)));
132 #elif defined (HAVE_DECL_FIONWRITE)
133 if (ioctl (client
->thd
->fd
, FIONWRITE
, &buffered
) == -1)
134 log_write ("%s(%i): ioctl: %s", __FUNCTION__
, __LINE__
,
135 pwmd_strerror (gpg_error_from_errno (errno
)));
140 socklen_t len
= sizeof(int);
142 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDBUF
, &sndbuf
,
144 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
, __LINE__
,
145 pwmd_strerror (gpg_error_from_errno (errno
)));
151 if (getsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
153 log_write ("%s(%i): getsockopt: %s", __FUNCTION__
,
154 __LINE__
, pwmd_strerror (gpg_error_from_errno (errno
)));
158 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
160 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
162 pwmd_strerror (gpg_error_from_errno (errno
)));
165 struct pollfd fds
[1];
168 fds
[0].fd
= client
->thd
->fd
;
169 fds
[0].events
= POLLOUT
;
171 buffered
= client
->thd
->last_buffer_size
+ 1;
172 n
= poll (fds
, 1, 0);
174 if (setsockopt (client
->thd
->fd
, SOL_SOCKET
, SO_SNDLOWAT
,
176 log_write ("%s(%i): setsockopt: %s", __FUNCTION__
,
178 pwmd_strerror (gpg_error_from_errno (errno
)));
179 if (n
> 0 && (fds
[0].revents
& POLLOUT
))
188 int interval
= config_get_integer ("global", "keepalive_interval");
189 int timeout
= config_get_integer ("global", "tls_timeout");
191 if (buffered
< client
->thd
->last_buffer_size
)
192 client
->thd
->buffer_timeout
= 0;
194 client
->thd
->last_buffer_size
= buffered
;
196 if (++client
->thd
->buffer_timeout
* interval
>= timeout
)
197 rc
= gpg_error (GPG_ERR_ETIMEDOUT
);
200 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
205 rc
= assuan_write_status (ctx
, status
, line
);
208 if (client
&& client
->thd
->remote
&& which
!= STATUS_KEEPALIVE
)
209 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
216 do_send_status_all (status_msg_t s
, const char *line
, pthread_t
*not_tid
,
219 MUTEX_LOCK (&cn_mutex
);
221 int t
= slist_length (cn_thread_list
);
225 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
226 struct status_msg_s
*msg
, *p
;
231 if (not_tid
&& pthread_equal (*not_tid
, thd
->tid
))
234 /* Only want to send this status message to invoking_user clients'. */
235 if (thd
->state
== CLIENT_STATE_UNKNOWN
236 || thd
->state
== CLIENT_STATE_DISCON
237 || (invoker
&& peer_is_invoker (thd
->cl
)))
240 MUTEX_LOCK (&thd
->status_mutex
);
242 for (p
= thd
->msg_queue
; p
; p
= p
->next
)
251 if (match
&& s
!= STATUS_STATE
)
254 p
->line
= line
? str_dup (line
) : NULL
;
256 if (!thd
->wrote_status
)
258 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
260 rc
= gpg_error_from_syserror ();
262 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
266 thd
->wrote_status
= 1;
267 MUTEX_UNLOCK (&thd
->status_mutex
);
271 msg
= xcalloc (1, sizeof (struct status_msg_s
));
273 msg
->line
= line
? str_dup (line
) : NULL
;
275 for (p
= thd
->msg_queue
; p
&& p
->next
; p
= p
->next
);
277 thd
->msg_queue
= msg
;
281 if (!thd
->wrote_status
)
283 ssize_t ret
= write (thd
->status_msg_pipe
[1], &c
, 1);
285 rc
= gpg_error_from_syserror ();
287 log_write ("%s (%i): %s", __FUNCTION__
, __LINE__
,
291 thd
->wrote_status
= 1;
292 MUTEX_UNLOCK (&thd
->status_mutex
);
295 MUTEX_UNLOCK (&cn_mutex
);
299 send_status_all_not_self (int invoker
, status_msg_t s
, const char *fmt
, ...)
302 pthread_t tid
= pthread_self ();
309 str_vasprintf (&line
, fmt
, ap
);
313 do_send_status_all (s
, line
, &tid
, invoker
);
318 send_status_all (status_msg_t s
, const char *fmt
, ...)
327 str_vasprintf (&line
, fmt
, ap
);
331 do_send_status_all (s
, line
, NULL
, 0);