… | |
… | |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
358 | |
358 | |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
361 | |
361 | |
|
|
362 | =item $ref = cf::yaml_load $scalar |
|
|
363 | |
|
|
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
365 | |
|
|
366 | =cut |
|
|
367 | |
|
|
368 | sub fork_call(&@); |
|
|
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
372 | } |
|
|
373 | |
362 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
363 | |
375 | |
364 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
365 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
366 | |
378 | |
367 | The cdoeblock will have a single boolean argument to indicate whether this |
379 | The codeblock will have a single boolean argument to indicate whether this |
368 | is a reload or not. |
380 | is a reload or not. |
369 | |
381 | |
370 | =cut |
382 | =cut |
371 | |
383 | |
372 | sub post_init(&) { |
384 | sub post_init(&) { |
373 | push @POST_INIT, shift; |
385 | push @POST_INIT, shift; |
|
|
386 | } |
|
|
387 | |
|
|
388 | sub _post_init { |
|
|
389 | trace "running post_init jobs"; |
|
|
390 | |
|
|
391 | # run them in parallel... |
|
|
392 | |
|
|
393 | my @join; |
|
|
394 | |
|
|
395 | while () { |
|
|
396 | push @join, map &Coro::async ($_, 0), @POST_INIT; |
|
|
397 | @POST_INIT = (); |
|
|
398 | |
|
|
399 | @join or last; |
|
|
400 | |
|
|
401 | (pop @join)->join; |
|
|
402 | } |
374 | } |
403 | } |
375 | |
404 | |
376 | =item cf::lock_wait $string |
405 | =item cf::lock_wait $string |
377 | |
406 | |
378 | Wait until the given lock is available. See cf::lock_acquire. |
407 | Wait until the given lock is available. See cf::lock_acquire. |
… | |
… | |
583 | $EXT_CORO{$coro+0} = $coro; |
612 | $EXT_CORO{$coro+0} = $coro; |
584 | |
613 | |
585 | $coro |
614 | $coro |
586 | } |
615 | } |
587 | |
616 | |
588 | =item fork_call { }, $args |
617 | =item fork_call { }, @args |
589 | |
618 | |
590 | Executes the given code block with the given arguments in a seperate |
619 | Executes the given code block with the given arguments in a seperate |
591 | process, returning the results. Everything must be serialisable with |
620 | process, returning the results. Everything must be serialisable with |
592 | Coro::Storable. May, of course, block. Note that the executed sub may |
621 | Coro::Storable. May, of course, block. Note that the executed sub may |
593 | never block itself or use any form of event handling. |
622 | never block itself or use any form of event handling. |
594 | |
623 | |
595 | =cut |
624 | =cut |
596 | |
625 | |
|
|
626 | sub post_fork { |
|
|
627 | reset_signals; |
|
|
628 | } |
|
|
629 | |
597 | sub fork_call(&@) { |
630 | sub fork_call(&@) { |
598 | my ($cb, @args) = @_; |
631 | my ($cb, @args) = @_; |
599 | |
632 | |
600 | # we seemingly have to make a local copy of the whole thing, |
633 | # we seemingly have to make a local copy of the whole thing, |
601 | # otherwise perl prematurely frees the stuff :/ |
634 | # otherwise perl prematurely frees the stuff :/ |
602 | # TODO: investigate and fix (likely this will be rather laborious) |
635 | # TODO: investigate and fix (likely this will be rather laborious) |
603 | |
636 | |
604 | my @res = Coro::Util::fork_eval { |
637 | my @res = Coro::Util::fork_eval { |
605 | reset_signals; |
638 | cf::post_fork; |
606 | &$cb |
639 | &$cb |
607 | }, @args; |
640 | } @args; |
608 | |
641 | |
609 | wantarray ? @res : $res[-1] |
642 | wantarray ? @res : $res[-1] |
610 | } |
643 | } |
611 | |
644 | |
612 | sub objinfo { |
645 | sub objinfo { |
… | |
… | |
1646 | my $name = $pl->ob->name; |
1679 | my $name = $pl->ob->name; |
1647 | |
1680 | |
1648 | $pl->{deny_save} = 1; |
1681 | $pl->{deny_save} = 1; |
1649 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1682 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1650 | |
1683 | |
1651 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1684 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1652 | $pl->deactivate; |
1685 | $pl->deactivate; |
|
|
1686 | |
1653 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1687 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1654 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1688 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1689 | ext::highscore::check ($pl->ob); |
|
|
1690 | |
1655 | $pl->ns->destroy if $pl->ns; |
1691 | $pl->ns->destroy if $pl->ns; |
1656 | |
1692 | |
1657 | my $path = playerdir $pl; |
1693 | my $path = playerdir $pl; |
1658 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1694 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1659 | aio_rename $path, $temp; |
1695 | aio_rename $path, $temp; |
… | |
… | |
1899 | $base =~ s{[^/]+/?$}{}; |
1935 | $base =~ s{[^/]+/?$}{}; |
1900 | $path = "$base/$path"; |
1936 | $path = "$base/$path"; |
1901 | } |
1937 | } |
1902 | |
1938 | |
1903 | for ($path) { |
1939 | for ($path) { |
1904 | redo if s{//}{/}; |
|
|
1905 | redo if s{/\.?/}{/}; |
1940 | redo if s{/\.?/}{/}; |
1906 | redo if s{/[^/]+/\.\./}{/}; |
1941 | redo if s{/[^/]+/\.\./}{/}; |
1907 | } |
1942 | } |
1908 | |
1943 | |
1909 | $path |
1944 | $path |
… | |
… | |
3547 | |
3582 | |
3548 | 0 < aio_load "$CONFDIR/config", my $config |
3583 | 0 < aio_load "$CONFDIR/config", my $config |
3549 | or die "$CONFDIR/config: $!"; |
3584 | or die "$CONFDIR/config: $!"; |
3550 | |
3585 | |
3551 | utf8::decode $config; |
3586 | utf8::decode $config; |
3552 | |
3587 | *CFG = yaml_load $config; |
3553 | cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow... |
|
|
3554 | *CFG = YAML::XS::Load $config; |
|
|
3555 | |
3588 | |
3556 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3589 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3557 | |
3590 | |
3558 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3591 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3559 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3592 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3643 | |
3676 | |
3644 | # no (long-running) fork's whatsoever before this point(!) |
3677 | # no (long-running) fork's whatsoever before this point(!) |
3645 | use POSIX (); |
3678 | use POSIX (); |
3646 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3679 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3647 | |
3680 | |
3648 | (pop @POST_INIT)->(0) while @POST_INIT; |
3681 | cf::_post_init 0; |
3649 | }; |
3682 | }; |
3650 | |
3683 | |
3651 | cf::object::thawer::errors_are_fatal 0; |
3684 | cf::object::thawer::errors_are_fatal 0; |
3652 | info "parse errors in files are no longer fatal from this point on.\n"; |
3685 | info "parse errors in files are no longer fatal from this point on.\n"; |
3653 | |
3686 | |
… | |
… | |
3876 | |
3909 | |
3877 | my $t1 = AE::time; |
3910 | my $t1 = AE::time; |
3878 | |
3911 | |
3879 | while ($RELOAD) { |
3912 | while ($RELOAD) { |
3880 | cf::get_slot 0.1, -1, "reload_perl"; |
3913 | cf::get_slot 0.1, -1, "reload_perl"; |
3881 | info "reloading..."; |
3914 | info "perl_reload: reloading..."; |
3882 | |
3915 | |
3883 | trace "entering sync_job"; |
3916 | trace "perl_reload: entering sync_job"; |
3884 | |
3917 | |
3885 | cf::sync_job { |
3918 | cf::sync_job { |
3886 | #cf::emergency_save; |
3919 | #cf::emergency_save; |
3887 | |
3920 | |
3888 | trace "cancelling all extension coros"; |
3921 | trace "perl_reload: cancelling all extension coros"; |
3889 | $_->cancel for values %EXT_CORO; |
3922 | $_->cancel for values %EXT_CORO; |
3890 | %EXT_CORO = (); |
3923 | %EXT_CORO = (); |
3891 | |
3924 | |
3892 | trace "removing commands"; |
3925 | trace "perl_reload: removing commands"; |
3893 | %COMMAND = (); |
3926 | %COMMAND = (); |
3894 | |
3927 | |
3895 | trace "removing ext/exti commands"; |
3928 | trace "perl_reload: removing ext/exti commands"; |
3896 | %EXTCMD = (); |
3929 | %EXTCMD = (); |
3897 | %EXTICMD = (); |
3930 | %EXTICMD = (); |
3898 | |
3931 | |
3899 | trace "unloading/nuking all extensions"; |
3932 | trace "perl_reload: unloading/nuking all extensions"; |
3900 | for my $pkg (@EXTS) { |
3933 | for my $pkg (@EXTS) { |
3901 | trace "... unloading $pkg"; |
3934 | trace "... unloading $pkg"; |
3902 | |
3935 | |
3903 | if (my $cb = $pkg->can ("unload")) { |
3936 | if (my $cb = $pkg->can ("unload")) { |
3904 | eval { |
3937 | eval { |
… | |
… | |
3909 | |
3942 | |
3910 | trace "... clearing $pkg"; |
3943 | trace "... clearing $pkg"; |
3911 | clear_package $pkg; |
3944 | clear_package $pkg; |
3912 | } |
3945 | } |
3913 | |
3946 | |
3914 | trace "unloading all perl modules loaded from $LIBDIR"; |
3947 | trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; |
3915 | while (my ($k, $v) = each %INC) { |
3948 | while (my ($k, $v) = each %INC) { |
3916 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3949 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3917 | |
3950 | |
3918 | trace "... unloading $k"; |
3951 | trace "... unloading $k"; |
3919 | delete $INC{$k}; |
3952 | delete $INC{$k}; |
… | |
… | |
3926 | } |
3959 | } |
3927 | |
3960 | |
3928 | clear_package $k; |
3961 | clear_package $k; |
3929 | } |
3962 | } |
3930 | |
3963 | |
3931 | trace "getting rid of safe::, as good as possible"; |
3964 | trace "perl_reload: getting rid of safe::, as good as possible"; |
3932 | clear_package "safe::$_" |
3965 | clear_package "safe::$_" |
3933 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3966 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3934 | |
3967 | |
3935 | trace "unloading cf.pm \"a bit\""; |
3968 | trace "perl_reload: unloading cf.pm \"a bit\""; |
3936 | delete $INC{"cf.pm"}; |
3969 | delete $INC{"cf.pm"}; |
3937 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3970 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3938 | |
3971 | |
3939 | # don't, removes xs symbols, too, |
3972 | # don't, removes xs symbols, too, |
3940 | # and global variables created in xs |
3973 | # and global variables created in xs |
3941 | #clear_package __PACKAGE__; |
3974 | #clear_package __PACKAGE__; |
3942 | |
3975 | |
3943 | info "unload completed, starting to reload now"; |
3976 | info "perl_reload: unload completed, starting to reload now"; |
3944 | |
3977 | |
3945 | trace "reloading cf.pm"; |
3978 | trace "perl_reload: reloading cf.pm"; |
3946 | require cf; |
3979 | require cf; |
3947 | cf::_connect_to_perl_1; |
3980 | cf::_connect_to_perl_1; |
3948 | |
3981 | |
3949 | trace "loading config and database again"; |
3982 | trace "perl_reload: loading config and database again"; |
3950 | cf::reload_config; |
3983 | cf::reload_config; |
3951 | |
3984 | |
3952 | trace "loading extensions"; |
3985 | trace "perl_reload: loading extensions"; |
3953 | cf::load_extensions; |
3986 | cf::load_extensions; |
3954 | |
3987 | |
3955 | if ($REATTACH_ON_RELOAD) { |
3988 | if ($REATTACH_ON_RELOAD) { |
3956 | trace "reattaching attachments to objects/players"; |
3989 | trace "perl_reload: reattaching attachments to objects/players"; |
3957 | _global_reattach; # objects, sockets |
3990 | _global_reattach; # objects, sockets |
3958 | trace "reattaching attachments to maps"; |
3991 | trace "perl_reload: reattaching attachments to maps"; |
3959 | reattach $_ for values %MAP; |
3992 | reattach $_ for values %MAP; |
3960 | trace "reattaching attachments to players"; |
3993 | trace "perl_reload: reattaching attachments to players"; |
3961 | reattach $_ for values %PLAYER; |
3994 | reattach $_ for values %PLAYER; |
3962 | } |
3995 | } |
3963 | |
3996 | |
3964 | trace "running post_init jobs"; |
3997 | cf::_post_init 1; |
3965 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3966 | |
3998 | |
3967 | trace "leaving sync_job"; |
3999 | trace "perl_reload: leaving sync_job"; |
3968 | |
4000 | |
3969 | 1 |
4001 | 1 |
3970 | } or do { |
4002 | } or do { |
3971 | error $@; |
4003 | error $@; |
3972 | cf::cleanup "error while reloading, exiting."; |
4004 | cf::cleanup "perl_reload: error, exiting."; |
3973 | }; |
4005 | }; |
3974 | |
4006 | |
3975 | info "reloaded"; |
|
|
3976 | --$RELOAD; |
4007 | --$RELOAD; |
3977 | } |
4008 | } |
3978 | |
4009 | |
3979 | $t1 = AE::time - $t1; |
4010 | $t1 = AE::time - $t1; |
3980 | info "reload completed in ${t1}s\n"; |
4011 | info "perl_reload: completed in ${t1}s\n"; |
3981 | }; |
4012 | }; |
3982 | |
4013 | |
3983 | our $RELOAD_WATCHER; # used only during reload |
4014 | our $RELOAD_WATCHER; # used only during reload |
3984 | |
4015 | |
3985 | sub reload_perl() { |
4016 | sub reload_perl() { |