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>
ramfs: Fix strlcpy() size in ramfs_rename_node()

Use ->rn_namelen as the destination buffer size for strlcpy().

Spotted by Coverity.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs: Add VOP_LINK to VFS interface

Add VOP_LINK to the VFS interface in preparation for sys_link().

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
[ penberg: split to separate commit ]
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
devfs: Wire up VOP_LINK

Wire up the VOP_LINK vnode operation for devfs in preparation for
sys_link().

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
[ penberg: split to separate commit ]
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
ramfs: Wire up VOP_LINK

Wire up the VOP_LINK vnode operation for ramfs in preparation for
sys_link().

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
[ penberg: split to separate commit ]
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
zfs: Wire up VOP_LINK

Wire up the VOP_LINK vnode operation for ZFS in preparation for
sys_link().

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
[ penberg: drop FIGNORE, cleanup, split ]
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
zfs: Update ->va_nlink in zfs_getattr()

Update ->va_nlink() in zfs_getattr() in preparation for sys_link().

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs: Add link() system call

This patch adds the link() system call.

The system call uses the filesystem specific VOP_LINK vnode operation to
do the actual work.  ZFS is currently the only available file system
that supports hard links and the other filesystems return EPERM.

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
vfs: Return inode number and link count in vn_stat()

Make vn_stat() return inode number and link count in st_ino and
st_nlink, respectively.

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tst-fs-link.so: link() test

Add a basic test case for filesystem hard link support. You can run it as
follows:

  ./scripts/run.py -e "tests/tst-fs-link.so"

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
tst-fs-link: Add extra checks

Add a new test to see if the content from one link would be the same in
another.

Signed-off-by: Raphael S. Carvalho <raphael.scarv@gmail.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>