fdpass 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,9 +1,13 @@
1
- = Libarchive/Ruby
1
+ = fdpass
2
2
 
3
3
  == Description
4
4
 
5
5
  This is a library to transmit the file descriptor between the processes.
6
6
 
7
+ == Install
8
+
9
+ gem install fdpass
10
+
7
11
  == Example
8
12
  === Server (Reciever)
9
13
  require 'fdpass'
@@ -16,6 +20,7 @@ This is a library to transmit the file descriptor between the processes.
16
20
  fd = fdpass.recv
17
21
  io = IO.open(fd)
18
22
  io.puts('%s: %s' % [fd, __FILE__])
23
+ FDPass.close_fd(fd)
19
24
  end
20
25
  }
21
26
 
data/ext/extconf.rb CHANGED
File without changes
data/ext/fdpass.c CHANGED
@@ -13,10 +13,20 @@ static VALUE rb_fdpass_client(VALUE self, VALUE path) {
13
13
  return rb_funcall(rb_cFDPassClient, rb_intern("new"), 1, path);
14
14
  }
15
15
 
16
+ static VALUE rd_fdpass_close_fd(VALUE self, VALUE fd) {
17
+ int ifd;
18
+
19
+ ifd = NUM2INT(fd);
20
+ close(ifd);
21
+
22
+ return Qnil;
23
+ }
24
+
16
25
  void Init_fdpass() {
17
26
  rb_mFDPass = rb_define_module("FDPass");
18
27
  rb_define_module_function(rb_mFDPass, "server", rb_fdpass_server, 1);
19
28
  rb_define_module_function(rb_mFDPass, "client", rb_fdpass_client, 1);
29
+ rb_define_module_function(rb_mFDPass, "close_fd", rd_fdpass_close_fd, 1);
20
30
 
21
31
  rb_eFDPassError = rb_define_class_under(rb_mFDPass, "Error", rb_eStandardError);
22
32
 
data/ext/fdpass.h CHANGED
@@ -20,6 +20,19 @@
20
20
  #define RSTRING_LEN(s) (RSTRING(s)->len)
21
21
  #endif
22
22
 
23
+ struct fdpass_socket {
24
+ int sock;
25
+ int closed;
26
+ VALUE path;
27
+ };
28
+
29
+ #define Check_Socket(p) do { \
30
+ if ((p)->sock < 0 || (p)->closed || NIL_P((p)->path)) { \
31
+ rb_raise(rb_eFDPassError, "Invalid socket"); \
32
+ } \
33
+ Check_Type((p)->path, T_STRING); \
34
+ } while(0)
35
+
23
36
  void Init_fdpass_server();
24
37
  void Init_fdpass_client();
25
38
 
data/ext/fdpass_client.c CHANGED
@@ -1,10 +1,50 @@
1
1
  #include "fdpass.h"
2
- #include "fdpass_socket.h"
3
2
 
4
3
  extern VALUE rb_mFDPass;
5
4
  extern VALUE rb_eFDPassError;
6
5
  VALUE rb_cFDPassClient;
7
6
 
7
+ static void fdpass_client_mark(struct fdpass_socket *p) {
8
+ rb_gc_mark(p->path);
9
+ }
10
+
11
+ static void fdpass_client_free(struct fdpass_socket *p) {
12
+ if (!p->closed) {
13
+ close(p->sock);
14
+ }
15
+
16
+ xfree(p);
17
+ }
18
+
19
+ static VALUE fdpass_client_alloc(VALUE klass) {
20
+ struct fdpass_socket *p = ALLOC(struct fdpass_socket);
21
+ p->sock = -1;
22
+ p->closed = 1;
23
+ p->path = Qnil;
24
+ return Data_Wrap_Struct(klass, fdpass_client_mark, fdpass_client_free, p);
25
+ }
26
+
27
+ static VALUE rd_fdpass_client_close(VALUE self) {
28
+ struct fdpass_socket *p;
29
+
30
+ Data_Get_Struct(self, struct fdpass_socket, p);
31
+
32
+ if (!p->closed) {
33
+ close(p->sock);
34
+ p->sock = -1;
35
+ p->closed = 1;
36
+ p->path = Qnil;
37
+ }
38
+
39
+ return Qnil;
40
+ }
41
+
42
+ static VALUE rd_fdpass_client_is_closed(VALUE self) {
43
+ struct fdpass_socket *p;
44
+ Data_Get_Struct(self, struct fdpass_socket, p);
45
+ return p->closed ? Qtrue : Qfalse;
46
+ }
47
+
8
48
  static VALUE rd_fdpass_client_initialize(VALUE self, VALUE path) {
9
49
  struct fdpass_socket *p;
10
50
  char *cpath;
@@ -34,7 +74,6 @@ static VALUE rd_fdpass_client_initialize(VALUE self, VALUE path) {
34
74
  p->sock = sock;
35
75
  p->closed = 0;
36
76
  p->path = path;
37
- p->is_server = 0;
38
77
 
39
78
  return Qnil;
40
79
  }
@@ -80,9 +119,9 @@ static VALUE rb_fdpass_client_send(VALUE self, VALUE fd) {
80
119
 
81
120
  void Init_fdpass_client() {
82
121
  rb_cFDPassClient = rb_define_class_under(rb_mFDPass, "Client", rb_cObject);
83
- rb_define_alloc_func(rb_cFDPassClient, fdpass_socket_alloc);
122
+ rb_define_alloc_func(rb_cFDPassClient, fdpass_client_alloc);
84
123
  rb_define_private_method(rb_cFDPassClient, "initialize", rd_fdpass_client_initialize, 1);
85
- rb_define_method(rb_cFDPassClient, "close", rd_fdpass_socket_close, 0);
86
- rb_define_method(rb_cFDPassClient, "closed?", rd_fdpass_socket_is_closed, 0);
124
+ rb_define_method(rb_cFDPassClient, "close", rd_fdpass_client_close, 0);
125
+ rb_define_method(rb_cFDPassClient, "closed?", rd_fdpass_client_is_closed, 0);
87
126
  rb_define_method(rb_cFDPassClient, "send", rb_fdpass_client_send, 1);
88
- }
127
+ }
data/ext/fdpass_server.c CHANGED
@@ -1,10 +1,70 @@
1
1
  #include "fdpass.h"
2
- #include "fdpass_socket.h"
3
2
 
4
3
  extern VALUE rb_mFDPass;
5
4
  extern VALUE rb_eFDPassError;
6
5
  VALUE rb_cFDPassServer;
7
6
 
7
+ static void unlink_socket(struct fdpass_socket *p) {
8
+ #ifdef S_ISSOCK
9
+ char *cpath;
10
+ struct stat st;
11
+
12
+ if (NIL_P(p->path)) {
13
+ return;
14
+ }
15
+
16
+ Check_Type(p->path, T_STRING);
17
+ cpath = RSTRING_PTR(p->path);
18
+
19
+ if (stat(cpath, &st) == 0 && S_ISSOCK(st.st_mode)) {
20
+ unlink(cpath);
21
+ }
22
+ #endif
23
+ }
24
+
25
+ static void fdpass_server_mark(struct fdpass_socket *p) {
26
+ rb_gc_mark(p->path);
27
+ }
28
+
29
+ static void fdpass_server_free(struct fdpass_socket *p) {
30
+ if (!p->closed) {
31
+ close(p->sock);
32
+ unlink_socket(p);
33
+ }
34
+
35
+ xfree(p);
36
+ }
37
+
38
+ static VALUE fdpass_server_alloc(VALUE klass) {
39
+ struct fdpass_socket *p = ALLOC(struct fdpass_socket);
40
+ p->sock = -1;
41
+ p->closed = 1;
42
+ p->path = Qnil;
43
+ return Data_Wrap_Struct(klass, fdpass_server_mark, fdpass_server_free, p);
44
+ }
45
+
46
+ static VALUE rd_fdpass_server_close(VALUE self) {
47
+ struct fdpass_socket *p;
48
+
49
+ Data_Get_Struct(self, struct fdpass_socket, p);
50
+
51
+ if (!p->closed) {
52
+ close(p->sock);
53
+ unlink_socket(p);
54
+ p->sock = -1;
55
+ p->closed = 1;
56
+ p->path = Qnil;
57
+ }
58
+
59
+ return Qnil;
60
+ }
61
+
62
+ static VALUE rd_fdpass_server_is_closed(VALUE self) {
63
+ struct fdpass_socket *p;
64
+ Data_Get_Struct(self, struct fdpass_socket, p);
65
+ return p->closed ? Qtrue : Qfalse;
66
+ }
67
+
8
68
  static VALUE rd_fdpass_server_initialize(VALUE self, VALUE path) {
9
69
  struct fdpass_socket *p;
10
70
  char *cpath;
@@ -34,7 +94,6 @@ static VALUE rd_fdpass_server_initialize(VALUE self, VALUE path) {
34
94
  p->sock = sock;
35
95
  p->closed = 0;
36
96
  p->path = path;
37
- p->is_server = 1;
38
97
 
39
98
  return Qnil;
40
99
  }
@@ -73,9 +132,9 @@ static VALUE rb_fdpass_server_recv(VALUE self) {
73
132
 
74
133
  void Init_fdpass_server() {
75
134
  rb_cFDPassServer = rb_define_class_under(rb_mFDPass, "Server", rb_cObject);
76
- rb_define_alloc_func(rb_cFDPassServer, fdpass_socket_alloc);
135
+ rb_define_alloc_func(rb_cFDPassServer, fdpass_server_alloc);
77
136
  rb_define_private_method(rb_cFDPassServer, "initialize", rd_fdpass_server_initialize, 1);
78
- rb_define_method(rb_cFDPassServer, "close", rd_fdpass_socket_close, 0);
79
- rb_define_method(rb_cFDPassServer, "closed?", rd_fdpass_socket_is_closed, 0);
137
+ rb_define_method(rb_cFDPassServer, "close", rd_fdpass_server_close, 0);
138
+ rb_define_method(rb_cFDPassServer, "closed?", rd_fdpass_server_is_closed, 0);
80
139
  rb_define_method(rb_cFDPassServer, "recv", rb_fdpass_server_recv, 0);
81
- }
140
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fdpass
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
4
+ hash: 25
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-01 00:00:00 +09:00
18
+ date: 2011-01-08 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,16 +28,14 @@ extensions:
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
- - ext/fdpass.c
32
31
  - ext/fdpass_client.c
33
32
  - ext/fdpass_server.c
34
- - ext/fdpass_socket.c
33
+ - ext/fdpass.c
35
34
  - ext/fdpass.h
36
- - ext/fdpass_socket.h
37
35
  - ext/extconf.rb
38
36
  - README
39
37
  has_rdoc: true
40
- homepage: https://github.com/winebarrel/fdpass
38
+ homepage: https://bitbucket.org/winebarrel/fdpass
41
39
  licenses: []
42
40
 
43
41
  post_install_message:
@@ -66,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
64
  requirements: []
67
65
 
68
66
  rubyforge_project:
69
- rubygems_version: 1.3.7
67
+ rubygems_version: 1.4.2
70
68
  signing_key:
71
69
  specification_version: 3
72
70
  summary: This is a library to transmit the file descriptor between the processes.
data/ext/fdpass_socket.c DELETED
@@ -1,64 +0,0 @@
1
- #include "fdpass_socket.h"
2
-
3
- extern VALUE rb_eFDPassError;
4
-
5
- static void unlink_socket(struct fdpass_socket *p) {
6
- #ifdef S_ISSOCK
7
- char *cpath;
8
- struct stat st;
9
-
10
- if (!p->is_server || NIL_P(p->path)) {
11
- return;
12
- }
13
-
14
- Check_Type(p->path, T_STRING);
15
- cpath = RSTRING_PTR(p->path);
16
-
17
- if (stat(cpath, &st) == 0 && S_ISSOCK(st.st_mode)) {
18
- unlink(cpath);
19
- }
20
- #endif
21
- }
22
-
23
- static void fdpass_socket_mark(struct fdpass_socket *p) {
24
- rb_gc_mark(p->path);
25
- }
26
-
27
- static void fdpass_socket_free(struct fdpass_socket *p) {
28
- if (!p->closed) {
29
- close(p->sock);
30
- unlink_socket(p);
31
- }
32
-
33
- xfree(p);
34
- }
35
-
36
- VALUE fdpass_socket_alloc(VALUE klass) {
37
- struct fdpass_socket *p = ALLOC(struct fdpass_socket);
38
- p->sock = -1;
39
- p->closed = 1;
40
- p->path = Qnil;
41
- return Data_Wrap_Struct(klass, fdpass_socket_mark, fdpass_socket_free, p);
42
- }
43
-
44
- VALUE rd_fdpass_socket_close(VALUE self) {
45
- struct fdpass_socket *p;
46
-
47
- Data_Get_Struct(self, struct fdpass_socket, p);
48
-
49
- if (!p->closed) {
50
- close(p->sock);
51
- unlink_socket(p);
52
- p->sock = -1;
53
- p->closed = 1;
54
- p->path = Qnil;
55
- }
56
-
57
- return Qnil;
58
- }
59
-
60
- VALUE rd_fdpass_socket_is_closed(VALUE self) {
61
- struct fdpass_socket *p;
62
- Data_Get_Struct(self, struct fdpass_socket, p);
63
- return p->closed ? Qtrue : Qfalse;
64
- }
data/ext/fdpass_socket.h DELETED
@@ -1,24 +0,0 @@
1
- #ifndef __FDPASS_SOCKET_H__
2
- #define __FDPASS_SOCKET_C__
3
-
4
- #include "fdpass.h"
5
-
6
- struct fdpass_socket {
7
- int sock;
8
- int closed;
9
- VALUE path;
10
- int is_server;
11
- };
12
-
13
- #define Check_Socket(p) do { \
14
- if ((p)->sock < 0 || (p)->closed || NIL_P((p)->path)) { \
15
- rb_raise(rb_eFDPassError, "Invalid socket"); \
16
- } \
17
- Check_Type((p)->path, T_STRING); \
18
- } while(0)
19
-
20
- VALUE fdpass_socket_alloc(VALUE klass);
21
- VALUE rd_fdpass_socket_close(VALUE self);
22
- VALUE rd_fdpass_socket_is_closed(VALUE self);
23
-
24
- #endif // __FDPASS_SOCKET_H__