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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 {
  75. waitpid(pid, NULL, 0);
  76. }
  77. munmap(shared, sizeof(struct npriv));
  78. OUT:
  79. free(data);
  80. pthread_exit(EXIT_SUCCESS);
  81. }
  82. static void *notify_thread(void *data) {
  83. struct notify *n = data;
  84. set_thread_name("tsdec-notify");
  85. while (1) {
  86. struct npriv *np = queue_get(n->notifications); // Waits...
  87. if (!np)
  88. break;
  89. pthread_t notifier; // The notifier frees the data
  90. if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
  91. perror("pthread_create");
  92. free(np);
  93. } else {
  94. if (np->sync)
  95. pthread_join(notifier, NULL);
  96. else
  97. pthread_detach(notifier);
  98. }
  99. }
  100. pthread_exit(EXIT_SUCCESS);
  101. }
  102. /* ======================================================================== */
  103. struct notify *notify_alloc(struct ts *ts) {
  104. unsigned int i;
  105. if (!ts->ident[0] || !ts->notify_program[0])
  106. return NULL;
  107. struct notify *n = calloc(1, sizeof(struct notify));
  108. n->notifications = queue_new();
  109. strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
  110. for (i=0; i<strlen(n->ident); i++) {
  111. if (n->ident[i] == '/')
  112. n->ident[i] = '-';
  113. }
  114. strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
  115. pthread_create(&n->thread, NULL , &notify_thread, n);
  116. return n;
  117. }
  118. static void npriv_init_defaults(struct notify *n, struct npriv *np) {
  119. strncpy(np->program, n->program, sizeof(np->program) - 1);
  120. strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
  121. }
  122. static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  123. struct npriv *np;
  124. if (!ts->notify)
  125. return;
  126. np = calloc(1, sizeof(struct npriv));
  127. np->sync = sync_msg;
  128. npriv_init_defaults(ts->notify, np);
  129. strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
  130. np->msg_id[sizeof(np->ident) - 1] = 0;
  131. strncpy(np->text, msg_text, sizeof(np->text) - 1);
  132. np->text[sizeof(np->text) - 1] = 0;
  133. queue_add(ts->notify->notifications, np);
  134. }
  135. #define MAX_MSG_TEXT 256
  136. void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  137. va_list args;
  138. char msg_text[MAX_MSG_TEXT];
  139. va_start(args, text_fmt);
  140. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  141. msg_text[sizeof(msg_text) - 1] = 0;
  142. va_end(args);
  143. notify_func(ts, 0, msg_id, msg_text);
  144. }
  145. void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  146. va_list args;
  147. char msg_text[MAX_MSG_TEXT];
  148. va_start(args, text_fmt);
  149. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  150. msg_text[sizeof(msg_text) - 1] = 0;
  151. va_end(args);
  152. notify_func(ts, 1, msg_id, msg_text);
  153. }
  154. void notify_free(struct notify **pn) {
  155. struct notify *n = *pn;
  156. if (n) {
  157. queue_add(n->notifications, NULL);
  158. pthread_join(n->thread, NULL);
  159. queue_free(&n->notifications);
  160. FREE(*pn);
  161. }
  162. }