kamunix.c 3.4 KB

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