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.2KB

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