Update devices-and-drivers.txt

Fixed some minor spelling mistakes
Our select() function is emulated using poll(), which is a sensible thing
to do. However, it did several things wrong that this patch fixes. Thanks
to Paolo Bonzini for finding these problems (see issue #35).

1. When poll() returned a bare POLLHUP, without POLLIN, our select() didn't
return a read event. But nothing in the manpages guarantees that POLLHUP
is accompanied by POLLIN, and some special file implementations might
forget it. As an example, in Linux POLLHUP without POLLIN is common.
But POLLHUP on its own already means that there's nothing more to read,
so a read() will return immediately without blocking - and therefore
select() needs to turn on the readable bit for this fd.

2. Similarly, a bare POLLRDHUP should turn on the writable bit: The
reader on this file hug up, so a write will fail immediately.

3. Our poll() and select() confused what POLLERR means. POLLERR does not
mean poll() found a bad file descriptor - there is POLLNVAL for that.
So this patch fixes poll() to set POLLNVAL, not POLLERR, and select()
to return with errno=EBADF when it sees POLLNVAL, not POLLERR.

4. Rather, POLLERR means the file descriptor is in an error state, so every
read() or write() will return immediately (with an error). So when we see
it, we need to turn on both read and write bits in this case.

5. The meaning of "exceptfds" isn't clear in any manual page, and it
seems there're a lot of opinions on what it might mean. In this patch I
did what Paolo suggested, which is to set the except bit when POLLPRI.
(I don't set exceptfds on POLLERR, or any other case).

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Merge pull request #41 from jnider/master

Update devices-and-drivers.txt
Clean up "tags" and "TAGS" target

Each of the "tags" and "TAGS" make targets was done in a different way,
causing each to have a different problem:

 1. "tags" (ctags) used the "-L" option which turns out is nonportable
    (only available in Exuberant Ctags).
 2. "TAGS" (etags) ran the etags command separately for each source file,
    slowing it down.

The best of both worlds is to use xargs to have ctags/etags operate on
multiple files in each run using xargs. Because we cannot be sure xargs
will run ctags/etags only once, we must delete the file first and use
 the "-a" (append) option.

Also, this patch reduces code duplication - there is now one rule for
both "tags" and "TAGS" targets that uses the correct tool (ctags or etags,
respectively).

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Makefile: Don't hide Gradle's progress

Currently, OSv's "make" builds both the kernel, using a normal Makefile,
and the sample management framework, using the "Gradle" tool.

We carefully hid Gradle's progress under one output line "GRADLE", but
this leads to the "make" process hanging for a long time - up to several
minutes, and all the puzzled user sees is the line "GRADLE". Users who
didn't go for coffee will likely kill the build at this point ;-) Even
worse, when Gradle fails, or just hangs (e.g., a bad or slow network
connection), the user won't even know why.

So let's just run Gradle normally, and let the user see its full output.
This output is rather pretty and organized, so no real reason to hide it.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Makefile: add 'make cscope' command

This will generate cscope index for the source files.

Signed-off-by: Liu Yuan <namei.unix@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
java.so: Fix JavaVMOption initialization

JavaVMOption has extraInfo member that's not initialized in mkoption().
It only used by few special case options that provide a function pointer
hook to the JVM so it's harmless but fix it anyway.

Spotted by Coverity.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
elf: Fix assert() in object::relocate_pltgot

The assertion in object::relocate_pltgot uses assignment instead of
comparison.  Fix that up.

Spotted by Coverity.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
README/debian: added build-essential, libtool, jdk to dependencies

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
Fix missing poll() wakeup on POLLHUP

Our poll_wake() code ignored calls with the POLLHUP event, because
the user did not explicitly ask for this event. This causes a poll()
waiting on read from a pipe whose write side closes not to wake up.

This patch adds a test for this case in tst-pipe.cc, and fixes the
bug by adding to the poll structure's _events also ~POLL_REQUESTABLE,
i.e., any bits which do not have to be explicitly requested by the
user (POLL_REQUESTABLE is a new macro defined in this patch).

After this patch, poll() wakes as needed in the test (instead of just
hang), but returns the wrong event because of another bug which will
be fixed in a separate patch.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Fix POLLHUP in pipes

The code in pipe_buffer.cc had the meaning of POLLHUP and POLLRDHUP
reversed, and as a result POLLHUP wasn't correctly returned when polling
a read-end of the pipe whose write-end is closed.

This fixes the mixup, and as a result the test added to tst-pipe.cc in
the previous commit, and also another one added in this patch, all pass.

We are still incompatible with Linux with what we do regarding POLLRDHUP.
This will be a separate patch.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Change pipe event from POLLRDHUP to POLLERR|POLLOUT

We used to return the POLLRDHUP event when polling a write-side of a pipe
whose reader is closed, but Linux returns POLLERR|POLLOUT, so let's do
the same.

Linux's behavior can be explained in that when the reader is closed, a write
will return immediately (and hence POLLOUT) but moreover, will return with
an error (EPIPE). And since a read will also cause an error (EBADF when
reading on the write side of the pipe), a POLLERR makes sense and says
that any operation on this fd will now result in an error.

This patch also adds a test for this case.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Move statement to appease Coverity

Coverity thinks we have a use-before-assigned of r2 here, despite the
join(), so let's just move the test inside the thread to make this
warning go away.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
netperf: Closes #44: test time tracking warning added to documentation

Signed-off-by: Dmitry Fleytman <dmitry@daynix.com>
Fix bug in "cscope" make target

The "cscope" make target piped a list of files into "cscope -bq", but
this command doesn't read its input. The command should be "cscope -bq -i-"
which reads the list of files from stdin.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Add stub setresuid(), setresgid()

Add to libc/user.cc stub setresgid(), setresgid(), which as usual in
OSv only allow user and group 0. These functions are used by Rogue ;-)

Also move getuid() and friends from runtime.cc to libc/user.cc, where
they feel more at home.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Dynamic linker: run finalizers when unloading shared object

ELF allows specifying initializers - functions to be run after loading a
a shared object, in DT_INIT_ARRAY, and also finalizers - functions to be
run before unloading a shared objects, in DT_FINI_ARRAY. The existing code
ran the initializers, but forgot to run the finalizers, and this patch
fixes this oversight.

This fix is necessary for destructors of static objects defined in the
shared object. But this fix is not sufficient for C++ destructors - see
also the next patch.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
C++ runtime: shared-object static destructors

GCC implements static destructors in shared-objects (DSO) by registering a
single finalization function, __cxa_finalize(), to run when the DSO is
unloaded, and each constructor calls __cxa_atexit() to register a destructor
which __cxa_finalize() should call.

This patch implements the missing __cxa_finalize() and __cxa_atexit()
functions. The do-nothing stubs we had never registered, and never ran,
destructors. The implementation in this patch is amazingly simple to
write in C++11 :-)

Before this fix, we had a crash when running tst-tracepoint.so twice:
The first run ran the tracepoints' constructors, which added to the global
linked list of tracepoints pointers into the mapped DSO. When the DSO
was later unmapped but destructors not run, the linked list of tracepoints
was left with pointers to unmapped memory. When we ran the test again,
and it wanted to construct more tracepoints, it accessed the broken linked
list and crashed.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
signal() off-by-one argument check

signal() verified that its argument is not >nsigs, but it should be >=nsigs
because nsigs itself is already illegal (0...nsigs-1 are valid signals).

Discovered by Coverity.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
ramfs: Fix use after free in ramfs_remove()

The ramfs_remove_node() will free vp->v_data so we cannot look it up in
ramfs_remove().

Spotted by Coverity.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>