2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/socket.h>
27 #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
;
56 struct client_s
*client
= ctx
? assuan_get_pointer (ctx
) : NULL
;
63 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
77 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
82 MUTEX_LOCK (&cn_mutex
);
83 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
85 MUTEX_UNLOCK (&cn_mutex
);
90 line
= _("Waiting for lock");
101 case STATUS_KEEPALIVE
:
102 status
= "KEEPALIVE";
105 p
= strchr (line
, ' ');
115 line
= line
+ strlen (status
) + 1;
122 log_write ("%s %s", status
, line
? line
: "");
127 if (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
)));
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 timeval tv
= { 0, 0 };
172 buffered
= client
->thd
->last_buffer_size
+ 1;
174 FD_SET (client
->thd
->fd
, &wfds
);
175 n
= select (client
->thd
->fd
+ 1, NULL
, &wfds
, NULL
, &tv
);
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 && FD_ISSET (client
->thd
->fd
, &wfds
))
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
->thd
->remote
&& which
!= STATUS_KEEPALIVE
)
212 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
219 send_status_all (status_msg_t s
, const char *fmt
, ...)
221 MUTEX_LOCK (&cn_mutex
);
223 int t
= slist_length (cn_thread_list
);
227 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
228 struct status_msg_s
*msg
, *p
;
232 MUTEX_LOCK (&thd
->status_mutex
);
234 for (p
= thd
->msg_queue
; p
; p
= p
->next
)
245 MUTEX_UNLOCK (&thd
->status_mutex
);
249 msg
= xcalloc (1, sizeof (struct status_msg_s
));
256 str_vasprintf (&msg
->line
, fmt
, ap
);
260 for (p
= thd
->msg_queue
; p
&& p
->next
; p
= p
->next
);
262 thd
->msg_queue
= msg
;
266 write (thd
->status_msg_pipe
[1], &c
, 1);
267 MUTEX_UNLOCK (&thd
->status_mutex
);
270 MUTEX_UNLOCK (&cn_mutex
);