preplay_unicorn 4.3.1.2.gc450

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/.CHANGELOG.old +25 -0
  2. data/.document +29 -0
  3. data/.gitignore +24 -0
  4. data/.mailmap +26 -0
  5. data/.manifest +258 -0
  6. data/.wrongdoc.yml +10 -0
  7. data/Application_Timeouts +77 -0
  8. data/CONTRIBUTORS +35 -0
  9. data/COPYING +674 -0
  10. data/ChangeLog +3839 -0
  11. data/DESIGN +97 -0
  12. data/Documentation/.gitignore +5 -0
  13. data/Documentation/GNUmakefile +30 -0
  14. data/Documentation/unicorn.1.txt +174 -0
  15. data/Documentation/unicorn_rails.1.txt +175 -0
  16. data/FAQ +53 -0
  17. data/GIT-VERSION-FILE +1 -0
  18. data/GIT-VERSION-GEN +40 -0
  19. data/GNUmakefile +294 -0
  20. data/HACKING +134 -0
  21. data/ISSUES +36 -0
  22. data/KNOWN_ISSUES +79 -0
  23. data/LATEST +15 -0
  24. data/LICENSE +64 -0
  25. data/Links +56 -0
  26. data/NEWS +1886 -0
  27. data/PHILOSOPHY +145 -0
  28. data/README +149 -0
  29. data/Rakefile +97 -0
  30. data/SIGNALS +114 -0
  31. data/Sandbox +96 -0
  32. data/TODO +5 -0
  33. data/TUNING +98 -0
  34. data/bin/unicorn +121 -0
  35. data/bin/unicorn_rails +209 -0
  36. data/examples/big_app_gc.rb +2 -0
  37. data/examples/echo.ru +27 -0
  38. data/examples/git.ru +13 -0
  39. data/examples/init.sh +74 -0
  40. data/examples/logger_mp_safe.rb +25 -0
  41. data/examples/logrotate.conf +29 -0
  42. data/examples/nginx.conf +156 -0
  43. data/examples/unicorn.conf.minimal.rb +13 -0
  44. data/examples/unicorn.conf.rb +94 -0
  45. data/ext/unicorn_http/CFLAGS +13 -0
  46. data/ext/unicorn_http/c_util.h +124 -0
  47. data/ext/unicorn_http/common_field_optimization.h +111 -0
  48. data/ext/unicorn_http/ext_help.h +86 -0
  49. data/ext/unicorn_http/extconf.rb +10 -0
  50. data/ext/unicorn_http/global_variables.h +97 -0
  51. data/ext/unicorn_http/httpdate.c +82 -0
  52. data/ext/unicorn_http/unicorn_http.c +4031 -0
  53. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  54. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  55. data/lib/unicorn/app/exec_cgi.rb +154 -0
  56. data/lib/unicorn/app/inetd.rb +109 -0
  57. data/lib/unicorn/app/old_rails/static.rb +59 -0
  58. data/lib/unicorn/app/old_rails.rb +35 -0
  59. data/lib/unicorn/cgi_wrapper.rb +147 -0
  60. data/lib/unicorn/configurator.rb +641 -0
  61. data/lib/unicorn/const.rb +40 -0
  62. data/lib/unicorn/http_request.rb +77 -0
  63. data/lib/unicorn/http_response.rb +45 -0
  64. data/lib/unicorn/http_server.rb +748 -0
  65. data/lib/unicorn/launcher.rb +62 -0
  66. data/lib/unicorn/oob_gc.rb +71 -0
  67. data/lib/unicorn/preread_input.rb +33 -0
  68. data/lib/unicorn/socket_helper.rb +208 -0
  69. data/lib/unicorn/ssl_client.rb +11 -0
  70. data/lib/unicorn/ssl_configurator.rb +104 -0
  71. data/lib/unicorn/ssl_server.rb +42 -0
  72. data/lib/unicorn/stream_input.rb +149 -0
  73. data/lib/unicorn/tee_input.rb +126 -0
  74. data/lib/unicorn/tmpio.rb +29 -0
  75. data/lib/unicorn/util.rb +68 -0
  76. data/lib/unicorn/worker.rb +88 -0
  77. data/lib/unicorn.rb +107 -0
  78. data/local.mk.sample +59 -0
  79. data/man/man1/unicorn.1 +201 -0
  80. data/man/man1/unicorn_rails.1 +208 -0
  81. data/preplay_unicorn.gemspec +44 -0
  82. data/script/isolate_for_tests +50 -0
  83. data/setup.rb +1586 -0
  84. data/t/.gitignore +5 -0
  85. data/t/GNUmakefile +82 -0
  86. data/t/README +42 -0
  87. data/t/bin/content-md5-put +36 -0
  88. data/t/bin/sha1sum.rb +17 -0
  89. data/t/bin/unused_listen +40 -0
  90. data/t/bin/utee +12 -0
  91. data/t/broken-app.ru +12 -0
  92. data/t/detach.ru +11 -0
  93. data/t/env.ru +3 -0
  94. data/t/heartbeat-timeout.ru +12 -0
  95. data/t/my-tap-lib.sh +201 -0
  96. data/t/oob_gc.ru +21 -0
  97. data/t/oob_gc_path.ru +21 -0
  98. data/t/pid.ru +3 -0
  99. data/t/preread_input.ru +17 -0
  100. data/t/rack-input-tests.ru +21 -0
  101. data/t/rails3-app/.gitignore +4 -0
  102. data/t/rails3-app/Gemfile +26 -0
  103. data/t/rails3-app/Rakefile +10 -0
  104. data/t/rails3-app/app/controllers/application_controller.rb +4 -0
  105. data/t/rails3-app/app/helpers/application_helper.rb +2 -0
  106. data/t/rails3-app/app/views/layouts/application.html.erb +14 -0
  107. data/t/rails3-app/config/application.rb +46 -0
  108. data/t/rails3-app/config/boot.rb +6 -0
  109. data/t/rails3-app/config/database.yml +22 -0
  110. data/t/rails3-app/config/environment.rb +5 -0
  111. data/t/rails3-app/config/environments/development.rb +19 -0
  112. data/t/rails3-app/config/environments/production.rb +42 -0
  113. data/t/rails3-app/config/environments/test.rb +32 -0
  114. data/t/rails3-app/config/initializers/backtrace_silencers.rb +7 -0
  115. data/t/rails3-app/config/initializers/inflections.rb +10 -0
  116. data/t/rails3-app/config/initializers/mime_types.rb +5 -0
  117. data/t/rails3-app/config/initializers/secret_token.rb +7 -0
  118. data/t/rails3-app/config/initializers/session_store.rb +8 -0
  119. data/t/rails3-app/config/locales/en.yml +5 -0
  120. data/t/rails3-app/config/routes.rb +58 -0
  121. data/t/rails3-app/config.ru +4 -0
  122. data/t/rails3-app/db/seeds.rb +7 -0
  123. data/t/rails3-app/doc/README_FOR_APP +2 -0
  124. data/t/rails3-app/lib/tasks/.gitkeep +0 -0
  125. data/t/rails3-app/public/404.html +1 -0
  126. data/t/rails3-app/public/500.html +1 -0
  127. data/t/rails3-app/public/x.txt +1 -0
  128. data/t/rails3-app/script/rails +9 -0
  129. data/t/rails3-app/test/performance/browsing_test.rb +9 -0
  130. data/t/rails3-app/test/test_helper.rb +13 -0
  131. data/t/rails3-app/vendor/plugins/.gitkeep +0 -0
  132. data/t/sslgen.sh +71 -0
  133. data/t/t0000-http-basic.sh +50 -0
  134. data/t/t0001-reload-bad-config.sh +53 -0
  135. data/t/t0002-config-conflict.sh +49 -0
  136. data/t/t0002-parser-error.sh +94 -0
  137. data/t/t0003-working_directory.sh +51 -0
  138. data/t/t0004-heartbeat-timeout.sh +69 -0
  139. data/t/t0004-working_directory_broken.sh +24 -0
  140. data/t/t0005-working_directory_app.rb.sh +37 -0
  141. data/t/t0006-reopen-logs.sh +83 -0
  142. data/t/t0006.ru +13 -0
  143. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  144. data/t/t0008-back_out_of_upgrade.sh +110 -0
  145. data/t/t0009-broken-app.sh +56 -0
  146. data/t/t0009-winch_ttin.sh +59 -0
  147. data/t/t0010-reap-logging.sh +55 -0
  148. data/t/t0011-active-unix-socket.sh +79 -0
  149. data/t/t0012-reload-empty-config.sh +85 -0
  150. data/t/t0013-rewindable-input-false.sh +24 -0
  151. data/t/t0013.ru +12 -0
  152. data/t/t0014-rewindable-input-true.sh +24 -0
  153. data/t/t0014.ru +12 -0
  154. data/t/t0015-configurator-internals.sh +25 -0
  155. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  156. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  157. data/t/t0018-write-on-close.sh +23 -0
  158. data/t/t0019-max_header_len.sh +49 -0
  159. data/t/t0020-at_exit-handler.sh +49 -0
  160. data/t/t0021-process_detach.sh +29 -0
  161. data/t/t0100-rack-input-tests.sh +124 -0
  162. data/t/t0116-client_body_buffer_size.sh +80 -0
  163. data/t/t0116.ru +16 -0
  164. data/t/t0300-rails3-basic.sh +28 -0
  165. data/t/t0301-rails3-missing-config-ru.sh +33 -0
  166. data/t/t0302-rails3-alt-working_directory.sh +32 -0
  167. data/t/t0303-rails3-alt-working_directory_config.ru.sh +56 -0
  168. data/t/t0304-rails3-alt-working_directory_no_embed_cli.sh +52 -0
  169. data/t/t0600-https-server-basic.sh +48 -0
  170. data/t/t9000-preread-input.sh +48 -0
  171. data/t/t9001-oob_gc.sh +47 -0
  172. data/t/t9002-oob_gc-path.sh +75 -0
  173. data/t/test-lib.sh +113 -0
  174. data/t/test-rails3.sh +27 -0
  175. data/t/write-on-close.ru +11 -0
  176. data/test/aggregate.rb +15 -0
  177. data/test/benchmark/README +50 -0
  178. data/test/benchmark/dd.ru +18 -0
  179. data/test/benchmark/stack.ru +8 -0
  180. data/test/exec/README +5 -0
  181. data/test/exec/test_exec.rb +1055 -0
  182. data/test/rails/app-1.2.3/.gitignore +2 -0
  183. data/test/rails/app-1.2.3/Rakefile +7 -0
  184. data/test/rails/app-1.2.3/app/controllers/application.rb +6 -0
  185. data/test/rails/app-1.2.3/app/controllers/foo_controller.rb +36 -0
  186. data/test/rails/app-1.2.3/app/helpers/application_helper.rb +4 -0
  187. data/test/rails/app-1.2.3/config/boot.rb +11 -0
  188. data/test/rails/app-1.2.3/config/database.yml +12 -0
  189. data/test/rails/app-1.2.3/config/environment.rb +13 -0
  190. data/test/rails/app-1.2.3/config/environments/development.rb +9 -0
  191. data/test/rails/app-1.2.3/config/environments/production.rb +5 -0
  192. data/test/rails/app-1.2.3/config/routes.rb +6 -0
  193. data/test/rails/app-1.2.3/db/.gitignore +0 -0
  194. data/test/rails/app-1.2.3/log/.gitignore +1 -0
  195. data/test/rails/app-1.2.3/public/404.html +1 -0
  196. data/test/rails/app-1.2.3/public/500.html +1 -0
  197. data/test/rails/app-2.0.2/.gitignore +2 -0
  198. data/test/rails/app-2.0.2/Rakefile +7 -0
  199. data/test/rails/app-2.0.2/app/controllers/application.rb +4 -0
  200. data/test/rails/app-2.0.2/app/controllers/foo_controller.rb +36 -0
  201. data/test/rails/app-2.0.2/app/helpers/application_helper.rb +4 -0
  202. data/test/rails/app-2.0.2/config/boot.rb +11 -0
  203. data/test/rails/app-2.0.2/config/database.yml +12 -0
  204. data/test/rails/app-2.0.2/config/environment.rb +17 -0
  205. data/test/rails/app-2.0.2/config/environments/development.rb +8 -0
  206. data/test/rails/app-2.0.2/config/environments/production.rb +5 -0
  207. data/test/rails/app-2.0.2/config/routes.rb +6 -0
  208. data/test/rails/app-2.0.2/db/.gitignore +0 -0
  209. data/test/rails/app-2.0.2/log/.gitignore +1 -0
  210. data/test/rails/app-2.0.2/public/404.html +1 -0
  211. data/test/rails/app-2.0.2/public/500.html +1 -0
  212. data/test/rails/app-2.1.2/.gitignore +2 -0
  213. data/test/rails/app-2.1.2/Rakefile +7 -0
  214. data/test/rails/app-2.1.2/app/controllers/application.rb +4 -0
  215. data/test/rails/app-2.1.2/app/controllers/foo_controller.rb +36 -0
  216. data/test/rails/app-2.1.2/app/helpers/application_helper.rb +4 -0
  217. data/test/rails/app-2.1.2/config/boot.rb +111 -0
  218. data/test/rails/app-2.1.2/config/database.yml +12 -0
  219. data/test/rails/app-2.1.2/config/environment.rb +17 -0
  220. data/test/rails/app-2.1.2/config/environments/development.rb +7 -0
  221. data/test/rails/app-2.1.2/config/environments/production.rb +5 -0
  222. data/test/rails/app-2.1.2/config/routes.rb +6 -0
  223. data/test/rails/app-2.1.2/db/.gitignore +0 -0
  224. data/test/rails/app-2.1.2/log/.gitignore +1 -0
  225. data/test/rails/app-2.1.2/public/404.html +1 -0
  226. data/test/rails/app-2.1.2/public/500.html +1 -0
  227. data/test/rails/app-2.2.2/.gitignore +2 -0
  228. data/test/rails/app-2.2.2/Rakefile +7 -0
  229. data/test/rails/app-2.2.2/app/controllers/application.rb +4 -0
  230. data/test/rails/app-2.2.2/app/controllers/foo_controller.rb +36 -0
  231. data/test/rails/app-2.2.2/app/helpers/application_helper.rb +4 -0
  232. data/test/rails/app-2.2.2/config/boot.rb +111 -0
  233. data/test/rails/app-2.2.2/config/database.yml +12 -0
  234. data/test/rails/app-2.2.2/config/environment.rb +17 -0
  235. data/test/rails/app-2.2.2/config/environments/development.rb +7 -0
  236. data/test/rails/app-2.2.2/config/environments/production.rb +5 -0
  237. data/test/rails/app-2.2.2/config/routes.rb +6 -0
  238. data/test/rails/app-2.2.2/db/.gitignore +0 -0
  239. data/test/rails/app-2.2.2/log/.gitignore +1 -0
  240. data/test/rails/app-2.2.2/public/404.html +1 -0
  241. data/test/rails/app-2.2.2/public/500.html +1 -0
  242. data/test/rails/test_rails.rb +287 -0
  243. data/test/test_helper.rb +300 -0
  244. data/test/unit/test_configurator.rb +158 -0
  245. data/test/unit/test_droplet.rb +28 -0
  246. data/test/unit/test_http_parser.rb +860 -0
  247. data/test/unit/test_http_parser_ng.rb +716 -0
  248. data/test/unit/test_http_parser_xftrust.rb +38 -0
  249. data/test/unit/test_request.rb +197 -0
  250. data/test/unit/test_response.rb +99 -0
  251. data/test/unit/test_server.rb +289 -0
  252. data/test/unit/test_signals.rb +207 -0
  253. data/test/unit/test_sni_hostnames.rb +47 -0
  254. data/test/unit/test_socket_helper.rb +192 -0
  255. data/test/unit/test_stream_input.rb +204 -0
  256. data/test/unit/test_tee_input.rb +296 -0
  257. data/test/unit/test_upload.rb +306 -0
  258. data/test/unit/test_util.rb +100 -0
  259. metadata +428 -0
@@ -0,0 +1,126 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # acts like tee(1) on an input input to provide a input-like stream
4
+ # while providing rewindable semantics through a File/StringIO backing
5
+ # store. On the first pass, the input is only read on demand so your
6
+ # Rack application can use input notification (upload progress and
7
+ # like). This should fully conform to the Rack::Lint::InputWrapper
8
+ # specification on the public API. This class is intended to be a
9
+ # strict interpretation of Rack::Lint::InputWrapper functionality and
10
+ # will not support any deviations from it.
11
+ #
12
+ # When processing uploads, Unicorn exposes a TeeInput object under
13
+ # "rack.input" of the Rack environment.
14
+ class Unicorn::TeeInput < Unicorn::StreamInput
15
+ # The maximum size (in +bytes+) to buffer in memory before
16
+ # resorting to a temporary file. Default is 112 kilobytes.
17
+ @@client_body_buffer_size = Unicorn::Const::MAX_BODY
18
+
19
+ # sets the maximum size of request bodies to buffer in memory,
20
+ # amounts larger than this are buffered to the filesystem
21
+ def self.client_body_buffer_size=(bytes)
22
+ @@client_body_buffer_size = bytes
23
+ end
24
+
25
+ # returns the maximum size of request bodies to buffer in memory,
26
+ # amounts larger than this are buffered to the filesystem
27
+ def self.client_body_buffer_size
28
+ @@client_body_buffer_size
29
+ end
30
+
31
+ # Initializes a new TeeInput object. You normally do not have to call
32
+ # this unless you are writing an HTTP server.
33
+ def initialize(socket, request)
34
+ @len = request.content_length
35
+ super
36
+ @tmp = @len && @len <= @@client_body_buffer_size ?
37
+ StringIO.new("") : Unicorn::TmpIO.new
38
+ end
39
+
40
+ # :call-seq:
41
+ # ios.size => Integer
42
+ #
43
+ # Returns the size of the input. For requests with a Content-Length
44
+ # header value, this will not read data off the socket and just return
45
+ # the value of the Content-Length header as an Integer.
46
+ #
47
+ # For Transfer-Encoding:chunked requests, this requires consuming
48
+ # all of the input stream before returning since there's no other
49
+ # way to determine the size of the request body beforehand.
50
+ #
51
+ # This method is no longer part of the Rack specification as of
52
+ # Rack 1.2, so its use is not recommended. This method only exists
53
+ # for compatibility with Rack applications designed for Rack 1.1 and
54
+ # earlier. Most applications should only need to call +read+ with a
55
+ # specified +length+ in a loop until it returns +nil+.
56
+ def size
57
+ @len and return @len
58
+ pos = @tmp.pos
59
+ consume!
60
+ @tmp.pos = pos
61
+ @len = @tmp.size
62
+ end
63
+
64
+ # :call-seq:
65
+ # ios.read([length [, buffer ]]) => string, buffer, or nil
66
+ #
67
+ # Reads at most length bytes from the I/O stream, or to the end of
68
+ # file if length is omitted or is nil. length must be a non-negative
69
+ # integer or nil. If the optional buffer argument is present, it
70
+ # must reference a String, which will receive the data.
71
+ #
72
+ # At end of file, it returns nil or "" depend on length.
73
+ # ios.read() and ios.read(nil) returns "".
74
+ # ios.read(length [, buffer]) returns nil.
75
+ #
76
+ # If the Content-Length of the HTTP request is known (as is the common
77
+ # case for POST requests), then ios.read(length [, buffer]) will block
78
+ # until the specified length is read (or it is the last chunk).
79
+ # Otherwise, for uncommon "Transfer-Encoding: chunked" requests,
80
+ # ios.read(length [, buffer]) will return immediately if there is
81
+ # any data and only block when nothing is available (providing
82
+ # IO#readpartial semantics).
83
+ def read(*args)
84
+ @socket ? tee(super) : @tmp.read(*args)
85
+ end
86
+
87
+ # :call-seq:
88
+ # ios.gets => string or nil
89
+ #
90
+ # Reads the next ``line'' from the I/O stream; lines are separated
91
+ # by the global record separator ($/, typically "\n"). A global
92
+ # record separator of nil reads the entire unread contents of ios.
93
+ # Returns nil if called at the end of file.
94
+ # This takes zero arguments for strict Rack::Lint compatibility,
95
+ # unlike IO#gets.
96
+ def gets
97
+ @socket ? tee(super) : @tmp.gets
98
+ end
99
+
100
+ # :call-seq:
101
+ # ios.rewind => 0
102
+ #
103
+ # Positions the *ios* pointer to the beginning of input, returns
104
+ # the offset (zero) of the +ios+ pointer. Subsequent reads will
105
+ # start from the beginning of the previously-buffered input.
106
+ def rewind
107
+ return 0 if 0 == @tmp.size
108
+ consume! if @socket
109
+ @tmp.rewind # Rack does not specify what the return value is here
110
+ end
111
+
112
+ private
113
+
114
+ # consumes the stream of the socket
115
+ def consume!
116
+ junk = ""
117
+ nil while read(@@io_chunk_size, junk)
118
+ end
119
+
120
+ def tee(buffer)
121
+ if buffer && buffer.size > 0
122
+ @tmp.write(buffer)
123
+ end
124
+ buffer
125
+ end
126
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: binary -*-
2
+ # :stopdoc:
3
+ require 'tmpdir'
4
+
5
+ # some versions of Ruby had a broken Tempfile which didn't work
6
+ # well with unlinked files. This one is much shorter, easier
7
+ # to understand, and slightly faster.
8
+ class Unicorn::TmpIO < File
9
+
10
+ # creates and returns a new File object. The File is unlinked
11
+ # immediately, switched to binary mode, and userspace output
12
+ # buffering is disabled
13
+ def self.new
14
+ fp = begin
15
+ super("#{Dir::tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600)
16
+ rescue Errno::EEXIST
17
+ retry
18
+ end
19
+ unlink(fp.path)
20
+ fp.binmode
21
+ fp.sync = true
22
+ fp
23
+ end
24
+
25
+ # for easier env["rack.input"] compatibility with Rack <= 1.1
26
+ def size
27
+ stat.size
28
+ end unless File.method_defined?(:size)
29
+ end
@@ -0,0 +1,68 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ module Unicorn::Util
4
+
5
+ # :stopdoc:
6
+ def self.is_log?(fp)
7
+ append_flags = File::WRONLY | File::APPEND
8
+
9
+ ! fp.closed? &&
10
+ fp.sync &&
11
+ (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
12
+ rescue IOError, Errno::EBADF
13
+ false
14
+ end
15
+
16
+ def self.chown_logs(uid, gid)
17
+ ObjectSpace.each_object(File) do |fp|
18
+ fp.chown(uid, gid) if is_log?(fp)
19
+ end
20
+ end
21
+ # :startdoc:
22
+
23
+ # This reopens ALL logfiles in the process that have been rotated
24
+ # using logrotate(8) (without copytruncate) or similar tools.
25
+ # A +File+ object is considered for reopening if it is:
26
+ # 1) opened with the O_APPEND and O_WRONLY flags
27
+ # 2) the current open file handle does not match its original open path
28
+ # 3) unbuffered (as far as userspace buffering goes, not O_SYNC)
29
+ # Returns the number of files reopened
30
+ #
31
+ # In Unicorn 3.5.x and earlier, files must be opened with an absolute
32
+ # path to be considered a log file.
33
+ def self.reopen_logs
34
+ to_reopen = []
35
+ nr = 0
36
+ ObjectSpace.each_object(File) { |fp| is_log?(fp) and to_reopen << fp }
37
+
38
+ to_reopen.each do |fp|
39
+ orig_st = begin
40
+ fp.stat
41
+ rescue IOError, Errno::EBADF
42
+ next
43
+ end
44
+
45
+ begin
46
+ b = File.stat(fp.path)
47
+ next if orig_st.ino == b.ino && orig_st.dev == b.dev
48
+ rescue Errno::ENOENT
49
+ end
50
+
51
+ begin
52
+ File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
53
+ fp.sync = true
54
+ new_st = fp.stat
55
+
56
+ # this should only happen in the master:
57
+ if orig_st.uid != new_st.uid || orig_st.gid != new_st.gid
58
+ fp.chown(orig_st.uid, orig_st.gid)
59
+ end
60
+
61
+ nr += 1
62
+ rescue IOError, Errno::EBADF
63
+ # not much we can do...
64
+ end
65
+ end
66
+ nr
67
+ end
68
+ end
@@ -0,0 +1,88 @@
1
+ # -*- encoding: binary -*-
2
+ require "raindrops"
3
+
4
+ # This class and its members can be considered a stable interface
5
+ # and will not change in a backwards-incompatible fashion between
6
+ # releases of \Unicorn. Knowledge of this class is generally not
7
+ # not needed for most users of \Unicorn.
8
+ #
9
+ # Some users may want to access it in the before_fork/after_fork hooks.
10
+ # See the Unicorn::Configurator RDoc for examples.
11
+ class Unicorn::Worker
12
+ # :stopdoc:
13
+ attr_accessor :nr, :switched
14
+ attr_writer :tmp
15
+
16
+ PER_DROP = Raindrops::PAGE_SIZE / Raindrops::SIZE
17
+ DROPS = []
18
+
19
+ def initialize(nr)
20
+ drop_index = nr / PER_DROP
21
+ @raindrop = DROPS[drop_index] ||= Raindrops.new(PER_DROP)
22
+ @offset = nr % PER_DROP
23
+ @raindrop[@offset] = 0
24
+ @nr = nr
25
+ @tmp = @switched = false
26
+ end
27
+
28
+ # worker objects may be compared to just plain Integers
29
+ def ==(other_nr) # :nodoc:
30
+ @nr == other_nr
31
+ end
32
+
33
+ # called in the worker process
34
+ def tick=(value) # :nodoc:
35
+ @raindrop[@offset] = value
36
+ end
37
+
38
+ # called in the master process
39
+ def tick # :nodoc:
40
+ @raindrop[@offset]
41
+ end
42
+
43
+ # only exists for compatibility
44
+ def tmp # :nodoc:
45
+ @tmp ||= begin
46
+ tmp = Unicorn::TmpIO.new
47
+ tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
48
+ tmp
49
+ end
50
+ end
51
+
52
+ def close # :nodoc:
53
+ @tmp.close if @tmp
54
+ end
55
+
56
+ # :startdoc:
57
+
58
+ # In most cases, you should be using the Unicorn::Configurator#user
59
+ # directive instead. This method should only be used if you need
60
+ # fine-grained control of exactly when you want to change permissions
61
+ # in your after_fork hooks.
62
+ #
63
+ # Changes the worker process to the specified +user+ and +group+
64
+ # This is only intended to be called from within the worker
65
+ # process from the +after_fork+ hook. This should be called in
66
+ # the +after_fork+ hook after any privileged functions need to be
67
+ # run (e.g. to set per-worker CPU affinity, niceness, etc)
68
+ #
69
+ # Any and all errors raised within this method will be propagated
70
+ # directly back to the caller (usually the +after_fork+ hook.
71
+ # These errors commonly include ArgumentError for specifying an
72
+ # invalid user/group and Errno::EPERM for insufficient privileges
73
+ def user(user, group = nil)
74
+ # we do not protect the caller, checking Process.euid == 0 is
75
+ # insufficient because modern systems have fine-grained
76
+ # capabilities. Let the caller handle any and all errors.
77
+ uid = Etc.getpwnam(user).uid
78
+ gid = Etc.getgrnam(group).gid if group
79
+ Unicorn::Util.chown_logs(uid, gid)
80
+ @tmp.chown(uid, gid) if @tmp
81
+ if gid && Process.egid != gid
82
+ Process.initgroups(user, gid)
83
+ Process::GID.change_privilege(gid)
84
+ end
85
+ Process.euid != uid and Process::UID.change_privilege(uid)
86
+ @switched = true
87
+ end
88
+ end
data/lib/unicorn.rb ADDED
@@ -0,0 +1,107 @@
1
+ # -*- encoding: binary -*-
2
+ require 'fcntl'
3
+ require 'etc'
4
+ require 'stringio'
5
+ require 'rack'
6
+ require 'kgio'
7
+
8
+ # :stopdoc:
9
+ # Unicorn module containing all of the classes (include C extensions) for
10
+ # running a Unicorn web server. It contains a minimalist HTTP server with just
11
+ # enough functionality to service web application requests fast as possible.
12
+ # :startdoc:
13
+
14
+ # \Unicorn exposes very little of an user-visible API and most of its
15
+ # internals are subject to change. \Unicorn is designed to host Rack
16
+ # applications, so applications should be written against the Rack SPEC
17
+ # and not \Unicorn internals.
18
+ module Unicorn
19
+
20
+ # Raised inside TeeInput when a client closes the socket inside the
21
+ # application dispatch. This is always raised with an empty backtrace
22
+ # since there is nothing in the application stack that is responsible
23
+ # for client shutdowns/disconnects. This exception is visible to Rack
24
+ # applications unless PrereadInput middleware is loaded.
25
+ class ClientShutdown < EOFError
26
+ end
27
+
28
+ # :stopdoc:
29
+
30
+ # This returns a lambda to pass in as the app, this does not "build" the
31
+ # app (which we defer based on the outcome of "preload_app" in the
32
+ # Unicorn config). The returned lambda will be called when it is
33
+ # time to build the app.
34
+ def self.builder(ru, op)
35
+ # allow Configurator to parse cli switches embedded in the ru file
36
+ op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
37
+
38
+ # always called after config file parsing, may be called after forking
39
+ lambda do ||
40
+ inner_app = case ru
41
+ when /\.ru$/
42
+ raw = File.read(ru)
43
+ raw.sub!(/^__END__\n.*/, '')
44
+ eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
45
+ else
46
+ require ru
47
+ Object.const_get(File.basename(ru, '.rb').capitalize)
48
+ end
49
+
50
+ pp({ :inner_app => inner_app }) if $DEBUG
51
+
52
+ # return value, matches rackup defaults based on env
53
+ # Unicorn does not support persistent connections, but Rainbows!
54
+ # and Zbatery both do. Users accustomed to the Rack::Server default
55
+ # middlewares will need ContentLength/Chunked middlewares.
56
+ case ENV["RACK_ENV"]
57
+ when "development"
58
+ Rack::Builder.new do
59
+ use Rack::ContentLength
60
+ use Rack::Chunked
61
+ use Rack::CommonLogger, $stderr
62
+ use Rack::ShowExceptions
63
+ use Rack::Lint
64
+ run inner_app
65
+ end.to_app
66
+ when "deployment"
67
+ Rack::Builder.new do
68
+ use Rack::ContentLength
69
+ use Rack::Chunked
70
+ use Rack::CommonLogger, $stderr
71
+ run inner_app
72
+ end.to_app
73
+ else
74
+ inner_app
75
+ end
76
+ end
77
+ end
78
+
79
+ # returns an array of strings representing TCP listen socket addresses
80
+ # and Unix domain socket paths. This is useful for use with
81
+ # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
82
+ def self.listener_names
83
+ Unicorn::HttpServer::LISTENERS.map do |io|
84
+ Unicorn::SocketHelper.sock_name(io)
85
+ end
86
+ end
87
+
88
+ def self.log_error(logger, prefix, exc)
89
+ message = exc.message
90
+ message = message.dump if /[[:cntrl:]]/ =~ message
91
+ logger.error "#{prefix}: #{message} (#{exc.class})"
92
+ exc.backtrace.each { |line| logger.error(line) }
93
+ end
94
+ # :startdoc:
95
+ end
96
+ # :enddoc:
97
+ require 'unicorn/const'
98
+ require 'unicorn/socket_helper'
99
+ require 'unicorn/stream_input'
100
+ require 'unicorn/tee_input'
101
+ require 'unicorn/http_request'
102
+ require 'unicorn/configurator'
103
+ require 'unicorn/tmpio'
104
+ require 'unicorn/util'
105
+ require 'unicorn/http_response'
106
+ require 'unicorn/worker'
107
+ require 'unicorn/http_server'
data/local.mk.sample ADDED
@@ -0,0 +1,59 @@
1
+ # this is the local.mk file used by Eric Wong on his dev boxes.
2
+ # GNUmakefile will source local.mk in the top-level source tree
3
+ # if it is present.
4
+ #
5
+ # This is depends on a bunch of GNU-isms from bash, sed, touch.
6
+
7
+ DLEXT := so
8
+
9
+ # Avoid loading rubygems to speed up tests because gmake is
10
+ # fork+exec heavy with Ruby.
11
+ prefix = $(HOME)
12
+
13
+ # XXX clean this up
14
+ ifeq ($(r192),)
15
+ ifeq ($(r19),)
16
+ ifeq ($(rbx),)
17
+ ifeq ($(r186),)
18
+ RUBY := $(prefix)/bin/ruby
19
+ else
20
+ prefix := $(prefix)/r186-p114
21
+ export PATH := $(prefix)/bin:$(PATH)
22
+ RUBY := $(prefix)/bin/ruby
23
+ endif
24
+ else
25
+ prefix := $(prefix)/rbx
26
+ export PATH := $(prefix)/bin:$(PATH)
27
+ RUBY := $(prefix)/bin/rbx
28
+ endif
29
+ else
30
+ prefix := $(prefix)/ruby-1.9
31
+ export PATH := $(prefix)/bin:$(PATH)
32
+ RUBY := $(prefix)/bin/ruby --disable-gems
33
+ endif
34
+ else
35
+ prefix := $(prefix)/ruby-1.9.2
36
+ export PATH := $(prefix)/bin:$(PATH)
37
+ RUBY := $(prefix)/bin/ruby --disable-gems
38
+ endif
39
+
40
+ # pipefail is THE reason to use bash (v3+) or never revisions of ksh93
41
+ # SHELL := /bin/bash -e -o pipefail
42
+ SHELL := /bin/ksh93 -e -o pipefail
43
+
44
+ full-test: test-18 test-191 test-192 test-rbx test-186
45
+
46
+ # FIXME: keep eye on Rubinius activity and wait for fixes from upstream
47
+ # so we don't need RBX_SKIP anymore
48
+ test-rbx: export RBX_SKIP := 1
49
+ test-rbx: export RUBY := $(RUBY)
50
+ test-rbx:
51
+ $(MAKE) test test-integration rbx=T 2>&1 |sed -e 's!^!rbx !'
52
+ test-186:
53
+ $(MAKE) test-all r186=1 2>&1 |sed 's!^!1.8.6 !'
54
+ test-18:
55
+ $(MAKE) test-all 2>&1 |sed 's!^!1.8 !'
56
+ test-191:
57
+ $(MAKE) test-all r19=1 2>&1 |sed 's!^!1.9.1 !'
58
+ test-192:
59
+ $(MAKE) test-all r192=1 2>&1 |sed 's!^!1.9.2 !'
@@ -0,0 +1,201 @@
1
+ .TH UNICORN 1 "September 15, 2009" "Unicorn User Manual"
2
+ .SH NAME
3
+ .PP
4
+ unicorn - a rackup-like command to launch the Unicorn HTTP server
5
+ .SH SYNOPSIS
6
+ .PP
7
+ unicorn [-c CONFIG_FILE] [-E RACK_ENV] [-D] [RACKUP_FILE]
8
+ .SH DESCRIPTION
9
+ .PP
10
+ A rackup(1)-like command to launch Rack applications using Unicorn.
11
+ It is expected to be started in your application root (APP_ROOT), but
12
+ the "working_directory" directive may be used in the CONFIG_FILE.
13
+ .PP
14
+ While unicorn takes a myriad of command-line options for compatibility
15
+ with ruby(1) and rackup(1), it is recommended to stick to the few
16
+ command-line options specified in the SYNOPSIS and use the CONFIG_FILE
17
+ as much as possible.
18
+ .SH RACKUP FILE
19
+ .PP
20
+ This defaults to "config.ru" in APP_ROOT.
21
+ It should be the same file used by rackup(1) and other Rack launchers,
22
+ it uses the \f[I]Rack::Builder\f[] DSL.
23
+ .PP
24
+ Embedded command-line options are mostly parsed for compatibility with
25
+ rackup(1) but strongly discouraged.
26
+ .SH UNICORN OPTIONS
27
+ .TP
28
+ .B -c, --config-file CONFIG_FILE
29
+ Path to the Unicorn-specific config file.
30
+ The config file is implemented as a Ruby DSL, so Ruby code may executed.
31
+ See the RDoc/ri for the \f[I]Unicorn::Configurator\f[] class for the
32
+ full list of directives available from the DSL.
33
+ Using an absolute path for for CONFIG_FILE is recommended as it makes
34
+ multiple instances of Unicorn easily distinguishable when viewing ps(1)
35
+ output.
36
+ .RS
37
+ .RE
38
+ .TP
39
+ .B -D, --daemonize
40
+ Run daemonized in the background.
41
+ The process is detached from the controlling terminal and stdin is
42
+ redirected to "/dev/null".
43
+ Unlike many common UNIX daemons, we do not chdir to "/" upon
44
+ daemonization to allow more control over the startup/upgrade process.
45
+ Unless specified in the CONFIG_FILE, stderr and stdout will also be
46
+ redirected to "/dev/null".
47
+ .RS
48
+ .RE
49
+ .TP
50
+ .B -E, --env RACK_ENV
51
+ Run under the given RACK_ENV.
52
+ See the RACK ENVIRONMENT section for more details.
53
+ .RS
54
+ .RE
55
+ .TP
56
+ .B -l, --listen ADDRESS
57
+ Listens on a given ADDRESS.
58
+ ADDRESS may be in the form of HOST:PORT or PATH, HOST:PORT is taken to
59
+ mean a TCP socket and PATH is meant to be a path to a UNIX domain
60
+ socket.
61
+ Defaults to "0.0.0.0:8080" (all addresses on TCP port 8080) For
62
+ production deployments, specifying the "listen" directive in CONFIG_FILE
63
+ is recommended as it allows fine-tuning of socket options.
64
+ .RS
65
+ .RE
66
+ .SH RACKUP COMPATIBILITY OPTIONS
67
+ .TP
68
+ .B -o, --host HOST
69
+ Listen on a TCP socket belonging to HOST, default is "0.0.0.0" (all
70
+ addresses).
71
+ If specified multiple times on the command-line, only the last-specified
72
+ value takes effect.
73
+ This option only exists for compatibility with the rackup(1) command,
74
+ use of "-l"/"--listen" switch is recommended instead.
75
+ .RS
76
+ .RE
77
+ .TP
78
+ .B -p, --port PORT
79
+ Listen on the specified TCP PORT, default is 8080.
80
+ If specified multiple times on the command-line, only the last-specified
81
+ value takes effect.
82
+ This option only exists for compatibility with the rackup(1) command,
83
+ use of "-l"/"--listen" switch is recommended instead.
84
+ .RS
85
+ .RE
86
+ .TP
87
+ .B -s, --server SERVER
88
+ No-op, this exists only for compatibility with rackup(1).
89
+ .RS
90
+ .RE
91
+ .SH RUBY OPTIONS
92
+ .TP
93
+ .B -e, --eval LINE
94
+ Evaluate a LINE of Ruby code.
95
+ This evaluation happens immediately as the command-line is being parsed.
96
+ .RS
97
+ .RE
98
+ .TP
99
+ .B -d, --debug
100
+ Turn on debug mode, the $DEBUG variable is set to true.
101
+ .RS
102
+ .RE
103
+ .TP
104
+ .B -w, --warn
105
+ Turn on verbose warnings, the $VERBOSE variable is set to true.
106
+ .RS
107
+ .RE
108
+ .TP
109
+ .B -I, --include PATH
110
+ specify $LOAD_PATH.
111
+ PATH will be prepended to $LOAD_PATH.
112
+ The \[aq]:\[aq] character may be used to delimit multiple directories.
113
+ This directive may be used more than once.
114
+ Modifications to $LOAD_PATH take place immediately and in the order they
115
+ were specified on the command-line.
116
+ .RS
117
+ .RE
118
+ .TP
119
+ .B -r, --require LIBRARY
120
+ require a specified LIBRARY before executing the application.
121
+ The "require" statement will be executed immediately and in the order
122
+ they were specified on the command-line.
123
+ .RS
124
+ .RE
125
+ .SH SIGNALS
126
+ .PP
127
+ The following UNIX signals may be sent to the master process:
128
+ .IP \[bu] 2
129
+ HUP - reload config file, app, and gracefully restart all workers
130
+ .IP \[bu] 2
131
+ INT/TERM - quick shutdown, kills all workers immediately
132
+ .IP \[bu] 2
133
+ QUIT - graceful shutdown, waits for workers to finish their current
134
+ request before finishing.
135
+ .IP \[bu] 2
136
+ USR1 - reopen all logs owned by the master and all workers See
137
+ Unicorn::Util.reopen_logs for what is considered a log.
138
+ .IP \[bu] 2
139
+ USR2 - reexecute the running binary.
140
+ A separate QUIT should be sent to the original process once the child is
141
+ verified to be up and running.
142
+ .IP \[bu] 2
143
+ WINCH - gracefully stops workers but keep the master running.
144
+ This will only work for daemonized processes.
145
+ .IP \[bu] 2
146
+ TTIN - increment the number of worker processes by one
147
+ .IP \[bu] 2
148
+ TTOU - decrement the number of worker processes by one
149
+ .PP
150
+ See the SIGNALS (http://unicorn.bogomips.org/SIGNALS.html) document for
151
+ full description of all signals used by Unicorn.
152
+ .SH RACK ENVIRONMENT
153
+ .PP
154
+ Accepted values of RACK_ENV and the middleware they automatically load
155
+ (outside of RACKUP_FILE) are exactly as those in rackup(1):
156
+ .IP \[bu] 2
157
+ development - loads Rack::CommonLogger, Rack::ShowExceptions, and
158
+ Rack::Lint middleware
159
+ .IP \[bu] 2
160
+ deployment - loads Rack::CommonLogger middleware
161
+ .IP \[bu] 2
162
+ none - loads no middleware at all, relying entirely on RACKUP_FILE
163
+ .PP
164
+ All unrecognized values for RACK_ENV are assumed to be "none".
165
+ Production deployments are strongly encouraged to use "deployment" or
166
+ "none" for maximum performance.
167
+ .PP
168
+ As of Unicorn 0.94.0, RACK_ENV is exported as a process-wide environment
169
+ variable as well.
170
+ While not current a part of the Rack specification as of Rack 1.0.1,
171
+ this has become a de facto standard in the Rack world.
172
+ .PP
173
+ Note that the Rack::ContentLength and Rack::Chunked middlewares are
174
+ never loaded by default.
175
+ If needed, they should be individually specified in the RACKUP_FILE,
176
+ some frameworks do not require them.
177
+ .SH ENVIRONMENT VARIABLES
178
+ .PP
179
+ The RACK_ENV variable is set by the aforementioned -E switch.
180
+ All application or library-specific environment variables (e.g.
181
+ TMPDIR) may always be set in the Unicorn CONFIG_FILE in addition to the
182
+ spawning shell.
183
+ When transparently upgrading Unicorn, all environment variables set in
184
+ the old master process are inherited by the new master process.
185
+ Unicorn only uses (and will overwrite) the UNICORN_FD environment
186
+ variable internally when doing transparent upgrades.
187
+ .SH SEE ALSO
188
+ .IP \[bu] 2
189
+ unicorn_rails(1)
190
+ .IP \[bu] 2
191
+ \f[I]Rack::Builder\f[] ri/RDoc
192
+ .IP \[bu] 2
193
+ \f[I]Unicorn::Configurator\f[] ri/RDoc
194
+ .IP \[bu] 2
195
+ Unicorn RDoc (http://unicorn.bogomips.org/)
196
+ .IP \[bu] 2
197
+ Rack RDoc (http://rack.rubyforge.org/doc/)
198
+ .IP \[bu] 2
199
+ Rackup HowTo (http://wiki.github.com/rack/rack/tutorial-rackup-howto)
200
+ .SH AUTHORS
201
+ The Unicorn Community <mongrel-unicorn@rubyforge.org>.