| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
require 'rubygems' |
| 4 |
require 'daemons' |
| 5 |
require 'geoip' |
| 6 |
require 'socket' |
| 7 |
require 'fcntl' |
| 8 |
require "optparse" |
| 9 |
|
| 10 |
ENV["RAILS_ENV"] ||= "production" |
| 11 |
require File.dirname(__FILE__)+'/../config/environment' |
| 12 |
|
| 13 |
Rails.configuration.log_level = :info # Disable debug |
| 14 |
ActiveRecord::Base.allow_concurrency = true |
| 15 |
|
| 16 |
ENV["PATH"] = "/usr/local/bin/:/opt/local/bin:#{ENV["PATH"]}" |
| 17 |
|
| 18 |
BASE_PATH = File.expand_path(GitoriousConfig['repository_base_path']) |
| 19 |
|
| 20 |
TIMEOUT = 30 |
| 21 |
MAX_CHILDREN = 30 |
| 22 |
$children_reaped = 0 |
| 23 |
$children_active = 0 |
| 24 |
|
| 25 |
class GeoIP |
| 26 |
def close |
| 27 |
@file.close |
| 28 |
end |
| 29 |
end |
| 30 |
|
| 31 |
module Git |
| 32 |
class Daemon |
| 33 |
include Daemonize |
| 34 |
|
| 35 |
SERVICE_READ_REGEXP = /^(git\-upload\-pack|git\ upload\-pack)\s(.+)\x00host=([\w\.\-]+)/.freeze |
| 36 |
SERVICE_WRITE_REGEXP = /^(git\-receive\-pack|git\ receive\-pack)\s(.+)\x00host=([\w\.\-]+)/.freeze |
| 37 |
|
| 38 |
def initialize(options) |
| 39 |
@options = options |
| 40 |
end |
| 41 |
|
| 42 |
def start |
| 43 |
if @options[:daemonize] |
| 44 |
daemonize(@options[:logfile]) |
| 45 |
end |
| 46 |
Dir.chdir(Rails.root) # So Logger don't get confused |
| 47 |
@socket = TCPServer.new(@options[:host], @options[:port]) |
| 48 |
@socket.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, !!@options[:reuseaddr]) |
| 49 |
@socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) |
| 50 |
log(Process.pid, "Listening on #{@options[:host]}:#{@options[:port]}...") |
| 51 |
ActiveRecord::Base.verify_active_connections! if @options[:daemonize] |
| 52 |
run |
| 53 |
end |
| 54 |
|
| 55 |
def run |
| 56 |
Dir.chdir(GitoriousConfig["repository_base_path"]) |
| 57 |
if @options[:pidfile] |
| 58 |
File.open(@options[:pidfile], "w") do |f| |
| 59 |
f.write(Process.pid) |
| 60 |
end |
| 61 |
end |
| 62 |
while session = accept_socket |
| 63 |
connections = $children_active - $children_reaped |
| 64 |
if connections > MAX_CHILDREN |
| 65 |
log(Process.pid, "too many active children #{connections}/#{MAX_CHILDREN}") |
| 66 |
session.close |
| 67 |
next |
| 68 |
end |
| 69 |
|
| 70 |
run_service(session) |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
def run_service(session) |
| 75 |
$children_active += 1 |
| 76 |
ip_family, port, name, ip = session.peeraddr |
| 77 |
|
| 78 |
line = receive_data(session) |
| 79 |
|
| 80 |
if line =~ SERVICE_READ_REGEXP |
| 81 |
start_time = Time.now |
| 82 |
service = $1 |
| 83 |
base_path = $2 |
| 84 |
host = $3 |
| 85 |
|
| 86 |
path = File.expand_path("#{BASE_PATH}/#{base_path}") |
| 87 |
log(Process.pid, "Connection from #{ip} for #{base_path.inspect}") |
| 88 |
|
| 89 |
repository = nil |
| 90 |
begin |
| 91 |
ActiveRecord::Base.verify_active_connections! |
| 92 |
repository = ::Repository.find_by_path(path) |
| 93 |
rescue => e |
| 94 |
log(Process.pid, "AR error: #{e.class.name} #{e.message}:\n #{e.backtrace.join("\n ")}") |
| 95 |
end |
| 96 |
|
| 97 |
unless repository |
| 98 |
log(Process.pid, "Cannot find repository: #{path}") |
| 99 |
write_error_message(session, "Cannot find repository: #{base_path}") |
| 100 |
$children_active -= 1 |
| 101 |
session.close |
| 102 |
return |
| 103 |
end |
| 104 |
|
| 105 |
real_path = File.expand_path(repository.full_repository_path) |
| 106 |
log(Process.pid, "#{ip} wants #{path.inspect} => #{real_path.inspect}") |
| 107 |
|
| 108 |
if real_path.index(BASE_PATH) != 0 || !File.directory?(real_path) |
| 109 |
log(Process.pid, "Invalid path: #{real_path}") |
| 110 |
write_error_message(session, "Cannot find repository: #{base_path}") |
| 111 |
session.close |
| 112 |
$children_active -= 1 |
| 113 |
return |
| 114 |
end |
| 115 |
|
| 116 |
if !File.exist?(File.join(real_path, "git-daemon-export-ok")) |
| 117 |
session.close |
| 118 |
$children_active -= 1 |
| 119 |
return |
| 120 |
end |
| 121 |
|
| 122 |
if ip_family == "AF_INET6" |
| 123 |
repository.cloned_from(ip) |
| 124 |
else |
| 125 |
geoip = GeoIP.new(File.join(RAILS_ROOT, "data", "GeoIP.dat")) |
| 126 |
localization = geoip.country(ip) |
| 127 |
geoip.close |
| 128 |
repository.cloned_from(ip, localization[3], localization[5], 'git') |
| 129 |
end |
| 130 |
|
| 131 |
Dir.chdir(real_path) do |
| 132 |
cmd = "git-upload-pack --strict --timeout=#{TIMEOUT} ." |
| 133 |
|
| 134 |
child_pid = fork do |
| 135 |
log(Process.pid, "Deferred in #{'%0.5f' % (Time.now - start_time)}s") |
| 136 |
|
| 137 |
$stdout.reopen(session) |
| 138 |
$stdin.reopen(session) |
| 139 |
$stderr.reopen("/dev/null") |
| 140 |
|
| 141 |
exec(cmd) |
| 142 |
# FIXME; we don't ever get here since we exec(), so reaped count may be incorrect |
| 143 |
$children_reaped += 1 |
| 144 |
exit! |
| 145 |
end |
| 146 |
end rescue Errno::EAGAIN |
| 147 |
elsif line =~ SERVICE_WRITE_REGEXP |
| 148 |
service, base_path, host = $1, $2, $3 |
| 149 |
log(Process.pid, "Not accepting #{service.inspect} for #{base_path.inspect}") |
| 150 |
write_error_message(session, "The git:// url is read-only. Please see " + |
| 151 |
"http://#{GitoriousConfig['gitorious_host']}#{base_path.sub(/\.git$/, '')} " + |
| 152 |
"for the push url, if you're a committer.") |
| 153 |
$children_active -= 1 |
| 154 |
session.close |
| 155 |
return |
| 156 |
else |
| 157 |
# $stderr.puts "Invalid request from #{ip}: #{line.inspect}" |
| 158 |
$children_active -= 1 |
| 159 |
end |
| 160 |
session.close |
| 161 |
end |
| 162 |
|
| 163 |
def handle_stop(signal) |
| 164 |
@socket.close |
| 165 |
log(Process.pid, "Received #{signal}, exiting..") |
| 166 |
exit 0 |
| 167 |
end |
| 168 |
|
| 169 |
def handle_cld |
| 170 |
loop do |
| 171 |
pid = nil |
| 172 |
begin |
| 173 |
pid = Process.wait(-1, Process::WNOHANG) |
| 174 |
rescue Errno::ECHILD |
| 175 |
break |
| 176 |
end |
| 177 |
|
| 178 |
if pid && $? |
| 179 |
$children_reaped += 1 |
| 180 |
log(pid, "Disconnected. (status=#{$?.exitstatus})") if pid > 0 |
| 181 |
if $children_reaped == $children_active |
| 182 |
$children_reaped = 0 |
| 183 |
$children_active = 0 |
| 184 |
end |
| 185 |
|
| 186 |
next |
| 187 |
end |
| 188 |
break |
| 189 |
end |
| 190 |
end |
| 191 |
|
| 192 |
def log(pid, msg) |
| 193 |
$stderr.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} [#{pid}] #{msg}" |
| 194 |
end |
| 195 |
|
| 196 |
def write_error_message(session, msg) |
| 197 |
message = ["\n----------------------------------------------"] |
| 198 |
message << msg |
| 199 |
message << "----------------------------------------------\n" |
| 200 |
write_into_sideband(session, message.join("\n"), 2) |
| 201 |
end |
| 202 |
|
| 203 |
def write_into_sideband(session, message, channel) |
| 204 |
msg = "%s%s" % [channel.chr, message] |
| 205 |
session.write("%04x%s" % [msg.length+4, msg]) |
| 206 |
end |
| 207 |
|
| 208 |
def accept_socket |
| 209 |
if RUBY_VERSION < '1.9' |
| 210 |
@socket.accept |
| 211 |
else |
| 212 |
begin |
| 213 |
@socket.accept_nonblock |
| 214 |
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR => e |
| 215 |
if IO.select([@socket]) |
| 216 |
retry |
| 217 |
else |
| 218 |
raise e |
| 219 |
end |
| 220 |
end |
| 221 |
end |
| 222 |
end |
| 223 |
|
| 224 |
def receive_data(session) |
| 225 |
if RUBY_VERSION < '1.9' |
| 226 |
read_data(session) |
| 227 |
else |
| 228 |
read_data_nonblock(session) |
| 229 |
end |
| 230 |
end |
| 231 |
|
| 232 |
def read_data(session) |
| 233 |
size_string = session.recv(4) |
| 234 |
return "" if !size_string |
| 235 |
size = size_string.to_i(16) |
| 236 |
return "" unless size > 4 |
| 237 |
session.recv(size - 4) |
| 238 |
rescue Errno::ECONNRESET |
| 239 |
return "" |
| 240 |
end |
| 241 |
|
| 242 |
def read_data_nonblock(session) |
| 243 |
begin |
| 244 |
size_string = session.recv_nonblock(4) |
| 245 |
return "" if !size_string |
| 246 |
size = size_string.to_i(16) |
| 247 |
return "" unless size > 4 |
| 248 |
session.recv_nonblock(size - 4) |
| 249 |
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR |
| 250 |
if IO.select([@socket]) |
| 251 |
retry |
| 252 |
else |
| 253 |
return "" |
| 254 |
end |
| 255 |
end |
| 256 |
end |
| 257 |
|
| 258 |
end |
| 259 |
end |
| 260 |
|
| 261 |
options = { |
| 262 |
:port => 9418, |
| 263 |
:host => "0.0.0.0", |
| 264 |
:logfile => File.join(RAILS_ROOT, "log", "git-daemon.log"), |
| 265 |
:pidfile => File.join(RAILS_ROOT, "log", "git-daemon.pid"), |
| 266 |
:daemonize => false, |
| 267 |
:reuseaddr => true, |
| 268 |
} |
| 269 |
|
| 270 |
OptionParser.new do |opts| |
| 271 |
opts.banner = "Usage: #{$0} [options]" |
| 272 |
|
| 273 |
opts.on("-p", "--port=[port]", Integer, "Port to listen on", "Default: #{options[:port]}") do |o| |
| 274 |
options[:port] = o |
| 275 |
end |
| 276 |
|
| 277 |
opts.on("-a", "--address=[host]", "Host to listen on", "Default: #{options[:host]}") do |o| |
| 278 |
options[:host] = o |
| 279 |
end |
| 280 |
|
| 281 |
opts.on("-l", "--logfile=[file]", "File to log to", "Default: #{options[:logfile]}") do |o| |
| 282 |
options[:logfile] = o |
| 283 |
end |
| 284 |
|
| 285 |
opts.on("-P", "--pidfile=[file]", "PID file to use (if daemonized)", "Default: #{options[:pidfile]}") do |o| |
| 286 |
options[:pidfile] = o |
| 287 |
end |
| 288 |
|
| 289 |
opts.on("-d", "--daemonize", "Daemonize or run in foreground", "Default: #{options[:daemonize]}") do |o| |
| 290 |
options[:daemonize] = o |
| 291 |
end |
| 292 |
|
| 293 |
opts.on("-r", "--reuseaddr", "Re-use addresses", "Default: #{options[:reuseaddr].inspect}") do |o| |
| 294 |
options[:reuseaddr] = o |
| 295 |
end |
| 296 |
|
| 297 |
opts.on_tail("-h", "--help", "Show this help message.") do |
| 298 |
puts opts |
| 299 |
exit |
| 300 |
end |
| 301 |
|
| 302 |
# opts.on("-e", "--environment", "RAILS_ENV to use") do |o| |
| 303 |
# options[:port] = o |
| 304 |
# end |
| 305 |
end.parse! |
| 306 |
|
| 307 |
@git_daemon = Git::Daemon.new(options) |
| 308 |
|
| 309 |
trap("SIGKILL") { @git_daemon.handle_stop("SIGKILL") } |
| 310 |
trap("TERM") { @git_daemon.handle_stop("TERM") } |
| 311 |
trap("SIGINT") { @git_daemon.handle_stop("SIGINT") } |
| 312 |
trap("CLD") { @git_daemon.handle_cld } |
| 313 |
|
| 314 |
@git_daemon.start |