remove autopush support and make it a no-op
[kgio.git] / ext / kgio / kgio_ext.c
blobc3e59ec7793cdefe6efe19eb39461f58d07677a8
1 #include "kgio.h"
2 #include <sys/utsname.h>
3 #include <stdio.h>
4 /* true if TCP Fast Open is usable */
5 unsigned kgio_tfo;
6 static VALUE eErrno_EPIPE, eErrno_ECONNRESET;
7 static ID id_set_backtrace;
9 static void tfo_maybe(void)
11 VALUE mKgio = rb_define_module("Kgio");
13 /* Deal with the case where system headers have not caught up */
14 if (KGIO_TFO_MAYBE) {
15 /* Ensure Linux 3.7 or later for TCP_FASTOPEN */
16 struct utsname buf;
17 unsigned maj, min;
19 if (uname(&buf) != 0)
20 rb_sys_fail("uname");
21 if (sscanf(buf.release, "%u.%u", &maj, &min) != 2)
22 return;
23 if (maj < 3 || (maj == 3 && min < 7))
24 return;
28 * KGIO_TFO_MAYBE will be false if a distro backports TFO
29 * to a pre-3.7 kernel, but includes the necessary constants
30 * in system headers
32 #if defined(MSG_FASTOPEN) && defined(TCP_FASTOPEN)
33 rb_define_const(mKgio, "TCP_FASTOPEN", INT2NUM(TCP_FASTOPEN));
34 rb_define_const(mKgio, "MSG_FASTOPEN", INT2NUM(MSG_FASTOPEN));
35 kgio_tfo = 1;
36 #endif
39 void kgio_raise_empty_bt(VALUE err, const char *msg)
41 VALUE exc = rb_exc_new2(err, msg);
42 VALUE bt = rb_ary_new();
44 rb_funcall(exc, id_set_backtrace, 1, bt);
45 rb_exc_raise(exc);
48 void kgio_wr_sys_fail(const char *msg)
50 switch (errno) {
51 case EPIPE:
52 errno = 0;
53 kgio_raise_empty_bt(eErrno_EPIPE, msg);
54 case ECONNRESET:
55 errno = 0;
56 kgio_raise_empty_bt(eErrno_ECONNRESET, msg);
58 rb_sys_fail(msg);
61 void kgio_rd_sys_fail(const char *msg)
63 if (errno == ECONNRESET) {
64 errno = 0;
65 kgio_raise_empty_bt(eErrno_ECONNRESET, msg);
67 rb_sys_fail(msg);
70 void Init_kgio_ext(void)
72 VALUE mKgio = rb_define_module("Kgio");
73 VALUE mPipeMethods = rb_define_module_under(mKgio, "PipeMethods");
74 VALUE mSocketMethods = rb_define_module_under(mKgio, "SocketMethods");
75 VALUE mWaiters = rb_define_module_under(mKgio, "DefaultWaiters");
77 id_set_backtrace = rb_intern("set_backtrace");
78 eErrno_EPIPE = rb_const_get(rb_mErrno, rb_intern("EPIPE"));
79 eErrno_ECONNRESET = rb_const_get(rb_mErrno, rb_intern("ECONNRESET"));
82 * Returns the client IP address of the socket as a string
83 * (e.g. "127.0.0.1" or "::1").
84 * This is always the value of the Kgio::LOCALHOST constant
85 * for UNIX domain sockets.
87 rb_define_attr(mSocketMethods, "kgio_addr", 1, 1);
88 rb_include_module(mPipeMethods, mWaiters);
89 rb_include_module(mSocketMethods, mWaiters);
91 tfo_maybe();
92 init_kgio_wait();
93 init_kgio_read();
94 init_kgio_write();
95 init_kgio_writev();
96 init_kgio_connect();
97 init_kgio_accept();
98 init_kgio_poll();
99 init_kgio_tryopen();