|
@@ -40,6 +40,7 @@ struct npriv {
|
40
|
40
|
char program[512];
|
41
|
41
|
char msg_id[512];
|
42
|
42
|
char text[512];
|
|
43
|
+ int sync; /* Wait for message to be delivered */
|
43
|
44
|
};
|
44
|
45
|
|
45
|
46
|
static void *do_notify(void *in) {
|
|
@@ -96,14 +97,15 @@ static void *notify_thread(void *data) {
|
96
|
97
|
if (!np)
|
97
|
98
|
break;
|
98
|
99
|
pthread_t notifier; // The notifier frees the data
|
99
|
|
- pthread_attr_t attr;
|
100
|
|
- pthread_attr_init(&attr);
|
101
|
|
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
102
|
|
- if (pthread_create(¬ifier, &attr, &do_notify, np) != 0) {
|
|
100
|
+ if (pthread_create(¬ifier, NULL, &do_notify, np) != 0) {
|
103
|
101
|
perror("pthread_create");
|
104
|
102
|
free(np);
|
|
103
|
+ } else {
|
|
104
|
+ if (np->sync)
|
|
105
|
+ pthread_join(notifier, NULL);
|
|
106
|
+ else
|
|
107
|
+ pthread_detach(notifier);
|
105
|
108
|
}
|
106
|
|
- pthread_attr_destroy(&attr);
|
107
|
109
|
}
|
108
|
110
|
pthread_exit(0);
|
109
|
111
|
}
|
|
@@ -131,21 +133,48 @@ static void npriv_init_defaults(struct notify *n, struct npriv *np) {
|
131
|
133
|
strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
|
132
|
134
|
}
|
133
|
135
|
|
134
|
|
-void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
|
135
|
|
- va_list args;
|
|
136
|
+static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
|
136
|
137
|
struct npriv *np = calloc(1, sizeof(struct npriv));
|
137
|
138
|
if (!ts->notify)
|
138
|
139
|
return;
|
|
140
|
+ np->sync = sync_msg;
|
139
|
141
|
npriv_init_defaults(ts->notify, np);
|
|
142
|
+
|
140
|
143
|
strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
|
141
|
144
|
np->msg_id[sizeof(np->ident) - 1] = 0;
|
142
|
|
- va_start(args, text_fmt);
|
143
|
|
- vsnprintf(np->text, sizeof(np->text) - 1, text_fmt, args);
|
|
145
|
+
|
|
146
|
+ strncpy(np->text, msg_text, sizeof(np->text) - 1);
|
144
|
147
|
np->text[sizeof(np->text) - 1] = 0;
|
145
|
|
- va_end(args);
|
|
148
|
+
|
146
|
149
|
queue_add(ts->notify->notifications, np);
|
147
|
150
|
}
|
148
|
151
|
|
|
152
|
+#define MAX_MSG_TEXT 256
|
|
153
|
+
|
|
154
|
+void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
|
|
155
|
+ va_list args;
|
|
156
|
+ char msg_text[MAX_MSG_TEXT];
|
|
157
|
+
|
|
158
|
+ va_start(args, text_fmt);
|
|
159
|
+ vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
|
|
160
|
+ msg_text[sizeof(msg_text) - 1] = 0;
|
|
161
|
+ va_end(args);
|
|
162
|
+
|
|
163
|
+ notify_func(ts, 0, msg_id, msg_text);
|
|
164
|
+}
|
|
165
|
+
|
|
166
|
+void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
|
|
167
|
+ va_list args;
|
|
168
|
+ char msg_text[MAX_MSG_TEXT];
|
|
169
|
+
|
|
170
|
+ va_start(args, text_fmt);
|
|
171
|
+ vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
|
|
172
|
+ msg_text[sizeof(msg_text) - 1] = 0;
|
|
173
|
+ va_end(args);
|
|
174
|
+
|
|
175
|
+ notify_func(ts, 1, msg_id, msg_text);
|
|
176
|
+}
|
|
177
|
+
|
149
|
178
|
void notify_free(struct notify **pn) {
|
150
|
179
|
struct notify *n = *pn;
|
151
|
180
|
if (n) {
|