2
0

kamunix.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2004 FhG FOKUS
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <errno.h>
  30. /* AF_LOCAL is not defined on solaris */
  31. #if !defined(AF_LOCAL)
  32. #define AF_LOCAL AF_UNIX
  33. #endif
  34. #if !defined(PF_LOCAL)
  35. #define PF_LOCAL PF_UNIX
  36. #endif
  37. /* solaris doesn't have SUN_LEN */
  38. #ifndef SUN_LEN
  39. #define SUN_LEN(sa) ( strlen((sa)->sun_path) + \
  40. (size_t)(((struct sockaddr_un*)0)->sun_path) )
  41. #endif
  42. #define BUF_SIZE 65536
  43. #define DEFAULT_TIMEOUT 5
  44. int main(int argc, char** argv)
  45. {
  46. int sock, len;
  47. socklen_t from_len;
  48. struct sockaddr_un from, to;
  49. char name[256];
  50. static char buffer[BUF_SIZE];
  51. char *chroot_dir;
  52. if (argc != 2) {
  53. printf("Usage: %s <path_to_socket>\n", argv[0]);
  54. return 1;
  55. }
  56. sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
  57. if (sock == -1) {
  58. fprintf(stderr, "Error while opening socket: %s\n", strerror(errno));
  59. return -1;
  60. }
  61. memset(&from, 0, sizeof(from));
  62. from.sun_family = PF_LOCAL;
  63. chroot_dir = getenv("CHROOT_DIR");
  64. if (chroot_dir == NULL)
  65. chroot_dir = "";
  66. sprintf(name, "%s/tmp/Kamailio.%d.XXXXXX", chroot_dir, getpid());
  67. umask(0);
  68. /* set mode to 0666 for when Kamailio is running as non-root user
  69. * and kamctl is running as root */
  70. if (mkstemp(name) == -1) {
  71. fprintf(stderr, "Error in mkstemp with name=%s: %s\n",
  72. name, strerror(errno));
  73. return -2;
  74. }
  75. if (unlink(name) == -1) {
  76. fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
  77. return -2;
  78. }
  79. strncpy(from.sun_path, name, strlen(name));
  80. if (bind(sock, (struct sockaddr*)&from, SUN_LEN(&from)) == -1) {
  81. fprintf(stderr, "Error in bind: %s\n", strerror(errno));
  82. goto err;
  83. }
  84. memset(&to, 0, sizeof(to));
  85. to.sun_family = PF_LOCAL;
  86. strncpy(to.sun_path, argv[1], sizeof(to.sun_path) - 1);
  87. len = fread(buffer, 1, BUF_SIZE, stdin);
  88. if (len) {
  89. if (sendto(sock, buffer, len, 0, (struct sockaddr*)&to,
  90. SUN_LEN(&to)) == -1) {
  91. fprintf(stderr, "Error in sendto: %s\n", strerror(errno));
  92. goto err;
  93. }
  94. from_len = sizeof(from);
  95. len = recvfrom(sock, buffer, BUF_SIZE, 0,
  96. (struct sockaddr*)&from, &from_len);
  97. if (len == -1) {
  98. fprintf(stderr, "Error in recvfrom: %s\n", strerror(errno));
  99. goto err;
  100. }
  101. fprintf(stdout, "%.*s", len, buffer);
  102. } else {
  103. fprintf(stderr, "Nothing to send\n");
  104. goto err;
  105. }
  106. close(sock);
  107. if (unlink(name) == -1)
  108. fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
  109. return 0;
  110. err:
  111. close(sock);
  112. if (unlink(name) == -1)
  113. fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
  114. return -1;
  115. }