|
@@ -0,0 +1,154 @@
|
|
1
|
+/*
|
|
2
|
+ * Exec external program to notify for an event
|
|
3
|
+ * Copyright (C) 2011 Unix Solutions Ltd.
|
|
4
|
+ *
|
|
5
|
+ * This program is free software; you can redistribute it and/or modify
|
|
6
|
+ * it under the terms of the GNU General Public License version 2
|
|
7
|
+ * as published by the Free Software Foundation.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful,
|
|
10
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+ * GNU General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+// Needed for asprintf
|
|
20
|
+#define _GNU_SOURCE 1
|
|
21
|
+
|
|
22
|
+#include <stdlib.h>
|
|
23
|
+#include <stdarg.h>
|
|
24
|
+#include <unistd.h>
|
|
25
|
+#include <string.h>
|
|
26
|
+#include <errno.h>
|
|
27
|
+#include <ctype.h>
|
|
28
|
+#include <pthread.h>
|
|
29
|
+#include <sys/types.h>
|
|
30
|
+#include <sys/wait.h>
|
|
31
|
+#include <sys/mman.h>
|
|
32
|
+
|
|
33
|
+#include "libfuncs/queue.h"
|
|
34
|
+
|
|
35
|
+#include "notify.h"
|
|
36
|
+
|
|
37
|
+struct npriv {
|
|
38
|
+ char ident[512];
|
|
39
|
+ char program[512];
|
|
40
|
+ char msg_id[512];
|
|
41
|
+ char text[512];
|
|
42
|
+};
|
|
43
|
+
|
|
44
|
+static void *do_notify(void *in) {
|
|
45
|
+ struct npriv *data = in;
|
|
46
|
+ struct npriv *shared = mmap(NULL, sizeof(struct npriv), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
|
|
47
|
+ if (!shared) {
|
|
48
|
+ perror("mmap");
|
|
49
|
+ goto OUT;
|
|
50
|
+ }
|
|
51
|
+ *shared = *data;
|
|
52
|
+ pid_t pid = fork();
|
|
53
|
+ if (pid==0) { // child process
|
|
54
|
+ char *args[] = { shared->program, shared->ident, NULL };
|
|
55
|
+ int e = 0;
|
|
56
|
+ unsigned int i, r;
|
|
57
|
+ char **env = calloc(32, sizeof(char *));
|
|
58
|
+ asprintf(&env[e++], "_TS=%ld" , time(NULL));
|
|
59
|
+ asprintf(&env[e++], "_IDENT=%s" , shared->ident);
|
|
60
|
+ asprintf(&env[e++], "_MESSAGE_ID=%s" , shared->msg_id);
|
|
61
|
+ asprintf(&env[e++], "_MESSAGE_TEXT=%s" , shared->text);
|
|
62
|
+ r = strlen(shared->msg_id);
|
|
63
|
+ for (i=0; i<r; i++) {
|
|
64
|
+ if (isalpha(shared->msg_id[i]))
|
|
65
|
+ shared->msg_id[i] = tolower(shared->msg_id[i]);
|
|
66
|
+ if (shared->msg_id[i] == '_')
|
|
67
|
+ shared->msg_id[i] = ' ';
|
|
68
|
+ }
|
|
69
|
+ asprintf(&env[e++], "_MESSAGE_MSG=%s" , shared->msg_id);
|
|
70
|
+ execve(args[0], args, env);
|
|
71
|
+ // We reach here only if there is an error.
|
|
72
|
+ fprintf(stderr, "execve('%s') failed: %s!\n", args[0], strerror(errno));
|
|
73
|
+ do {
|
|
74
|
+ free(env[e--]);
|
|
75
|
+ } while (e);
|
|
76
|
+ free(env);
|
|
77
|
+ exit(127);
|
|
78
|
+ } else {
|
|
79
|
+ waitpid(pid, NULL, 0);
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ munmap(shared, sizeof(struct npriv));
|
|
83
|
+OUT:
|
|
84
|
+ free(data);
|
|
85
|
+ pthread_exit(0);
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+static void *notify_thread(void *data) {
|
|
89
|
+ struct notify *n = data;
|
|
90
|
+
|
|
91
|
+ while (1) {
|
|
92
|
+ struct npriv *np = queue_get(n->notifications); // Waits...
|
|
93
|
+ if (!np)
|
|
94
|
+ break;
|
|
95
|
+ pthread_t notifier; // The notifier frees the data
|
|
96
|
+ pthread_attr_t attr;
|
|
97
|
+ pthread_attr_init(&attr);
|
|
98
|
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
99
|
+ if (pthread_create(¬ifier, &attr, &do_notify, np) != 0) {
|
|
100
|
+ perror("pthread_create");
|
|
101
|
+ free(np);
|
|
102
|
+ }
|
|
103
|
+ pthread_attr_destroy(&attr);
|
|
104
|
+ }
|
|
105
|
+ pthread_exit(0);
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+/* ======================================================================== */
|
|
109
|
+
|
|
110
|
+struct notify *notify_alloc(struct ts *ts) {
|
|
111
|
+ unsigned int i;
|
|
112
|
+ if (!ts->ident[0] || !ts->notify_program[0])
|
|
113
|
+ return NULL;
|
|
114
|
+ struct notify *n = calloc(1, sizeof(struct notify));
|
|
115
|
+ n->notifications = queue_new("notifications");
|
|
116
|
+ strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
|
|
117
|
+ for (i=0; i<strlen(n->ident); i++) {
|
|
118
|
+ if (n->ident[i] == '/')
|
|
119
|
+ n->ident[i] = '-';
|
|
120
|
+ }
|
|
121
|
+ strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
|
|
122
|
+ pthread_create(&n->thread, NULL , ¬ify_thread, n);
|
|
123
|
+ return n;
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+static void npriv_init_defaults(struct notify *n, struct npriv *np) {
|
|
127
|
+ strncpy(np->program, n->program, sizeof(np->program) - 1);
|
|
128
|
+ strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
|
|
132
|
+ va_list args;
|
|
133
|
+ struct npriv *np = calloc(1, sizeof(struct npriv));
|
|
134
|
+ if (!ts->notify)
|
|
135
|
+ return;
|
|
136
|
+ npriv_init_defaults(ts->notify, np);
|
|
137
|
+ strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
|
|
138
|
+ np->msg_id[sizeof(np->ident) - 1] = 0;
|
|
139
|
+ va_start(args, text_fmt);
|
|
140
|
+ vsnprintf(np->text, sizeof(np->text) - 1, text_fmt, args);
|
|
141
|
+ np->text[sizeof(np->text) - 1] = 0;
|
|
142
|
+ va_end(args);
|
|
143
|
+ queue_add(ts->notify->notifications, np);
|
|
144
|
+}
|
|
145
|
+
|
|
146
|
+void notify_free(struct notify **pn) {
|
|
147
|
+ struct notify *n = *pn;
|
|
148
|
+ if (n) {
|
|
149
|
+ queue_add(n->notifications, NULL);
|
|
150
|
+ pthread_join(n->thread, NULL);
|
|
151
|
+ queue_free(&n->notifications);
|
|
152
|
+ FREE(*pn);
|
|
153
|
+ }
|
|
154
|
+}
|