ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.216 by root, Thu Feb 15 14:30:21 2007 UTC vs.
Revision 1.262 by root, Sat Apr 28 05:55:55 2007 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Symbol; 6use Symbol;
7use List::Util; 7use List::Util;
8use Socket;
8use Storable; 9use Storable;
10use Event;
9use Opcode; 11use Opcode;
10use Safe; 12use Safe;
11use Safe::Hole; 13use Safe::Hole;
12 14
13use Coro 3.5 (); 15use Coro 3.61 ();
16use Coro::State;
17use Coro::Handle;
14use Coro::Event; 18use Coro::Event;
15use Coro::Timer; 19use Coro::Timer;
16use Coro::Signal; 20use Coro::Signal;
17use Coro::Semaphore; 21use Coro::Semaphore;
18use Coro::AIO; 22use Coro::AIO;
23use Coro::Storable;
19 24
20use BDB (); 25use BDB ();
21use Data::Dumper; 26use Data::Dumper;
22use Digest::MD5; 27use Digest::MD5;
23use Fcntl; 28use Fcntl;
29use YAML::Syck ();
24use IO::AIO 2.32 (); 30use IO::AIO 2.32 ();
25use YAML::Syck ();
26use Time::HiRes; 31use Time::HiRes;
27use Compress::LZF; 32use Compress::LZF;
28 33
34# configure various modules to our taste
35#
36$Storable::canonical = 1; # reduce rsync transfers
37Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator
29Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later 38Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later
30 39
31use Event; $Event::Eval = 1; # no idea why this is required, but it is 40$Event::Eval = 1; # no idea why this is required, but it is
32
33sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
34 41
35# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? 42# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
36$YAML::Syck::ImplicitUnicode = 1; 43$YAML::Syck::ImplicitUnicode = 1;
37 44
38$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority 45$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
46
47sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
39 48
40our %COMMAND = (); 49our %COMMAND = ();
41our %COMMAND_TIME = (); 50our %COMMAND_TIME = ();
42 51
43our @EXTS = (); # list of extension package names 52our @EXTS = (); # list of extension package names
44our %EXTCMD = (); 53our %EXTCMD = ();
45our %EXT_CORO = (); # coroutines bound to extensions 54our %EXT_CORO = (); # coroutines bound to extensions
46our %EXT_MAP = (); # pluggable maps 55our %EXT_MAP = (); # pluggable maps
47 56
57our $RELOAD; # number of reloads so far
48our @EVENT; 58our @EVENT;
49our $LIBDIR = datadir . "/ext";
50 59
51our $TICK = MAX_TIME * 1e-6; 60our $CONFDIR = confdir;
61our $DATADIR = datadir;
62our $LIBDIR = "$DATADIR/ext";
63our $PODDIR = "$DATADIR/pod";
64our $MAPDIR = "$DATADIR/" . mapdir;
65our $LOCALDIR = localdir;
66our $TMPDIR = "$LOCALDIR/" . tmpdir;
67our $UNIQUEDIR = "$LOCALDIR/" . uniquedir;
68our $PLAYERDIR = "$LOCALDIR/" . playerdir;
69our $RANDOMDIR = "$LOCALDIR/random";
70our $BDBDIR = "$LOCALDIR/db";
71
72our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
52our $TICK_WATCHER; 73our $TICK_WATCHER;
53our $AIO_POLL_WATCHER; 74our $AIO_POLL_WATCHER;
54our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 75our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
55our $NEXT_TICK; 76our $NEXT_TICK;
56our $NOW; 77our $NOW;
65our $RUNTIME; 86our $RUNTIME;
66 87
67our %PLAYER; # all users 88our %PLAYER; # all users
68our %MAP; # all maps 89our %MAP; # all maps
69our $LINK_MAP; # the special {link} map, which is always available 90our $LINK_MAP; # the special {link} map, which is always available
70our $RANDOM_MAPS = cf::localdir . "/random";
71our $BDB_ENV_DIR = cf::localdir . "/db";
72
73our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal;
74our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal;
75 91
76# used to convert map paths into valid unix filenames by replacing / by ∕ 92# used to convert map paths into valid unix filenames by replacing / by ∕
77our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons 93our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons
78 94
79binmode STDOUT; 95binmode STDOUT;
80binmode STDERR; 96binmode STDERR;
81 97
82# read virtual server time, if available 98# read virtual server time, if available
83unless ($RUNTIME || !-e cf::localdir . "/runtime") { 99unless ($RUNTIME || !-e "$LOCALDIR/runtime") {
84 open my $fh, "<", cf::localdir . "/runtime" 100 open my $fh, "<", "$LOCALDIR/runtime"
85 or die "unable to read runtime file: $!"; 101 or die "unable to read runtime file: $!";
86 $RUNTIME = <$fh> + 0.; 102 $RUNTIME = <$fh> + 0.;
87} 103}
88 104
89mkdir cf::localdir; 105mkdir $_
90mkdir cf::localdir . "/" . cf::playerdir; 106 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
91mkdir cf::localdir . "/" . cf::tmpdir;
92mkdir cf::localdir . "/" . cf::uniquedir;
93mkdir $RANDOM_MAPS;
94mkdir $BDB_ENV_DIR;
95 107
96our $EMERGENCY_POSITION; 108our $EMERGENCY_POSITION;
97 109
98sub cf::map::normalise; 110sub cf::map::normalise;
99 111
110=item $cf::RUNTIME 122=item $cf::RUNTIME
111 123
112The time this server has run, starts at 0 and is increased by $cf::TICK on 124The time this server has run, starts at 0 and is increased by $cf::TICK on
113every server tick. 125every server tick.
114 126
115=item $cf::LIBDIR 127=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR
128$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR
129$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR
116 130
117The perl library directory, where extensions and cf-specific modules can 131Various directories - "/etc", read-only install directory, perl-library
118be found. It will be added to C<@INC> automatically. 132directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
133unique-items directory, player file directory, random maps directory and
134database environment.
119 135
120=item $cf::NOW 136=item $cf::NOW
121 137
122The time of the last (current) server tick. 138The time of the last (current) server tick.
123 139
128=item %cf::CFG 144=item %cf::CFG
129 145
130Configuration for the server, loaded from C</etc/crossfire/config>, or 146Configuration for the server, loaded from C</etc/crossfire/config>, or
131from wherever your confdir points to. 147from wherever your confdir points to.
132 148
133=item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE 149=item cf::wait_for_tick, cf::wait_for_tick_begin
134 150
135These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) 151These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
136or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick 152returns directly I<after> the tick processing (and consequently, can only wake one process
137processing has been done. Call C<< ->wait >> on them to maximise the 153per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
138window of cpu time available, or simply to synchronise to the server tick.
139 154
140=back 155=back
141 156
142=cut 157=cut
143 158
144BEGIN { 159BEGIN {
145 *CORE::GLOBAL::warn = sub { 160 *CORE::GLOBAL::warn = sub {
146 my $msg = join "", @_; 161 my $msg = join "", @_;
147 utf8::encode $msg;
148 162
149 $msg .= "\n" 163 $msg .= "\n"
150 unless $msg =~ /\n$/; 164 unless $msg =~ /\n$/;
151 165
166 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
167
168 utf8::encode $msg;
152 LOG llevError, $msg; 169 LOG llevError, $msg;
153 }; 170 };
154} 171}
155 172
156@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 173@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
202 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 219 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
203 $d 220 $d
204 } || "[unable to dump $_[0]: '$@']"; 221 } || "[unable to dump $_[0]: '$@']";
205} 222}
206 223
207use JSON::Syck (); # TODO# replace by JSON::PC once working 224use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working
208 225
209=item $ref = cf::from_json $json 226=item $ref = cf::from_json $json
210 227
211Converts a JSON string into the corresponding perl data structure. 228Converts a JSON string into the corresponding perl data structure.
212 229
213=cut
214
215sub from_json($) {
216 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
217 JSON::Syck::Load $_[0]
218}
219
220=item $json = cf::to_json $ref 230=item $json = cf::to_json $ref
221 231
222Converts a perl data structure into its JSON representation. 232Converts a perl data structure into its JSON representation.
223
224=cut
225
226sub to_json($) {
227 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
228 JSON::Syck::Dump $_[0]
229}
230 233
231=item cf::lock_wait $string 234=item cf::lock_wait $string
232 235
233Wait until the given lock is available. See cf::lock_acquire. 236Wait until the given lock is available. See cf::lock_acquire.
234 237
240returned. 243returned.
241 244
242Lock names should begin with a unique identifier (for example, cf::map::find 245Lock names should begin with a unique identifier (for example, cf::map::find
243uses map_find and cf::map::load uses map_load). 246uses map_find and cf::map::load uses map_load).
244 247
248=item $locked = cf::lock_active $string
249
250Return true if the lock is currently active, i.e. somebody has locked it.
251
245=cut 252=cut
246 253
247our %LOCK; 254our %LOCK;
248 255
249sub lock_wait($) { 256sub lock_wait($) {
266 273
267 Coro::guard { 274 Coro::guard {
268 # wake up all waiters, to be on the safe side 275 # wake up all waiters, to be on the safe side
269 $_->ready for @{ delete $LOCK{$key} }; 276 $_->ready for @{ delete $LOCK{$key} };
270 } 277 }
278}
279
280sub lock_active($) {
281 my ($key) = @_;
282
283 ! ! $LOCK{$key}
271} 284}
272 285
273sub freeze_mainloop { 286sub freeze_mainloop {
274 return unless $TICK_WATCHER->is_active; 287 return unless $TICK_WATCHER->is_active;
275 288
354 367
355 $coro 368 $coro
356} 369}
357 370
358sub write_runtime { 371sub write_runtime {
359 my $runtime = cf::localdir . "/runtime"; 372 my $guard = cf::lock_acquire "write_runtime";
373
374 my $runtime = "$LOCALDIR/runtime";
360 375
361 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 376 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
362 or return; 377 or return;
363 378
364 my $value = $cf::RUNTIME + 90 + 10; 379 my $value = $cf::RUNTIME + 90 + 10;
377 392
378 aio_rename "$runtime~", $runtime 393 aio_rename "$runtime~", $runtime
379 and return; 394 and return;
380 395
381 1 396 1
397}
398
399=item cf::datalog type => key => value, ...
400
401Log a datalog packet of the given type with the given key-value pairs.
402
403=cut
404
405sub datalog($@) {
406 my ($type, %kv) = @_;
407 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
382} 408}
383 409
384=back 410=back
385 411
386=cut 412=cut
948Returns the given player object, loading it if necessary (might block). 974Returns the given player object, loading it if necessary (might block).
949 975
950=cut 976=cut
951 977
952sub playerdir($) { 978sub playerdir($) {
953 cf::localdir 979 "$PLAYERDIR/"
954 . "/"
955 . cf::playerdir
956 . "/"
957 . (ref $_[0] ? $_[0]->ob->name : $_[0]) 980 . (ref $_[0] ? $_[0]->ob->name : $_[0])
958} 981}
959 982
960sub path($) { 983sub path($) {
961 my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; 984 my $login = ref $_[0] ? $_[0]->ob->name : $_[0];
962 985
963 (playerdir $login) . "/$login.pl" 986 (playerdir $login) . "/playerdata"
964} 987}
965 988
966sub find_active($) { 989sub find_active($) {
967 $cf::PLAYER{$_[0]} 990 $cf::PLAYER{$_[0]}
968 and $cf::PLAYER{$_[0]}->active 991 and $cf::PLAYER{$_[0]}->active
981 my $login = $_[0]; 1004 my $login = $_[0];
982 1005
983 my $guard = cf::lock_acquire "user_find:$login"; 1006 my $guard = cf::lock_acquire "user_find:$login";
984 1007
985 $cf::PLAYER{$_[0]} || do { 1008 $cf::PLAYER{$_[0]} || do {
1009 # rename old playerfiles to new ones
1010 #TODO: remove when no longer required
1011 aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst";
1012 aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata";
1013 aio_unlink +(playerdir $login) . "/$login.pl.pst";
1014 aio_unlink +(playerdir $login) . "/$login.pl";
1015
986 my $pl = load_pl path $login 1016 my $pl = load_pl path $login
987 or return; 1017 or return;
988 $cf::PLAYER{$login} = $pl 1018 $cf::PLAYER{$login} = $pl
989 } 1019 }
990 } 1020 }
1027 1057
1028=cut 1058=cut
1029 1059
1030sub quit_character { 1060sub quit_character {
1031 my ($pl) = @_; 1061 my ($pl) = @_;
1062
1063 my $name = $pl->ob->name;
1032 1064
1033 $pl->{deny_save} = 1; 1065 $pl->{deny_save} = 1;
1034 $pl->password ("*"); # this should lock out the player until we nuked the dir 1066 $pl->password ("*"); # this should lock out the player until we nuked the dir
1035 1067
1036 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1068 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1041 my $path = playerdir $pl; 1073 my $path = playerdir $pl;
1042 my $temp = "$path~$cf::RUNTIME~deleting~"; 1074 my $temp = "$path~$cf::RUNTIME~deleting~";
1043 aio_rename $path, $temp; 1075 aio_rename $path, $temp;
1044 delete $cf::PLAYER{$pl->ob->name}; 1076 delete $cf::PLAYER{$pl->ob->name};
1045 $pl->destroy; 1077 $pl->destroy;
1078
1079 my $prefix = qr<^~\Q$name\E/>;
1080
1081 # nuke player maps
1082 $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP;
1083
1046 IO::AIO::aio_rmtree $temp; 1084 IO::AIO::aio_rmtree $temp;
1085}
1086
1087=item $pl->kick
1088
1089Kicks a player out of the game. This destroys the connection.
1090
1091=cut
1092
1093sub kick {
1094 my ($pl, $kicker) = @_;
1095
1096 $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker);
1097 $pl->killer ("kicked");
1098 $pl->ns->destroy;
1047} 1099}
1048 1100
1049=item cf::player::list_logins 1101=item cf::player::list_logins
1050 1102
1051Returns am arrayref of all valid playernames in the system, can take a 1103Returns am arrayref of all valid playernames in the system, can take a
1052while and may block, so not sync_job-capable, ever. 1104while and may block, so not sync_job-capable, ever.
1053 1105
1054=cut 1106=cut
1055 1107
1056sub list_logins { 1108sub list_logins {
1057 my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir 1109 my $dirs = aio_readdir $PLAYERDIR
1058 or return []; 1110 or return [];
1059 1111
1060 my @logins; 1112 my @logins;
1061 1113
1062 for my $login (@$dirs) { 1114 for my $login (@$dirs) {
1097 } 1149 }
1098 1150
1099 \@paths 1151 \@paths
1100} 1152}
1101 1153
1102=item $player->ext_reply ($msgid, $msgtype, %msg) 1154=item $player->ext_reply ($msgid, %msg)
1103 1155
1104Sends an ext reply to the player. 1156Sends an ext reply to the player.
1105 1157
1106=cut 1158=cut
1107 1159
1108sub ext_reply($$$%) { 1160sub ext_reply($$%) {
1109 my ($self, $id, %msg) = @_; 1161 my ($self, $id, %msg) = @_;
1110 1162
1111 $msg{msgid} = $id; 1163 $msg{msgid} = $id;
1112 1164
1113 $self->send ("ext " . cf::to_json \%msg); 1165 $self->send ("ext " . cf::to_json \%msg);
1114} 1166}
1115 1167
1116package cf; 1168=item $player->ext_event ($type, %msg)
1169
1170Sends an ext event to the client.
1171
1172=cut
1173
1174sub ext_event($$%) {
1175 my ($self, $type, %msg) = @_;
1176
1177 $self->ns->ext_event ($type, %msg);
1178}
1179
1180=head3 cf::region
1181
1182=over 4
1183
1184=cut
1185
1186package cf::region;
1187
1188=item cf::region::find_by_path $path
1189
1190Tries to decuce the probable region for a map knowing only its path.
1191
1192=cut
1193
1194sub find_by_path($) {
1195 my ($path) = @_;
1196
1197 my ($match, $specificity);
1198
1199 for my $region (list) {
1200 if ($region->match && $path =~ $region->match) {
1201 ($match, $specificity) = ($region, $region->specificity)
1202 if $region->specificity > $specificity;
1203 }
1204 }
1205
1206 $match
1207}
1117 1208
1118=back 1209=back
1119
1120 1210
1121=head3 cf::map 1211=head3 cf::map
1122 1212
1123=over 4 1213=over 4
1124 1214
1262 1352
1263# the original (read-only) location 1353# the original (read-only) location
1264sub load_path { 1354sub load_path {
1265 my ($self) = @_; 1355 my ($self) = @_;
1266 1356
1267 sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} 1357 "$MAPDIR/$self->{path}.map"
1268} 1358}
1269 1359
1270# the temporary/swap location 1360# the temporary/swap location
1271sub save_path { 1361sub save_path {
1272 my ($self) = @_; 1362 my ($self) = @_;
1273 1363
1274 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; 1364 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g;
1275 sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path 1365 "$TMPDIR/$path.map"
1276} 1366}
1277 1367
1278# the unique path, undef == no special unique path 1368# the unique path, undef == no special unique path
1279sub uniq_path { 1369sub uniq_path {
1280 my ($self) = @_; 1370 my ($self) = @_;
1281 1371
1282 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; 1372 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g;
1283 sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path 1373 "$UNIQUEDIR/$path"
1284} 1374}
1285 1375
1286# and all this just because we cannot iterate over 1376# and all this just because we cannot iterate over
1287# all maps in C++... 1377# all maps in C++...
1288sub change_all_map_light { 1378sub change_all_map_light {
1355 $self->load_header_orig 1445 $self->load_header_orig
1356 or return; 1446 or return;
1357 $self->prepare_orig; 1447 $self->prepare_orig;
1358 } 1448 }
1359 1449
1450 $self->default_region (cf::region::find_by_path $self->{path})
1451 unless $self->default_region;
1452
1360 1 1453 1
1361} 1454}
1362 1455
1363sub find; 1456sub find;
1364sub find { 1457sub find {
1397 my ($self) = @_; 1490 my ($self) = @_;
1398 1491
1399 local $self->{deny_reset} = 1; # loading can take a long time 1492 local $self->{deny_reset} = 1; # loading can take a long time
1400 1493
1401 my $path = $self->{path}; 1494 my $path = $self->{path};
1495
1496 {
1402 my $guard = cf::lock_acquire "map_load:$path"; 1497 my $guard = cf::lock_acquire "map_load:$path";
1403 1498
1404 return if $self->in_memory != cf::MAP_SWAPPED; 1499 return if $self->in_memory != cf::MAP_SWAPPED;
1405 1500
1406 $self->in_memory (cf::MAP_LOADING); 1501 $self->in_memory (cf::MAP_LOADING);
1407 1502
1408 $self->alloc; 1503 $self->alloc;
1409 1504
1410 $self->pre_load; 1505 $self->pre_load;
1411
1412 $self->_load_objects ($self->{load_path}, 1)
1413 or return;
1414
1415 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1416 if delete $self->{load_original};
1417
1418 if (my $uniq = $self->uniq_path) {
1419 utf8::encode $uniq;
1420 if (aio_open $uniq, O_RDONLY, 0) {
1421 $self->clear_unique_items;
1422 $self->_load_objects ($uniq, 0);
1423 }
1424 }
1425
1426 Coro::cede;
1427
1428 # now do the right thing for maps
1429 $self->link_multipart_objects;
1430
1431 unless ($self->{deny_activate}) {
1432 $self->decay_objects;
1433 $self->fix_auto_apply;
1434 $self->update_buttons;
1435 Coro::cede; 1506 Coro::cede;
1436 $self->set_darkness_map; 1507
1508 $self->_load_objects ($self->{load_path}, 1)
1509 or return;
1510
1511 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1512 if delete $self->{load_original};
1513
1514 if (my $uniq = $self->uniq_path) {
1515 utf8::encode $uniq;
1516 if (aio_open $uniq, O_RDONLY, 0) {
1517 $self->clear_unique_items;
1518 $self->_load_objects ($uniq, 0);
1519 }
1520 }
1521
1522 Coro::cede;
1523 # now do the right thing for maps
1524 $self->link_multipart_objects;
1437 $self->difficulty ($self->estimate_difficulty) 1525 $self->difficulty ($self->estimate_difficulty)
1438 unless $self->difficulty; 1526 unless $self->difficulty;
1439 Coro::cede; 1527 Coro::cede;
1528
1529 unless ($self->{deny_activate}) {
1530 $self->decay_objects;
1531 $self->fix_auto_apply;
1532 $self->update_buttons;
1533 Coro::cede;
1534 $self->set_darkness_map;
1535 Coro::cede;
1440 $self->activate; 1536 $self->activate;
1537 }
1538
1539 $self->in_memory (cf::MAP_IN_MEMORY);
1441 } 1540 }
1442 1541
1443 $self->post_load; 1542 $self->post_load;
1444
1445 $self->in_memory (cf::MAP_IN_MEMORY);
1446} 1543}
1447 1544
1448sub customise_for { 1545sub customise_for {
1449 my ($self, $ob) = @_; 1546 my ($self, $ob) = @_;
1450 1547
1641instantiated unique items. May block. 1738instantiated unique items. May block.
1642 1739
1643=cut 1740=cut
1644 1741
1645sub unique_maps() { 1742sub unique_maps() {
1646 my $files = aio_readdir cf::localdir . "/" . cf::uniquedir 1743 my $files = aio_readdir $UNIQUEDIR
1647 or return; 1744 or return;
1648 1745
1649 my @paths; 1746 my @paths;
1650 1747
1651 for (@$files) { 1748 for (@$files) {
1907 2004
1908 $rmp->{random_seed} ||= $exit->random_seed; 2005 $rmp->{random_seed} ||= $exit->random_seed;
1909 2006
1910 my $data = cf::to_json $rmp; 2007 my $data = cf::to_json $rmp;
1911 my $md5 = Digest::MD5::md5_hex $data; 2008 my $md5 = Digest::MD5::md5_hex $data;
1912 my $meta = "$cf::RANDOM_MAPS/$md5.meta"; 2009 my $meta = "$RANDOMDIR/$md5.meta";
1913 2010
1914 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { 2011 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) {
1915 aio_write $fh, 0, (length $data), $data, 0; 2012 aio_write $fh, 0, (length $data), $data, 0;
1916 undef $fh; 2013 undef $fh;
1917 aio_rename "$meta~", $meta; 2014 aio_rename "$meta~", $meta;
1926 2023
1927 return unless $self->type == cf::PLAYER; 2024 return unless $self->type == cf::PLAYER;
1928 2025
1929 if ($exit->slaying eq "/!") { 2026 if ($exit->slaying eq "/!") {
1930 #TODO: this should de-fi-ni-te-ly not be a sync-job 2027 #TODO: this should de-fi-ni-te-ly not be a sync-job
2028 # the problem is that $exit might not survive long enough
2029 # so it needs to be done right now, right here
1931 cf::sync_job { prepare_random_map $exit }; 2030 cf::sync_job { prepare_random_map $exit };
1932 } 2031 }
1933 2032
1934 my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2033 my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path;
1935 my $hp = $exit->stats->hp; 2034 my $hp = $exit->stats->hp;
1943 $self->goto ($slaying, $hp, $sp); 2042 $self->goto ($slaying, $hp, $sp);
1944 2043
1945 1; 2044 1;
1946 }) { 2045 }) {
1947 $self->message ("Something went wrong deep within the crossfire server. " 2046 $self->message ("Something went wrong deep within the crossfire server. "
1948 . "I'll try to bring you back to the map you were before. " 2047 . "I'll try to bring you back to the map you were before. "
1949 . "Please report this to the dungeon master!", 2048 . "Please report this to the dungeon master!",
1950 cf::NDI_UNIQUE | cf::NDI_RED); 2049 cf::NDI_UNIQUE | cf::NDI_RED);
1951 2050
1952 warn "ERROR in enter_exit: $@"; 2051 warn "ERROR in enter_exit: $@";
1953 $self->leave_link; 2052 $self->leave_link;
1954 } 2053 }
1955 })->prio (1); 2054 })->prio (1);
1968 2067
1969sub cf::client::send_drawinfo { 2068sub cf::client::send_drawinfo {
1970 my ($self, $text, $flags) = @_; 2069 my ($self, $text, $flags) = @_;
1971 2070
1972 utf8::encode $text; 2071 utf8::encode $text;
1973 $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); 2072 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text);
1974} 2073}
1975 2074
2075=item $client->ext_event ($type, %msg)
2076
2077Sends an exti event to the client.
2078
2079=cut
2080
2081sub cf::client::ext_event($$%) {
2082 my ($self, $type, %msg) = @_;
2083
2084 $msg{msgtype} = "event_$type";
2085 $self->send_packet ("ext " . cf::to_json \%msg);
2086}
1976 2087
1977=item $success = $client->query ($flags, "text", \&cb) 2088=item $success = $client->query ($flags, "text", \&cb)
1978 2089
1979Queues a query to the client, calling the given callback with 2090Queues a query to the client, calling the given callback with
1980the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, 2091the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
2077 2188
2078# here we export the classes and methods available to script code 2189# here we export the classes and methods available to script code
2079 2190
2080=pod 2191=pod
2081 2192
2082The following fucntions and emthods are available within a safe environment: 2193The following functions and methods are available within a safe environment:
2083 2194
2084 cf::object contr pay_amount pay_player map 2195 cf::object contr pay_amount pay_player map
2085 cf::object::player player 2196 cf::object::player player
2086 cf::player peaceful 2197 cf::player peaceful
2087 cf::map trigger 2198 cf::map trigger
2172############################################################################# 2283#############################################################################
2173 2284
2174=head2 EXTENSION DATABASE SUPPORT 2285=head2 EXTENSION DATABASE SUPPORT
2175 2286
2176Crossfire maintains a very simple database for extension use. It can 2287Crossfire maintains a very simple database for extension use. It can
2177currently store anything that can be serialised using Storable, which 2288currently store binary data only (use Compress::LZF::sfreeze_cr/sthaw to
2178excludes objects. 2289convert to/from binary).
2179 2290
2180The parameter C<$family> should best start with the name of the extension 2291The parameter C<$family> should best start with the name of the extension
2181using it, it should be unique. 2292using it, it should be unique.
2182 2293
2183=over 4 2294=over 4
2206 BDB::CREATE | BDB::AUTO_COMMIT, 0666; 2317 BDB::CREATE | BDB::AUTO_COMMIT, 0666;
2207 cf::cleanup "db_open(db): $!" if $!; 2318 cf::cleanup "db_open(db): $!" if $!;
2208 }; 2319 };
2209 cf::cleanup "db_open(db): $@" if $@; 2320 cf::cleanup "db_open(db): $@" if $@;
2210 }; 2321 };
2211
2212 my $path = cf::localdir . "/database.pst";
2213 if (stat $path) {
2214 cf::sync_job {
2215 my $pst = Storable::retrieve $path;
2216
2217 cf::db_put (board => data => $pst->{board});
2218 cf::db_put (guildrules => data => $pst->{guildrules});
2219 cf::db_put (rent => balance => $pst->{rent}{balance});
2220 BDB::db_env_txn_checkpoint $DB_ENV;
2221
2222 unlink $path;
2223 };
2224 }
2225 } 2322 }
2226} 2323}
2227 2324
2228sub db_get($$) { 2325sub db_get($$) {
2229 my $key = "$_[0]/$_[1]"; 2326 my $key = "$_[0]/$_[1]";
2230 2327
2231 cf::sync_job { 2328 cf::sync_job {
2232 BDB::db_get $DB, undef, $key, my $data; 2329 BDB::db_get $DB, undef, $key, my $data;
2233 2330
2234 $! ? () 2331 $! ? ()
2235 : Compress::LZF::sthaw $data 2332 : $data
2236 } 2333 }
2237} 2334}
2238 2335
2239sub db_put($$$) { 2336sub db_put($$$) {
2240 BDB::dbreq_pri 4; 2337 BDB::dbreq_pri 4;
2241 BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; 2338 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
2242} 2339}
2340
2341=item cf::cache $id => [$paths...], $processversion => $process
2342
2343Generic caching function that returns the value of the resource $id,
2344caching and regenerating as required.
2345
2346This function can block.
2347
2348=cut
2349
2350sub cache {
2351 my ($id, $src, $processversion, $process) = @_;
2352
2353 my $meta =
2354 join "\x00",
2355 $processversion,
2356 map {
2357 aio_stat $_
2358 and Carp::croak "$_: $!";
2359
2360 ($_, (stat _)[7,9])
2361 } @$src;
2362
2363 my $dbmeta = db_get cache => "$id/meta";
2364 if ($dbmeta ne $meta) {
2365 # changed, we may need to process
2366
2367 my @data;
2368 my $md5;
2369
2370 for (0 .. $#$src) {
2371 0 <= aio_load $src->[$_], $data[$_]
2372 or Carp::croak "$src->[$_]: $!";
2373 }
2374
2375 # if processing is expensive, check
2376 # checksum first
2377 if (1) {
2378 $md5 =
2379 join "\x00",
2380 $processversion,
2381 map {
2382 Coro::cede;
2383 ($src->[$_], Digest::MD5::md5_hex $data[$_])
2384 } 0.. $#$src;
2385
2386
2387 my $dbmd5 = db_get cache => "$id/md5";
2388 if ($dbmd5 eq $md5) {
2389 db_put cache => "$id/meta", $meta;
2390
2391 return db_get cache => "$id/data";
2392 }
2393 }
2394
2395 my $t1 = Time::HiRes::time;
2396 my $data = $process->(\@data);
2397 my $t2 = Time::HiRes::time;
2398
2399 warn "cache: '$id' processed in ", $t2 - $t1, "s\n";
2400
2401 db_put cache => "$id/data", $data;
2402 db_put cache => "$id/md5" , $md5;
2403 db_put cache => "$id/meta", $meta;
2404
2405 return $data;
2406 }
2407
2408 db_get cache => "$id/data"
2409}
2410
2411=item fork_call { }, $args
2412
2413Executes the given code block with the given arguments in a seperate
2414process, returning the results. Everything must be serialisable with
2415Coro::Storable. May, of course, block. Note that the executed sub may
2416never block itself or use any form of Event handling.
2417
2418=cut
2419
2420sub fork_call(&@) {
2421 my ($cb, @args) = @_;
2422
2423# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
2424# or die "socketpair: $!";
2425 pipe my $fh1, my $fh2
2426 or die "pipe: $!";
2427
2428 if (my $pid = fork) {
2429 close $fh2;
2430
2431 my $res = (Coro::Handle::unblock $fh1)->readline (undef);
2432 $res = Coro::Storable::thaw $res;
2433
2434 waitpid $pid, 0; # should not block anymore, we expect the child to simply behave
2435
2436 die $$res unless "ARRAY" eq ref $res;
2437
2438 return wantarray ? @$res : $res->[-1];
2439 } else {
2440 reset_signals;
2441 local $SIG{__WARN__};
2442 local $SIG{__DIE__};
2443 eval {
2444 close $fh1;
2445
2446 my @res = eval { $cb->(@args) };
2447 syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res);
2448 };
2449
2450 warn $@ if $@;
2451 _exit 0;
2452 }
2453}
2454
2455
2243 2456
2244############################################################################# 2457#############################################################################
2245# the server's init and main functions 2458# the server's init and main functions
2246 2459
2247sub load_resources { 2460sub load_facedata($) {
2248 load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir 2461 my ($path) = @_;
2462
2463 warn "loading facedata from $path\n";
2464
2465 my $facedata;
2466 0 < aio_load $path, $facedata
2467 or die "$path: $!";
2468
2469 $facedata = Coro::Storable::thaw $facedata;
2470
2471 $facedata->{version} == 2
2472 or cf::cleanup "$path: version mismatch, cannot proceed.";
2473
2474 {
2475 my $faces = $facedata->{faceinfo};
2476
2477 while (my ($face, $info) = each %$faces) {
2478 my $idx = (cf::face::find $face) || cf::face::alloc $face;
2479 cf::face::set $idx, $info->{visibility}, $info->{magicmap};
2480 cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32};
2481 cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64};
2482 Coro::cede;
2483 }
2484
2485 while (my ($face, $info) = each %$faces) {
2486 next unless $info->{smooth};
2487 my $idx = cf::face::find $face
2488 or next;
2489 if (my $smooth = cf::face::find $info->{smooth}) {
2490 cf::face::set_smooth $idx, $smooth, $info->{smoothlevel};
2491 } else {
2492 warn "smooth face '$info->{smooth}' not found for face '$face'";
2493 }
2494 Coro::cede;
2495 }
2496 }
2497
2498 {
2499 my $anims = $facedata->{animinfo};
2500
2501 while (my ($anim, $info) = each %$anims) {
2502 cf::anim::set $anim, $info->{frames}, $info->{facings};
2503 Coro::cede;
2504 }
2505
2506 cf::anim::invalidate_all; # d'oh
2507 }
2508
2509 1
2510}
2511
2512sub reload_regions {
2513 load_resource_file "$MAPDIR/regions"
2249 or die "unable to load regions file\n";#d# 2514 or die "unable to load regions file\n";
2515}
2516
2517sub reload_facedata {
2518 load_facedata "$DATADIR/facedata"
2519 or die "unable to load facedata\n";
2520}
2521
2522sub reload_archetypes {
2523 load_resource_file "$DATADIR/archetypes"
2524 or die "unable to load archetypes\n";
2525}
2526
2527sub reload_treasures {
2528 load_resource_file "$DATADIR/treasures"
2529 or die "unable to load treasurelists\n";
2530}
2531
2532sub reload_resources {
2533 warn "reloading resource files...\n";
2534
2535 reload_regions;
2536 reload_facedata;
2537 reload_archetypes;
2538 reload_treasures;
2539
2540 warn "finished reloading resource files\n";
2541}
2542
2543sub init {
2544 reload_resources;
2250} 2545}
2251 2546
2252sub cfg_load { 2547sub cfg_load {
2253 open my $fh, "<:utf8", cf::confdir . "/config" 2548 open my $fh, "<:utf8", "$CONFDIR/config"
2254 or return; 2549 or return;
2255 2550
2256 local $/; 2551 local $/;
2257 *CFG = YAML::Syck::Load <$fh>; 2552 *CFG = YAML::Syck::Load <$fh>;
2258 2553
2266 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 2561 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
2267 and die "WARNING: m(un)lockall failed: $!\n"; 2562 and die "WARNING: m(un)lockall failed: $!\n";
2268 }; 2563 };
2269 warn $@ if $@; 2564 warn $@ if $@;
2270 } 2565 }
2271}
2272
2273sub init {
2274 load_resources;
2275} 2566}
2276 2567
2277sub main { 2568sub main {
2278 # we must not ever block the main coroutine 2569 # we must not ever block the main coroutine
2279 local $Coro::idle = sub { 2570 local $Coro::idle = sub {
2347 2638
2348 warn Carp::longmess "post_cleanup backtrace" 2639 warn Carp::longmess "post_cleanup backtrace"
2349 if $make_core; 2640 if $make_core;
2350} 2641}
2351 2642
2352sub reload() { 2643sub do_reload_perl() {
2353 # can/must only be called in main 2644 # can/must only be called in main
2354 if ($Coro::current != $Coro::main) { 2645 if ($Coro::current != $Coro::main) {
2355 warn "can only reload from main coroutine"; 2646 warn "can only reload from main coroutine";
2356 return; 2647 return;
2357 } 2648 }
2382 Coro::cede; 2673 Coro::cede;
2383 last unless IO::AIO::nreqs || BDB::nreqs; 2674 last unless IO::AIO::nreqs || BDB::nreqs;
2384 warn "iterate..."; 2675 warn "iterate...";
2385 } 2676 }
2386 2677
2678 ++$RELOAD;
2679
2387 warn "cancelling all extension coros"; 2680 warn "cancelling all extension coros";
2388 $_->cancel for values %EXT_CORO; 2681 $_->cancel for values %EXT_CORO;
2389 %EXT_CORO = (); 2682 %EXT_CORO = ();
2390 2683
2391 warn "removing commands"; 2684 warn "removing commands";
2430 Symbol::delete_package "safe::$_" 2723 Symbol::delete_package "safe::$_"
2431 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 2724 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
2432 2725
2433 warn "unloading cf.pm \"a bit\""; 2726 warn "unloading cf.pm \"a bit\"";
2434 delete $INC{"cf.pm"}; 2727 delete $INC{"cf.pm"};
2728 delete $INC{"cf/pod.pm"};
2435 2729
2436 # don't, removes xs symbols, too, 2730 # don't, removes xs symbols, too,
2437 # and global variables created in xs 2731 # and global variables created in xs
2438 #Symbol::delete_package __PACKAGE__; 2732 #Symbol::delete_package __PACKAGE__;
2439 2733
2448 2742
2449 warn "loading extensions"; 2743 warn "loading extensions";
2450 cf::load_extensions; 2744 cf::load_extensions;
2451 2745
2452 warn "reattaching attachments to objects/players"; 2746 warn "reattaching attachments to objects/players";
2453 _global_reattach; 2747 _global_reattach; # objects, sockets
2454 warn "reattaching attachments to maps"; 2748 warn "reattaching attachments to maps";
2455 reattach $_ for values %MAP; 2749 reattach $_ for values %MAP;
2456 2750 warn "reattaching attachments to players";
2457 warn "loading reloadable resources"; 2751 reattach $_ for values %PLAYER;
2458 load_resources;
2459 2752
2460 warn "leaving sync_job"; 2753 warn "leaving sync_job";
2461 2754
2462 1 2755 1
2463 } or do { 2756 } or do {
2469 warn "reloaded"; 2762 warn "reloaded";
2470}; 2763};
2471 2764
2472our $RELOAD_WATCHER; # used only during reload 2765our $RELOAD_WATCHER; # used only during reload
2473 2766
2767sub reload_perl() {
2768 # doing reload synchronously and two reloads happen back-to-back,
2769 # coro crashes during coro_state_free->destroy here.
2770
2771 $RELOAD_WATCHER ||= Event->timer (
2772 reentrant => 0,
2773 after => 0,
2774 data => WF_AUTOCANCEL,
2775 cb => sub {
2776 do_reload_perl;
2777 undef $RELOAD_WATCHER;
2778 },
2779 );
2780}
2781
2474register_command "reload" => sub { 2782register_command "reload" => sub {
2475 my ($who, $arg) = @_; 2783 my ($who, $arg) = @_;
2476 2784
2477 if ($who->flag (FLAG_WIZ)) { 2785 if ($who->flag (FLAG_WIZ)) {
2478 $who->message ("reloading server."); 2786 $who->message ("reloading server.");
2479 2787 async { reload_perl };
2480 # doing reload synchronously and two reloads happen back-to-back,
2481 # coro crashes during coro_state_free->destroy here.
2482
2483 $RELOAD_WATCHER ||= Event->timer (
2484 reentrant => 0,
2485 after => 0,
2486 data => WF_AUTOCANCEL,
2487 cb => sub {
2488 reload;
2489 undef $RELOAD_WATCHER;
2490 },
2491 );
2492 } 2788 }
2493}; 2789};
2494 2790
2495unshift @INC, $LIBDIR; 2791unshift @INC, $LIBDIR;
2496 2792
2497my $bug_warning = 0; 2793my $bug_warning = 0;
2794
2795our @WAIT_FOR_TICK;
2796our @WAIT_FOR_TICK_BEGIN;
2797
2798sub wait_for_tick {
2799 return unless $TICK_WATCHER->is_active;
2800 return if $Coro::current == $Coro::main;
2801
2802 my $signal = new Coro::Signal;
2803 push @WAIT_FOR_TICK, $signal;
2804 $signal->wait;
2805}
2806
2807sub wait_for_tick_begin {
2808 return unless $TICK_WATCHER->is_active;
2809 return if $Coro::current == $Coro::main;
2810
2811 my $signal = new Coro::Signal;
2812 push @WAIT_FOR_TICK_BEGIN, $signal;
2813 $signal->wait;
2814}
2498 2815
2499$TICK_WATCHER = Event->timer ( 2816$TICK_WATCHER = Event->timer (
2500 reentrant => 0, 2817 reentrant => 0,
2501 parked => 1, 2818 parked => 1,
2502 prio => 0, 2819 prio => 0,
2510 } 2827 }
2511 2828
2512 $NOW = Event::time; 2829 $NOW = Event::time;
2513 2830
2514 cf::server_tick; # one server iteration 2831 cf::server_tick; # one server iteration
2832
2515 $RUNTIME += $TICK; 2833 $RUNTIME += $TICK;
2516 $NEXT_TICK += $TICK; 2834 $NEXT_TICK += $TICK;
2517 2835
2518 if ($NOW >= $NEXT_RUNTIME_WRITE) { 2836 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2519 $NEXT_RUNTIME_WRITE = $NOW + 10; 2837 $NEXT_RUNTIME_WRITE = $NOW + 10;
2521 write_runtime 2839 write_runtime
2522 or warn "ERROR: unable to write runtime file: $!"; 2840 or warn "ERROR: unable to write runtime file: $!";
2523 }; 2841 };
2524 } 2842 }
2525 2843
2526
2527 $WAIT_FOR_TICK->broadcast;
2528 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2529
2530# my $AFTER = Event::time; 2844# my $AFTER = Event::time;
2531# warn $AFTER - $NOW;#d# 2845# warn $AFTER - $NOW;#d#
2532 2846
2533 # if we are delayed by four ticks or more, skip them all 2847 # if we are delayed by four ticks or more, skip them all
2534 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2848 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2535 2849
2536 $TICK_WATCHER->at ($NEXT_TICK); 2850 $TICK_WATCHER->at ($NEXT_TICK);
2537 $TICK_WATCHER->start; 2851 $TICK_WATCHER->start;
2852
2853 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
2854 $sig->send;
2855 }
2856 while (my $sig = shift @WAIT_FOR_TICK) {
2857 $sig->send;
2858 }
2859
2860 _post_tick;
2538 }, 2861 },
2539); 2862);
2540 2863
2541{ 2864{
2542 BDB::max_poll_time $TICK * 0.1; 2865 BDB::max_poll_time $TICK * 0.1;
2570 2893
2571 cf::sync_job { 2894 cf::sync_job {
2572 eval { 2895 eval {
2573 BDB::db_env_open 2896 BDB::db_env_open
2574 $DB_ENV, 2897 $DB_ENV,
2575 $BDB_ENV_DIR, 2898 $BDBDIR,
2576 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 2899 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
2577 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 2900 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
2578 0666; 2901 0666;
2579 2902
2580 cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; 2903 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
2581 2904
2582 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); 2905 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1);
2583 $DB_ENV->set_lk_detect; 2906 $DB_ENV->set_lk_detect;
2584 }; 2907 };
2585 2908
2601 prio => 6, 2924 prio => 6,
2602 cb => \&IO::AIO::poll_cb, 2925 cb => \&IO::AIO::poll_cb,
2603 ); 2926 );
2604} 2927}
2605 2928
2929my $_log_backtrace;
2930
2931sub _log_backtrace {
2932 my ($msg, @addr) = @_;
2933
2934 $msg =~ s/\n//;
2935
2936 # limit the # of concurrent backtraces
2937 if ($_log_backtrace < 2) {
2938 ++$_log_backtrace;
2939 async {
2940 my @bt = fork_call {
2941 @addr = map { sprintf "%x", $_ } @addr;
2942 my $self = (-f "/proc/$$/exe") ? "/proc/$$/exe" : $^X;
2943 open my $fh, "exec addr2line -C -f -i -e \Q$self\E @addr 2>&1 |"
2944 or die "addr2line: $!";
2945
2946 my @funcs;
2947 my @res = <$fh>;
2948 chomp for @res;
2949 while (@res) {
2950 my ($func, $line) = splice @res, 0, 2, ();
2951 push @funcs, "[$func] $line";
2952 }
2953
2954 @funcs
2955 };
2956
2957 LOG llevInfo, "[ABT] $msg\n";
2958 LOG llevInfo, "[ABT] $_\n" for @bt;
2959 --$_log_backtrace;
2960 };
2961 } else {
2962 LOG llevInfo, "[ABT] $msg\n";
2963 LOG llevInfo, "[ABT] [suppressed]\n";
2964 }
2965}
2966
2967# load additional modules
2968use cf::pod;
2969
2606END { cf::emergency_save } 2970END { cf::emergency_save }
2607 2971
26081 29721
2609 2973

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines