File: passenger-spawn-server

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (106 lines) | stat: -rwxr-xr-x 3,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
#  Phusion Passenger - http://www.modrails.com/
#  Copyright (c) 2010 Phusion
#
#  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

require 'socket'

SERVER_SOCKET_FD = 3
OWNER_SOCKET_FD  = 4

begin
	STDOUT.sync = true
	STDERR.sync = true
	$0 = "Passenger spawn server"
	if GC.respond_to?(:copy_on_write_friendly=)
		GC.copy_on_write_friendly = true
	end
	
	server_socket = UNIXServer.for_fd(SERVER_SOCKET_FD)
	owner_socket  = UNIXSocket.for_fd(OWNER_SOCKET_FD)
	begin
		socket_filename = owner_socket.readline.strip
		socket_password = owner_socket.readline.strip
		generation_path = owner_socket.readline.strip
		logging_agent_address = owner_socket.readline.strip
		logging_agent_username = owner_socket.readline.strip
		logging_agent_password_base64 = owner_socket.readline.strip
		node_name = owner_socket.readline.strip
		log_level = owner_socket.readline.to_i
		debug_log_file = owner_socket.readline.strip
	rescue EOFError
		exit
	end
	
	# Optimization for decreasing startup time. Since Apache starts the spawn
	# server twice during startup, we don't want to load the Passenger classes
	# if we don't need them.
	# We check whether Apache immediately closes the connection. If so,
	# we exit without loading the rest of Passenger. If Apache doesn't close
	# the connection within 4 seconds then we continue with loading Passenger,
	# so that loading doesn't happen during the first spawn.
	begin
		ios = select([server_socket, owner_socket], nil, nil, 4)
		if ios
			readable = ios[0]
			if !readable.include?(server_socket) && readable.include?(owner_socket)
				exit
			end
		end
	rescue Interrupt
		exit
	end
	
	source_root = File.expand_path(File.dirname(__FILE__) + "/..")
	$LOAD_PATH.unshift("#{source_root}/lib")
	require 'rubygems' rescue nil
	require 'phusion_passenger'
	require 'phusion_passenger/debug_logging'
	require 'phusion_passenger/utils/tmpdir'
	require 'phusion_passenger/native_support'
	if defined?(PhusionPassenger::NativeSupport)
		PhusionPassenger::NativeSupport.disable_stdio_buffering
	end
	PhusionPassenger::DebugLogging.log_level = log_level
	PhusionPassenger::DebugLogging.log_file  = debug_log_file
	PhusionPassenger::Utils.passenger_tmpdir = generation_path
	if logging_agent_address.empty?
		options = {}
	else
		options = {
			"logging_agent_address" => logging_agent_address,
			"logging_agent_username" => logging_agent_username,
			"logging_agent_password_base64" => logging_agent_password_base64,
			"node_name" => node_name
		}
	end
	
	require 'phusion_passenger/spawn_manager'
	spawn_manager = PhusionPassenger::SpawnManager.new(options)
	spawn_manager.start_synchronously(socket_filename, socket_password, server_socket, owner_socket)
	spawn_manager.cleanup
rescue => e
	require 'phusion_passenger/utils'
	include PhusionPassenger::Utils
	print_exception("spawn manager", e)
	exit 10
end