|
@@ -33,12 +33,13 @@
|
33
|
33
|
#include "util.h"
|
34
|
34
|
|
35
|
35
|
struct npriv {
|
36
|
|
- char ident[512];
|
|
36
|
+ char ident[128];
|
37
|
37
|
char program[512];
|
38
|
|
- char msg_id[512];
|
39
|
|
- char text[512];
|
|
38
|
+ char msg_id[64];
|
|
39
|
+ char text[256];
|
40
|
40
|
char input[128];
|
41
|
41
|
char output[128];
|
|
42
|
+ time_t ts;
|
42
|
43
|
int sync; /* Wait for message to be delivered */
|
43
|
44
|
};
|
44
|
45
|
|
|
@@ -56,7 +57,7 @@ static void *do_notify(void *in) {
|
56
|
57
|
int e = 0;
|
57
|
58
|
unsigned int i, r;
|
58
|
59
|
char **env = calloc(32, sizeof(char *));
|
59
|
|
- if (asprintf(&env[e++], "_TS=%ld" , time(NULL)) < 0) exit(EXIT_FAILURE);
|
|
60
|
+ if (asprintf(&env[e++], "_TS=%ld" , shared->ts) < 0) exit(EXIT_FAILURE);
|
60
|
61
|
if (asprintf(&env[e++], "_IDENT=%s" , shared->ident) < 0) exit(EXIT_FAILURE);
|
61
|
62
|
if (asprintf(&env[e++], "_INPUT_ADDR=%s" , shared->input) < 0) exit(EXIT_FAILURE);
|
62
|
63
|
if (asprintf(&env[e++], "_OUTPUT_ADDR=%s" , shared->output) < 0) exit(EXIT_FAILURE);
|
|
@@ -117,45 +118,39 @@ static void *notify_thread(void *data) {
|
117
|
118
|
|
118
|
119
|
struct notify *notify_alloc(struct ts *ts) {
|
119
|
120
|
unsigned int i;
|
120
|
|
- if (!ts->ident || !ts->notify_program)
|
121
|
|
- return NULL;
|
122
|
121
|
struct notify *n = calloc(1, sizeof(struct notify));
|
123
|
|
- n->notifications = queue_new();
|
124
|
|
- strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
|
125
|
|
- n->ident[sizeof(n->ident) - 1] = '\0';
|
|
122
|
+ if (!n)
|
|
123
|
+ return NULL;
|
|
124
|
+
|
|
125
|
+ if (!ts->ident)
|
|
126
|
+ return NULL;
|
|
127
|
+
|
|
128
|
+ // Init notify members
|
|
129
|
+ strncpy(n->ident, ts->ident, sizeof(n->ident) - 2);
|
126
|
130
|
for (i=0; i<strlen(n->ident); i++) {
|
127
|
131
|
if (n->ident[i] == '/')
|
128
|
132
|
n->ident[i] = '-';
|
129
|
133
|
}
|
130
|
|
- strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
|
131
|
|
- n->program[sizeof(n->program) - 1] = '\0';
|
132
|
|
- pthread_create(&n->thread, &ts->thread_attr , ¬ify_thread, n);
|
|
134
|
+
|
|
135
|
+ // We'll need the notify thread and the queue only if 'notify_program' is set
|
|
136
|
+ if (ts->ident && ts->notify_program) {
|
|
137
|
+ strncpy(n->program, ts->notify_program, sizeof(n->program) - 2);
|
|
138
|
+ n->notifications = queue_new();
|
|
139
|
+ pthread_create(&n->thread, &ts->thread_attr , ¬ify_thread, n);
|
|
140
|
+ }
|
133
|
141
|
return n;
|
134
|
142
|
}
|
135
|
143
|
|
136
|
|
-static void npriv_init_defaults(struct notify *n, struct npriv *np) {
|
137
|
|
- strncpy(np->program, n->program, sizeof(np->program) - 1);
|
138
|
|
- strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
|
139
|
|
-}
|
140
|
|
-
|
141
|
|
-static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
|
142
|
|
- struct npriv *np;
|
143
|
|
-
|
144
|
|
- if (!ts->notify)
|
145
|
|
- return;
|
146
|
|
-
|
147
|
|
- np = calloc(1, sizeof(struct npriv));
|
148
|
|
-
|
|
144
|
+static void npriv_struct_fill(struct npriv *np, struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
|
149
|
145
|
np->sync = sync_msg;
|
150
|
146
|
if (ts->notify_wait)
|
151
|
147
|
np->sync = 1;
|
152
|
|
- npriv_init_defaults(ts->notify, np);
|
|
148
|
+ np->ts = time(NULL);
|
153
|
149
|
|
154
|
|
- strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
|
155
|
|
- np->msg_id[sizeof(np->ident) - 1] = 0;
|
156
|
|
-
|
157
|
|
- strncpy(np->text, msg_text, sizeof(np->text) - 1);
|
158
|
|
- np->text[sizeof(np->text) - 1] = 0;
|
|
150
|
+ strncpy(np->program, ts->notify->program, sizeof(np->program) - 2);
|
|
151
|
+ strncpy(np->ident, ts->notify->ident, sizeof(np->ident) - 2);
|
|
152
|
+ strncpy(np->msg_id, msg_id, sizeof(np->msg_id) - 2);
|
|
153
|
+ strncpy(np->text, msg_text, sizeof(np->text) - 2);
|
159
|
154
|
|
160
|
155
|
if (ts->input.type == NET_IO) {
|
161
|
156
|
snprintf(np->input, sizeof(np->input), "%s:%s", ts->input.hostname, ts->input.service);
|
|
@@ -171,8 +166,36 @@ static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_tex
|
171
|
166
|
} else {
|
172
|
167
|
snprintf(np->output, sizeof(np->output), "DISABLED");
|
173
|
168
|
}
|
|
169
|
+}
|
174
|
170
|
|
175
|
|
- queue_add(ts->notify->notifications, np);
|
|
171
|
+static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
|
|
172
|
+ struct npriv np_local;
|
|
173
|
+ int np_local_inited = 0;
|
|
174
|
+
|
|
175
|
+ if (ts->status_file) {
|
|
176
|
+ memset(&np_local, 0, sizeof(np_local));
|
|
177
|
+ npriv_struct_fill(&np_local, ts, sync_msg, msg_id, msg_text);
|
|
178
|
+ np_local_inited = 1;
|
|
179
|
+ // Write status file
|
|
180
|
+ FILE *status_file = fopen(ts->status_file_tmp, "w");
|
|
181
|
+ if (status_file) {
|
|
182
|
+ fprintf(status_file, "%s|%ld|%s|%s|%s|%s\n", np_local.ident, np_local.ts, np_local.msg_id, np_local.text, np_local.input, np_local.output);
|
|
183
|
+ rename(ts->status_file_tmp, ts->status_file);
|
|
184
|
+ fclose(status_file);
|
|
185
|
+ }
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ if (ts->notify->notifications) {
|
|
189
|
+ struct npriv *np = calloc(1, sizeof(struct npriv));
|
|
190
|
+ if (np) {
|
|
191
|
+ if (np_local_inited) {
|
|
192
|
+ memcpy(np, &np_local, sizeof(*np));
|
|
193
|
+ } else {
|
|
194
|
+ npriv_struct_fill(np, ts, sync_msg, msg_id, msg_text);
|
|
195
|
+ }
|
|
196
|
+ queue_add(ts->notify->notifications, np);
|
|
197
|
+ }
|
|
198
|
+ }
|
176
|
199
|
}
|
177
|
200
|
|
178
|
201
|
#define MAX_MSG_TEXT 256
|
|
@@ -204,9 +227,11 @@ void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
|
204
|
227
|
void notify_free(struct notify **pn) {
|
205
|
228
|
struct notify *n = *pn;
|
206
|
229
|
if (n) {
|
207
|
|
- queue_add(n->notifications, NULL);
|
208
|
|
- pthread_join(n->thread, NULL);
|
209
|
|
- queue_free(&n->notifications);
|
|
230
|
+ if (n->notifications) {
|
|
231
|
+ queue_add(n->notifications, NULL);
|
|
232
|
+ pthread_join(n->thread, NULL);
|
|
233
|
+ queue_free(&n->notifications);
|
|
234
|
+ }
|
210
|
235
|
FREE(*pn);
|
211
|
236
|
}
|
212
|
237
|
}
|