unsigned int port; /**< port we're exporting this file at */
char* authname; /**< filename of the authorization file */
int flags; /**< flags associated with this exported file */
- unsigned int timeout;/**< how long a connection may be idle
- (0=forever) */
int socket; /**< The socket of this server. */
VIRT_STYLE virtstyle;/**< The style of virtualization, if any */
uint8_t cidrlen; /**< The length of the mask when we use
*/
void usage() {
printf("This is nbd-server version " VERSION "\n");
- printf("Usage: [ip:]port file_to_export [size][kKmM] [-l authorize_file] [-r] [-m] [-c] [-a timeout_sec] [-C configuration file] [-p PID file name] [-o section name]\n"
+ printf("Usage: [ip:]port file_to_export [size][kKmM] [-l authorize_file] [-r] [-m] [-c] [-C configuration file] [-p PID file name] [-o section name]\n"
"\t-r|--read-only\t\tread only\n"
"\t-m|--multi-file\t\tmultiple file\n"
"\t-c|--copy-on-write\tcopy on write\n"
printf("\tcopyonwrite = true\n");
}
if(serve->expected_size) {
- printf("\tfilesize = %Ld\n", (long long int)serve->expected_size);
+ printf("\tfilesize = %lld\n", (long long int)serve->expected_size);
}
if(serve->authname) {
printf("\tauthfile = %s\n", serve->authname);
}
- if(serve->timeout) {
- printf("\ttimeout = %d\n", serve->timeout);
- }
exit(EXIT_SUCCESS);
}
size_t last;
char suffix;
gboolean do_output=FALSE;
- gchar* section_header;
+ gchar* section_header="";
gchar** addr_port;
if(argc==1) {
if (suffix == 'k' || suffix == 'K' ||
suffix == 'm' || suffix == 'M')
optarg[last] = '\0';
- es = (off_t)atol(optarg);
+ es = (off_t)atoll(optarg);
switch (suffix) {
case 'm':
case 'M': es <<= 10;
g_free(serve->authname);
serve->authname=g_strdup(optarg);
break;
- case 'a':
- serve->timeout=strtol(optarg, NULL, 0);
- break;
default:
usage();
exit(EXIT_FAILURE);
CFILE_KEY_MISSING, /**< A (required) key is missing */
CFILE_VALUE_INVALID, /**< A value is syntactically invalid */
CFILE_VALUE_UNSUPPORTED,/**< A value is not supported in this build */
- CFILE_PROGERR /**< Programmer error */
+ CFILE_PROGERR, /**< Programmer error */
+ CFILE_NO_EXPORTS /**< A config file was specified that does not
+ define any exports */
} CFILE_ERRORS;
/**
{ "exportname", TRUE, PARAM_STRING, NULL, 0 },
{ "port", TRUE, PARAM_INT, NULL, 0 },
{ "authfile", FALSE, PARAM_STRING, NULL, 0 },
- { "timeout", FALSE, PARAM_INT, NULL, 0 },
{ "filesize", FALSE, PARAM_INT, NULL, 0 },
{ "virtstyle", FALSE, PARAM_STRING, NULL, 0 },
{ "prerun", FALSE, PARAM_STRING, NULL, 0 },
{ "readonly", FALSE, PARAM_BOOL, NULL, F_READONLY },
{ "multifile", FALSE, PARAM_BOOL, NULL, F_MULTIFILE },
{ "copyonwrite", FALSE, PARAM_BOOL, NULL, F_COPYONWRITE },
- { "autoreadonly", FALSE, PARAM_BOOL, NULL, F_AUTOREADONLY },
{ "sparse_cow", FALSE, PARAM_BOOL, NULL, F_SPARSE },
{ "sdp", FALSE, PARAM_BOOL, NULL, F_SDP },
{ "listenaddr", FALSE, PARAM_STRING, NULL, 0 },
};
- const int lp_size=15;
+ const int lp_size=sizeof(lp)/sizeof(PARAM);
PARAM gp[] = {
{ "user", FALSE, PARAM_STRING, &runuser, 0 },
{ "group", FALSE, PARAM_STRING, &rungroup, 0 },
};
PARAM* p=gp;
- int p_size=2;
+ int p_size=sizeof(gp)/sizeof(PARAM);
GKeyFile *cfile;
GError *err = NULL;
const char *err_msg=NULL;
GArray *retval=NULL;
gchar **groups;
gboolean value;
+ gchar* startgroup;
gint i;
gint j;
g_key_file_free(cfile);
return retval;
}
- if(strcmp(g_key_file_get_start_group(cfile), "generic")) {
+ startgroup = g_key_file_get_start_group(cfile);
+ if(!startgroup || strcmp(startgroup, "generic")) {
g_set_error(e, errdomain, CFILE_MISSING_GENERIC, "Config file does not contain the [generic] group!");
g_key_file_free(cfile);
return NULL;
lp[0].target=&(s.exportname);
lp[1].target=&(s.port);
lp[2].target=&(s.authname);
- lp[3].target=&(s.timeout);
- lp[4].target=&(s.expected_size);
- lp[5].target=&(virtstyle);
- lp[6].target=&(s.prerun);
- lp[7].target=&(s.postrun);
- lp[8].target=lp[9].target=lp[10].target=
- lp[11].target=lp[12].target=
- lp[13].target=&(s.flags);
- lp[14].target=&(s.listenaddr);
+ lp[3].target=&(s.expected_size);
+ lp[4].target=&(virtstyle);
+ lp[5].target=&(s.prerun);
+ lp[6].target=&(s.postrun);
+ lp[7].target=lp[8].target=lp[9].target=
+ lp[10].target=lp[11].target=&(s.flags);
+ lp[12].target=&(s.listenaddr);
/* After the [generic] group, start parsing exports */
if(i==1) {
}
#endif
}
+ if(i==1) {
+ g_set_error(e, errdomain, CFILE_NO_EXPORTS, "The config file does not specify any exports");
+ }
+ g_key_file_free(cfile);
return retval;
}
unlink(pidfname);
}
- exit(0);
+ exit(EXIT_SUCCESS);
}
/**
**/
off_t size_autodetect(int fhandle) {
off_t es;
- unsigned long sectors;
+ u64 bytes;
struct stat stat_buf;
int error;
#ifdef HAVE_SYS_MOUNT_H
#ifdef HAVE_SYS_IOCTL_H
-#ifdef BLKGETSIZE
- DEBUG("looking for export size with ioctl BLKGETSIZE\n");
- if (!ioctl(fhandle, BLKGETSIZE, §ors) && sectors) {
- es = (off_t)sectors * (off_t)512;
- return es;
+#ifdef BLKGETSIZE64
+ DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
+ if (!ioctl(fhandle, BLKGETSIZE64, bytes) && bytes) {
+ return (off_t)bytes;
}
-#endif /* BLKGETSIZE */
+#endif /* BLKGETSIZE64 */
#endif /* HAVE_SYS_IOCTL_H */
#endif /* HAVE_SYS_MOUNT_H */
if(maxbytes && len > maxbytes)
len = maxbytes;
- DEBUG4("(WRITE to fd %d offset %Lu len %u), ", fhandle, foffset, len);
+ DEBUG4("(WRITE to fd %d offset %llu len %u), ", fhandle, foffset, len);
myseek(fhandle, foffset);
return write(fhandle, buf, len);
* @return 0 on success, nonzero on failure
**/
int rawexpwrite_fully(off_t a, char *buf, size_t len, CLIENT *client) {
- ssize_t ret;
+ ssize_t ret=0;
while(len > 0 && (ret=rawexpwrite(a, buf, len, client)) > 0 ) {
a += ret;
if(maxbytes && len > maxbytes)
len = maxbytes;
- DEBUG4("(READ from fd %d offset %Lu len %u), ", fhandle, foffset, len);
+ DEBUG4("(READ from fd %d offset %llu len %u), ", fhandle, foffset, len);
myseek(fhandle, foffset);
return read(fhandle, buf, len);
* @return 0 on success, nonzero on failure
**/
int rawexpread_fully(off_t a, char *buf, size_t len, CLIENT *client) {
- ssize_t ret;
+ ssize_t ret=0;
while(len > 0 && (ret=rawexpread(a, buf, len, client)) > 0 ) {
a += ret;
if (!(client->server->flags & F_COPYONWRITE))
return(rawexpread_fully(a, buf, len, client));
- DEBUG3("Asked to read %d bytes at %Lu.\n", len, (unsigned long long)a);
+ DEBUG3("Asked to read %d bytes at %llu.\n", len, (unsigned long long)a);
mapl=a/DIFFPAGESIZE; maph=(a+len-1)/DIFFPAGESIZE;
rdlen=(0<DIFFPAGESIZE-offset && len<(size_t)(DIFFPAGESIZE-offset)) ?
len : (size_t)DIFFPAGESIZE-offset;
if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
- DEBUG3("Page %Lu is at %lu\n", (unsigned long long)mapcnt,
+ DEBUG3("Page %llu is at %lu\n", (unsigned long long)mapcnt,
(unsigned long)(client->difmap[mapcnt]));
myseek(client->difffile, client->difmap[mapcnt]*DIFFPAGESIZE+offset);
if (read(client->difffile, buf, rdlen) != rdlen) return -1;
} else { /* the block is not there */
- DEBUG2("Page %Lu is not here, we read the original one\n",
+ DEBUG2("Page %llu is not here, we read the original one\n",
(unsigned long long)mapcnt);
if(rawexpread_fully(a, buf, rdlen, client)) return -1;
}
if (!(client->server->flags & F_COPYONWRITE))
return(rawexpwrite_fully(a, buf, len, client));
- DEBUG3("Asked to write %d bytes at %Lu.\n", len, (unsigned long long)a);
+ DEBUG3("Asked to write %d bytes at %llu.\n", len, (unsigned long long)a);
mapl=a/DIFFPAGESIZE ; maph=(a+len-1)/DIFFPAGESIZE ;
len : (size_t)DIFFPAGESIZE-offset;
if (client->difmap[mapcnt]!=(u32)(-1)) { /* the block is already there */
- DEBUG3("Page %Lu is at %lu\n", (unsigned long long)mapcnt,
+ DEBUG3("Page %llu is at %lu\n", (unsigned long long)mapcnt,
(unsigned long)(client->difmap[mapcnt])) ;
myseek(client->difffile,
client->difmap[mapcnt]*DIFFPAGESIZE+offset);
} else { /* the block is not there */
myseek(client->difffile,client->difffilelen*DIFFPAGESIZE) ;
client->difmap[mapcnt]=(client->server->flags&F_SPARSE)?mapcnt:client->difffilelen++;
- DEBUG3("Page %Lu is not here, we put it at %lu\n",
+ DEBUG3("Page %llu is not here, we put it at %lu\n",
(unsigned long long)mapcnt,
(unsigned long)(client->difmap[mapcnt]));
rdlen=DIFFPAGESIZE ;
i++;
printf("%d: ", i);
#endif
- if (client->server->timeout)
- alarm(client->server->timeout);
readit(client->net, &request, sizeof(request));
request.from = ntohll(request.from);
request.type = ntohl(request.type);
if (len > BUFSIZE + sizeof(struct nbd_reply))
err("Request too big!");
#ifdef DODBG
- printf("%s from %Lu (%Lu) len %d, ", request.type ? "WRITE" :
+ printf("%s from %llu (%llu) len %d, ", request.type ? "WRITE" :
"READ", (unsigned long long)request.from,
(unsigned long long)request.from / 512, len);
#endif
/* Try again because maybe media was read-only */
fi.fhandle = open(tmpname, O_RDONLY);
if(fi.fhandle != -1) {
- client->server->flags |= F_AUTOREADONLY;
- client->server->flags |= F_READONLY;
+ /* Opening the base file in copyonwrite mode is
+ * okay */
+ if(!(client->server->flags & F_COPYONWRITE)) {
+ client->server->flags |= F_AUTOREADONLY;
+ client->server->flags |= F_READONLY;
+ }
}
}
if(fi.fhandle == -1) {
client->exportsize = client->server->expected_size;
}
- msg3(LOG_INFO, "Size of exported file/device is %Lu", (unsigned long long)client->exportsize);
+ msg3(LOG_INFO, "Size of exported file/device is %llu", (unsigned long long)client->exportsize);
if(multifile) {
msg3(LOG_INFO, "Total number of files: %d", i);
}
int sock;
fd_set mset;
fd_set rset;
- struct timeval tv;
/*
* Set up the master fd_set. The set of descriptors we need
pid_t *pid;
memcpy(&rset, &mset, sizeof(fd_set));
- tv.tv_sec=0;
- tv.tv_usec=500;
- if(select(max+1, &rset, NULL, NULL, &tv)>0) {
+ if(select(max+1, &rset, NULL, NULL, NULL)>0) {
DEBUG("accept, ");
for(i=0;i<servers->len;i++) {
serve=&(g_array_index(servers, SERVER, i));
if(FD_ISSET(serve->socket, &rset)) {
+ int sock_flags;
if ((net=accept(serve->socket, (struct sockaddr *) &addrin, &addrinlen)) < 0)
err("accept: %m");
+ if((sock_flags = fcntl(net, F_GETFL, 0))==-1) {
+ err("fcntl F_GETFL");
+ }
+ if(fcntl(net, F_SETFL, sock_flags &~O_NONBLOCK)==-1) {
+ err("fcntl F_SETFL ~O_NONBLOCK");
+ }
client = g_malloc(sizeof(CLIENT));
client->server=serve;
client->exportsize=OFFT_MAX;
/* child */
g_hash_table_destroy(children);
for(i=0;i<servers->len;i++) {
- serve=g_array_index(servers, SERVER*, i);
+ serve=&g_array_index(servers, SERVER, i);
close(serve->socket);
}
/* FALSE does not free the
}
if(!*pidftemplate) {
if(serve) {
- strncpy(pidftemplate, "/var/run/server.%d.pid", 255);
+ strncpy(pidftemplate, "/var/run/nbd-server.%d.pid", 255);
} else {
- strncpy(pidftemplate, "/var/run/server.pid", 255);
+ strncpy(pidftemplate, "/var/run/nbd-server.pid", 255);
}
}
snprintf(pidfname, 255, pidftemplate, serve ? serve->port : 0);
}
}
+#ifndef ISSERVER
+void glib_message_syslog_redirect(const gchar *log_domain,
+ GLogLevelFlags log_level,
+ const gchar *message,
+ gpointer user_data)
+{
+ int level=LOG_DEBUG;
+
+ switch( log_level )
+ {
+ case G_LOG_FLAG_FATAL:
+ case G_LOG_LEVEL_CRITICAL:
+ case G_LOG_LEVEL_ERROR:
+ level=LOG_ERR;
+ break;
+ case G_LOG_LEVEL_WARNING:
+ level=LOG_WARNING;
+ break;
+ case G_LOG_LEVEL_MESSAGE:
+ case G_LOG_LEVEL_INFO:
+ level=LOG_INFO;
+ break;
+ case G_LOG_LEVEL_DEBUG:
+ level=LOG_DEBUG;
+ }
+ syslog(level, message);
+}
+#endif
+
/**
* Main entry point...
**/
if (sizeof( struct nbd_request )!=28) {
fprintf(stderr,"Bad size of structure. Alignment problems?\n");
- exit(-1) ;
+ exit(EXIT_FAILURE) ;
}
memset(pidftemplate, '\0', 256);
config_file_pos = g_strdup(CFILE);
serve=cmdline(argc, argv);
servers = parse_cfile(config_file_pos, &err);
- if(!servers || !servers->len) {
- g_warning("Could not parse config file: %s",
- err ? err->message : "Unknown error");
- }
+
if(serve) {
g_array_append_val(servers, *serve);
close(2);
open("/dev/null", O_WRONLY);
open("/dev/null", O_WRONLY);
+ g_log_set_default_handler( glib_message_syslog_redirect, NULL );
#endif
client=g_malloc(sizeof(CLIENT));
client->server=serve;
return 0;
}
}
+
+ if(!servers || !servers->len) {
+ g_warning("Could not parse config file: %s",
+ err ? err->message : "Unknown error");
+ }
+
if((!serve) && (!servers||!servers->len)) {
g_message("Nothing to do! Bye!");
exit(EXIT_FAILURE);