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.

241 lines
6.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xioinitialize.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for the initialize function */
  5. #include "xiosysincludes.h"
  6. #include "xioopen.h"
  7. #include "xiolockfile.h"
  8. #include "xio-openssl.h" /* xio_reset_fips_mode() */
  9. static int xioinitialized;
  10. xiofile_t *sock[XIO_MAXSOCK];
  11. int (*xiohook_newchild)(void); /* xio calls this function from a new child
  12. process */
  13. int num_child = 0;
  14. /* returns 0 on success or != if an error occurred */
  15. int xioinitialize(void) {
  16. if (xioinitialized) return 0;
  17. /* configure and .h's cannot guarantee this */
  18. assert(sizeof(uint8_t)==1);
  19. assert(sizeof(uint16_t)==2);
  20. assert(sizeof(uint32_t)==4);
  21. /* assertions regarding O_ flags - important for XIO_READABLE() etc. */
  22. assert(O_RDONLY==0);
  23. assert(O_WRONLY==1);
  24. assert(O_RDWR==2);
  25. assert(SHUT_RD==0);
  26. assert(SHUT_WR==1);
  27. assert(SHUT_RDWR==2);
  28. /* some assertions about termios */
  29. #if WITH_TERMIOS
  30. #if defined(CRDLY) && CRDLY_SHIFT >= 0
  31. assert(3 << opt_crdly.arg3 == CRDLY);
  32. #endif
  33. #if defined(TABDLY) && TABDLY_SHIFT >= 0
  34. assert(3 << opt_tabdly.arg3 == TABDLY);
  35. #endif
  36. #if CSIZE_SHIFT >= 0
  37. assert(3 << opt_csize.arg3 == CSIZE);
  38. #endif
  39. {
  40. union {
  41. struct termios termarg;
  42. tcflag_t flags[4];
  43. #if HAVE_TERMIOS_ISPEED
  44. speed_t speeds[sizeof(struct termios)/sizeof(speed_t)];
  45. #endif
  46. } tdata;
  47. tdata.termarg.c_iflag = 0x12345678;
  48. tdata.termarg.c_oflag = 0x23456789;
  49. tdata.termarg.c_cflag = 0x3456789a;
  50. tdata.termarg.c_lflag = 0x456789ab;
  51. assert(tdata.termarg.c_iflag == tdata.flags[0]);
  52. assert(tdata.termarg.c_oflag == tdata.flags[1]);
  53. assert(tdata.termarg.c_cflag == tdata.flags[2]);
  54. assert(tdata.termarg.c_lflag == tdata.flags[3]);
  55. }
  56. #endif
  57. /* these dependencies required in applyopts() for OFUNC_FCNTL */
  58. assert(F_GETFD == F_SETFD-1);
  59. assert(F_GETFL == F_SETFL-1);
  60. {
  61. const char *default_ip;
  62. default_ip = getenv("SOCAT_DEFAULT_LISTEN_IP");
  63. if (default_ip != NULL) {
  64. switch (default_ip[0]) {
  65. case '4':
  66. case '6':
  67. xioopts.default_ip = default_ip[0]; break;
  68. }
  69. }
  70. }
  71. {
  72. const char *preferred_ip;
  73. preferred_ip = getenv("SOCAT_PREFERRED_RESOLVE_IP");
  74. if (preferred_ip != NULL) {
  75. switch (preferred_ip[0]) {
  76. case '4':
  77. case '6':
  78. xioopts.preferred_ip = preferred_ip[0]; break;
  79. default:
  80. xioopts.preferred_ip = '0'; break;
  81. }
  82. }
  83. }
  84. if (Atexit(xioexit) < 0) {
  85. Error("atexit(xioexit) failed");
  86. return -1;
  87. }
  88. xioinitialized = 1;
  89. return 0;
  90. }
  91. /* call this function when option -lp (reset program name) has been applied */
  92. int xioinitialize2(void) {
  93. pid_t pid = Getpid();
  94. xiosetenvulong("PID", pid, 1);
  95. xiosetenvulong("PPID", pid, 1);
  96. return 0;
  97. }
  98. /* well, this function is not for initialization, but I could not find a better
  99. place for it
  100. it is called in the child process after fork
  101. it drops the locks of the xiofile's so only the parent owns them
  102. */
  103. void xiodroplocks(void) {
  104. int i;
  105. for (i = 0; i < XIO_MAXSOCK; ++i) {
  106. if (sock[i] != NULL && sock[i]->tag != XIO_TAG_INVALID) {
  107. xiofiledroplock(sock[i]);
  108. }
  109. }
  110. }
  111. /* consider an invokation like this:
  112. socat -u exec:'some program that accepts data' tcp-l:...,fork
  113. we do not want the program to be killed by the first tcp-l sub process, it's
  114. better if it survives all sub processes. Thus, it must not be killed when
  115. the sub process delivers EOF. Also, a socket that is reused in sub processes
  116. should not be shut down (affects the connection), but closed (affects only
  117. sub processes copy of file descriptor) */
  118. static int xio_nokill(xiofile_t *sock) {
  119. int result = 0;
  120. switch (sock->tag) {
  121. case XIO_TAG_INVALID:
  122. default:
  123. return -1;
  124. case XIO_TAG_DUAL:
  125. if ((result = xio_nokill((xiofile_t *)sock->dual.stream[0])) != 0)
  126. return result;
  127. result = xio_nokill((xiofile_t *)sock->dual.stream[1]);
  128. break;
  129. case XIO_TAG_RDONLY:
  130. case XIO_TAG_WRONLY:
  131. case XIO_TAG_RDWR:
  132. /* here is the core of this function */
  133. switch (sock->stream.howtoend) {
  134. case END_SHUTDOWN_KILL: sock->stream.howtoend = END_CLOSE; break;
  135. case END_CLOSE_KILL: sock->stream.howtoend = END_CLOSE; break;
  136. case END_SHUTDOWN: sock->stream.howtoend = END_CLOSE; break;
  137. default: break;
  138. }
  139. break;
  140. }
  141. return result;
  142. }
  143. /* call this function immediately after fork() in child process */
  144. /* it performs some neccessary actions
  145. returns 0 on success or != 0 if an error occurred */
  146. int xio_forked_inchild(void) {
  147. int result = 0;
  148. int i;
  149. diag_fork();
  150. for (i=0; i<NUMUNKNOWN; ++i) {
  151. diedunknown[i] = 0;
  152. }
  153. num_child = 0;
  154. xiodroplocks();
  155. #if WITH_FIPS
  156. if (xio_reset_fips_mode() != 0) {
  157. result = 1;
  158. }
  159. #endif /* WITH_FIPS */
  160. /* some locks belong to parent process, so "drop" them now */
  161. if (xiohook_newchild) {
  162. if ((*xiohook_newchild)() != 0) {
  163. Exit(1);
  164. }
  165. }
  166. /* change XIO_SHUTDOWN_KILL to XIO_SHUTDOWN */
  167. if (sock1 != NULL) {
  168. int result2;
  169. result2 = xio_nokill(sock1);
  170. if (result2 < 0) Exit(1);
  171. result |= result2;
  172. }
  173. return result;
  174. }
  175. /* subchild != 0 means that the current process is already a child process of
  176. the master process and thus the new sub child process should not set the
  177. SOCAT_PID variable */
  178. pid_t xio_fork(bool subchild, int level) {
  179. pid_t pid;
  180. const char *forkwaitstring;
  181. int forkwaitsecs = 0;
  182. if ((pid = Fork()) < 0) {
  183. Msg1(level, "fork(): %s", strerror(errno));
  184. return pid;
  185. }
  186. if (pid == 0) { /* child process */
  187. pid_t cpid = Getpid();
  188. Info1("just born: child process "F_pid, cpid);
  189. if (!subchild) {
  190. /* set SOCAT_PID to new value */
  191. xiosetenvulong("PID", pid, 1);
  192. }
  193. /* gdb recommends to have env controlled sleep after fork */
  194. if (forkwaitstring = getenv("SOCAT_FORK_WAIT")) {
  195. forkwaitsecs = atoi(forkwaitstring);
  196. Sleep(forkwaitsecs);
  197. }
  198. if (xio_forked_inchild() != 0) {
  199. Exit(1);
  200. }
  201. return 0;
  202. }
  203. num_child++;
  204. /* parent process */
  205. Notice1("forked off child process "F_pid, pid);
  206. /* gdb recommends to have env controlled sleep after fork */
  207. if (forkwaitstring = getenv("SOCAT_FORK_WAIT")) {
  208. forkwaitsecs = atoi(forkwaitstring);
  209. Sleep(forkwaitsecs);
  210. }
  211. return pid;
  212. }