unicorn-heroku-wait 4.8.0.1.g0ed2.dirty

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +29 -0
  4. data/.gitignore +25 -0
  5. data/.mailmap +26 -0
  6. data/.manifest +166 -0
  7. data/.wrongdoc.yml +10 -0
  8. data/Application_Timeouts +77 -0
  9. data/CONTRIBUTORS +35 -0
  10. data/COPYING +674 -0
  11. data/ChangeLog +4861 -0
  12. data/DESIGN +97 -0
  13. data/Documentation/.gitignore +5 -0
  14. data/Documentation/GNUmakefile +30 -0
  15. data/Documentation/unicorn.1.txt +178 -0
  16. data/Documentation/unicorn_rails.1.txt +175 -0
  17. data/FAQ +53 -0
  18. data/GIT-VERSION-FILE +1 -0
  19. data/GIT-VERSION-GEN +39 -0
  20. data/GNUmakefile +267 -0
  21. data/HACKING +134 -0
  22. data/ISSUES +36 -0
  23. data/KNOWN_ISSUES +79 -0
  24. data/LATEST +28 -0
  25. data/LICENSE +67 -0
  26. data/Links +56 -0
  27. data/NEWS +2067 -0
  28. data/PHILOSOPHY +145 -0
  29. data/README +150 -0
  30. data/Rakefile +60 -0
  31. data/SIGNALS +123 -0
  32. data/Sandbox +103 -0
  33. data/TODO +5 -0
  34. data/TUNING +98 -0
  35. data/bin/unicorn +126 -0
  36. data/bin/unicorn_rails +209 -0
  37. data/examples/big_app_gc.rb +2 -0
  38. data/examples/echo.ru +27 -0
  39. data/examples/git.ru +13 -0
  40. data/examples/init.sh +74 -0
  41. data/examples/logger_mp_safe.rb +25 -0
  42. data/examples/logrotate.conf +29 -0
  43. data/examples/nginx.conf +156 -0
  44. data/examples/unicorn.conf.minimal.rb +13 -0
  45. data/examples/unicorn.conf.rb +102 -0
  46. data/ext/unicorn_http/CFLAGS +13 -0
  47. data/ext/unicorn_http/c_util.h +124 -0
  48. data/ext/unicorn_http/common_field_optimization.h +111 -0
  49. data/ext/unicorn_http/ext_help.h +82 -0
  50. data/ext/unicorn_http/extconf.rb +10 -0
  51. data/ext/unicorn_http/global_variables.h +97 -0
  52. data/ext/unicorn_http/httpdate.c +78 -0
  53. data/ext/unicorn_http/unicorn_http.c +4031 -0
  54. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  55. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  56. data/lib/unicorn/app/exec_cgi.rb +154 -0
  57. data/lib/unicorn/app/inetd.rb +109 -0
  58. data/lib/unicorn/app/old_rails/static.rb +59 -0
  59. data/lib/unicorn/app/old_rails.rb +35 -0
  60. data/lib/unicorn/cgi_wrapper.rb +147 -0
  61. data/lib/unicorn/configurator.rb +679 -0
  62. data/lib/unicorn/const.rb +44 -0
  63. data/lib/unicorn/http_request.rb +122 -0
  64. data/lib/unicorn/http_response.rb +75 -0
  65. data/lib/unicorn/http_server.rb +803 -0
  66. data/lib/unicorn/launcher.rb +62 -0
  67. data/lib/unicorn/oob_gc.rb +71 -0
  68. data/lib/unicorn/preread_input.rb +33 -0
  69. data/lib/unicorn/socket_helper.rb +231 -0
  70. data/lib/unicorn/ssl_client.rb +11 -0
  71. data/lib/unicorn/ssl_configurator.rb +104 -0
  72. data/lib/unicorn/ssl_server.rb +42 -0
  73. data/lib/unicorn/stream_input.rb +146 -0
  74. data/lib/unicorn/tee_input.rb +126 -0
  75. data/lib/unicorn/tmpio.rb +29 -0
  76. data/lib/unicorn/util.rb +89 -0
  77. data/lib/unicorn/version.rb +1 -0
  78. data/lib/unicorn/worker.rb +152 -0
  79. data/lib/unicorn.rb +118 -0
  80. data/local.mk.sample +59 -0
  81. data/man/man1/unicorn.1 +211 -0
  82. data/man/man1/unicorn_rails.1 +210 -0
  83. data/script/isolate_for_tests +32 -0
  84. data/setup.rb +1586 -0
  85. data/t/.gitignore +5 -0
  86. data/t/GNUmakefile +82 -0
  87. data/t/README +42 -0
  88. data/t/bin/content-md5-put +36 -0
  89. data/t/bin/sha1sum.rb +17 -0
  90. data/t/bin/unused_listen +40 -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/fails-rack-lint.ru +5 -0
  95. data/t/heartbeat-timeout.ru +12 -0
  96. data/t/hijack.ru +42 -0
  97. data/t/listener_names.ru +4 -0
  98. data/t/my-tap-lib.sh +201 -0
  99. data/t/oob_gc.ru +20 -0
  100. data/t/oob_gc_path.ru +20 -0
  101. data/t/pid.ru +3 -0
  102. data/t/preread_input.ru +17 -0
  103. data/t/rack-input-tests.ru +21 -0
  104. data/t/sslgen.sh +71 -0
  105. data/t/t0000-http-basic.sh +50 -0
  106. data/t/t0001-reload-bad-config.sh +53 -0
  107. data/t/t0002-config-conflict.sh +49 -0
  108. data/t/t0002-parser-error.sh +94 -0
  109. data/t/t0003-working_directory.sh +51 -0
  110. data/t/t0004-heartbeat-timeout.sh +69 -0
  111. data/t/t0004-working_directory_broken.sh +24 -0
  112. data/t/t0005-working_directory_app.rb.sh +40 -0
  113. data/t/t0006-reopen-logs.sh +83 -0
  114. data/t/t0006.ru +13 -0
  115. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  116. data/t/t0008-back_out_of_upgrade.sh +110 -0
  117. data/t/t0009-broken-app.sh +56 -0
  118. data/t/t0009-winch_ttin.sh +59 -0
  119. data/t/t0010-reap-logging.sh +55 -0
  120. data/t/t0011-active-unix-socket.sh +79 -0
  121. data/t/t0012-reload-empty-config.sh +85 -0
  122. data/t/t0013-rewindable-input-false.sh +24 -0
  123. data/t/t0013.ru +12 -0
  124. data/t/t0014-rewindable-input-true.sh +24 -0
  125. data/t/t0014.ru +12 -0
  126. data/t/t0015-configurator-internals.sh +25 -0
  127. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  128. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  129. data/t/t0018-write-on-close.sh +23 -0
  130. data/t/t0019-max_header_len.sh +49 -0
  131. data/t/t0020-at_exit-handler.sh +49 -0
  132. data/t/t0021-process_detach.sh +29 -0
  133. data/t/t0022-listener_names-preload_app.sh +32 -0
  134. data/t/t0100-rack-input-tests.sh +124 -0
  135. data/t/t0116-client_body_buffer_size.sh +80 -0
  136. data/t/t0116.ru +16 -0
  137. data/t/t0200-rack-hijack.sh +27 -0
  138. data/t/t0300-no-default-middleware.sh +20 -0
  139. data/t/t0600-https-server-basic.sh +48 -0
  140. data/t/t9000-preread-input.sh +48 -0
  141. data/t/t9001-oob_gc.sh +47 -0
  142. data/t/t9002-oob_gc-path.sh +75 -0
  143. data/t/test-lib.sh +128 -0
  144. data/t/write-on-close.ru +11 -0
  145. data/test/aggregate.rb +15 -0
  146. data/test/benchmark/README +50 -0
  147. data/test/benchmark/dd.ru +18 -0
  148. data/test/benchmark/stack.ru +8 -0
  149. data/test/exec/README +5 -0
  150. data/test/exec/test_exec.rb +1047 -0
  151. data/test/test_helper.rb +297 -0
  152. data/test/unit/test_configurator.rb +175 -0
  153. data/test/unit/test_droplet.rb +28 -0
  154. data/test/unit/test_http_parser.rb +854 -0
  155. data/test/unit/test_http_parser_ng.rb +731 -0
  156. data/test/unit/test_http_parser_xftrust.rb +38 -0
  157. data/test/unit/test_request.rb +182 -0
  158. data/test/unit/test_response.rb +99 -0
  159. data/test/unit/test_server.rb +268 -0
  160. data/test/unit/test_signals.rb +188 -0
  161. data/test/unit/test_sni_hostnames.rb +47 -0
  162. data/test/unit/test_socket_helper.rb +197 -0
  163. data/test/unit/test_stream_input.rb +203 -0
  164. data/test/unit/test_tee_input.rb +294 -0
  165. data/test/unit/test_upload.rb +306 -0
  166. data/test/unit/test_util.rb +105 -0
  167. data/unicorn.gemspec +44 -0
  168. metadata +328 -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,89 @@
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.stat.file? &&
11
+ fp.sync &&
12
+ (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
13
+ rescue IOError, Errno::EBADF
14
+ false
15
+ end
16
+
17
+ def self.chown_logs(uid, gid)
18
+ ObjectSpace.each_object(File) do |fp|
19
+ fp.chown(uid, gid) if is_log?(fp)
20
+ end
21
+ end
22
+ # :startdoc:
23
+
24
+ # This reopens ALL logfiles in the process that have been rotated
25
+ # using logrotate(8) (without copytruncate) or similar tools.
26
+ # A +File+ object is considered for reopening if it is:
27
+ # 1) opened with the O_APPEND and O_WRONLY flags
28
+ # 2) the current open file handle does not match its original open path
29
+ # 3) unbuffered (as far as userspace buffering goes, not O_SYNC)
30
+ # Returns the number of files reopened
31
+ #
32
+ # In Unicorn 3.5.x and earlier, files must be opened with an absolute
33
+ # path to be considered a log file.
34
+ def self.reopen_logs
35
+ to_reopen = []
36
+ nr = 0
37
+ ObjectSpace.each_object(File) { |fp| is_log?(fp) and to_reopen << fp }
38
+
39
+ to_reopen.each do |fp|
40
+ orig_st = begin
41
+ fp.stat
42
+ rescue IOError, Errno::EBADF # race
43
+ next
44
+ end
45
+
46
+ begin
47
+ b = File.stat(fp.path)
48
+ next if orig_st.ino == b.ino && orig_st.dev == b.dev
49
+ rescue Errno::ENOENT
50
+ end
51
+
52
+ begin
53
+ # stdin, stdout, stderr are special. The following dance should
54
+ # guarantee there is no window where `fp' is unwritable in MRI
55
+ # (or any correct Ruby implementation).
56
+ #
57
+ # Fwiw, GVL has zero bearing here. This is tricky because of
58
+ # the unavoidable existence of stdio FILE * pointers for
59
+ # std{in,out,err} in all programs which may use the standard C library
60
+ if fp.fileno <= 2
61
+ # We do not want to hit fclose(3)->dup(2) window for std{in,out,err}
62
+ # MRI will use freopen(3) here internally on std{in,out,err}
63
+ fp.reopen(fp.path, "a")
64
+ else
65
+ # We should not need this workaround, Ruby can be fixed:
66
+ # http://bugs.ruby-lang.org/issues/9036
67
+ # MRI will not call call fclose(3) or freopen(3) here
68
+ # since there's no associated std{in,out,err} FILE * pointer
69
+ # This should atomically use dup3(2) (or dup2(2)) syscall
70
+ File.open(fp.path, "a") { |tmpfp| fp.reopen(tmpfp) }
71
+ end
72
+
73
+ fp.sync = true
74
+ fp.flush # IO#sync=true may not implicitly flush
75
+ new_st = fp.stat
76
+
77
+ # this should only happen in the master:
78
+ if orig_st.uid != new_st.uid || orig_st.gid != new_st.gid
79
+ fp.chown(orig_st.uid, orig_st.gid)
80
+ end
81
+
82
+ nr += 1
83
+ rescue IOError, Errno::EBADF
84
+ # not much we can do...
85
+ end
86
+ end
87
+ nr
88
+ end
89
+ end
@@ -0,0 +1 @@
1
+ Unicorn::Const::UNICORN_VERSION = '4.8.0.1.g0ed2.dirty'
@@ -0,0 +1,152 @@
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
+ attr_reader :to_io # IO.select-compatible
16
+
17
+ PER_DROP = Raindrops::PAGE_SIZE / Raindrops::SIZE
18
+ DROPS = []
19
+
20
+ def initialize(nr)
21
+ drop_index = nr / PER_DROP
22
+ @raindrop = DROPS[drop_index] ||= Raindrops.new(PER_DROP)
23
+ @offset = nr % PER_DROP
24
+ @raindrop[@offset] = 0
25
+ @nr = nr
26
+ @tmp = @switched = false
27
+ @to_io, @master = Unicorn.pipe
28
+ end
29
+
30
+ def atfork_child # :nodoc:
31
+ # we _must_ close in child, parent just holds this open to signal
32
+ @master = @master.close
33
+ end
34
+
35
+ # master fakes SIGQUIT using this
36
+ def quit # :nodoc:
37
+ @master = @master.close if @master
38
+ end
39
+
40
+ # parent does not read
41
+ def atfork_parent # :nodoc:
42
+ @to_io = @to_io.close
43
+ end
44
+
45
+ # call a signal handler immediately without triggering EINTR
46
+ # We do not use the more obvious Process.kill(sig, $$) here since
47
+ # that signal delivery may be deferred. We want to avoid signal delivery
48
+ # while the Rack app.call is running because some database drivers
49
+ # (e.g. ruby-pg) may cancel pending requests.
50
+ def fake_sig(sig) # :nodoc:
51
+ old_cb = trap(sig, "IGNORE")
52
+ old_cb.call
53
+ ensure
54
+ trap(sig, old_cb)
55
+ end
56
+
57
+ # master sends fake signals to children
58
+ def soft_kill(sig) # :nodoc:
59
+ case sig
60
+ when Integer
61
+ signum = sig
62
+ else
63
+ signum = Signal.list[sig.to_s] or
64
+ raise ArgumentError, "BUG: bad signal: #{sig.inspect}"
65
+ end
66
+ # writing and reading 4 bytes on a pipe is atomic on all POSIX platforms
67
+ # Do not care in the odd case the buffer is full, here.
68
+ @master.kgio_trywrite([signum].pack('l'))
69
+ rescue Errno::EPIPE
70
+ # worker will be reaped soon
71
+ end
72
+
73
+ # this only runs when the Rack app.call is not running
74
+ # act like a listener
75
+ def kgio_tryaccept # :nodoc:
76
+ case buf = @to_io.kgio_tryread(4)
77
+ when String
78
+ # unpack the buffer and trigger the signal handler
79
+ signum = buf.unpack('l')
80
+ fake_sig(signum[0])
81
+ # keep looping, more signals may be queued
82
+ when nil # EOF: master died, but we are at a safe place to exit
83
+ fake_sig(:QUIT)
84
+ when :wait_readable # keep waiting
85
+ return false
86
+ end while true # loop, as multiple signals may be sent
87
+ end
88
+
89
+ # worker objects may be compared to just plain Integers
90
+ def ==(other_nr) # :nodoc:
91
+ @nr == other_nr
92
+ end
93
+
94
+ # called in the worker process
95
+ def tick=(value) # :nodoc:
96
+ @raindrop[@offset] = value
97
+ end
98
+
99
+ # called in the master process
100
+ def tick # :nodoc:
101
+ @raindrop[@offset]
102
+ end
103
+
104
+ # only exists for compatibility
105
+ def tmp # :nodoc:
106
+ @tmp ||= begin
107
+ tmp = Unicorn::TmpIO.new
108
+ tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
109
+ tmp
110
+ end
111
+ end
112
+
113
+ # called in both the master (reaping worker) and worker (SIGQUIT handler)
114
+ def close # :nodoc:
115
+ @tmp.close if @tmp
116
+ @master.close if @master
117
+ @to_io.close if @to_io
118
+ end
119
+
120
+ # :startdoc:
121
+
122
+ # In most cases, you should be using the Unicorn::Configurator#user
123
+ # directive instead. This method should only be used if you need
124
+ # fine-grained control of exactly when you want to change permissions
125
+ # in your after_fork hooks.
126
+ #
127
+ # Changes the worker process to the specified +user+ and +group+
128
+ # This is only intended to be called from within the worker
129
+ # process from the +after_fork+ hook. This should be called in
130
+ # the +after_fork+ hook after any privileged functions need to be
131
+ # run (e.g. to set per-worker CPU affinity, niceness, etc)
132
+ #
133
+ # Any and all errors raised within this method will be propagated
134
+ # directly back to the caller (usually the +after_fork+ hook.
135
+ # These errors commonly include ArgumentError for specifying an
136
+ # invalid user/group and Errno::EPERM for insufficient privileges
137
+ def user(user, group = nil)
138
+ # we do not protect the caller, checking Process.euid == 0 is
139
+ # insufficient because modern systems have fine-grained
140
+ # capabilities. Let the caller handle any and all errors.
141
+ uid = Etc.getpwnam(user).uid
142
+ gid = Etc.getgrnam(group).gid if group
143
+ Unicorn::Util.chown_logs(uid, gid)
144
+ @tmp.chown(uid, gid) if @tmp
145
+ if gid && Process.egid != gid
146
+ Process.initgroups(user, gid)
147
+ Process::GID.change_privilege(gid)
148
+ end
149
+ Process.euid != uid and Process::UID.change_privilege(uid)
150
+ @switched = true
151
+ end
152
+ end
data/lib/unicorn.rb ADDED
@@ -0,0 +1,118 @@
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
+ # Op is going to get cleared before the returned lambda is called, so
39
+ # save this value so that it's still there when we need it:
40
+ no_default_middleware = op[:no_default_middleware]
41
+
42
+ # always called after config file parsing, may be called after forking
43
+ lambda do ||
44
+ inner_app = case ru
45
+ when /\.ru$/
46
+ raw = File.read(ru)
47
+ raw.sub!(/^__END__\n.*/, '')
48
+ eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
49
+ else
50
+ require ru
51
+ Object.const_get(File.basename(ru, '.rb').capitalize)
52
+ end
53
+
54
+ pp({ :inner_app => inner_app }) if $DEBUG
55
+
56
+ return inner_app if no_default_middleware
57
+
58
+ # return value, matches rackup defaults based on env
59
+ # Unicorn does not support persistent connections, but Rainbows!
60
+ # and Zbatery both do. Users accustomed to the Rack::Server default
61
+ # middlewares will need ContentLength/Chunked middlewares.
62
+ case ENV["RACK_ENV"]
63
+ when "development"
64
+ Rack::Builder.new do
65
+ use Rack::ContentLength
66
+ use Rack::Chunked
67
+ use Rack::CommonLogger, $stderr
68
+ use Rack::ShowExceptions
69
+ use Rack::Lint
70
+ run inner_app
71
+ end.to_app
72
+ when "deployment"
73
+ Rack::Builder.new do
74
+ use Rack::ContentLength
75
+ use Rack::Chunked
76
+ use Rack::CommonLogger, $stderr
77
+ run inner_app
78
+ end.to_app
79
+ else
80
+ inner_app
81
+ end
82
+ end
83
+ end
84
+
85
+ # returns an array of strings representing TCP listen socket addresses
86
+ # and Unix domain socket paths. This is useful for use with
87
+ # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
88
+ def self.listener_names
89
+ Unicorn::HttpServer::LISTENERS.map do |io|
90
+ Unicorn::SocketHelper.sock_name(io)
91
+ end + Unicorn::HttpServer::NEW_LISTENERS
92
+ end
93
+
94
+ def self.log_error(logger, prefix, exc)
95
+ message = exc.message
96
+ message = message.dump if /[[:cntrl:]]/ =~ message
97
+ logger.error "#{prefix}: #{message} (#{exc.class})"
98
+ exc.backtrace.each { |line| logger.error(line) }
99
+ end
100
+
101
+ # remove this when we only support Ruby >= 2.0
102
+ def self.pipe # :nodoc:
103
+ Kgio::Pipe.new.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
104
+ end
105
+ # :startdoc:
106
+ end
107
+ # :enddoc:
108
+ require 'unicorn/const'
109
+ require 'unicorn/socket_helper'
110
+ require 'unicorn/stream_input'
111
+ require 'unicorn/tee_input'
112
+ require 'unicorn/http_request'
113
+ require 'unicorn/configurator'
114
+ require 'unicorn/tmpio'
115
+ require 'unicorn/util'
116
+ require 'unicorn/http_response'
117
+ require 'unicorn/worker'
118
+ 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 !'