Uses CLOCK_MONOTONIC_RAW on Linux, this is unaffected by synchronisation
adjustments and is used by SDL_GetPerformanceCounter() (we'll use it
directly to support dedicated servers without SDL).
Removes gettimeofday() and usleep() which were marked obsolete and
removed in POSIX.1-2008 so the old comment // FIXME improve this check
was impossible to address. The gettimeofday() replacement is very well
tested now (sys_useclockgettime 1 is default even in div0-stable).
Removes host_maxwait as the problems it worked around were fixed.
For 231beb2c234914f942166eb152c44e55b5b01f86 I tested incorrectly, using
Task Manager inside the VM which reports low CPU load even when spinning.
DP's `status` command showed the correct result.
Changes the default path from select() to: usleep() on POSIX, Sleep() on
Windows.
bones_was_here [Thu, 7 Mar 2024 20:31:33 +0000 (06:31 +1000)]
sys: allow the platform to handle crashes after DP does
As suggested by divVerent in chat.
In SDL builds (including when using -dedicated on the cmdline) the OS
handler runs after the user clicks OK on the SDL dialog.
bones_was_here [Thu, 7 Mar 2024 18:32:06 +0000 (04:32 +1000)]
Make the Sys_Error() message more informative
Re: 24550510e6ff331c1749707df7fad608f618bdd3 as discussed in chat, there
was a good reason to change this word: it indicates DP deliberately
stopped execution, whereas "Error" is generic (most problems that can
happen on a computer are some form of error). Also "Engine Error"
makes it sound like there's a bug in DP, which isn't necessarily the
case: there are several scenarios where user error can trigger this.
This message was already different from Quake's "Quake Error".
Pragmatically speaking, absent any good reason otherwise, naming conventions for functions like Sys_Error should remain consistent with Quake for developer sanity.
bones_was_here [Sun, 11 Feb 2024 13:54:39 +0000 (23:54 +1000)]
PRVM: refactor error handling
Cleans up PRVM_Crash() calling and removes redundant code.
Directs menu errors to stderr instead of stdout.
Aborts if a loop is detected in MVM_error_cmd(), matching Host_Error().
Prevents QC's CSQC_Shutdown() being called when CSQC crashes.
Makes cvar prvm_errordump ignore MENUQC crashes.
Improves some messages and comments.
bones_was_here [Sun, 25 Feb 2024 22:27:35 +0000 (08:27 +1000)]
PRVM: don't break csqc when menuqc has a problem
Fixes a regression in 3d84db99ed9d8234809d237512f44cfa6d7ce1a0 caused by
moving this line such that it executes even for MVM errors. We don't
need it because it's done by Host_Error->CL_DisconnectEx->CL_VM_ShutDown
bones_was_here [Fri, 9 Feb 2024 08:12:34 +0000 (18:12 +1000)]
Fix changing of player name during a game
Name changes applied when connecting or when the map changed, but not
during the game.
I broke it in 74b49d6e5412af206448119963e0a4b3617e8ca4 by not
noticing the user-visible name of the cvar is used to determine which
command (if any) is sent.
bones_was_here [Fri, 9 Feb 2024 09:14:59 +0000 (19:14 +1000)]
Fix two memory allocation issues
In fa06dd40f48b20d738b6bd604758c81defd76cfd I misunderstood the
subdivision code, causing sentinel2 to be written past the end of the
buffer... somehow many maps and games worked fine despite this. This
commit reverts the bad line from that commit.
A very old (div0-stable) bug when loading Q1BSP meant we didn't allocate
enough texture memory when a "sky" texture name wasn't all lowercase,
causing a crash when loading custom map e2m9.
bones_was_here [Thu, 8 Feb 2024 20:21:05 +0000 (06:21 +1000)]
server: always use correct case for map names
Previously whatever was typed in as the map/changelevel argument was
used even if it didn't match the case of the filename in the pak, which
meant QC could see multiple instances of the same map that differed only
by case, causing problems for gameplay stats.
Now DP will always use the same case as the real .bsp filename for the
`mapname` globals, and the sv_world* and cl_world* cvars, and the server
status and getinfo/getstatus responses. This matches the behaviour of
QC code which retrieved the name from the VFS, such as the `gotomap` and
voting code in Nexuiz and Xonotic.
bones_was_here [Wed, 7 Feb 2024 16:21:51 +0000 (02:21 +1000)]
rcon: rewrite variable expansion support, related fixes
Fixes https://gitlab.com/xonotic/darkplaces/-/issues/407
In 48d83538ef13596d4569d46ae66467b63b945546 I broke the return of the
rcon command output to the client.
Fixes unterminated string warnings on the server when the
rcon_password feature of multiple space-delimited passwords is used.
Fixes possibility of the cbuf to use more memory than its intended
maximum.
In the client you can send the variable such that the server will
expand it (not the client) like this: rcon echo $$sv_worldmessage
bones_was_here [Wed, 7 Feb 2024 14:19:05 +0000 (00:19 +1000)]
SVVM: Fix segfault during shutdown, improve previous commit
In 30c47b1aff3ac28359c469a40c44ee700c80e814 a bug in SVVM_reset_cmd was
revealed because that code never ran before that commit:
sometimes the server segfaulted at end of match because SVVM_reset_cmd
called QC code even though SVVM_reset_cmd would only be called by
PRVM_Prog_Reset after it had freed the tempstring memory... so if QC
created a tempstring, crash.
This commit refactors the SV VM shutdown to be consistent with the CL
and MENU shutdown by moving the QC SV_Shutdown() call into a separate
func that (optionally) resets the VM afterwards. As per 30c47b1aff3ac28359c469a40c44ee700c80e814 we MUST reset when stopping a
server to avoid crashes later, but we can still skip it when changing
map (like in previous versions).
bones_was_here [Tue, 6 Feb 2024 11:22:06 +0000 (21:22 +1000)]
SVVM: fully shut down when stopping a server to avoid segfaulting later
Fixes segfaults when functions that check if the progs is loaded (to
avoid crashing) are called after stopping a server.
Example repro: load a map, disconnect, `set sv_cheats 0`.
Fixes wrong SV_Shutdown order: SV_DropClient must be done before
shutting down SVQC because it calls SVQC functions.
bones_was_here [Mon, 5 Feb 2024 22:59:24 +0000 (08:59 +1000)]
fs: save files in the correct place when gamedir(s) are in use
As per the description of the `gamedir` command, it's expected that a
gamedir can be added without needing to re-add the primary
(gamedirname1) or secondary/mod (gamedirname2) as the last gamedir just
to ensure that files still get saved there as usual.
bones_was_here [Mon, 5 Feb 2024 20:38:45 +0000 (06:38 +1000)]
sv_cheats: fix two segfaults when setting it to 0
Assuming the SVQC program is always running crashes when it's not.
Testing if the edict pointer is NULL isn't a valid way to stop at the
end of the client slots, which caused this to crash when Xonotic SVQC
was running.
bones_was_here [Sun, 4 Feb 2024 16:18:03 +0000 (02:18 +1000)]
cvar: fix many bugs in gamedir, loadconfig, unset, cvar_resettodefaults* commands
Fixes a segfault when Cvar_RestoreInitState() deleted cvars (null ptr deref).
Fixes an infinite loop in Host_LoadConfig_f() due to calling Cbuf_Execute()
from inside Cbuf_Execute() via Host_AddConfigText().
Fixes `unset` being able to delete autocvars while they're in use by QC.
Fixes Cvar_RestoreInitState() not updating autocvars when necessary.
Fixes Cvar_RestoreInitState() not calling cvar callbacks.
Fixes Cvar_RestoreInitState() sometimes restoring such that .string
doesn't match .value, or such that .defstring is wrong. This was caused
by Cvar_SaveInitState() saving only string pointers and not strings.
Fixes CF_READONLY cvars like gl_info_* getting restored/reset. It
doesn't make sense for any of these to be changed by code outside their
own subsystems.
Fixes Xonotic menu QC errors caused by Host_LoadConfig_f deleting cvars
before calling QC's m_shutdown().
Saves a little memory by storing only minimal init state data for
Cvar_RestoreInitState(), and by relaxing alignment requirements in
_Mem_strdup().
Improves some warns and docs and tidies up a little.
Removes `vid_restart` from `gamedir`, it usually isn't needed because
current DP applies many vid_ cvar changes without it, and because of
the ubiquity of flat panel displays.
bones_was_here [Tue, 30 Jan 2024 11:38:52 +0000 (21:38 +1000)]
Work around Microsoft's broken vsnprintf implementation
Microsoft's documentation says it has supported %zu (C99 standard format
for size_t) for some years but trying to use it causes crashes at least
on older versions of Windows.
Makes dpsnprintf and dpvsnprintf documentation more accessible (these
funcs have different return behaviour than the C standard ones).
bones_was_here [Fri, 26 Jan 2024 06:58:50 +0000 (16:58 +1000)]
MD3: fix misaligned memory access
The memory subdivision was ordered such that this format likely had no
issues on a 32-bit machine but on 64-bit the alignment requirement of
texture_t increases to 8 bytes, invoking undefined behaviour.
bones_was_here [Thu, 25 Jan 2024 06:25:48 +0000 (16:25 +1000)]
Update and micro-optimise memory allocation
The max alignment requirement no longer needs to be hard-coded.
We were allocating slightly more than necessary for alignment padding.
Makes a (currently unused) macro more useful and robust.
Fixes bitfield data type.
bones_was_here [Mon, 18 Dec 2023 09:24:15 +0000 (19:24 +1000)]
Fix uses of strlcpy on unterminated source strings
BSD strlcpy() keeps reading until a \0 or a segfault, even after it
stops writing. In these cases the source was part of a longer string so
it wouldn't have crashed.
These were caught by the new warning.
The +1 on the source sizes copied an extra char that would be changed to
\0 and is now redundant because the real dest and src sizes are always
passed to a purpose-written func.
bones_was_here [Wed, 13 Dec 2023 07:16:24 +0000 (17:16 +1000)]
com: replace BSD strlcpy and strlcat
Compared to BSD strlcpy and strlcat, dp_strlcpy and dp_strlcat are
faster, never crash, and have a more useful return (DP didn't use the
strlcpy return at all), see included docs.
Adds dp_stpecpy() for efficient chain copying and dp_ustr2stp() for
copying measured byte sequences (unterminated strings) to strings.
Replaces the only use of the strlcat() return with dp_stpecpy().
bones_was_here [Fri, 15 Dec 2023 10:16:15 +0000 (20:16 +1000)]
com: rename BSD strlcpy and strlcat
The C standard reserves str* and mem* for the implementation.
Checking for BSD or OS X is not a robust way to determine if libc
provides these (eg glibc may implement them).
bones_was_here [Wed, 3 Jan 2024 16:58:15 +0000 (02:58 +1000)]
sys: make stdout/stderr configurable during runtime
Adds cvar sys_stdout which offers the same behaviours as the -nostdout
and -stderr cmdline options and is initialised by them if they're passed.
Makes -noterminal cmdline option a synonym for -nostdout, previously it
was effectively the same but less efficient.
Platforms with the fcntl() syscall (ie non-Windows):
Changes to non-blocking stdout/stderr by default to prevent stutters and
halts in certain scenarios.
Adds cvar sys_stdout_blocks for enabling blocking stdout/stderr if
required.
Retrieves stdio stream FDs instead of assuming they're always 0,1,2.