tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notify.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (COPYING file) for more details.
  13. *
  14. */
  15. // Needed for asprintf
  16. #define _GNU_SOURCE 1
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ctype.h>
  23. #include <pthread.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <sys/mman.h>
  27. #include "libfuncs/queue.h"
  28. #include "notify.h"
  29. #include "util.h"
  30. struct npriv {
  31. char ident[128];
  32. char program[512];
  33. char msg_id[64];
  34. char text[256];
  35. char input[128];
  36. char output[128];
  37. time_t ts;
  38. int sync; /* Wait for message to be delivered */
  39. };
  40. static void *do_notify(void *in) {
  41. struct npriv *data = in;
  42. struct npriv *shared = mmap(NULL, sizeof(struct npriv), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
  43. if (!shared) {
  44. perror("mmap");
  45. goto OUT;
  46. }
  47. *shared = *data;
  48. pid_t pid = fork();
  49. if (pid==0) { // child process
  50. char *args[] = { shared->program, shared->ident, NULL };
  51. int e = 0;
  52. unsigned int i, r;
  53. char **env = calloc(32, sizeof(char *));
  54. if (asprintf(&env[e++], "_TS=%ld" , shared->ts) < 0) exit(EXIT_FAILURE);
  55. if (asprintf(&env[e++], "_IDENT=%s" , shared->ident) < 0) exit(EXIT_FAILURE);
  56. if (asprintf(&env[e++], "_INPUT_ADDR=%s" , shared->input) < 0) exit(EXIT_FAILURE);
  57. if (asprintf(&env[e++], "_OUTPUT_ADDR=%s" , shared->output) < 0) exit(EXIT_FAILURE);
  58. if (asprintf(&env[e++], "_MESSAGE_ID=%s" , shared->msg_id) < 0) exit(EXIT_FAILURE);
  59. if (asprintf(&env[e++], "_MESSAGE_TEXT=%s" , shared->text) < 0) exit(EXIT_FAILURE);
  60. r = strlen(shared->msg_id);
  61. for (i=0; i<r; i++) {
  62. if (isalpha(shared->msg_id[i]))
  63. shared->msg_id[i] = tolower(shared->msg_id[i]);
  64. if (shared->msg_id[i] == '_')
  65. shared->msg_id[i] = ' ';
  66. }
  67. if (asprintf(&env[e++], "_MESSAGE_MSG=%s" , shared->msg_id) < 0) exit(EXIT_FAILURE);
  68. execve(args[0], args, env);
  69. // We reach here only if there is an error.
  70. fprintf(stderr, "execve('%s') failed: %s!\n", args[0], strerror(errno));
  71. do {
  72. free(env[e--]);
  73. } while (e);
  74. free(env);
  75. exit(EXIT_FAILURE);
  76. } else if (pid < 0) {
  77. fprintf(stderr, "fork() failed: %s\n", strerror(errno));
  78. } else {
  79. waitpid(pid, NULL, 0);
  80. }
  81. munmap(shared, sizeof(struct npriv));
  82. OUT:
  83. free(data);
  84. pthread_exit(EXIT_SUCCESS);
  85. }
  86. static void *notify_thread(void *data) {
  87. struct notify *n = data;
  88. set_thread_name("tsdec-notify");
  89. while (1) {
  90. struct npriv *np = queue_get(n->notifications); // Waits...
  91. if (!np)
  92. break;
  93. pthread_t notifier; // The notifier frees the data
  94. if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
  95. perror("pthread_create");
  96. free(np);
  97. } else {
  98. if (np->sync)
  99. pthread_join(notifier, NULL);
  100. else
  101. pthread_detach(notifier);
  102. }
  103. }
  104. pthread_exit(EXIT_SUCCESS);
  105. }
  106. /* ======================================================================== */
  107. struct notify *notify_alloc(struct ts *ts) {
  108. unsigned int i;
  109. struct notify *n = calloc(1, sizeof(struct notify));
  110. if (!n)
  111. return NULL;
  112. if (!ts->ident)
  113. return NULL;
  114. // Init notify members
  115. strncpy(n->ident, ts->ident, sizeof(n->ident) - 2);
  116. for (i=0; i<strlen(n->ident); i++) {
  117. if (n->ident[i] == '/')
  118. n->ident[i] = '-';
  119. }
  120. // We'll need the notify thread and the queue only if 'notify_program' is set
  121. if (ts->ident && ts->notify_program) {
  122. strncpy(n->program, ts->notify_program, sizeof(n->program) - 2);
  123. n->notifications = queue_new();
  124. pthread_create(&n->thread, &ts->thread_attr , &notify_thread, n);
  125. }
  126. return n;
  127. }
  128. static void npriv_struct_fill(struct npriv *np, struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  129. np->sync = sync_msg;
  130. if (ts->notify_wait)
  131. np->sync = 1;
  132. np->ts = time(NULL);
  133. strncpy(np->program, ts->notify->program, sizeof(np->program) - 2);
  134. strncpy(np->ident, ts->notify->ident, sizeof(np->ident) - 2);
  135. strncpy(np->msg_id, msg_id, sizeof(np->msg_id) - 2);
  136. strncpy(np->text, msg_text, sizeof(np->text) - 2);
  137. if (ts->input.type == NET_IO) {
  138. snprintf(np->input, sizeof(np->input), "%s:%s", ts->input.hostname, ts->input.service);
  139. } else if (ts->input.type == FILE_IO) {
  140. snprintf(np->input, sizeof(np->input), "%s", ts->input.fd == 0 ? "STDIN" : "FILE");
  141. }
  142. if (ts->output_stream) {
  143. if (ts->output.type == NET_IO) {
  144. snprintf(np->output, sizeof(np->output), "%s:%s", ts->output.hostname, ts->output.service);
  145. } else if (ts->output.type == FILE_IO) {
  146. snprintf(np->output, sizeof(np->output), "%s", ts->output.fd == 1 ? "STDOUT" : "FILE");
  147. }
  148. } else {
  149. snprintf(np->output, sizeof(np->output), "DISABLED");
  150. }
  151. }
  152. static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  153. struct npriv np_local;
  154. int np_local_inited = 0;
  155. if (ts->status_file) {
  156. memset(&np_local, 0, sizeof(np_local));
  157. npriv_struct_fill(&np_local, ts, sync_msg, msg_id, msg_text);
  158. np_local_inited = 1;
  159. // Write status file
  160. FILE *status_file = fopen(ts->status_file_tmp, "w");
  161. if (status_file) {
  162. 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);
  163. rename(ts->status_file_tmp, ts->status_file);
  164. fclose(status_file);
  165. }
  166. }
  167. if (ts->notify && ts->notify->notifications) {
  168. struct npriv *np = calloc(1, sizeof(struct npriv));
  169. if (np) {
  170. if (np_local_inited) {
  171. memcpy(np, &np_local, sizeof(*np));
  172. } else {
  173. npriv_struct_fill(np, ts, sync_msg, msg_id, msg_text);
  174. }
  175. queue_add(ts->notify->notifications, np);
  176. }
  177. }
  178. }
  179. #define MAX_MSG_TEXT 256
  180. void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  181. va_list args;
  182. char msg_text[MAX_MSG_TEXT];
  183. va_start(args, text_fmt);
  184. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  185. msg_text[sizeof(msg_text) - 1] = 0;
  186. va_end(args);
  187. notify_func(ts, 0, msg_id, msg_text);
  188. }
  189. void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  190. va_list args;
  191. char msg_text[MAX_MSG_TEXT];
  192. va_start(args, text_fmt);
  193. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  194. msg_text[sizeof(msg_text) - 1] = 0;
  195. va_end(args);
  196. notify_func(ts, 1, msg_id, msg_text);
  197. }
  198. void notify_free(struct notify **pn) {
  199. struct notify *n = *pn;
  200. if (n) {
  201. if (n->notifications) {
  202. queue_add(n->notifications, NULL);
  203. pthread_join(n->thread, NULL);
  204. queue_free(&n->notifications);
  205. }
  206. FREE(*pn);
  207. }
  208. }