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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // Needed for asprintf
  19. #define _GNU_SOURCE 1
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <pthread.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <sys/mman.h>
  30. #include "libfuncs/queue.h"
  31. #include "notify.h"
  32. #include "util.h"
  33. struct npriv {
  34. char ident[512];
  35. char program[512];
  36. char msg_id[512];
  37. char text[512];
  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. asprintf(&env[e++], "_TS=%ld" , time(NULL));
  55. asprintf(&env[e++], "_IDENT=%s" , shared->ident);
  56. asprintf(&env[e++], "_MESSAGE_ID=%s" , shared->msg_id);
  57. asprintf(&env[e++], "_MESSAGE_TEXT=%s" , shared->text);
  58. r = strlen(shared->msg_id);
  59. for (i=0; i<r; i++) {
  60. if (isalpha(shared->msg_id[i]))
  61. shared->msg_id[i] = tolower(shared->msg_id[i]);
  62. if (shared->msg_id[i] == '_')
  63. shared->msg_id[i] = ' ';
  64. }
  65. asprintf(&env[e++], "_MESSAGE_MSG=%s" , shared->msg_id);
  66. execve(args[0], args, env);
  67. // We reach here only if there is an error.
  68. fprintf(stderr, "execve('%s') failed: %s!\n", args[0], strerror(errno));
  69. do {
  70. free(env[e--]);
  71. } while (e);
  72. free(env);
  73. exit(EXIT_FAILURE);
  74. } else if (pid < 0) {
  75. fprintf(stderr, "fork() failed: %s\n", strerror(errno));
  76. } else {
  77. waitpid(pid, NULL, 0);
  78. }
  79. munmap(shared, sizeof(struct npriv));
  80. OUT:
  81. free(data);
  82. pthread_exit(EXIT_SUCCESS);
  83. }
  84. static void *notify_thread(void *data) {
  85. struct notify *n = data;
  86. set_thread_name("tsdec-notify");
  87. while (1) {
  88. struct npriv *np = queue_get(n->notifications); // Waits...
  89. if (!np)
  90. break;
  91. pthread_t notifier; // The notifier frees the data
  92. if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
  93. perror("pthread_create");
  94. free(np);
  95. } else {
  96. if (np->sync)
  97. pthread_join(notifier, NULL);
  98. else
  99. pthread_detach(notifier);
  100. }
  101. }
  102. pthread_exit(EXIT_SUCCESS);
  103. }
  104. /* ======================================================================== */
  105. struct notify *notify_alloc(struct ts *ts) {
  106. unsigned int i;
  107. if (!ts->ident || !ts->notify_program)
  108. return NULL;
  109. struct notify *n = calloc(1, sizeof(struct notify));
  110. n->notifications = queue_new();
  111. strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
  112. n->ident[sizeof(n->ident) - 1] = '\0';
  113. for (i=0; i<strlen(n->ident); i++) {
  114. if (n->ident[i] == '/')
  115. n->ident[i] = '-';
  116. }
  117. strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
  118. n->program[sizeof(n->program) - 1] = '\0';
  119. pthread_create(&n->thread, NULL , &notify_thread, n);
  120. return n;
  121. }
  122. static void npriv_init_defaults(struct notify *n, struct npriv *np) {
  123. strncpy(np->program, n->program, sizeof(np->program) - 1);
  124. strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
  125. }
  126. static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  127. struct npriv *np;
  128. if (!ts->notify)
  129. return;
  130. np = calloc(1, sizeof(struct npriv));
  131. np->sync = sync_msg;
  132. npriv_init_defaults(ts->notify, np);
  133. strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
  134. np->msg_id[sizeof(np->ident) - 1] = 0;
  135. strncpy(np->text, msg_text, sizeof(np->text) - 1);
  136. np->text[sizeof(np->text) - 1] = 0;
  137. queue_add(ts->notify->notifications, np);
  138. }
  139. #define MAX_MSG_TEXT 256
  140. void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  141. va_list args;
  142. char msg_text[MAX_MSG_TEXT];
  143. va_start(args, text_fmt);
  144. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  145. msg_text[sizeof(msg_text) - 1] = 0;
  146. va_end(args);
  147. notify_func(ts, 0, msg_id, msg_text);
  148. }
  149. void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  150. va_list args;
  151. char msg_text[MAX_MSG_TEXT];
  152. va_start(args, text_fmt);
  153. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  154. msg_text[sizeof(msg_text) - 1] = 0;
  155. va_end(args);
  156. notify_func(ts, 1, msg_id, msg_text);
  157. }
  158. void notify_free(struct notify **pn) {
  159. struct notify *n = *pn;
  160. if (n) {
  161. queue_add(n->notifications, NULL);
  162. pthread_join(n->thread, NULL);
  163. queue_free(&n->notifications);
  164. FREE(*pn);
  165. }
  166. }