… | |
… | |
81 | our %EXT_MAP = (); # pluggable maps |
81 | our %EXT_MAP = (); # pluggable maps |
82 | |
82 | |
83 | our $RELOAD; # number of reloads so far |
83 | our $RELOAD; # number of reloads so far |
84 | our @EVENT; |
84 | our @EVENT; |
85 | |
85 | |
86 | our $CONFDIR = confdir; |
86 | our $CONFDIR = confdir; |
87 | our $DATADIR = datadir; |
87 | our $DATADIR = datadir; |
88 | our $LIBDIR = "$DATADIR/ext"; |
88 | our $LIBDIR = "$DATADIR/ext"; |
89 | our $PODDIR = "$DATADIR/pod"; |
89 | our $PODDIR = "$DATADIR/pod"; |
90 | our $MAPDIR = "$DATADIR/" . mapdir; |
90 | our $MAPDIR = "$DATADIR/" . mapdir; |
91 | our $LOCALDIR = localdir; |
91 | our $LOCALDIR = localdir; |
92 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
92 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
93 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
93 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
94 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
94 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
95 | our $RANDOMDIR = "$LOCALDIR/random"; |
95 | our $RANDOMDIR = "$LOCALDIR/random"; |
96 | our $BDBDIR = "$LOCALDIR/db"; |
96 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
97 | our $PIDFILE = "$LOCALDIR/pid"; |
|
|
98 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
|
|
99 | |
97 | our %RESOURCE; |
100 | our %RESOURCE; |
98 | |
101 | |
99 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
102 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
100 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
103 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
101 | our $NEXT_TICK; |
104 | our $NEXT_TICK; |
… | |
… | |
126 | |
129 | |
127 | binmode STDOUT; |
130 | binmode STDOUT; |
128 | binmode STDERR; |
131 | binmode STDERR; |
129 | |
132 | |
130 | # read virtual server time, if available |
133 | # read virtual server time, if available |
131 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
134 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
132 | open my $fh, "<", "$LOCALDIR/runtime" |
135 | open my $fh, "<", $RUNTIMEFILE |
133 | or die "unable to read runtime file: $!"; |
136 | or die "unable to read $RUNTIMEFILE file: $!"; |
134 | $RUNTIME = <$fh> + 0.; |
137 | $RUNTIME = <$fh> + 0.; |
135 | } |
138 | } |
136 | |
139 | |
137 | mkdir $_ |
140 | mkdir $_ |
138 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
141 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
… | |
… | |
3353 | }; |
3356 | }; |
3354 | warn $@ if $@; |
3357 | warn $@ if $@; |
3355 | } |
3358 | } |
3356 | } |
3359 | } |
3357 | |
3360 | |
|
|
3361 | sub pidfile() { |
|
|
3362 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
|
|
3363 | or die "$PIDFILE: $!"; |
|
|
3364 | flock $fh, &Fcntl::LOCK_EX |
|
|
3365 | or die "$PIDFILE: flock: $!"; |
|
|
3366 | $fh |
|
|
3367 | } |
|
|
3368 | |
|
|
3369 | # make sure only one server instance is running at any one time |
|
|
3370 | sub atomic { |
|
|
3371 | my $fh = pidfile; |
|
|
3372 | |
|
|
3373 | my $pid = <$fh>; |
|
|
3374 | kill 9, $pid if $pid > 0; |
|
|
3375 | |
|
|
3376 | seek $fh, 0, 0; |
|
|
3377 | print $fh $$; |
|
|
3378 | } |
|
|
3379 | |
3358 | sub main { |
3380 | sub main { |
|
|
3381 | atomic; |
|
|
3382 | |
3359 | # we must not ever block the main coroutine |
3383 | # we must not ever block the main coroutine |
3360 | local $Coro::idle = sub { |
3384 | local $Coro::idle = sub { |
3361 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3385 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3362 | (async { |
3386 | (async { |
3363 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3387 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
… | |
… | |
3372 | load_extensions; |
3396 | load_extensions; |
3373 | |
3397 | |
3374 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3398 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3375 | } |
3399 | } |
3376 | |
3400 | |
|
|
3401 | utime time, time, $RUNTIMEFILE; |
|
|
3402 | |
|
|
3403 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3404 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
|
|
3405 | |
3377 | EV::loop; |
3406 | EV::loop; |
3378 | } |
3407 | } |
3379 | |
3408 | |
3380 | ############################################################################# |
3409 | ############################################################################# |
3381 | # initialisation and cleanup |
3410 | # initialisation and cleanup |
… | |
… | |
3389 | }; |
3418 | }; |
3390 | } |
3419 | } |
3391 | } |
3420 | } |
3392 | |
3421 | |
3393 | sub write_runtime_sync { |
3422 | sub write_runtime_sync { |
3394 | my $runtime = "$LOCALDIR/runtime"; |
|
|
3395 | |
|
|
3396 | # first touch the runtime file to show we are still running: |
3423 | # first touch the runtime file to show we are still running: |
3397 | # the fsync below can take a very very long time. |
3424 | # the fsync below can take a very very long time. |
3398 | |
3425 | |
3399 | IO::AIO::aio_utime $runtime, undef, undef; |
3426 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3400 | |
3427 | |
3401 | my $guard = cf::lock_acquire "write_runtime"; |
3428 | my $guard = cf::lock_acquire "write_runtime"; |
3402 | |
3429 | |
3403 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
3430 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3404 | or return; |
3431 | or return; |
3405 | |
3432 | |
3406 | my $value = $cf::RUNTIME + 90 + 10; |
3433 | my $value = $cf::RUNTIME + 90 + 10; |
3407 | # 10 is the runtime save interval, for a monotonic clock |
3434 | # 10 is the runtime save interval, for a monotonic clock |
3408 | # 60 allows for the watchdog to kill the server. |
3435 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3418 | aio_utime $fh, undef, undef; |
3445 | aio_utime $fh, undef, undef; |
3419 | |
3446 | |
3420 | close $fh |
3447 | close $fh |
3421 | or return; |
3448 | or return; |
3422 | |
3449 | |
3423 | aio_rename "$runtime~", $runtime |
3450 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3424 | and return; |
3451 | and return; |
3425 | |
3452 | |
3426 | warn "runtime file written.\n"; |
3453 | warn "runtime file written.\n"; |
3427 | |
3454 | |
3428 | 1 |
3455 | 1 |
… | |
… | |
3512 | sub post_cleanup { |
3539 | sub post_cleanup { |
3513 | my ($make_core) = @_; |
3540 | my ($make_core) = @_; |
3514 | |
3541 | |
3515 | warn Carp::longmess "post_cleanup backtrace" |
3542 | warn Carp::longmess "post_cleanup backtrace" |
3516 | if $make_core; |
3543 | if $make_core; |
|
|
3544 | |
|
|
3545 | my $fh = pidfile; |
|
|
3546 | unlink $PIDFILE if <$fh> == $$; |
3517 | } |
3547 | } |
3518 | |
3548 | |
3519 | # a safer delete_package, copied from Symbol |
3549 | # a safer delete_package, copied from Symbol |
3520 | sub clear_package($) { |
3550 | sub clear_package($) { |
3521 | my $pkg = shift; |
3551 | my $pkg = shift; |