Network Block Device  @PACKAGE_VERSION@
Data Structures | Macros | Enumerations | Functions | Variables
nbd-server.c File Reference
#include "lfs.h"
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <signal.h>
#include <errno.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <syslog.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/falloc.h>
#include <arpa/inet.h>
#include <strings.h>
#include <dirent.h>
#include <getopt.h>
#include <pwd.h>
#include <grp.h>
#include <glib.h>
#include "cliserv.h"
#include "netdb-compat.h"

Go to the source code of this file.

Data Structures

struct  SERVER
 Variables associated with a server. More...
 
struct  FILE_INFO
 Variables associated with a client socket. More...
 
struct  CLIENT
 
struct  PARAM
 Configuration file values. More...
 
struct  generic_conf
 Configuration file values of the "generic" section. More...
 

Macros

#define MY_NAME   "nbd_server"
 
#define SYSCONFDIR   "/etc"
 Default position of the config file. More...
 
#define CFILE   SYSCONFDIR "/nbd-server/config"
 
#define msg(prio,...)   g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
 Logging macros, now nothing goes to syslog unless you say ISSERVER. More...
 
#define DEBUG(...)
 
#define OFFT_MAX   ~((off_t)1<<(sizeof(off_t)*8-1))
 The highest value a variable of type off_t can reach. More...
 
#define LINELEN   256
 Size of static buffer used to read the authorization file (yuck) More...
 
#define BUFSIZE   ((1024*1024)+sizeof(struct nbd_reply))
 Size of buffer that can hold requests. More...
 
#define DIFFPAGESIZE   4096
 diff file uses those chunks More...
 
#define F_READONLY   1
 Per-export flags: More...
 
#define F_MULTIFILE   2
 flag to tell us a file is exported using -m More...
 
#define F_COPYONWRITE   4
 flag to tell us a file is exported using copyonwrite More...
 
#define F_AUTOREADONLY   8
 flag to tell us a file is set to autoreadonly More...
 
#define F_SPARSE   16
 flag to tell us copyronwrite should use a sparse file More...
 
#define F_SDP   32
 flag to tell us the export should be done using the Socket Direct Protocol for RDMA More...
 
#define F_SYNC   64
 Whether to fsync() after a write. More...
 
#define F_FLUSH   128
 Whether server wants FLUSH to be sent by the client. More...
 
#define F_FUA   256
 Whether server wants FUA to be sent by the client. More...
 
#define F_ROTATIONAL   512
 Whether server wants the client to implement the elevator algorithm. More...
 
#define F_TEMPORARY   1024
 Whether the backing file is temporary and should be created then unlinked. More...
 
#define F_TRIM   2048
 Whether server wants TRIM (discard) to be sent by the client. More...
 
#define F_FIXED   4096
 Client supports fixed new-style protocol (and can thus send us extra options. More...
 
#define F_OLDSTYLE   1
 Global flags: More...
 
#define F_LIST   2
 Allow clients to list the exports on a server. More...
 
#define NEG_INIT   (1 << 0)
 
#define NEG_OLD   (1 << 1)
 
#define NEG_MODERN   (1 << 2)
 
#define NBDS_ERR   g_quark_from_static_string("server-error-quark")
 Error domain common for all NBD server errors. More...
 
#define SEND(net, reply)
 sending macro. More...
 
#define ERROR(client, reply, errcode)   { reply.error = htonl(errcode); SEND(client->net,reply); reply.error = 0; }
 error macro. More...
 

Enumerations

enum  VIRT_STYLE { VIRT_NONE =0, VIRT_IPLIT, VIRT_IPHASH, VIRT_CIDR }
 Types of virtuatlization. More...
 
enum  PARAM_TYPE { PARAM_INT, PARAM_INT64, PARAM_STRING, PARAM_BOOL }
 Type of configuration file values. More...
 
enum  NBDS_ERRS {
  NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_KEY_MISSING, NBDS_ERR_CFILE_VALUE_INVALID,
  NBDS_ERR_CFILE_VALUE_UNSUPPORTED, NBDS_ERR_CFILE_NO_EXPORTS, NBDS_ERR_CFILE_INCORRECT_PORT, NBDS_ERR_CFILE_DIR_UNKNOWN,
  NBDS_ERR_CFILE_READDIR_ERR, NBDS_ERR_SO_LINGER, NBDS_ERR_SO_REUSEADDR, NBDS_ERR_SO_KEEPALIVE,
  NBDS_ERR_GAI, NBDS_ERR_SOCKET, NBDS_ERR_BIND, NBDS_ERR_LISTEN,
  NBDS_ERR_SYS
}
 NBD server error codes. More...
 

Functions

static const char * getcommandname (uint64_t command)
 Translate a command name into human readable form. More...
 
int authorized_client (CLIENT *opts)
 Check whether a client is allowed to connect. More...
 
static void readit (int f, void *buf, size_t len)
 Read data from a file descriptor into a buffer. More...
 
static void consume (int f, void *buf, size_t len, size_t bufsiz)
 Consume data from an FD that we don't want. More...
 
static void writeit (int f, void *buf, size_t len)
 Write data from a buffer into a filedescriptor. More...
 
void usage ()
 Print out a message about how to use nbd-server. More...
 
void dump_section (SERVER *serve, gchar *section_header)
 
SERVERcmdline (int argc, char *argv[])
 Parse the command line. More...
 
SERVERdup_serve (const SERVER *const s)
 duplicate server More...
 
int append_serve (const SERVER *const s, GArray *const a)
 append new server to array More...
 
GArray * parse_cfile (gchar *f, struct generic_conf *const genconf, GError **e)
 Parse the config file. More...
 
GArray * do_cfile_dir (gchar *dir, GError **e)
 Parse config file snippets in a directory. More...
 
static void sigchld_handler (const int s G_GNUC_UNUSED)
 Handle SIGCHLD by setting atomically a flag which will be evaluated in the main loop of the root server process. More...
 
void killchild (gpointer key, gpointer value, gpointer user_data)
 Kill a child. More...
 
static void sigterm_handler (const int s G_GNUC_UNUSED)
 Handle SIGTERM by setting atomically a flag which will be evaluated in the main loop of the root server process. More...
 
static void sighup_handler (const int s G_GNUC_UNUSED)
 Handle SIGHUP by setting atomically a flag which will be evaluated in the main loop of the root server process. More...
 
off_t size_autodetect (int fhandle)
 Detect the size of a file. More...
 
int get_filepos (GArray *export, off_t a, int *fhandle, off_t *foffset, size_t *maxbytes)
 Get the file handle and offset, given an export offset. More...
 
void myseek (int handle, off_t a)
 seek to a position in a file, with error handling. More...
 
ssize_t rawexpwrite (off_t a, char *buf, size_t len, CLIENT *client, int fua)
 Write an amount of bytes at a given offset to the right file. More...
 
int rawexpwrite_fully (off_t a, char *buf, size_t len, CLIENT *client, int fua)
 Call rawexpwrite repeatedly until all data has been written. More...
 
ssize_t rawexpread (off_t a, char *buf, size_t len, CLIENT *client)
 Read an amount of bytes at a given offset from the right file. More...
 
int rawexpread_fully (off_t a, char *buf, size_t len, CLIENT *client)
 Call rawexpread repeatedly until all data has been read. More...
 
int expread (off_t a, char *buf, size_t len, CLIENT *client)
 Read an amount of bytes at a given offset from the right file. More...
 
int expwrite (off_t a, char *buf, size_t len, CLIENT *client, int fua)
 Write an amount of bytes at a given offset to the right file. More...
 
int expflush (CLIENT *client)
 Flush data to a client. More...
 
int exptrim (struct nbd_request *req, CLIENT *client)
 
static void send_reply (uint32_t opt, int net, uint32_t reply_type, size_t datasize, void *data)
 
static CLIENThandle_export_name (uint32_t opt, int net, GArray *servers, uint32_t cflags)
 
static void handle_list (uint32_t opt, int net, GArray *servers, uint32_t cflags)
 
CLIENTnegotiate (int net, CLIENT *client, GArray *servers, int phase)
 Do the initial negotiation. More...
 
int mainloop (CLIENT *client)
 Serve a file to a single client. More...
 
void setupexport (CLIENT *client)
 Set up client export array, which is an array of FILE_INFO. More...
 
int copyonwrite_prepare (CLIENT *client)
 
int do_run (gchar *command, gchar *file)
 Run a command. More...
 
void serveconnection (CLIENT *client)
 Serve a connection. More...
 
int set_peername (int net, CLIENT *client)
 Find the name of the file we have to serve. More...
 
void destroy_pid_t (gpointer data)
 Destroy a pid_t*. More...
 
static pid_t spawn_child ()
 
static int socket_accept (const int sock)
 
static void handle_modern_connection (GArray *const servers, const int sock)
 
static void handle_connection (GArray *servers, int net, SERVER *serve, CLIENT *client)
 
static int get_index_by_servename (const gchar *const servename, const GArray *const servers)
 Return the index of the server whose servename matches the given name. More...
 
int setup_serve (SERVER *const serve, GError **const gerror)
 Connect a server's socket. More...
 
static int append_new_servers (GArray *const servers, GError **const gerror)
 Parse configuration files and add servers to the array if they don't already exist there. More...
 
void serveloop (GArray *servers)
 Loop through the available servers, and serve them. More...
 
int dosockopts (const int socket, GError **const gerror)
 Set server socket options. More...
 
int open_modern (const gchar *const addr, const gchar *const port, GError **const gerror)
 
void setup_servers (GArray *const servers, const gchar *const modernaddr, const gchar *const modernport)
 Connect our servers. More...
 
void daemonize (SERVER *serve)
 Go daemon (unless we specified at compile time that we didn't want this) More...
 
void dousers (const gchar *const username, const gchar *const groupname)
 Set up user-ID and/or group-ID. More...
 
void glib_message_syslog_redirect (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
 
int main (int argc, char *argv[])
 Main entry point... More...
 

Variables

gchar * config_file_pos
 Where our config file actually is. More...
 
int glob_flags =0
 global flags More...
 
int dontfork = 0
 
GHashTable * children
 
char pidfname [256]
 name of our PID file More...
 
char pidftemplate [256]
 template to be used for the filename of the PID file More...
 
char default_authname [] = SYSCONFDIR "/nbd-server/allow"
 default name of allow file More...
 
static volatile sig_atomic_t is_sigchld_caught
 Flag set by SIGCHLD handler to mark a child exit. More...
 
static volatile sig_atomic_t is_sigterm_caught
 Flag set by SIGTERM handler to mark a exit request. More...
 
static volatile sig_atomic_t is_sighup_caught
 Flag set by SIGHUP handler to mark a reconfiguration request. More...
 
GArray * modernsocks
 Sockets for the modern handler. More...
 
bool logged_oversized =false
 whether we logged oversized requests already More...
 

Macro Definition Documentation

#define BUFSIZE   ((1024*1024)+sizeof(struct nbd_reply))

Size of buffer that can hold requests.

Definition at line 150 of file nbd-server.c.

#define CFILE   SYSCONFDIR "/nbd-server/config"

Definition at line 114 of file nbd-server.c.

#define DEBUG (   ...)

Definition at line 137 of file nbd-server.c.

Referenced by readit(), and writeit().

#define DIFFPAGESIZE   4096

diff file uses those chunks

Definition at line 151 of file nbd-server.c.

#define ERROR (   client,
  reply,
  errcode 
)    { reply.error = htonl(errcode); SEND(client->net,reply); reply.error = 0; }

error macro.

Definition at line 1739 of file nbd-server.c.

#define F_AUTOREADONLY   8

flag to tell us a file is set to autoreadonly

Definition at line 159 of file nbd-server.c.

#define F_COPYONWRITE   4

flag to tell us a file is exported using copyonwrite

Definition at line 156 of file nbd-server.c.

Referenced by parse_cfile().

#define F_FIXED   4096

Client supports fixed new-style protocol (and can thus send us extra options.

Definition at line 168 of file nbd-server.c.

#define F_FLUSH   128

Whether server wants FLUSH to be sent by the client.

Definition at line 163 of file nbd-server.c.

Referenced by parse_cfile().

#define F_FUA   256

Whether server wants FUA to be sent by the client.

Definition at line 164 of file nbd-server.c.

Referenced by parse_cfile().

#define F_LIST   2

Allow clients to list the exports on a server.

Definition at line 172 of file nbd-server.c.

Referenced by parse_cfile().

#define F_MULTIFILE   2

flag to tell us a file is exported using -m

Definition at line 155 of file nbd-server.c.

Referenced by parse_cfile().

#define F_OLDSTYLE   1

Global flags:

Allow oldstyle (port-based) exports

Definition at line 171 of file nbd-server.c.

Referenced by parse_cfile().

#define F_READONLY   1

Per-export flags:

flag to tell us a file is readonly

Definition at line 154 of file nbd-server.c.

Referenced by parse_cfile().

#define F_ROTATIONAL   512

Whether server wants the client to implement the elevator algorithm.

Definition at line 165 of file nbd-server.c.

Referenced by parse_cfile().

#define F_SDP   32

flag to tell us the export should be done using the Socket Direct Protocol for RDMA

Definition at line 161 of file nbd-server.c.

Referenced by parse_cfile().

#define F_SPARSE   16

flag to tell us copyronwrite should use a sparse file

Definition at line 160 of file nbd-server.c.

Referenced by parse_cfile().

#define F_SYNC   64

Whether to fsync() after a write.

Definition at line 162 of file nbd-server.c.

Referenced by parse_cfile().

#define F_TEMPORARY   1024

Whether the backing file is temporary and should be created then unlinked.

Definition at line 166 of file nbd-server.c.

Referenced by parse_cfile().

#define F_TRIM   2048

Whether server wants TRIM (discard) to be sent by the client.

Definition at line 167 of file nbd-server.c.

Referenced by parse_cfile().

#define LINELEN   256

Size of static buffer used to read the authorization file (yuck)

Definition at line 147 of file nbd-server.c.

#define msg (   prio,
  ... 
)    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)

Logging macros, now nothing goes to syslog unless you say ISSERVER.

Definition at line 129 of file nbd-server.c.

Referenced by handle_connection().

#define MY_NAME   "nbd_server"

Definition at line 102 of file nbd-server.c.

#define NBDS_ERR   g_quark_from_static_string("server-error-quark")

Error domain common for all NBD server errors.

Definition at line 638 of file nbd-server.c.

Referenced by parse_cfile().

#define NEG_INIT   (1 << 0)

Definition at line 178 of file nbd-server.c.

#define NEG_MODERN   (1 << 2)

Definition at line 180 of file nbd-server.c.

#define NEG_OLD   (1 << 1)

Definition at line 179 of file nbd-server.c.

#define OFFT_MAX   ~((off_t)1<<(sizeof(off_t)*8-1))

The highest value a variable of type off_t can reach.

This is a signed integer, so set all bits except for the leftmost one.

Definition at line 146 of file nbd-server.c.

#define SEND (   net,
  reply 
)
Value:
{ writeit( net, &reply, sizeof( reply )); \
if (client->transactionlogfd != -1) \
writeit(client->transactionlogfd, &reply, sizeof(reply)); }
static void writeit(int f, void *buf, size_t len)
Write data from a buffer into a filedescriptor.
Definition: nbd-server.c:435

sending macro.

Definition at line 1735 of file nbd-server.c.

#define SYSCONFDIR   "/etc"

Default position of the config file.

Definition at line 112 of file nbd-server.c.

Enumeration Type Documentation

enum NBDS_ERRS

NBD server error codes.

Enumerator
NBDS_ERR_CFILE_NOTFOUND 

The configuration file is not found.

NBDS_ERR_CFILE_MISSING_GENERIC 

The (required) group "generic" is missing.

NBDS_ERR_CFILE_KEY_MISSING 

A (required) key is missing.

NBDS_ERR_CFILE_VALUE_INVALID 

A value is syntactically invalid.

NBDS_ERR_CFILE_VALUE_UNSUPPORTED 

A value is not supported in this build.

NBDS_ERR_CFILE_NO_EXPORTS 

A config file was specified that does not define any exports.

NBDS_ERR_CFILE_INCORRECT_PORT 

The reserved port was specified for an old-style export.

NBDS_ERR_CFILE_DIR_UNKNOWN 

A directory requested does not exist.

NBDS_ERR_CFILE_READDIR_ERR 

Error occurred during readdir()

NBDS_ERR_SO_LINGER 

Failed to set SO_LINGER to a socket.

NBDS_ERR_SO_REUSEADDR 

Failed to set SO_REUSEADDR to a socket.

NBDS_ERR_SO_KEEPALIVE 

Failed to set SO_KEEPALIVE to a socket.

NBDS_ERR_GAI 

Failed to get address info.

NBDS_ERR_SOCKET 

Failed to create a socket.

NBDS_ERR_BIND 

Failed to bind an address to socket.

NBDS_ERR_LISTEN 

Failed to start listening on a socket.

NBDS_ERR_SYS 

Underlying system call or library error.

Definition at line 643 of file nbd-server.c.

enum PARAM_TYPE

Type of configuration file values.

Enumerator
PARAM_INT 

This parameter is an integer.

PARAM_INT64 

This parameter is an integer.

PARAM_STRING 

This parameter is a string.

PARAM_BOOL 

This parameter is a boolean.

Definition at line 276 of file nbd-server.c.

enum VIRT_STYLE

Types of virtuatlization.

Enumerator
VIRT_NONE 

No virtualization.

VIRT_IPLIT 

Literal IP address as part of the filename.

VIRT_IPHASH 

Replacing all dots in an ip address by a / before doing the same as in IPLIT.

VIRT_CIDR 

Every subnet in its own directory.

Definition at line 212 of file nbd-server.c.

Function Documentation

static int append_new_servers ( GArray *const  servers,
GError **const  gerror 
)
static

Parse configuration files and add servers to the array if they don't already exist there.

The existence is tested by comparing servenames. A server is appended to the array only if its servename is unique among all other servers.

Parameters
serversan array of servers
Returns
the number of new servers appended to the array, or -1 in case of an error

Definition at line 2489 of file nbd-server.c.

int append_serve ( const SERVER *const  s,
GArray *const  a 
)

append new server to array

Parameters
sserver
aserver array
Returns
0 success, -1 error

Definition at line 719 of file nbd-server.c.

Referenced by parse_cfile().

int authorized_client ( CLIENT opts)

Check whether a client is allowed to connect.

Works with an authorization file which contains one line per machine, no wildcards.

Parameters
optsThe client who's trying to connect.
Returns
0 - authorization refused, 1 - OK

Definition at line 341 of file nbd-server.c.

SERVER* cmdline ( int  argc,
char *  argv[] 
)

Parse the command line.

Parameters
argcthe argc argument to main()
argvthe argv argument to main()

Definition at line 498 of file nbd-server.c.

static void consume ( int  f,
void *  buf,
size_t  len,
size_t  bufsiz 
)
inlinestatic

Consume data from an FD that we don't want.

Parameters
fa file descriptor
bufa buffer
lenthe number of bytes to consume
bufsizthe size of the buffer

Definition at line 418 of file nbd-server.c.

References readit().

int copyonwrite_prepare ( CLIENT client)

Definition at line 2014 of file nbd-server.c.

void daemonize ( SERVER serve)

Go daemon (unless we specified at compile time that we didn't want this)

Parameters
servethe first server of our configuration. If its port is zero, then do not daemonize, because we're doing inetd then. This parameter is only used to create a PID file of the form /var/run/nbd-server.<port>.pid; it's not modified in any way.

Definition at line 2977 of file nbd-server.c.

void destroy_pid_t ( gpointer  data)

Destroy a pid_t*.

Parameters
dataa pointer to pid_t which should be freed

Definition at line 2199 of file nbd-server.c.

GArray* do_cfile_dir ( gchar *  dir,
GError **  e 
)

Parse config file snippets in a directory.

Uses readdir() and friends to find files and open them, then passes them on to parse_cfile with have_global set false

Definition at line 784 of file nbd-server.c.

Referenced by parse_cfile().

int do_run ( gchar *  command,
gchar *  file 
)

Run a command.

This is used for the prerun'' andpostrun'' config file options

Parameters
commandthe command to be ran. Read from the config file
filethe file name we're about to export

Definition at line 2038 of file nbd-server.c.

int dosockopts ( const int  socket,
GError **const  gerror 
)

Set server socket options.

Parameters
socketa socket descriptor of the server
gerrora pointer to an error object pointer used for reporting errors. On error, if gerror is not NULL, *gerror is set and -1 is returned.
Returns
0 on success, -1 on error

Definition at line 2681 of file nbd-server.c.

void dousers ( const gchar *const  username,
const gchar *const  groupname 
)

Set up user-ID and/or group-ID.

Definition at line 3015 of file nbd-server.c.

References err().

void dump_section ( SERVER serve,
gchar *  section_header 
)

Definition at line 469 of file nbd-server.c.

SERVER* dup_serve ( const SERVER *const  s)

duplicate server

Parameters
sthe old server we want to duplicate
Returns
new duplicated server

Definition at line 670 of file nbd-server.c.

int expflush ( CLIENT client)

Flush data to a client.

Parameters
clientThe client we're going to write for.
Returns
0 on success, nonzero on failure

Definition at line 1495 of file nbd-server.c.

References CLIENT::difffile.

int expread ( off_t  a,
char *  buf,
size_t  len,
CLIENT client 
)

Read an amount of bytes at a given offset from the right file.

This abstracts the read-side of the copyonwrite stuff, and calls rawexpread() with the right parameters to do the actual work.

Parameters
aThe offset where the read should start
bufA buffer to read into
lenThe size of buf
clientThe client we're going to read for
Returns
0 on success, nonzero on failure

Definition at line 1395 of file nbd-server.c.

int exptrim ( struct nbd_request req,
CLIENT client 
)

Definition at line 1515 of file nbd-server.c.

int expwrite ( off_t  a,
char *  buf,
size_t  len,
CLIENT client,
int  fua 
)

Write an amount of bytes at a given offset to the right file.

This abstracts the write-side of the copyonwrite option, and calls rawexpwrite() with the right parameters to do the actual work.

Parameters
aThe offset where the write should start
bufThe buffer to write from
lenThe length of buf
clientThe client we're going to write for.
fuaFlag to indicate 'Force Unit Access'
Returns
0 on success, nonzero on failure

Definition at line 1437 of file nbd-server.c.

int get_filepos ( GArray *  export,
off_t  a,
int *  fhandle,
off_t *  foffset,
size_t *  maxbytes 
)

Get the file handle and offset, given an export offset.

Parameters
exportAn array of export files
aThe offset to get corresponding file/offset for
fhandle[out] File descriptor
foffset[out] Offset into fhandle
maxbytes[out] Tells how many bytes can be read/written from fhandle starting at foffset (0 if there is no limit)
Returns
0 on success, -1 on failure

Definition at line 1200 of file nbd-server.c.

References FILE_INFO::startoff.

static int get_index_by_servename ( const gchar *const  servename,
const GArray *const  servers 
)
static

Return the index of the server whose servename matches the given name.

Parameters
servenamea string to match
serversan array of servers
Returns
the first index of the server whose servename matches the given name or -1 if one cannot be found

Definition at line 2463 of file nbd-server.c.

References SERVER::servename.

static const char* getcommandname ( uint64_t  command)
inlinestatic

Translate a command name into human readable form.

Parameters
commandThe command number (after applying NBD_CMD_MASK_COMMAND)
Returns
pointer to the command name

Definition at line 317 of file nbd-server.c.

void glib_message_syslog_redirect ( const gchar *  log_domain,
GLogLevelFlags  log_level,
const gchar *  message,
gpointer  user_data 
)

Definition at line 3042 of file nbd-server.c.

static void handle_connection ( GArray *  servers,
int  net,
SERVER serve,
CLIENT client 
)
static

Definition at line 2359 of file nbd-server.c.

References msg.

static CLIENT* handle_export_name ( uint32_t  opt,
int  net,
GArray *  servers,
uint32_t  cflags 
)
static

Definition at line 1554 of file nbd-server.c.

References err().

static void handle_list ( uint32_t  opt,
int  net,
GArray *  servers,
uint32_t  cflags 
)
static

Definition at line 1590 of file nbd-server.c.

static void handle_modern_connection ( GArray *const  servers,
const int  sock 
)
static

Definition at line 2252 of file nbd-server.c.

void killchild ( gpointer  key,
gpointer  value,
gpointer  user_data 
)

Kill a child.

Called from sigterm_handler::g_hash_table_foreach.

Parameters
keythe key
valuethe value corresponding to the above key
user_dataa pointer which we always set to 1, so that we know what will happen next.

Definition at line 1110 of file nbd-server.c.

int main ( int  argc,
char *  argv[] 
)

Main entry point...

Definition at line 3076 of file nbd-server.c.

int mainloop ( CLIENT client)

Serve a file to a single client.

Todo:
This beast needs to be split up in many tiny little manageable pieces. Preferably with a chainsaw.
Parameters
clientThe client we're going to serve to.
Returns
when the client disconnects

Definition at line 1749 of file nbd-server.c.

void myseek ( int  handle,
off_t  a 
)

seek to a position in a file, with error handling.

Parameters
handlea filedescriptor
aposition to seek to
Todo:
get rid of this; lastpoint is a global variable right now, but it shouldn't be. If we pass it on as a parameter, that makes things a lot easier.

Definition at line 1245 of file nbd-server.c.

CLIENT* negotiate ( int  net,
CLIENT client,
GArray *  servers,
int  phase 
)

Do the initial negotiation.

Parameters
clientThe client we're negotiating with.

Definition at line 1622 of file nbd-server.c.

int open_modern ( const gchar *const  addr,
const gchar *const  port,
GError **const  gerror 
)

Definition at line 2825 of file nbd-server.c.

GArray * parse_cfile ( gchar *  f,
struct generic_conf *const  genconf,
GError **  e 
)

Parse the config file.

Parameters
fthe name of the config file
genconfa pointer to generic configuration which will get updated with parsed values. If NULL, then parsed generic configuration values are safely and silently discarded.
ea GError. Error code can be any of the following: NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_VALUE_INVALID, NBDS_ERR_CFILE_VALUE_UNSUPPORTED or NBDS_ERR_CFILE_NO_EXPORTS.
See Also
NBDS_ERRS.
Returns
a Array of SERVER* pointers, If the config file is empty or does not exist, returns an empty GHashTable; if the config file contains an error, returns NULL, and e is set appropriately

Definition at line 860 of file nbd-server.c.

References append_serve(), SERVER::authname, SERVER::cidrlen, do_cfile_dir(), err(), SERVER::expected_size, SERVER::exportname, F_COPYONWRITE, F_FLUSH, F_FUA, F_LIST, F_MULTIFILE, F_OLDSTYLE, F_READONLY, F_ROTATIONAL, F_SDP, F_SPARSE, F_SYNC, F_TEMPORARY, F_TRIM, SERVER::flags, generic_conf::flags, G_GNUC_UNUSED, glob_flags, generic_conf::group, is_sigchld_caught, SERVER::listenaddr, SERVER::max_connections, generic_conf::modernaddr, generic_conf::modernport, NBDS_ERR, NBDS_ERR_CFILE_MISSING_GENERIC, NBDS_ERR_CFILE_NO_EXPORTS, NBDS_ERR_CFILE_NOTFOUND, NBDS_ERR_CFILE_VALUE_INVALID, NBDS_ERR_CFILE_VALUE_UNSUPPORTED, PARAM_BOOL, PARAM_INT, PARAM_INT64, PARAM_OFFT, PARAM_STRING, SERVER::port, SERVER::postrun, SERVER::prerun, PARAM::required, SERVER::servename, sigchld_handler(), SERVER::socket_family, SERVER::transactionlog, generic_conf::user, VIRT_CIDR, VIRT_IPHASH, VIRT_IPLIT, VIRT_NONE, and SERVER::virtstyle.

ssize_t rawexpread ( off_t  a,
char *  buf,
size_t  len,
CLIENT client 
)

Read an amount of bytes at a given offset from the right file.

This abstracts the read-side of the multiple files option.

Parameters
aThe offset where the read should start
bufA buffer to read into
lenThe size of buf
clientThe client we're serving for
Returns
The number of bytes actually read, or -1 in case of an error.

Definition at line 1354 of file nbd-server.c.

int rawexpread_fully ( off_t  a,
char *  buf,
size_t  len,
CLIENT client 
)

Call rawexpread repeatedly until all data has been read.

Returns
0 on success, nonzero on failure

Definition at line 1374 of file nbd-server.c.

ssize_t rawexpwrite ( off_t  a,
char *  buf,
size_t  len,
CLIENT client,
int  fua 
)

Write an amount of bytes at a given offset to the right file.

This abstracts the write-side of the multiple file option.

Parameters
aThe offset where the write should start
bufThe buffer to write from
lenThe length of buf
clientThe client we're serving for
fuaFlag to indicate 'Force Unit Access'
Returns
The number of bytes actually written, or -1 in case of an error

Definition at line 1262 of file nbd-server.c.

int rawexpwrite_fully ( off_t  a,
char *  buf,
size_t  len,
CLIENT client,
int  fua 
)

Call rawexpwrite repeatedly until all data has been written.

Parameters
aThe offset where the write should start
bufThe buffer to write from
lenThe length of buf
clientThe client we're serving for
fuaFlag to indicate 'Force Unit Access'
Returns
0 on success, nonzero on failure

Definition at line 1332 of file nbd-server.c.

static void readit ( int  f,
void *  buf,
size_t  len 
)
inlinestatic

Read data from a file descriptor into a buffer.

Parameters
fa file descriptor
bufa buffer
lenthe number of bytes to be read

Definition at line 395 of file nbd-server.c.

References DEBUG, and err().

Referenced by consume().

static void send_reply ( uint32_t  opt,
int  net,
uint32_t  reply_type,
size_t  datasize,
void *  data 
)
static

Definition at line 1540 of file nbd-server.c.

void serveconnection ( CLIENT client)

Serve a connection.

Todo:
allow for multithreading, perhaps use libevent. Not just yet, though; follow the road map.
Parameters
clienta connected client

Definition at line 2058 of file nbd-server.c.

References CLIENT::server, SERVER::transactionlog, and CLIENT::transactionlogfd.

void serveloop ( GArray *  servers)

Loop through the available servers, and serve them.

Never returns.

Definition at line 2523 of file nbd-server.c.

int set_peername ( int  net,
CLIENT client 
)

Find the name of the file we have to serve.

This will use g_strdup_printf to put the IP address of the client inside a filename containing "%s" (in the form as specified by the "virtstyle" option). That name is then written to client->exportname.

Parameters
netA socket connected to an nbd client
clientinformation about the client. The IP address in human-readable format will be written to a new char* buffer, the address of which will be stored in client->clientname.
Returns
: 0 - OK, -1 - failed.

Definition at line 2101 of file nbd-server.c.

int setup_serve ( SERVER *const  serve,
GError **const  gerror 
)

Connect a server's socket.

Parameters
servethe server we want to connect.

Definition at line 2719 of file nbd-server.c.

References SERVER::servename.

void setup_servers ( GArray *const  servers,
const gchar *const  modernaddr,
const gchar *const  modernport 
)

Connect our servers.

Definition at line 2917 of file nbd-server.c.

void setupexport ( CLIENT client)

Set up client export array, which is an array of FILE_INFO.

Also, split a single exportfile into multiple ones, if that was asked.

Parameters
clientinformation on the client which we want to setup export for

Definition at line 1912 of file nbd-server.c.

static void sigchld_handler ( const int s  G_GNUC_UNUSED)
static

Handle SIGCHLD by setting atomically a flag which will be evaluated in the main loop of the root server process.

This allows us to separate the signal catching from th actual task triggered by SIGCHLD and hence processing in the interrupt context is kept as minimial as possible.

Parameters
sthe signal we're handling (must be SIGCHLD, or something is severely wrong)

Definition at line 1098 of file nbd-server.c.

Referenced by parse_cfile().

static void sighup_handler ( const int s  G_GNUC_UNUSED)
static

Handle SIGHUP by setting atomically a flag which will be evaluated in the main loop of the root server process.

This allows us to separate the signal catching from th actual task triggered by SIGHUP and hence processing in the interrupt context is kept as minimial as possible.

Parameters
sthe signal we're handling (must be SIGHUP, or something is severely wrong).

Definition at line 1138 of file nbd-server.c.

static void sigterm_handler ( const int s  G_GNUC_UNUSED)
static

Handle SIGTERM by setting atomically a flag which will be evaluated in the main loop of the root server process.

This allows us to separate the signal catching from th actual task triggered by SIGTERM and hence processing in the interrupt context is kept as minimial as possible.

Parameters
sthe signal we're handling (must be SIGTERM, or something is severely wrong).

Definition at line 1125 of file nbd-server.c.

off_t size_autodetect ( int  fhandle)

Detect the size of a file.

Parameters
fhandleAn open filedescriptor
Returns
the size of the file, or OFFT_MAX if detection was impossible.

Definition at line 1149 of file nbd-server.c.

static int socket_accept ( const int  sock)
static

Definition at line 2237 of file nbd-server.c.

static pid_t spawn_child ( )
static

Definition at line 2204 of file nbd-server.c.

void usage ( )

Print out a message about how to use nbd-server.

Split out to a separate function so that we can call it from multiple places

Definition at line 450 of file nbd-server.c.

static void writeit ( int  f,
void *  buf,
size_t  len 
)
inlinestatic

Write data from a buffer into a filedescriptor.

Parameters
fa file descriptor
bufa buffer containing data
lenthe number of bytes to be written

Definition at line 435 of file nbd-server.c.

References DEBUG, and err().

Variable Documentation

GHashTable* children

Definition at line 173 of file nbd-server.c.

gchar* config_file_pos

Where our config file actually is.

Definition at line 117 of file nbd-server.c.

char default_authname[] = SYSCONFDIR "/nbd-server/allow"

default name of allow file

Definition at line 176 of file nbd-server.c.

int dontfork = 0

Definition at line 123 of file nbd-server.c.

int glob_flags =0

global flags

Definition at line 120 of file nbd-server.c.

Referenced by parse_cfile().

volatile sig_atomic_t is_sigchld_caught
static

Flag set by SIGCHLD handler to mark a child exit.

Definition at line 182 of file nbd-server.c.

Referenced by parse_cfile().

volatile sig_atomic_t is_sighup_caught
static

Flag set by SIGHUP handler to mark a reconfiguration request.

Definition at line 192 of file nbd-server.c.

volatile sig_atomic_t is_sigterm_caught
static

Flag set by SIGTERM handler to mark a exit request.

Definition at line 187 of file nbd-server.c.

bool logged_oversized =false

whether we logged oversized requests already

Definition at line 207 of file nbd-server.c.

GArray* modernsocks

Sockets for the modern handler.

Not used if a client was only specified on the command line; only port used if oldstyle is set to false (and then the command-line client isn't used, gna gna). This may be more than one socket on systems that don't support serving IPv4 and IPv6 from the same socket (like, e.g., FreeBSD)

Definition at line 197 of file nbd-server.c.

char pidfname[256]

name of our PID file

Definition at line 174 of file nbd-server.c.

char pidftemplate[256]

template to be used for the filename of the PID file

Definition at line 175 of file nbd-server.c.