1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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 this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
24 #include "pwmd_error.h"
26 struct status_thread_s
{
34 static void cleanup(void *arg
)
36 struct status_thread_s
*s
= arg
;
41 pth_event_free(s
->ev
, PTH_FREE_ALL
);
45 static void *write_status_thread(void *arg
)
47 struct status_thread_s
*s
= arg
;
49 pth_attr_t attr
= pth_attr_of(pth_self());
51 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
52 pth_attr_destroy(attr
);
53 pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS
, NULL
);
54 rc
= assuan_write_status(s
->ctx
, s
->status
, s
->line
);
59 gpg_error_t
send_status(assuan_context_t ctx
, status_msg_t which
,
60 const gchar
*fmt
, ...)
62 const gchar
*line
= NULL
;
63 gchar buf
[ASSUAN_LINELENGTH
+1];
68 struct status_thread_s
*s
;
69 gint to
= get_key_file_integer("global", "keepalive");
76 g_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
86 CACHE_LOCK(client
->ctx
);
87 line
= print_fmt(buf
, sizeof(buf
), "%i", cache_file_count());
92 MUTEX_LOCK(&cn_mutex
);
93 line
= print_fmt(buf
, sizeof(buf
), "%i", g_slist_length(cn_thread_list
));
94 MUTEX_UNLOCK(&cn_mutex
);
100 case STATUS_KEEPALIVE
:
101 status
= "KEEPALIVE";
105 line
= N_("Waiting for lock");
113 case STATUS_DECOMPRESS
:
114 status
= "DECOMPRESS";
116 case STATUS_COMPRESS
:
122 log_write("%s %s", status
, line
);
126 s
= g_malloc0(sizeof(struct status_thread_s
));
129 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
130 return gpg_error_from_errno(ENOMEM
);
136 pth_cleanup_push(cleanup
, s
);
137 attr
= pth_attr_new();
139 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, TRUE
);
140 s
->tid
= pth_spawn(attr
, write_status_thread
, s
);
142 pth_attr_destroy(attr
);
145 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
146 _gpg_strerror(gpg_error_from_errno(n
)));
148 return gpg_error_from_errno(n
);
151 /* Since we use the keepalive from the configuration, it may be 0. If so,
152 * status messages would fail and abort the connection. So use a default
153 * that doesn't affect the configured keepalive value. */
154 to
= to
<= 0 ? DEFAULT_KEEPALIVE_TO
: to
;
155 s
->ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, s
->tid
);
156 tev
= pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0));
157 s
->ev
= pth_event_concat(s
->ev
, tev
, NULL
);
160 st
= pth_event_status(s
->ev
);
162 if (st
== PTH_STATUS_FAILED
) {
165 rc
= GPG_ERR_ASS_WRITE_ERROR
;
168 else if (st
== PTH_STATUS_OCCURRED
) {
169 pth_join(s
->tid
, (void **)&rc
);
173 st
= pth_event_status(tev
);
175 if (st
== PTH_STATUS_OCCURRED
) {
178 rc
= GPG_ERR_ASS_WRITE_ERROR
;
186 void *client_msg_thread(void *arg
)
188 struct client_thread_s
*thd
= arg
;
190 pth_attr_t attr
= pth_attr_of(pth_self());
192 pth_attr_set(attr
, PTH_ATTR_NAME
, __FUNCTION__
);
193 pth_attr_destroy(attr
);
195 pth_cond_init(&thd
->msg_cond
);
196 pth_mutex_acquire(&m
, FALSE
, NULL
);
199 pth_cond_await(&thd
->msg_cond
, &m
, NULL
);
206 MUTEX_LOCK(&thd
->msg_list_mutex
);
207 msg
= g_slist_nth_data(thd
->msg_list
, 0);
210 thd
->msg_list
= g_slist_remove(thd
->msg_list
, msg
);
212 MUTEX_UNLOCK(&thd
->msg_list_mutex
);
217 rc
= send_status(thd
->cl
->ctx
, *msg
, NULL
);
222 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(rc
));
223 pth_cancel(thd
->tid
);
232 static gboolean
msg_list_dup(GSList
*list
, status_msg_t which
)
236 for (t
= g_slist_length(list
), i
= 0; i
< t
; i
++) {
237 status_msg_t
*m
= g_slist_nth_data(list
, i
);
246 void send_status_all(status_msg_t which
)
250 MUTEX_LOCK(&cn_mutex
);
252 for (t
= g_slist_length(cn_thread_list
), i
= 0; i
< t
; i
++) {
253 struct client_thread_s
*cn
= g_slist_nth_data(cn_thread_list
, i
);
256 if (msg_list_dup(cn
->msg_list
, which
))
259 m
= g_malloc(sizeof(status_msg_t
));
262 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
267 MUTEX_LOCK(&cn
->msg_list_mutex
);
268 cn
->msg_list
= g_slist_append(cn
->msg_list
, m
);
269 MUTEX_UNLOCK(&cn
->msg_list_mutex
);
270 pth_cond_notify(&cn
->msg_cond
, FALSE
);
273 MUTEX_UNLOCK(&cn_mutex
);