Andre Noll [Thu, 17 Nov 2011 09:27:30 +0000 (10:27 +0100)]
Compilation fix for FreeBSD.
Commit 25ca796b (daemon: Introduce parent_waits flag for
daemonize().) broke the compilation on FreeBSD due to signal.h not
being included. This patch adds the missing include.
Andre Noll [Sat, 12 Nov 2011 13:37:01 +0000 (14:37 +0100)]
daemon: Introduce parent_waits flag for daemonize().
In daemon mode, para_server should not detach from the console
until it is listening on its command socket. The previous approach
turned out to be buggy and has been reverted in the previous commit.
This second attempt tries to get it right. It adds a boolean parameter
"parent_waits" to daemonize(). After daemonize() has forked, the
parent process does not exit immediately if parent_waits is true but
waits until the child process sends SIGTERM to its parent, or exits.
para_server makes use of the new flag in server_init(). The daemon
process (child) sends SIGTERM to its parent after the command socket
has been initialized. para_audiod, on the other hand, does not need
this feature, so it calls daemonize() with parent_waits == false to
get the old behaviour.
Andre Noll [Fri, 11 Nov 2011 20:50:56 +0000 (21:50 +0100)]
Revert "server: Listen on command socket before daemonizing."
This reverts commit 7a4b6d5f19976ceac581f6dc235fcbd9b30c767f, which
was a rather bad idea because it caused the afs process to inherit
the open file descriptor of the command socket, so two processes
listened on the command socket at the same time.
Even worse, the afs process also inherited the command task in its
scheduler setup, causing it to answer incoming connections as well.
In fact it was more or less random which of the two processes served
an incoming connection.
If it was the afs process, interesting things happened in the command
handler due to its address space being a copy of the afs process,
where parts of what is needed for some commands not properly set
up. For example the si and the sender commands segfaulted due to a
NULL pointer dereference.
Andre Noll [Thu, 10 Nov 2011 08:22:25 +0000 (09:22 +0100)]
vss: Avoid read-overflowing the header buffer for ogg streams.
valgrind complains because of invalid reads/writes in vss.c:
==998== Invalid write of size 1
==998== at 0x8050B09: vss_post_select (vss.c:574)
==998== by 0x806106C: schedule (sched.c:71)
==998== by 0x804EE04: main (server.c:579)
==998== Address 0x46d99bc is 0 bytes after a block of size 548 alloc'd
==998== at 0x4028A3B: realloc (vg_replace_malloc.c:632)
==998== by 0x805356B: para_realloc (string.c:40)
==998== by 0x80506EC: vss_post_select (vss.c:331)
==998== by 0x806106C: schedule (sched.c:71)
==998== by 0x804EE04: main (server.c:579)
==998==
...
==5543== Invalid read of size 1
==5543== at 0x8050EBD: vss_post_select (vss.c:1099)
==5543== by 0x806108E: schedule (sched.c:71)
==5543== by 0x804EE04: main (server.c:579)
==5543== Address 0x47c70ac is 0 bytes after a block of size 3,956 alloc'd
==5543== at 0x4028A3B: realloc (vg_replace_malloc.c:632)
==5543== by 0x805358D: para_realloc (string.c:40)
==5543== by 0x80642AA: add_ogg_page (ogg_afh.c:78)
==5543== by 0x8064458: vorbis_get_header_callback (ogg_afh.c:132)
==5543== by 0x8063EF1: process_ogg_packets (ogg_afh_common.c:48)
==5543== by 0x8063F9A: ogg_get_file_info (ogg_afh_common.c:144)
==5543== by 0x8064200: vorbis_get_header (ogg_afh.c:149)
==5543== by 0x804FDD9: recv_afs_result (vss.c:1006)
==5543== by 0x80503F4: vss_post_select (vss.c:1124)
==5543== by 0x806108E: schedule (sched.c:71)
==5543== by 0x804EE04: main (server.c:579)
The problem is that for ogg streams chunk 0 points to a buffer on
the heap rather than to the mapped audio file, but we are checking
the buffer bounds against the memory map.
The fix consists of two parts. (a) We now treat a FEC group special
if it starts at chunk zero: Such a group now contains only this single
chunk. (b) When setting up the FEC group we always compare the buffer
bounds against the start of the first buffer in the group rather than
the memory map.
Andre Noll [Thu, 10 Nov 2011 09:14:21 +0000 (10:14 +0100)]
vss: Don't prefault header.
Commit 7bba6232 (vss: Mmap audio files using MAP_POPULATE.) introduced
read-ahead for chunks of the mmapped audio file. However, it missed
the fact that for ogg streams chunk 0 is created on the fly and stored
in a dynamically allocated buffer. Read-ahead on this buffer is likely
to access memory not owned by the process and might lead to a segfault.
Fix this bug by not performing read-ahead for chunk zero.
Andre Noll [Tue, 1 Nov 2011 19:05:07 +0000 (20:05 +0100)]
com_ls: Print an error message if no file matched.
In case at least one pattern was given and no matches were found,
the ls command returned with no output at all. This makes it print a
"no matches" error message instead.
Andre Noll [Mon, 26 Sep 2011 20:51:48 +0000 (22:51 +0200)]
catblob: Fix "no matches" message.
The catblob commands are supposed to print this message only if none
of the given patterns matches any blob in the database. However, in
case all of the matching blobs are empty, we do print the message.
The problem is that the match count is not being increased due
to cat_blob() returning negative for empty blobs. This count is
computed in action_if_pattern_matches() which calls cat_blob() as
its action handler which in turn calls osl_open_disk_object(). But
this function returns -E_OSL_EMPTY for empty blobs, and cat_blob()
just passes through this value.
This patch changes cat_blob() so that it explicitly checks for
-E_OSL_EMPTY and returns zero in this case so that the match counter
will be increased.
Andre Noll [Wed, 6 Jul 2011 20:30:59 +0000 (22:30 +0200)]
afs: Provide generic counter for matching rows.
Several afs commands implemented their own counters, often only to
find out whether they should print an error message because no rows
matched the given pattern(s).
This patch makes counting matching rows generic by increasing the new
pattern_match_data->num_matches counter in action_if_pattern_matches()
so that all commands which use this facility for iterating over all
audio files can evaluate the counter afterwards. This allows to remove
several per-command counters.
Andre Noll [Wed, 2 Nov 2011 16:25:02 +0000 (17:25 +0100)]
web: Rework text on download page.
The git link to kernel.org was defunct and is probably not the best
pointer anyway. These days everybody knows git, so let's simply not
link to any git page.
This patch also reorders the three options for downloading (clone repo,
download snaphot, download release) so that git comes first.
Andre Noll [Tue, 20 Sep 2011 22:55:05 +0000 (00:55 +0200)]
http_recv: Fill both buffer pool buffers simultaneously.
This changes http_recv_post_select() to call readv_nonblock() rather
than read_nonblock() to read data from the socket, just like the udp
and dccp receivers do.
This saves one iteration of the scheduler loop if the end of the
buffer pool area is reached.
Andre Noll [Tue, 20 Sep 2011 22:36:08 +0000 (00:36 +0200)]
recv: Make ->btrp and ->fd generic.
All three receivers maintain a file descriptor and a buffer tree
pool per connection. Currently these are part of the receiver node's
private_data structure, which is a needless code duplication.
This patch moves both fields to the generic struct receiver_node. Since
the private_data structure of the udp and dccp receivers contained
no other fields, this allows to get rid of it completely for these
two receivers.
Andre Noll [Fri, 29 Jul 2011 21:46:28 +0000 (23:46 +0200)]
alsa: Make two variables local to alsa_init().
The two members buffer_frames and buffer_time of struct
private_alsa_data are only needed in alsa_init(), so make them
local. Rename buffer_frames to buffer_size since this is the value
returned by snd_pcm_hw_params_get_buffer_size().
Andre Noll [Tue, 26 Jul 2011 20:47:20 +0000 (22:47 +0200)]
alsa: Dump the configuration on startup.
Currently we log selected alsa parameters, but it is sometimes
useful to see the full configuration. This patch adds a call to
snd_pcm_dump() to alsa_init() which obtains the alsa hardware and
software configuration. It is printed for loglevels info and debug.
Andre Noll [Sat, 8 Oct 2011 17:16:14 +0000 (19:16 +0200)]
alsa: Avoid busy loop with dmix.
When using alsa's direct mixing plugin, the alsa poll fd might be ready
for IO even though nothing can be written to the alsa handle. This
leads to a busy loop in para_audiod and para_write and makes these
applications eat up considerable amounts of CPU time during playback.
This patch fixes the problem by reading from the alsa fd after a
failed write. It also adjusts the test for when to return early from
alsa_post_select(). The old code was not wrong, however, since we
should never reach the test if poll_fd is negative.
Andre Noll [Fri, 16 Sep 2011 20:16:45 +0000 (22:16 +0200)]
Makefile: Drop dependencies on *_command_list.[ch].
It is enough to let para_audiod and para_server depend on their object
files only as these object files in turn depend on _command_list.[ch],
so Mr Transitivity does the work for us.
Andre Noll [Wed, 12 Oct 2011 16:13:40 +0000 (18:13 +0200)]
server: Kill temporary SIGCHLD handler.
During startup, para_server sets up a temporary signal handler for
SIGCHLD in order to notice if the afs child process terminates
immediately (doing the signal setup after the fork would be too
late). This patch makes it block this signal during this period
instead. This works equally well and it fixes a weird problem on Mac
OS where the temporary SIGCHLD handler was executed although it had
been replaced by the generic signal handler of signal.c.
Andre Noll [Tue, 20 Sep 2011 10:13:16 +0000 (12:13 +0200)]
server: Listen on command socket before daemonizing.
This makes sure that para_server is already listening on its TCP
command socket before it detaches from the console. This is desirable
because otherwise commands like
para_server -d -L /dev/null && para_client help
may fail due to para_client connecting before para_server starts to
listen.
Andre Noll [Sun, 11 Sep 2011 01:02:41 +0000 (03:02 +0200)]
testsuite: Don't write ANSI escapes to test output files.
The say() function adds ANSI escape sequences for colors to the output
if stdout is a tty. However, we don't want colors for output written
to a file, so don't use say() in this case.
Andre Noll [Thu, 13 Oct 2011 14:10:11 +0000 (16:10 +0200)]
para_filter: Decode also small ogg vorbis files properly.
ogg_post_select() returns after initializing the decoder without
decoding anything, which is usually OK. However, if the file is smaller
than half of the size of the stdin buffer tree area, the whole file
is going to be fed to the decoder during the first iteration of the
scheduler loop. Currently this results in an empty output file.
The problem with small files is that the post select function of the
stdin buffer tree node encounters EOF on the next read and returns
EOF. This causes oggdec_post_select() to abort without producing any
output since its parent (stdin) is gone and there is no input pending.
This patch fixes the problem by setting the "have_more" flag after
ogg_init() returned successfully. oggdec_post_select() checks this
flag and only returns negative if it is unset.
Andre Noll [Sun, 11 Sep 2011 02:21:40 +0000 (04:21 +0200)]
afs: Write 'afs' to argv[0].
This allows to distinguish the afs process from the main server
process by looking at the ps output. Although there are less hacky
ways to achieve this (prctl(PR_SET_NAME, ...), setproctitle()), the
argv approach has the advantage of working on all supported operating
systems except FreeBSD. We have been doing the argv mangling for a
long time now in the command handlers and it has never caused any
problems, so it should be safe.
Andre Noll [Sat, 1 Oct 2011 21:35:18 +0000 (23:35 +0200)]
doc: Minor doxygen fixes.
doxygen's \cond and \endcond commands treat the word after the
command as the name of the section which should be excluded from the
documentatation. So it is wrong to put an arbitrary comment there.
This fixes a couple of doxygen warnings about unknown loglevel defines
and makes these defines actually show up in the generated HTML.
Andre Noll [Mon, 19 Sep 2011 17:12:40 +0000 (19:12 +0200)]
Fix UNIX_PATH_MAX on NetBSD.
UNIX_PATH_MAX was hardcoded to 108 for all systems. However, at
least on 32 bit NetBSD the size of the sun_path buffer is only
104 bytes. This resulted in a very nasty bug caused by a memset()
overwriting 4 bytes on the stack.
Fix this by calculating the correct value at compile time.
Andre Noll [Mon, 26 Sep 2011 12:34:29 +0000 (14:34 +0200)]
openssl RC4: Fix another invalid read.
Commit 11993981 fixed this bug for the RC4 sending function
but missed to change sc_recv_bin_buffer() accordingly.
This patch avoids the following invalid read detected by valgrind:
==23299== Invalid read of size 8
==23299== at 0x510A020: RC4 (in /lib/libcrypto.so.0.9.8)
==23299== by 0x408771: sc_recv_bin_buffer (crypt.c:313)
==23299== by 0x40837F: sc_recv_buffer (crypt_common.c:331)
==23299== by 0x4073C2: T.41 (client_common.c:139)
==23299== by 0x407758: client_post_select (client_common.c:268)
==23299== by 0x406A34: schedule (sched.c:71)
==23299== by 0x405625: main (client.c:572)
==23299== Address 0x613ccb8 is 3,992 bytes inside a block of size 3,999 alloc'd
==23299== at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==23299== by 0x405D9F: para_malloc (string.c:65)
==23299== by 0x408741: sc_recv_bin_buffer (crypt.c:309)
==23299== by 0x40837F: sc_recv_buffer (crypt_common.c:331)
==23299== by 0x4073C2: T.41 (client_common.c:139)
==23299== by 0x407758: client_post_select (client_common.c:268)
==23299== by 0x406A34: schedule (sched.c:71)
==23299== by 0x405625: main (client.c:572)
Andre Noll [Mon, 26 Sep 2011 06:27:18 +0000 (08:27 +0200)]
vss: Mmap audio files using MAP_POPULATE.
This fixes buffer underruns on an old laptop containing a
slow IDE disk. The problem was that getting the next chunk
from the map sometimes hit the disk and took more than 300ms.
This patch adds MAP_POPULATE to the flags for mmap() to
turn on read-ahead for the mapping. This almost fixed the
problem, but some buffer underruns remained. Moreover,
MAP_POPULATE is only available on Linux. To fix also the
remaining cases, we now read one byte from each of the next
few pages in the map after a chunk has been sent. This way
the next chunk should already be cached when it is needed.
Andre Noll [Mon, 15 Aug 2011 18:20:02 +0000 (20:20 +0200)]
command: Remove two dead stores.
command.c:771:2: warning: Value stored to 'ret' is never read
ret = -E_BAD_USER;
^ ~~~~~~~~~~~
command.c:765:2: warning: Value stored to 'numbytes' is never read
numbytes = ret;
^ ~~~
Andre Noll [Mon, 15 Aug 2011 18:16:38 +0000 (20:16 +0200)]
aac_afh: Don't compute the number of milliseconds twice.
aac_afh.c:194:16: warning: Value stored to 'ms' during its initialization is never read
long unsigned ms = 1000.0 * afhi->chunks_total * tmp
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Andre Noll [Mon, 15 Aug 2011 18:05:11 +0000 (20:05 +0200)]
crypt: Streamline get_asymmetric_key().
The clang analyzer says
crypt.c:202:9: warning: Function call argument is an uninitialized value
ret2 = para_munmap(map, map_size);
^ ~~~~~~~~
And right it is. This is not a bug though as map_size is only undefined
if map is NULL and in this case para_munmap does not look at its second
argument. However, this is rather subtle, so introduce a new label out_unmap
and jump there only if we really must unmap the file.
Andre Noll [Mon, 15 Aug 2011 17:42:15 +0000 (19:42 +0200)]
wmadec: Fix two dead stores.
wmadec_filter.c:772:3: warning: Value stored to 'mult1' is never read
mult1 = mult;
^ ~~~~
wmadec_filter.c:1015:3: warning: Value stored to 'n' is never read
n = pwd->block_len;
^ ~~~~~~~~~~~~~~
Also convert a statement of the form x = x * y to x *= y;
Andre Noll [Thu, 15 Sep 2011 12:33:14 +0000 (14:33 +0200)]
dccp_recv: Avoid NULL pointer dereference.
dccp_recv.c:48:16: warning: Access to field 'btrp' results in a dereference of a null pointer (loaded from variable 'pdd')
btr_pool_free(pdd->btrp);
^~~
In dccp_recv_close(), if pdd is NULL, we avoid closing pdd->fd but
dereference pdd nevertheless one line later. Fix this by returning
early if pdd is NULL.
Andre Noll [Tue, 20 Sep 2011 20:27:18 +0000 (22:27 +0200)]
recv: Improve documentation of generic_recv_pre_select().
Make it clear that this function returns the state of the given buffer
tree node, i.e. > 0 on success, == 0 if there is nothing to do and <
0 on errors.
Andre Noll [Wed, 10 Aug 2011 22:01:11 +0000 (00:01 +0200)]
Overhaul the daemon uptime functions.
The public interface of daemon.c offers some helpers for maintaining
and printing the uptime of the service. This interface consists of
the "uptime" enumeration and the public functions server_uptime()
and uptime_str(). The former function takes an uptime enum which is
either 'UPTIME_GET' or 'UPTIME_SET'.
It is cleaner to avoid the enumeration and have two separate
functions for getting and setting the uptime, so this patch
replaces server_uptime() by two new functions get_server_uptime()
and set_server_start_time(), renames uptime_str() to
get_server_uptime_str() and gets rid of the uptime enum.
All three new functions take an optional struct timeval * type argument
for the common case where the caller already knows the current time, or
wishes to specify another time for whatever reason. This allows to save
one time() system call per scheduler loop in para_server since with
this patch status_refresh() can pass the global "now" pointer (which
us updated by the scheduler once per loop) to get_server_uptime().
Andre Noll [Wed, 10 Aug 2011 17:50:38 +0000 (19:50 +0200)]
ipc: Determine maximal size of a shared memory area at runtime.
During command dispatch, the afs process allocates shm areas for the
query result and passes the identifiers via the local socket to the
child process of para_server which is executing the command. If the
write to the (non-blocking) local socket fails, for example because
the call would block, afs closes the connection to the child process
immediately to avoid deadlocks.
Therefore the maximal output size of an (afs) command depends
linearly on the size of the shared memory areas, so it is desirable
to allocate areas as large as possible. Currently, we use the SHMMAX
if it is defined and fall back to the safe default value of 64K
otherwise. However, this default is much smaller than the typical limit
of 32M on Linux. Moreover, the maximal size of a shared memory area
(shmmax) can be set at any time on most if not all operating systems,
so runtime detection of shmmax is to be preferred.
Unfortunately the way to obtain shmmax varies between operating
systems. On Linux, the value is available as the contents of a file
in /proc while on {Free,Net}BSD and Darwin sysctlbyname() must be
called. Moreover, BSD and Darwin use different identifiers for the
sysctlbyname() call.
So any code that determines shmmax at runtime and works on all
of the above systems must necessarily be ugly. This patch tries to
concentrate all ugliness in the new shm_get_shmmax() function, so that
the changes outside of ipc.c consist only of replacements SHMMAX ->
shm_get_shmmax() and of the removal of the default SHMMAX setting.
The new function only determines shmmax once when it is called for
the first time.
Andre Noll [Thu, 21 Apr 2011 21:33:06 +0000 (23:33 +0200)]
autogen: Run parallel make if compiling with distcc.
This adjusts the value of the make -j option in case the DISTCC_HOSTS
environment variable is set. There is no way to determine the number of
processing units (cores) for the remote hosts listed in DISTCC_HOSTS,
so we set the number of jobs to run simultaneously to
n + 2 * n * d
where n is the number of cores on the local system and d is the
number of hosts in DISTCC_HOSTS. This seems to work fine on one such
distributed setup.
Andre Noll [Wed, 31 Aug 2011 03:32:04 +0000 (05:32 +0200)]
mp3dec: Avoid possible endless loop.
If an error occurs during frame decode at the end of an 8K buffer,
the code in mp3dec.c might loop forever because we miss to consume
the data of all frames that have been decoded so far during this
iteration of the scheduler loop.
The two callers of used_mad_buffer_bytes() both call btr_consume()
next, and this fix requires to call the same two function once more
from another location. So it is natural to move the btr_consume()
call into used_mad_buffer_bytes() and rename the latter function
to mp3dec_consume().
Andre Noll [Wed, 17 Aug 2011 17:08:17 +0000 (19:08 +0200)]
mp3dec: Fix possible endless loop.
If the header of the last frame of a (corrupt) mp3 file can be decoded
but the rest of the frame can not, the mp3 decoder may end up in a
busy loop.
Fix this by performing the same check as for errors during header
decode. This adds some code duplication but as we are late in the
release cycle, let's go for the minimal fix for now.
Andre Noll [Mon, 15 Aug 2011 20:56:01 +0000 (22:56 +0200)]
sched: Zero-out fd sets on min delay.
If at least one pre_select method requests a minimal delay, we don't
call select() anymore as it would return immediately anyway. However,
the fds that have already been set by previous pre_select methods are
then treated as ready for IO in post_seelect which they most likely
are not. This causes unnecessary calls to readv() and friends which
returned EAGAIN.
Fix this by clearing all fd sets in case of minimal timeouts.
Andre Noll [Mon, 15 Aug 2011 18:09:54 +0000 (20:09 +0200)]
crypt: Really set result to NULL if get_asymmetric_key() fails.
The error path of this function sets the local variable "result"
is set to NULL, which is useless. We must store NULL in the pointer
variable whose address was passed to the function, so "*result =
NULL" is correct.
Andre Noll [Sun, 29 May 2011 10:27:43 +0000 (12:27 +0200)]
oggdec: Take internal state into account.
It is incorrect to look only at the node status of the oggdec buffer
tree node and do nothing if the status is zero. In case the previous
call to ogg_post_select() returned early because it hit the maximal
output buffer size limit, the node status is zero if there is no more
input available, but ov_read() will nevertheless return data which
should be fed to the children of the decoder node as soon as possible.
Andre Noll [Sat, 21 May 2011 19:06:01 +0000 (21:06 +0200)]
Simplify ogg_post_select().
Currently, in each invocation of post_select(), we allocate a
640K buffer and shrink it afterwards to the actual size of the
decoded data. This is ugly and unnecessary.
This patch changes ogg_post_select() to allocate and fill 32K
buffers until the maximal output size is reached or there is
nothing left to decode.
Andre Noll [Sat, 6 Aug 2011 20:32:49 +0000 (22:32 +0200)]
write_common: Make check_writer_arg() exit on syntax errors.
This function parses the argument of the --writer option to extract
the name of the writer, which is the first word of the argument. If
this word is not the name of a supported writer, the function
returns NULL to indicate that the given writer does not exist.
Otherwise check_writer_arg() calls the ->parse_config_or_die method
of this writer which parses the remaining part of the argument and
exits on errors.
It is more consistent and simpler to let this function exit also
in case the writer was not found. This simplifies the callers a bit
since they do not have to check the return value any more. It also
allows to kill E_WRITE_COMMON_SYNTAX.
Andre Noll [Sat, 6 Aug 2011 19:44:24 +0000 (21:44 +0200)]
audiod: Fix memory leak in writer setup.
If no writers are given, the default writer is used for each supported
audio format. In this case, we called the writer's command line parser
on each open, rather than once at startup as we fo for the non-default
case. This resulted in a memory leak which is fixed in this patch by
setting up the default writer only once, in the same way the default
receivers and filters are set up.
Andre Noll [Tue, 26 Jul 2011 18:51:24 +0000 (20:51 +0200)]
alsa: Select on the alsa poll fd.
This teaches the post_select method of the alsa writer to get a control
file descriptor from the alsa library via snd_pcm_poll_descriptors().
This file descriptor becomes readable when a buffer period has passed
and new samples can be written to the alsa handle. We add the fd to
the read fd set so that the select() call of the main scheduler loop
returns just in time.
This is more precise and simpler than the previous approach to compute
the time until a buffer underrun occurs.
Andre Noll [Sat, 30 Jul 2011 16:14:25 +0000 (18:14 +0200)]
mp3_afh: Take padding into account when computing frame duration.
Without this fix, the computed duration of a frame can be slightly
larger than its actual duration. If this happens for many frames of
an mp3 file, the chunk size will be slightly too large, which leads
to buffer underruns during streaming.
Andre Noll [Sun, 7 Aug 2011 10:33:03 +0000 (12:33 +0200)]
mp3dec: Handle decode errors gracefully.
Currently decoding damaged mp3 files leads to very audible artefacts
even if only a single frame is corrupt. This patch instructs the mp3
decoder to synchronize the stream on decode errors and to continue the
decode process. Only on fatal errors the input buffer is discarded.