nbd-server: remove unused function

Something like this might be needed in the future, once dynamic
reconfiguration has landed. But it's better to rewrite and test it then
and not keep unreferenced code hanging around cluttering the code base.

Signed-off-by: Tuomas Jorma Juhani Räsänen <tuomasjjrasanen@tjjr.fi>
nbd-server: remove commented code

Signed-off-by: Tuomas Jorma Juhani Räsänen <tuomasjjrasanen@tjjr.fi>
nbd-server: fix open_modern() to report errors

This commit is part of the refactoring work on internal error
management. The goal of the work is to separate error reporting from
error handling to make it possible to call functions from different code
paths with varying needs for error handling.

Another goal is to unify the error management throughout the code base.

Signed-off-by: Tuomas Räsänen <tuomasjjrasanen@tjjr.fi>
nbd-server: fix setup_serve() to report errors

This commit changes setup_serve() to report its errors via GError object
to its caller. It also add resource deallocation code to return points
to free all local resources correctly. Note that, the previous version
of the setup_serve() did not free its resources properly on error, but
just called functions which terminated the process. This was techincally
a leak, but without any practical significance due to immediate process
termination. However, now that error handling (process termination) is
separeted from error reporting, we must deallocate/free all locally
allocated resources correctly on errors.

Signed-off-by: Tuomas Räsänen <tuomasjjrasanen@tjjr.fi>
nbd-server: use one error domain for all NBD server errors

The purpose of the commit is to move all error identifiers to one
namespace common for all errors originating from NBD server. Using
multiple internal error domains would add only bureaucracy without any
real gain. Error domains should be used to separate errors between
modules, not within modules. For example, NBD client would define its
own error domain and codes.

Signed-off-by: Tuomas Räsänen <tuomasjjrasanen@tjjr.fi>
Add TODO file.

This contains just what I could remember off the top of my head, but I'm
fairly sure there's lots of stuff missing. I'll add to the list as I
remember them.
Add doc/* to distributed files.
nbd-server: handle port number string conversion errors

g_strdup_printf() tries to allocate a string large enough to hold the
string representation of the passed value and coverts the value to
string. It returns NULL if memory allocation fails, or some other error
occurs. Previously, NULL was interpreted as "modern style socket not
needed for this export" by returning zero to the caller, which was
utterly wrong.

Signed-off-by: Tuomas Räsänen <tuomasjjrasanen@tjjr.fi>
Detect server disconnection after NBD_OPT_EXPORTNAME

If the export is unknown to the server, it may disconnect (new-style handshake
un-fixed). In that case the read call to get export size may return 0 (non error, EOF).

This change fixes following error:

    nbd-client -N unexistent 127.0.0.1 /dev/nbd0
    Negotiation: ..size = 2314498962MBError: Exported device is too big for me. Get 64-bit machine :-(

    Exiting.
actually check that read may return zero on EOF
Merge pull request #12 from reidrac/master

Detect server disconnection after NBD_OPT_EXPORTNAME
Bring documentation in sync with code.

Spotted by: Juan J. Martínez
Be a bit more helpful when someone tries something silly.
Document that people should really use newstyle when possible.
Add missing closing bracket (whoops).

Spotted by: Michal Belczyk <belczyk@bsd.krakow.pl>
nbd-server reports 'negotiation failed' after 'nbd-client -l', and fails on next connection

When a client requests an export list from the server,
the latter issues the message 'Error: negotiation failed'.
This is rather confusing, as the only 'problem' is (AFAICT)
that the negotiation phase did everything.

On a subsequent attempt to connect a block device, the server
fails altogether:
Client output:
	--------------------
	nasc0:root ~ 216 # /tmp/nbd-client.master -l nass0
	Negotiation: ..
	nasc0-nbd0
	nasc0:root ~ 217 # /tmp/nbd-client.master nass0 -N nasc0-nbd0 /dev/nbd0
	Negotiation: ..size = 856941734MBError: Exported device is too big for me. Get 64-bit machine :-(

	Exiting.
	--------------------
Server output:
	--------------------
	nass0:root ~ 135 # /tmp/nbd-server.master -d -C /etc/nbd-server/config
	Error: negotiation failed
	Exiting.
	Error: Read failed: Connection reset by peer
	Exiting.
	--------------------
From a tcpdump, it can be observed that the syslog messages end up
on the client socket, which makes the client choke, naturally.

I tested this for the latest git version, and the same problem
occurs. I have traced the problem to the fact that negotiate(),
in two locations, closes the socket, and returns NULL , after which
serveloop also closes it, actually closing the syslog socket instead.
The next accept() returns that same filedescriptor, which syslog()
still thinks it owns.

Note: I added the message 'Session terminated by client', because
the 'ABORT' might also be intended to be used as an irregular
end-of-session (in the future?), besides indicating a regular
end-of-session after a LIST.

--------------------------------------------
Update manpage to reflect reality

Originally, it wasn't allowed to specify both a port and a name. It is
now, so update the manpage.
Clarify error handling.

While working with nbd-server, I tried to allow the clients to
fetch a list of exports. After including the following in the
generic section of /etc/nbd-server/config:
	[generic]
		# other lines ...
		allowlist
nbd-server refused to start, complaining there were no configured
exports, even though there were:
	nass0:root ~ 17 # nbd-server -d
	** Message: No configured exports; quitting.

Obviously, the line should read 'allowlist = 1', but It would be
helpful if nbd-server detected the error, and complained about
that instead of (apparently) ignoring the rest of the config file,
and issuing the confusing complaint about no configured exports.

I also tried 'allowlist = yes'. In that case it gives a
descriptive message, although it still confusingly and incorrectly
complains that there are no exports:
	nass0:root ~ 14 # nbd-server -d

	** (process:3482): WARNING **: Could not parse config file: Could not parse allowlist in group
generic: Key file contains key 'allowlist' which has a value that cannot be interpreted.
	** Message: No configured exports; quitting.

The cause seems to be that g_key_file_load_from_file fails not only
if the file cannot be found or read, but also if it contains syntax
errors.

Original patch by Rogier <rogier777@gmail.com>, but reworked enough by
$SELF that it isn't the same thing anymore.
Remove cryptic error

We already made it slightly less cryptic when you tried to connect to a
newstyle server without a name for the export. Do the same for the
reverse, too.
More manpage updates