nbd-client: adjust the oom-killer score in swap mode

In swap mode, it is highly desirable to take all available measures that
could help nbd-client survival in low memory conditions.  One of such
measures is to disable oom killing of nbd-client by adjusting the
oom-killer score.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-client: try harder to reconnect in persist mode

When nbd-client in persist mode is trying to reconnect because of
nbd-server restart, it is highly probable that nbd-server will not be
ready to serve a reconnection request in time causing nbd-client to exit
with "connection refused" diagnostics.  This change makes nbd-client in
persist mode retry its reconnection attempts until succeeded.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: zeroise hash table pointer after destroying the table

This is required to avoid accessing freed data later in
sigterm_handler.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: fix modernsock descriptor initialization and use

Change the value for undefined "modern handler" descriptor from 0 to -1
because any value greater or equal to 0 can be a valid file descriptor.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: avoid modernsock descriptor leaking to child processes

Close modernsock descriptor in child processes.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: fix accepted connections descriptor initialization and use

Change the value for undefined descriptor from 0 to -1 because
any value greater or equal to 0 can be a valid file descriptor.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: treat accept errors as non-fatal

In most cases, an error returned by accept system call is a network
error, which should not terminate the whole nbd-server.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: move connection handling code to a separate function

The serveloop function has grown too much, and the indentation of
connection handling code unnecessarily complicates further changes.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: do not lose connections

When select reports that more than one connection is ready to be
accepted, handle them all instead of losing all of them but one.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: do not call fcntl F_SETFL unnecessarily

Call fcntl F_SETFL only when new file status flags differ from the old one.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: fix several memory leaks in connection handling code

This change fixes a CLIENT leak on normal path in the parent process,
several CLIENT leaks on error path in the parent process, a pid_t leak
on error path in the parent process, and a pid_t leak in the child
process.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: block SIGCHLD when handling SIGTERM, and visa versa

SIGCHLD handler must be blocked during execution of SIGTERM handler,
and visa versa, because the former changes "children" hash table
iterated by the latter.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
nbd-server: fix signal handling

Reset SIGCHLD and SIGTERM handlers to SIG_DFL in child processes because
it is not their business to handle these signals.  Block SIGCHLD and
SIGTERM for the short time of fork, changing signal handlers and
changing "children" hash table to avoid race conditions with nasty
consequences.  Fix SIGTERM handler to always unlink pidfile.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Remove redundant __linux__ #ifdef lines

nbd-client is Linux-only, so we don't actually need to check for that.