Browse Source

Add notify_sync() and use it when reporting STOP event.

Without this the STOP event was lost most of the times because
tsdecrypt exited before the thread delivered the event.
Georgi Chorbadzhiyski 13 years ago
parent
commit
3706892d96
3 changed files with 43 additions and 11 deletions
  1. 39
    10
      notify.c
  2. 3
    0
      notify.h
  3. 1
    1
      tsdecrypt.c

+ 39
- 10
notify.c View File

40
 	char	program[512];
40
 	char	program[512];
41
 	char	msg_id[512];
41
 	char	msg_id[512];
42
 	char	text[512];
42
 	char	text[512];
43
+	int		sync;			/* Wait for message to be delivered */
43
 };
44
 };
44
 
45
 
45
 static void *do_notify(void *in) {
46
 static void *do_notify(void *in) {
96
 		if (!np)
97
 		if (!np)
97
 			break;
98
 			break;
98
 		pthread_t notifier; // The notifier frees the data
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(&notifier, &attr, &do_notify, np) != 0) {
100
+		if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
103
 			perror("pthread_create");
101
 			perror("pthread_create");
104
 			free(np);
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
 	pthread_exit(0);
110
 	pthread_exit(0);
109
 }
111
 }
131
 	strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
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
 	struct npriv *np = calloc(1, sizeof(struct npriv));
137
 	struct npriv *np = calloc(1, sizeof(struct npriv));
137
 	if (!ts->notify)
138
 	if (!ts->notify)
138
 		return;
139
 		return;
140
+	np->sync = sync_msg;
139
 	npriv_init_defaults(ts->notify, np);
141
 	npriv_init_defaults(ts->notify, np);
142
+
140
 	strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
143
 	strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
141
 	np->msg_id[sizeof(np->ident) - 1] = 0;
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
 	np->text[sizeof(np->text) - 1] = 0;
147
 	np->text[sizeof(np->text) - 1] = 0;
145
-	va_end(args);
148
+
146
 	queue_add(ts->notify->notifications, np);
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
 void notify_free(struct notify **pn) {
178
 void notify_free(struct notify **pn) {
150
 	struct notify *n = *pn;
179
 	struct notify *n = *pn;
151
 	if (n) {
180
 	if (n) {

+ 3
- 0
notify.h View File

26
 __attribute__ ((format(printf, 3, 4)))
26
 __attribute__ ((format(printf, 3, 4)))
27
 void notify(struct ts *ts, char *msg_id, char *text_fmt, ...);
27
 void notify(struct ts *ts, char *msg_id, char *text_fmt, ...);
28
 
28
 
29
+__attribute__ ((format(printf, 3, 4)))
30
+void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...);
31
+
29
 void notify_free(struct notify **pn);
32
 void notify_free(struct notify **pn);
30
 
33
 
31
 #endif
34
 #endif

+ 1
- 1
tsdecrypt.c View File

630
 			pthread_join(ts.write_thread, NULL);
630
 			pthread_join(ts.write_thread, NULL);
631
 	}
631
 	}
632
 
632
 
633
-	notify(&ts, "STOP", "Stopping %s", program_id);
633
+	notify_sync(&ts, "STOP", "Stopping %s", program_id);
634
 	ts_LOGf("Stop %s\n", program_id);
634
 	ts_LOGf("Stop %s\n", program_id);
635
 
635
 
636
 	if (ts.syslog_active)
636
 	if (ts.syslog_active)

Loading…
Cancel
Save