Commit graph

110 commits

Author SHA1 Message Date
20b31defe7 Generation numbers to catch write back of stale copies
Oops when a stale copy is written back, i.e. the processor was yielded
since the copy was made.  Such bugs are difficult to spot.  Sequence
numbers catch them when they do actual harm (they also catch different
bugs).  Generation numbers catch them even when they don't.

New ef_generation to count generations.  Call new ef_make_stale() to
step it whenever the processor may be yielded.

New struct emptypedstr member generation.  Make sure all members of
unit empobj_storage share it.  It is only used in copies; its value on
disk and in the cache is meaningless.  Set it to ef_generation by
calling new ef_mark_fresh() when making copies in ef_read() and
ef_blank().  Do the same in obj_changed() to make check_sect_ok() &
friends freshen their argument when it is unchanged.  Copies with
generation other than ef_generation are stale.

Oops in ef_write() when a stale copy is written back.
2009-03-10 15:27:52 +01:00
35ef345ecb Update copyright notice 2009-02-08 09:33:18 +01:00
d702068457 Fix trailing whitespace 2008-09-17 21:31:40 -04:00
f21cb48f69 New struct empfile callback onresize 2008-09-08 21:30:37 -04:00
a1f20efd02 Revive struct empfile callback init as oninit
Commit a71f0158 removed unused callback init.  Bring it back renamed
to oninit, and without the redundant first argument.
2008-09-08 21:30:37 -04:00
c5482e4bfb Pass old element to empfile callback prewrite()
Change sct_prewrite(), shp_prewrite(), pln_prewrite(), lnd_prewrite(),
nuk_prewrite() accordingly.  New argument isn't used for anything,
yet.
2008-09-08 21:26:40 -04:00
afcde86ca3 Make ef_close() clear baseid, cids and fids 2008-09-03 21:17:46 -04:00
62076fbed7 Move view open/close into src/lib/common/file.c
Really belongs there, because it manipulates empfile[].

New ef_open_view() to replace ef_init_view().  Make ef_close() cope
with views, and remove ef_fina_view().  Make ef_extend() and
ef_truncate() oops on views.
2008-09-03 21:17:46 -04:00
50cfdcb5a7 Check argument of ef_cadef(), ef_nelem(), ef_flags(), ef_mtime()
This removes any need for calling ef_check() outside of file.c.
Remove its only occurence, from symval(), and give it internal
linkage.
2008-09-03 20:43:13 -04:00
1492845c12 Clean up maintenance of config table sentinels
Xundump had special hackery to maintain configuration tables'
sentinels: xubody() and getobj() added a sentinel element when
initializing or growing a table, which xubody() stripped off again
before returning.  The latter was an unclean hack.

Replace this by building knowledge of sentinels into struct empfile:
new flag EFF_SENTINEL, set for the appropriate members of empfile[],
obeyed by ef_extend() and ef_truncate().
2008-09-03 20:43:13 -04:00
e438f6b4cd Fix/improve logging in ef_close(), ef_extend(), ef_truncate()
Change ef_close() to log ep->file instead of ep->name, to match
ef_open().

Fix ef_extend() to log ep->name instead of ep->file, which could be
null.  Also fix ef_ensure_space()'s function comment.  Both broken in
commit 2eb8672b.

ef_truncate()'s error logging lacked detail when ef_realloc_cache()
failed, fix.
2008-09-03 20:43:13 -04:00
536ef0b0a2 Add sequence numbers to game state
This oopses on output dependency violations, e.g. two threads doing a
read-modify-write without synchronization, or the one thread nesting
several read-modify-writes.  Such bugs are difficult to spot, and tend
to be abusable.  I figure we have quite a few of them.

New struct emptypedstr member seqno.  Make sure all members of unit
empobj_storage share it.  Initialize it in files: main() and
file_sct_init().  Set it in ef_blank() and new ef_set_uid() by calling
new get_seqno().  Use ef_set_uid() when copying objects: swaps(),
doland(), doship(), doplane(), dounit(), delete_old_news().  Step it
in ef_write() by calling new new_seqno().

Factor do_read() out of fillcache() to make it available for
get_seqno().
2008-05-17 22:50:30 +02:00
967122ae0b Fix files utility not to set timestamps
Commit f33b96b1 (v4.3.12) made files again set timestamps.  That was
intentionally suppressed in commit 990eb46b (v4.3.10), because it
facilitates attacks against the PRNG.  Commit 8f98e53a (v4.3.0) had
added it as a feature.

Fix by making files's main() pass new flag EFF_NOTIME to ef_open().
Implement the flag in do_write().
2008-05-17 19:35:22 +02:00
2a125c5463 Simplify empfile timestamping
do_write() sets the timestamp from a parameter.  All callers pass
time(), and don't use that value themselves.  Call time() in do_write
and remove the parameter.
2008-05-17 19:01:59 +02:00
d39a1f11cc Update timestamps in privately mapped tables, too
Commit f33b96b1 made ef_flush(), ef_write() and ef_extend() update
timestamps automatically.  Change ef_write() and ef_extend() to do
that even when table is privately mapped, by making do_write() cope
with privately mapped tables.  Current users don't care, but it's a
saner interface.
2008-05-17 18:56:38 +02:00
c3d5771054 Fix ef_write() for privately mapped non-file-backed table
Commit f8dcb7b0 promised that combination to work, but got it wrong.
It hasn't been used, so no real harm done.
2008-05-17 18:48:48 +02:00
5750107b65 Make server check game state file sizes on startup
Certain tables have a fixed size depending on configuration: EF_SECTOR
has WORLD_SZ() elements, EF_NATION, EF_MAP and EF_BMAP have MAXNOC
elements, and EF_REALM has MAXNOC * MAXNOR elements.  Bad things
happen if the files backing them are shorter.

Pass expected size to ef_open(), and make it fail when the actual size
differs.
2008-05-17 17:00:57 +02:00
570ce80edf Fix fillcache() and do_write() error handling
Fix test for interrupted I/O.  Catch read hitting EOF unexpectedly.
Log errors in more detail.
2008-03-21 09:13:04 +01:00
0dd6702df1 Update known contributors comments 2008-03-14 20:25:44 +01:00
f33b96b1d1 Set timestamp automatically on write
Do it in do_write().  Remove the setting of timestamps elsewhere.

This fixes empdump to set timestamps to the current time instead of
zero on import.
2008-03-14 20:25:43 +01:00
cf16c74a3d New ef_truncate() 2008-03-14 20:25:10 +01:00
99594c1898 Oops when bad ef_type or uid is written to disk 2008-03-14 20:25:08 +01:00
c21c4ff9a7 New ef_blank()
It performs the same initialization as ef_extend().
2008-03-05 22:48:24 +01:00
a71f01585f empfile's init callback is now unused, remove 2008-03-05 22:48:24 +01:00
6cd3d55c4e Initialize ef_type and uid automatically in ef_extend()
New struct emptypedstr to avoid depending on empobj.h there.

Remove now superfluous manual initializations elsewhere.

This doesn't fix any missing initializations.
2008-03-05 22:48:23 +01:00
2eb8672b5e Generalize ef_extend() to non-file-backed tables
ef_extend() extended the file bypassing the cache, which screws up the
cache if it's EFF_MEM.  It fixed that by closing and reopening the
table.  Cheesy, and worked only for file-backed tables.

Rewrite ef_extend() to remap the cache properly for EFF_MEM.  While
there, simplify the !EFF_MEM case: steal a cache slot instead of
allocating a buffer.

Factor cache mapping out of ef_open() and ef_extend() into new
ef_realloc_cache().
2008-02-26 21:07:55 +01:00
f8dcb7b07b Replace EFF_RDONLY by EFF_PRIVATE
Read-only was a bit of a misnomer: you could write to the table by
obtaining a pointer into it from ef_ptr(), you just couldn't write to
the backing file.

Semantic changes:

* ef_flush() is now allowed when the table is file-backed or privately
  mapped.  Before, it had to be file-backed.  Flushing a privately
  mapped table does nothing, just like flushing a read-only table did.

* ef_write() is now allowed when the table is file-backed or fully
  cached.  Before, it had to be file-backed and not read-only.
  Writing to a privately mapped file-backed table doesn't write to the
  file.

* ef_extend() is not implemented for privately mapped tables, just
  like it wasn't implemented for read-only tables.
2008-02-26 20:40:59 +01:00
ef71897051 Remove obsolete comment
Obsolete since ef_open() locks the file (commit de124108).
2008-02-26 20:40:59 +01:00
55ff4f8e3a Clean up initialization of empfile[]
Split ef_init() into two functions: empfile_init() for initialization,
and empfile_fixup() to fix it up for configuration.  Put them next to
empfile[].  Move the call to empfile_init() from behind emp_config()
to before it.
2008-02-10 11:40:57 +01:00
db02dda32f Update copyright notice 2008-01-19 10:15:37 +01:00
404095d2fa Trim system includes. 2007-08-18 17:03:14 +00:00
Ron Koenderink
4bbc3f4286 Simple POSIX I/O emulation layer to work around Windows's defective
Unix I/O:
[_WIN32] (socklen_t, accept, posix_accept, bind. posix_bind, listen)
(posix_listen, setsockopt, posix_setsockopt, shutdown, posix_shutdown)
(socket, posix_socket, close, posix_close, creat, fstat, posix_fstat)
(lseek, posix_lseek, open, posix_open, read, posix_read, write)
(posix_write, fileno, posix_fileno, fcntl, O_NONBLOCK, F_RDLCK)
(F_WRLCK, F_GETFL, F_SETFL, F_SETLK, EWOULDBLOCK, ENOTSOCK)
(flock, fsync, posix_fsync):
New.
(ef_open, io_close, io_input, io_output, io_shutdown, io_noblocking)
(player_accept): Use them to simplify.
[_WIN32] (posix_fd2socket): New.
(empth_select): Use it.
(gen_power): Use it.
2007-08-14 03:33:28 +00:00
e1926d9ce9 (ef_open): Leaked file descriptor when fcntl() failed. 2007-07-31 05:03:25 +00:00
56de7a2151 (WORLD_SZ): New.
(isok, ef_init, bmaps_intersect, bp_init, player_accept)
(finish_sects, main): Use it.

(bp_neighbors, bp_lbcost, pathcost): Use XYOFFSET().  No
functional change.
2007-07-28 12:12:17 +00:00
Ron Koenderink
c97d79c0ee (add, disa, ef_open, logerror, main): Switch permissions to
standard defines instead of magic numbers to improve portability.
Also define WIN32 equivalents to the standard defines.
2007-03-10 16:29:51 +00:00
Ron Koenderink
7356da4775 (add.c, disa.c, log.c, new.c, wantupd.c, wu.c) [_WIN32,__GNUC__]:
Remove the !defined(__GNUC__) for <io.h>.
Not required for VC8.
2007-02-23 22:35:50 +00:00
7db3523f62 Update known contributors comment. 2007-01-13 10:16:43 +00:00
63bdc89835 Update copyright notice. 2007-01-09 19:09:31 +00:00
Ron Koenderink
ce63991c9f (ef_open) [_WIN32]: Fix to get a read lock instead of a write lock for
EFF_RDONLY and to get a write lock instead of a total file lock for
file opened for writing.
2006-11-29 12:16:43 +00:00
Ron Koenderink
2742a2db62 (ef_open) [_WIN32]: Add shared.h for MinGW environment,
needed for WIN32 file locking. Broken (missed) in rev 1.70.
2006-08-08 22:43:18 +00:00
Ron Koenderink
a89e21037e (ef_open) [_WIN32]: Provide file locking for WIN32. 2006-08-07 17:15:04 +00:00
de124108de (ef_open): Lock the file. This prevents two programs (servers in
particular) running on the same game state.  fairland does not use
ef_open() at this time, and thus ignores the lock.
2006-08-02 18:05:04 +00:00
Ron Koenderink
d413884a37 [_WIN32, __GNUC__]: Reorganize the include files for WIN32 to
separate the MinGW environment from the MSVC environment and
WIN32 environment.  Reorganize the order of the includes so
the system files are always loaded first to prevent with misc.h defines.
Remove system file includes from misc.h.
2006-07-13 21:14:16 +00:00
Ron Koenderink
e55198f2c3 [_WIN32]: Add missing include sys/types.h to get off_t. 2006-07-11 03:16:02 +00:00
e42053d928 Break inclusion cycle: prototypes.h and commands.h included each
other.  Ensure headers in include/ can be included in any order
(except for econfig-spec.h, which is special).  New header types.h to
help avoid inclusion cycles.  Sort include directives.  Remove some
superflous includes.
2006-07-10 06:37:23 +00:00
211a56489b (ef_open): Zero cache size (empty EFF_MEM file) can lead to null cache
on some systems, which confuses assertions.  Force non-zero size.
2006-03-06 18:23:41 +00:00
6bd0eabb21 (ef_init): Remove superflous size initialization for tables that are
initialized from builtin config files.
(ef_init_chr): Unused, remove.
2006-02-19 17:07:04 +00:00
4ff857507e Doc fix. 2006-02-12 23:01:47 +00:00
4515b84c59 COPYING duplicates information from README. Remove. Move GPL from
LICENSE to COPYING, because that's where it usually is.  Update all
the references to these files.
2006-01-21 19:48:41 +00:00
Ron Koenderink
04a8b84592 (ship_bomb, deli, do_demo, fuel, grin, look_ship)
(ltend, multifire, quite_bigdef, mine, landmine)
(do_loan, prod, printdiff, sell, sona, stre)
(tend, fire_dchrg, vers, work, ac_planedamage)
(ac_shipflak, ask_off, get_mine_dsupport, att_fight)
(ask_move_in_off, detonate, sd, land_gun)
(land_unitgun, lnd_fort_interdiction, lnd_fortify)
(perform_mission, pln_mine, pln_mobcost)
(retreat_ship1, retreat_land1, shp_sweep)
(shp_fort_interdiction, shp_missle_defense)
(new_work, growfood, upd_land, land_repair)
(get_materials, do_mob_ship, do_mob_land)
(load_it, unload_it, prod_plane, produce)
(guerrilla, upd_buildeff, spread_fallout)
(upd_ship, ship_repair, min, dmin, MIN):
Remove min() and dmin() functions and replace
with a MIN macro in misc.h.  Remove local MIN
macros and use the new one in misc.h.  This
change removes the need for the special
case for _WIN32.

(fuel, look_ship, multifire, mission, sona)
(plane_sona, ef_open, player_accept, player_main)
(ac_dog, att_get_combat, calc_mobcost)
(ask_move_in_off, intelligence_report)
(build_mission_list_type, perform_mission)
(show_mission, use_supply, dodistribute)
(allocate_memory, max, dmax, MAX):
Remove max() and dmax() functions and replace
with a MAX macro in misc.h.  Remove local MAX
macros and use the new one in misc.h.  This
change removes the need for the special
case for _WIN32.
2006-01-12 14:23:55 +00:00