Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2015 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c8a8a7130b78c7373f40 to your computer and use it in GitHub Desktop.
Save anonymous/c8a8a7130b78c7373f40 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Server.pm b/lib/Mojo/Server.pm
index bb02b16..6d01653 100644
--- a/lib/Mojo/Server.pm
+++ b/lib/Mojo/Server.pm
@@ -5,11 +5,9 @@ use Carp 'croak';
use Cwd 'abs_path';
use Mojo::Loader;
use Mojo::Util 'md5_sum';
-use POSIX;
use Scalar::Util 'blessed';
-has app => sub { shift->build_app('Mojo::HelloWorld') };
-has [qw(group user)];
+has app => sub { shift->build_app('Mojo::HelloWorld') };
has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };
sub build_app {
@@ -26,19 +24,6 @@ sub build_tx {
return $tx;
}
-sub daemonize {
-
- # Fork and kill parent
- die "Can't fork: $!" unless defined(my $pid = fork);
- exit 0 if $pid;
- POSIX::setsid or die "Can't start a new session: $!";
-
- # Close filehandles
- open STDIN, '</dev/null';
- open STDOUT, '>/dev/null';
- open STDERR, '>&STDOUT';
-}
-
sub load_app {
my ($self, $path) = @_;
@@ -71,29 +56,6 @@ sub new {
sub run { croak 'Method "run" not implemented by subclass' }
-sub setuidgid {
- my $self = shift;
-
- # Group (make sure secondary groups are reassigned too)
- if (my $group = $self->group) {
- return $self->_log(qq{Group "$group" does not exist.})
- unless defined(my $gid = getgrnam $group);
- return $self->_log(qq{Can't switch to group "$group": $!})
- unless ($( = $) = "$gid $gid") && $) eq "$gid $gid" && $( eq "$gid $gid";
- }
-
- # User
- return $self unless my $user = $self->user;
- return $self->_log(qq{User "$user" does not exist.})
- unless defined(my $uid = getpwnam $user);
- return $self->_log(qq{Can't switch to user "$user": $!})
- unless POSIX::setuid($uid);
-
- return $self;
-}
-
-sub _log { $_[0]->app->log->error($_[1]) and return $_[0] }
-
1;
=encoding utf8
@@ -155,13 +117,6 @@ L<Mojo::Server> implements the following attributes.
Application this server handles, defaults to a L<Mojo::HelloWorld> object.
-=head2 group
-
- my $group = $server->group;
- $server = $server->group('users');
-
-Group for server process.
-
=head2 reverse_proxy
my $bool = $server->reverse_proxy;
@@ -170,13 +125,6 @@ Group for server process.
This server operates behind a reverse proxy, defaults to the value of the
C<MOJO_REVERSE_PROXY> environment variable.
-=head2 user
-
- my $user = $server->user;
- $server = $server->user('web');
-
-User for the server process.
-
=head1 METHODS
L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements
@@ -194,12 +142,6 @@ Build application from class.
Let application build a transaction.
-=head2 daemonize
-
- $server->daemonize;
-
-Daemonize server process.
-
=head2 load_app
my $app = $server->load_app('/home/sri/myapp.pl');
@@ -221,12 +163,6 @@ with default request handling.
Run server. Meant to be overloaded in a subclass.
-=head2 setuidgid
-
- $server = $server->setuidgid;
-
-Set L</"user"> and L</"group"> for process.
-
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm
index 0415cbc..fa05e6b 100644
--- a/lib/Mojo/Server/Daemon.pm
+++ b/lib/Mojo/Server/Daemon.pm
@@ -25,7 +25,7 @@ sub DESTROY {
sub run {
my $self = shift;
local $SIG{INT} = local $SIG{TERM} = sub { $self->ioloop->stop };
- $self->start->setuidgid->ioloop->start;
+ $self->start->ioloop->start;
}
sub start {
diff --git a/lib/Mojo/Server/Hypnotoad.pm b/lib/Mojo/Server/Hypnotoad.pm
index d8e1682..695f79c 100644
--- a/lib/Mojo/Server/Hypnotoad.pm
+++ b/lib/Mojo/Server/Hypnotoad.pm
@@ -28,9 +28,9 @@ sub configure {
$prefork->max_requests($c->{keep_alive_requests})
if $c->{keep_alive_requests};
defined $c->{$_} and $prefork->$_($c->{$_})
- for qw(accept_interval accepts backlog graceful_timeout group),
+ for qw(accept_interval accepts backlog graceful_timeout),
qw(heartbeat_interval heartbeat_timeout inactivity_timeout listen),
- qw(lock_file lock_timeout multi_accept pid_file user workers);
+ qw(lock_file lock_timeout multi_accept pid_file workers);
}
sub run {
@@ -69,10 +69,6 @@ sub run {
# Initiate hot deployment
$self->_hot_deploy unless $ENV{HYPNOTOAD_PID};
- # Daemonize as early as possible (but not for restarts)
- $prefork->daemonize
- if !$ENV{HYPNOTOAD_FOREGROUND} && $ENV{HYPNOTOAD_REV} < 3;
-
# Start accepting connections
local $SIG{USR2} = sub { $self->{upgrade} ||= steady_time };
$prefork->cleanup(1)->run;
@@ -158,8 +154,8 @@ Note that the server uses signals for process management, so you should avoid
modifying signal handlers in your applications.
To start applications with it you can use the L<hypnotoad> script, which
-automatically daemonizes the server process and defaults to C<production> mode
-for L<Mojolicious> and L<Mojolicious::Lite> applications.
+defaults to C<production> mode for L<Mojolicious> and L<Mojolicious::Lite>
+applications.
$ hypnotoad ./myapp.pl
Server available at http://127.0.0.1:8080.
@@ -282,13 +278,6 @@ Maximum amount of time in seconds stopping a worker gracefully may take before
being forced, defaults to the value of
L<Mojo::Server::Prefork/"graceful_timeout">.
-=head2 group
-
- group => 'staff'
-
-Group name for worker processes, defaults to the value of
-L<Mojo::Server/"group">.
-
=head2 heartbeat_interval
heartbeat_interval => 3
@@ -372,13 +361,6 @@ value of L<Mojo::Server/"reverse_proxy">.
Maximum amount of time in seconds a zero downtime software upgrade may take
before getting canceled, defaults to the value of L</"upgrade_timeout">.
-=head2 user
-
- user => 'sri'
-
-Username for worker processes, defaults to the value of
-L<Mojo::Server/"user">.
-
=head2 workers
workers => 10
diff --git a/lib/Mojo/Server/Prefork.pm b/lib/Mojo/Server/Prefork.pm
index 1052111..b6652d3 100644
--- a/lib/Mojo/Server/Prefork.pm
+++ b/lib/Mojo/Server/Prefork.pm
@@ -166,13 +166,10 @@ sub _spawn {
if $pid;
# Prepare lock file
- my $file = $self->{lock_file};
+ my $file = $self->cleanup(0)->{lock_file};
$self->app->log->error(qq{Can't open lock file "$file": $!})
unless open my $handle, '>', $file;
- # Change user/group
- $self->setuidgid->cleanup(0);
-
# Accept mutex
weaken $self;
my $loop = $self->ioloop->lock(
diff --git a/lib/Mojolicious/Command/daemon.pm b/lib/Mojolicious/Command/daemon.pm
index 7749e14..0bd5036 100644
--- a/lib/Mojolicious/Command/daemon.pm
+++ b/lib/Mojolicious/Command/daemon.pm
@@ -14,12 +14,10 @@ sub run {
GetOptionsFromArray \@args,
'b|backlog=i' => sub { $daemon->backlog($_[1]) },
'c|clients=i' => sub { $daemon->max_clients($_[1]) },
- 'g|group=s' => sub { $daemon->group($_[1]) },
'i|inactivity=i' => sub { $daemon->inactivity_timeout($_[1]) },
'l|listen=s' => \my @listen,
'p|proxy' => sub { $daemon->reverse_proxy(1) },
- 'r|requests=i' => sub { $daemon->max_requests($_[1]) },
- 'u|user=s' => sub { $daemon->user($_[1]) };
+ 'r|requests=i' => sub { $daemon->max_requests($_[1]) };
$daemon->listen(\@listen) if @listen;
$daemon->run;
@@ -46,7 +44,6 @@ Mojolicious::Command::daemon - Daemon command
-b, --backlog <size> Listen backlog size, defaults to SOMAXCONN.
-c, --clients <number> Maximum number of concurrent clients,
defaults to 1000.
- -g, --group <name> Group name for process.
-i, --inactivity <seconds> Inactivity timeout, defaults to the value of
MOJO_INACTIVITY_TIMEOUT or 15.
-l, --listen <location> One or more locations you want to listen on,
@@ -56,7 +53,6 @@ Mojolicious::Command::daemon - Daemon command
the value of MOJO_REVERSE_PROXY.
-r, --requests <number> Maximum number of requests per keep-alive
connection, defaults to 25.
- -u, --user <name> Username for process.
=head1 DESCRIPTION
diff --git a/lib/Mojolicious/Command/prefork.pm b/lib/Mojolicious/Command/prefork.pm
index 8015b84..f23fa4c 100644
--- a/lib/Mojolicious/Command/prefork.pm
+++ b/lib/Mojolicious/Command/prefork.pm
@@ -18,7 +18,6 @@ sub run {
'b|backlog=i' => sub { $prefork->backlog($_[1]) },
'c|clients=i' => sub { $prefork->max_clients($_[1]) },
'G|graceful-timeout=i' => sub { $prefork->graceful_timeout($_[1]) },
- 'g|group=s' => sub { $prefork->group($_[1]) },
'heartbeat-interval=i' => sub { $prefork->heartbeat_interval($_[1]) },
'H|heartbeat-timeout=i' => sub { $prefork->heartbeat_timeout($_[1]) },
'i|inactivity=i' => sub { $prefork->inactivity_timeout($_[1]) },
@@ -29,7 +28,6 @@ sub run {
'P|pid-file=s' => sub { $prefork->pid_file($_[1]) },
'p|proxy' => sub { $prefork->reverse_proxy(1) },
'r|requests=i' => sub { $prefork->max_requests($_[1]) },
- 'u|user=s' => sub { $prefork->user($_[1]) },
'w|workers=i' => sub { $prefork->workers($_[1]) };
$prefork->listen(\@listen) if @listen;
@@ -62,7 +60,6 @@ Mojolicious::Command::prefork - Prefork command
-c, --clients <number> Maximum number of concurrent clients,
defaults to 1000.
-G, --graceful-timeout <seconds> Graceful timeout, defaults to 20.
- -g, --group <name> Group name for process.
--heartbeat-interval <seconds> Heartbeat interval, defaults to 5.
-H, --heartbeat-timeout <seconds> Heartbeat timeout, defaults to 20.
-i, --inactivity <seconds> Inactivity timeout, defaults to the
@@ -84,7 +81,6 @@ Mojolicious::Command::prefork - Prefork command
-r, --requests <number> Maximum number of requests per
keep-alive connection, defaults to
25.
- -u, --user <name> Username for process.
-w, --workers <number> Number of workers, defaults to 4.
=head1 DESCRIPTION
diff --git a/script/hypnotoad b/script/hypnotoad
index 1a0b3b0..cbf81aa 100755
--- a/script/hypnotoad
+++ b/script/hypnotoad
@@ -6,10 +6,9 @@ use warnings;
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
GetOptions
- 'f|foreground' => \$ENV{HYPNOTOAD_FOREGROUND},
- 'h|help' => \my $help,
- 's|stop' => \$ENV{HYPNOTOAD_STOP},
- 't|test' => \$ENV{HYPNOTOAD_TEST};
+ 'h|help' => \my $help,
+ 's|stop' => \$ENV{HYPNOTOAD_STOP},
+ 't|test' => \$ENV{HYPNOTOAD_TEST};
my $app = shift || $ENV{HYPNOTOAD_APP};
if ($help || !$app) {
@@ -32,13 +31,12 @@ hypnotoad - Hypnotoad HTTP and WebSocket server
hypnotoad ./script/my_app
hypnotoad ./myapp.pl
- hypnotoad -f ./myapp.pl
+ hypnotoad -s ./myapp.pl
Options:
- -f, --foreground Keep manager process in foreground.
- -h, --help Show this message.
- -s, --stop Stop server gracefully.
- -t, --test Test application and exit.
+ -h, --help Show this message.
+ -s, --stop Stop server gracefully.
+ -t, --test Test application and exit.
=head1 DESCRIPTION
diff --git a/t/mojo/hypnotoad.t b/t/mojo/hypnotoad.t
index 65b12de..c533f51 100644
--- a/t/mojo/hypnotoad.t
+++ b/t/mojo/hypnotoad.t
@@ -25,7 +25,6 @@ use Mojo::Util qw(slurp spurt);
backlog => 43,
clients => 1,
graceful_timeout => 23,
- group => 'testers',
heartbeat_interval => 7,
heartbeat_timeout => 9,
keep_alive_requests => 3,
@@ -37,21 +36,19 @@ use Mojo::Util qw(slurp spurt);
pid_file => '/foo/bar.pid',
proxy => 1,
upgrade_timeout => 45,
- user => 'tester',
workers => 7
};
is $hypnotoad->upgrade_timeout, 60, 'right default';
$hypnotoad->configure('test');
is_deeply $hypnotoad->prefork->listen, ['http://*:8080'], 'right value';
$hypnotoad->configure('myserver');
- is $hypnotoad->prefork->accept_interval, 33, 'right value';
- is $hypnotoad->prefork->accepts, 13, 'right value';
- is $hypnotoad->prefork->backlog, 43, 'right value';
- is $hypnotoad->prefork->graceful_timeout, 23, 'right value';
- is $hypnotoad->prefork->group, 'testers', 'right value';
- is $hypnotoad->prefork->heartbeat_interval, 7, 'right value';
- is $hypnotoad->prefork->heartbeat_timeout, 9, 'right value';
- is $hypnotoad->prefork->inactivity_timeout, 5, 'right value';
+ is $hypnotoad->prefork->accept_interval, 33, 'right value';
+ is $hypnotoad->prefork->accepts, 13, 'right value';
+ is $hypnotoad->prefork->backlog, 43, 'right value';
+ is $hypnotoad->prefork->graceful_timeout, 23, 'right value';
+ is $hypnotoad->prefork->heartbeat_interval, 7, 'right value';
+ is $hypnotoad->prefork->heartbeat_timeout, 9, 'right value';
+ is $hypnotoad->prefork->inactivity_timeout, 5, 'right value';
is_deeply $hypnotoad->prefork->listen, ['http://*:8081'], 'right value';
is $hypnotoad->prefork->lock_file, '/foo/bar.lock', 'right value';
is $hypnotoad->prefork->lock_timeout, 14, 'right value';
@@ -60,8 +57,7 @@ use Mojo::Util qw(slurp spurt);
is $hypnotoad->prefork->multi_accept, 16, 'right value';
is $hypnotoad->prefork->pid_file, '/foo/bar.pid', 'right value';
ok $hypnotoad->prefork->reverse_proxy, 'reverse proxy enabled';
- is $hypnotoad->prefork->user, 'tester', 'right value';
- is $hypnotoad->prefork->workers, 7, 'right value';
+ is $hypnotoad->prefork->workers, 7, 'right value';
is $hypnotoad->upgrade_timeout, 45, 'right value';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment