Include all required m4 macros.
[pwmd.git] / src / status.c
blobb1a2fa473249794ddad73fb5e4cc918a5bff0189
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
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/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <pthread.h>
25 #include <stdarg.h>
27 #include "pwmd-error.h"
28 #include "common.h"
29 #include "mutex.h"
30 #include "util-misc.h"
31 #include "util-string.h"
32 #include "status.h"
33 #include "cache.h"
34 #include "mem.h"
36 gpg_error_t
37 send_status (assuan_context_t ctx, status_msg_t which, const char *fmt, ...)
39 const char *line = NULL;
40 char buf[ASSUAN_LINELENGTH + 1];
41 const char *status = NULL;
42 va_list ap;
43 char *p;
45 if (fmt)
47 va_start (ap, fmt);
48 vsnprintf (buf, sizeof (buf), fmt, ap);
49 va_end (ap);
50 line = buf;
53 switch (which)
55 case STATUS_GENKEY:
56 status = "GENKEY";
57 break;
58 case STATUS_XFER:
59 status = "XFER";
60 break;
61 case STATUS_CACHE:
62 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
63 line = buf;
64 status = "CACHE";
65 break;
66 case STATUS_CLIENTS:
67 MUTEX_LOCK (&cn_mutex);
68 snprintf (buf, sizeof (buf), "%i", slist_length (cn_thread_list));
69 line = buf;
70 MUTEX_UNLOCK (&cn_mutex);
71 status = "CLIENTS";
72 break;
73 case STATUS_LOCKED:
74 status = "LOCKED";
75 line = _("Waiting for lock");
76 break;
77 case STATUS_ENCRYPT:
78 status = "ENCRYPT";
79 break;
80 case STATUS_DECRYPT:
81 status = "DECRYPT";
82 break;
83 case STATUS_NEWFILE:
84 status = "NEWFILE";
85 break;
86 case STATUS_AGENT:
87 p = strchr (line, ' ');
89 if (!p)
91 status = line;
92 line = NULL;
94 else
96 *p = 0;
97 status = line;
98 line = line + strlen (status) + 1;
100 break;
103 if (!ctx)
105 log_write ("%s %s", status, line ? line : "");
106 return 0;
109 return assuan_write_status (ctx, status, line);
112 void
113 send_status_all (status_msg_t s, const char *fmt, ...)
115 MUTEX_LOCK (&cn_mutex);
116 int i = 0;
117 int t = slist_length (cn_thread_list);
119 for (; i < t; i++)
121 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
122 struct status_msg_s *msg, *p;
123 char c = 0xff;
124 int match = 0;
126 MUTEX_LOCK (&thd->status_mutex);
128 for (p = thd->msg_queue; p; p = p->next)
130 if (p->s == s)
132 match = 1;
133 break;
137 if (match)
139 MUTEX_UNLOCK (&thd->status_mutex);
140 continue;
143 msg = xcalloc (1, sizeof (struct status_msg_s));
144 msg->s = s;
145 if (fmt)
147 va_list ap;
149 va_start (ap, fmt);
150 str_vasprintf (&msg->line, fmt, ap);
151 va_end (ap);
154 for (p = thd->msg_queue; p && p->next; p = p->next);
155 if (!p)
156 thd->msg_queue = msg;
157 else
158 p->next = msg;
160 write (thd->status_msg_pipe[1], &c, 1);
161 MUTEX_UNLOCK (&thd->status_mutex);
164 MUTEX_UNLOCK (&cn_mutex);