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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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[512];
  32. char program[512];
  33. char msg_id[512];
  34. char text[512];
  35. int sync; /* Wait for message to be delivered */
  36. };
  37. static void *do_notify(void *in) {
  38. struct npriv *data = in;
  39. struct npriv *shared = mmap(NULL, sizeof(struct npriv), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
  40. if (!shared) {
  41. perror("mmap");
  42. goto OUT;
  43. }
  44. *shared = *data;
  45. pid_t pid = fork();
  46. if (pid==0) { // child process
  47. char *args[] = { shared->program, shared->ident, NULL };
  48. int e = 0;
  49. unsigned int i, r;
  50. char **env = calloc(32, sizeof(char *));
  51. asprintf(&env[e++], "_TS=%ld" , time(NULL));
  52. asprintf(&env[e++], "_IDENT=%s" , shared->ident);
  53. asprintf(&env[e++], "_MESSAGE_ID=%s" , shared->msg_id);
  54. asprintf(&env[e++], "_MESSAGE_TEXT=%s" , shared->text);
  55. r = strlen(shared->msg_id);
  56. for (i=0; i<r; i++) {
  57. if (isalpha(shared->msg_id[i]))
  58. shared->msg_id[i] = tolower(shared->msg_id[i]);
  59. if (shared->msg_id[i] == '_')
  60. shared->msg_id[i] = ' ';
  61. }
  62. asprintf(&env[e++], "_MESSAGE_MSG=%s" , shared->msg_id);
  63. execve(args[0], args, env);
  64. // We reach here only if there is an error.
  65. fprintf(stderr, "execve('%s') failed: %s!\n", args[0], strerror(errno));
  66. do {
  67. free(env[e--]);
  68. } while (e);
  69. free(env);
  70. exit(EXIT_FAILURE);
  71. } else if (pid < 0) {
  72. fprintf(stderr, "fork() failed: %s\n", strerror(errno));
  73. } else {
  74. waitpid(pid, NULL, 0);
  75. }
  76. munmap(shared, sizeof(struct npriv));
  77. OUT:
  78. free(data);
  79. pthread_exit(EXIT_SUCCESS);
  80. }
  81. static void *notify_thread(void *data) {
  82. struct notify *n = data;
  83. set_thread_name("tsdec-notify");
  84. while (1) {
  85. struct npriv *np = queue_get(n->notifications); // Waits...
  86. if (!np)
  87. break;
  88. pthread_t notifier; // The notifier frees the data
  89. if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
  90. perror("pthread_create");
  91. free(np);
  92. } else {
  93. if (np->sync)
  94. pthread_join(notifier, NULL);
  95. else
  96. pthread_detach(notifier);
  97. }
  98. }
  99. pthread_exit(EXIT_SUCCESS);
  100. }
  101. /* ======================================================================== */
  102. struct notify *notify_alloc(struct ts *ts) {
  103. unsigned int i;
  104. if (!ts->ident || !ts->notify_program)
  105. return NULL;
  106. struct notify *n = calloc(1, sizeof(struct notify));
  107. n->notifications = queue_new();
  108. strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
  109. n->ident[sizeof(n->ident) - 1] = '\0';
  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. n->program[sizeof(n->program) - 1] = '\0';
  116. pthread_create(&n->thread, &ts->thread_attr , &notify_thread, n);
  117. return n;
  118. }
  119. static void npriv_init_defaults(struct notify *n, struct npriv *np) {
  120. strncpy(np->program, n->program, sizeof(np->program) - 1);
  121. strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
  122. }
  123. static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  124. struct npriv *np;
  125. if (!ts->notify)
  126. return;
  127. np = calloc(1, sizeof(struct npriv));
  128. np->sync = sync_msg;
  129. npriv_init_defaults(ts->notify, np);
  130. strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
  131. np->msg_id[sizeof(np->ident) - 1] = 0;
  132. strncpy(np->text, msg_text, sizeof(np->text) - 1);
  133. np->text[sizeof(np->text) - 1] = 0;
  134. queue_add(ts->notify->notifications, np);
  135. }
  136. #define MAX_MSG_TEXT 256
  137. void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  138. va_list args;
  139. char msg_text[MAX_MSG_TEXT];
  140. va_start(args, text_fmt);
  141. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  142. msg_text[sizeof(msg_text) - 1] = 0;
  143. va_end(args);
  144. notify_func(ts, 0, msg_id, msg_text);
  145. }
  146. void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  147. va_list args;
  148. char msg_text[MAX_MSG_TEXT];
  149. va_start(args, text_fmt);
  150. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  151. msg_text[sizeof(msg_text) - 1] = 0;
  152. va_end(args);
  153. notify_func(ts, 1, msg_id, msg_text);
  154. }
  155. void notify_free(struct notify **pn) {
  156. struct notify *n = *pn;
  157. if (n) {
  158. queue_add(n->notifications, NULL);
  159. pthread_join(n->thread, NULL);
  160. queue_free(&n->notifications);
  161. FREE(*pn);
  162. }
  163. }