r264: Fix segfault
[nbd.git] / nbd-server.c
index 4ba40aa..e6ff422 100644 (file)
@@ -295,33 +295,12 @@ int authorized_client(CLIENT *opts) {
  **/
 inline void readit(int f, void *buf, size_t len) {
        ssize_t res;
-       gboolean tried = FALSE;
-
        while (len > 0) {
                DEBUG("*");
-               if ((res = read(f, buf, len)) <= 0) {
-                       if(!tried && errno==EAGAIN) {
-                               /* Assume the connection will work some time in
-                                * the future, but don't run away with CPU time
-                                * in case it doesn't */
-                               fd_set set;
-                               struct timeval tv;
-
-                               DEBUG("Read failed, trying again");
-                               tried=TRUE;
-                               FD_ZERO(&set);
-                               FD_SET(f, &set);
-                               tv.tv_sec=30;
-                               tv.tv_usec=0;
-                               select(f+1, &set, NULL, NULL, &tv);
-                       } else {
-                               err("Read failed: %m");
-                       }
-               } else {
-                       len -= res;
-                       buf += res;
-                       tried=FALSE;
-               }
+               if ((res = read(f, buf, len)) <= 0)
+                       err("Read failed: %m");
+               len -= res;
+               buf += res;
        }
 }
 
@@ -334,33 +313,12 @@ inline void readit(int f, void *buf, size_t len) {
  **/
 inline void writeit(int f, void *buf, size_t len) {
        ssize_t res;
-       gboolean tried=FALSE;
-
        while (len > 0) {
                DEBUG("+");
-               if ((res = write(f, buf, len)) <= 0) {
-                       if(!tried && errno==EAGAIN) {
-                               /* Assume the connection will work some time in
-                                * the future, but don't run away with CPU time
-                                * in case it doesn't */
-                               fd_set set;
-                               struct timeval tv;
-
-                               DEBUG("Write failed, trying again");
-                               tried=TRUE;
-                               FD_ZERO(&set);
-                               FD_SET(f, &set);
-                               tv.tv_sec=30;
-                               tv.tv_usec=0;
-                               select(f+1, NULL, &set, NULL, &tv);
-                       } else {
-                               err("Send failed: %m");
-                       }
-               } else {
-                       len -= res;
-                       buf += res;
-                       tried=FALSE;
-               }
+               if ((res = write(f, buf, len)) <= 0)
+                       err("Send failed: %m");
+               len -= res;
+               buf += res;
        }
 }
 
@@ -370,20 +328,47 @@ inline void writeit(int f, void *buf, size_t len) {
  */
 void usage() {
        printf("This is nbd-server version " VERSION "\n");
-       printf("Usage: port file_to_export [size][kKmM] [-l authorize_file] [-r] [-m] [-c] [-a timeout_sec] [-C configuration file] [-p PID file name]\n"
+       printf("Usage: 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"
               "\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"
               "\t-C|--config-file\tspecify an alternate configuration file\n"
               "\t-l|--authorize-file\tfile with list of hosts that are allowed to\n\t\t\t\tconnect.\n"
               "\t-a|--idle-time\t\tmaximum idle seconds; server terminates when\n\t\t\t\tidle time exceeded\n"
-              "\t-p|--pid-file\t\tspecify a filename to write our PID to\n\n"
+              "\t-p|--pid-file\t\tspecify a filename to write our PID to\n"
+              "\t-o|--output-config\toutput a config file section for what you\n\t\t\t\tspecified on the command line, with the\n\t\t\t\tspecified section name\n\n"
               "\tif port is set to 0, stdin is used (for running from inetd)\n"
               "\tif file_to_export contains '%%s', it is substituted with the IP\n"
               "\t\taddress of the machine trying to connect\n" );
        printf("Using configuration file %s\n", CFILE);
 }
 
+/* Dumps a config file section of the given SERVER*, and exits. */
+void dump_section(SERVER* serve, gchar* section_header) {
+       printf("[%s]\n", section_header);
+       printf("\texportname = %s\n", serve->exportname);
+       printf("\tport = %d\n", serve->port);
+       if(serve->flags & F_READONLY) {
+               printf("\treadonly = true\n");
+       }
+       if(serve->flags & F_MULTIFILE) {
+               printf("\tmultifile = true\n");
+       }
+       if(serve->flags & F_COPYONWRITE) {
+               printf("\tcopyonwrite = true\n");
+       }
+       if(serve->expected_size) {
+               printf("\tfilesize = %Ld\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);
+}
+
 /**
  * Parse the command line.
  *
@@ -402,19 +387,22 @@ SERVER* cmdline(int argc, char *argv[]) {
                {"idle-time", required_argument, NULL, 'a'},
                {"config-file", required_argument, NULL, 'C'},
                {"pid-file", required_argument, NULL, 'p'},
+               {"output-config", required_argument, NULL, 'o'},
                {0,0,0,0}
        };
        SERVER *serve;
        off_t es;
        size_t last;
        char suffix;
+       gboolean do_output=FALSE;
+       gchar* section_header;
 
        if(argc==1) {
                return NULL;
        }
        serve=g_new0(SERVER, 1);
        serve->authname = g_strdup(default_authname);
-       while((c=getopt_long(argc, argv, "-a:C:cl:mrp:", long_options, &i))>=0) {
+       while((c=getopt_long(argc, argv, "-a:C:cl:mo:rp:", long_options, &i))>=0) {
                switch (c) {
                case 1:
                        /* non-option argument */
@@ -453,6 +441,10 @@ SERVER* cmdline(int argc, char *argv[]) {
                case 'm':
                        serve->flags |= F_MULTIFILE;
                        break;
+               case 'o':
+                       do_output = TRUE;
+                       section_header = g_strdup(optarg);
+                       break;
                case 'p':
                        strncpy(pidftemplate, optarg, 256);
                        break;
@@ -482,6 +474,13 @@ SERVER* cmdline(int argc, char *argv[]) {
                g_free(serve);
                serve=NULL;
        }
+       if(do_output) {
+               if(!serve) {
+                       g_critical("Need a complete configuration on the command line to output a config file section!");
+                       exit(EXIT_FAILURE);
+               }
+               dump_section(serve, section_header);
+       }
        return serve;
 }
 
@@ -1299,10 +1298,10 @@ void set_peername(int net, CLIENT *client) {
                        break;
        }
 
-       g_free(peername);
        msg4(LOG_INFO, "connect from %s, assigned file is %s", 
             peername, client->exportname);
        client->clientname=g_strdup(peername);
+       g_free(peername);
 }
 
 /**
@@ -1324,6 +1323,9 @@ void destroy_pid_t(gpointer data) {
 void daemonize(SERVER* serve) {
        FILE*pidf;
 
+       if(!(serve->port)) {
+               return;
+       }
        if(daemon(0,0)<0) {
                err("daemon");
        }
@@ -1379,7 +1381,7 @@ void setup_serve(SERVER *serve) {
                err("fcntl F_GETFL");
        }
        if (fcntl(serve->socket, F_SETFL, sock_flags | O_NONBLOCK) == -1) {
-               err("fcntl F_SETFL O_NONBLOCK on server socket");
+               err("fcntl F_SETFL O_NONBLOCK");
        }
 
        DEBUG("Waiting for connections... bind, ");
@@ -1456,20 +1458,12 @@ int serveloop(GArray* servers) {
                        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");
 
                                        client = g_malloc(sizeof(CLIENT));
                                        client->server=serve;
                                        client->exportsize=OFFT_MAX;
-                                       if ((sock_flags = fcntl(serve->socket, F_GETFL, 0)) == -1) {
-                                               err("fcntl F_GETFL");
-                                       }
-                                       if (fcntl(net, F_SETFL, sock_flags | O_NONBLOCK) == -1) {
-                                               err("fcntl F_SETFL O_NONBLOCK on client socket");
-                                       }
                                        client->net=net;
                                        set_peername(net, client);
                                        if (!authorized_client(client)) {
@@ -1553,31 +1547,28 @@ int main(int argc, char *argv[]) {
        }
        if(serve) {
                g_array_append_val(servers, *serve);
-       }
-
-/* We don't support this at this time */
-#if 0
-       if (!(serve->port)) {
-               CLIENT *client;
+     
+               if (!(serve->port)) {
+                       CLIENT *client;
 #ifndef ISSERVER
-               /* You really should define ISSERVER if you're going to use
-                * inetd mode, but if you don't, closing stdout and stderr
-                * (which inetd had connected to the client socket) will let it
-                * work. */
-               close(1);
-               close(2);
-               open("/dev/null", O_WRONLY);
-               open("/dev/null", O_WRONLY);
-#endif
-               client=g_malloc(sizeof(CLIENT));
-               client->server=serve;
-               client->net=0;
-               client->exportsize=OFFT_MAX;
-               set_peername(0,client);
-               serveconnection(client);
-               return 0;
-        }
+                       /* You really should define ISSERVER if you're going to use
+                        * inetd mode, but if you don't, closing stdout and stderr
+                        * (which inetd had connected to the client socket) will let it
+                        * work. */
+                       close(1);
+                       close(2);
+                       open("/dev/null", O_WRONLY);
+                       open("/dev/null", O_WRONLY);
 #endif
+                       client=g_malloc(sizeof(CLIENT));
+                       client->server=serve;
+                       client->net=0;
+                       client->exportsize=OFFT_MAX;
+                       set_peername(0,client);
+                       serveconnection(client);
+                       return 0;
+               }
+       }
        if((!serve) && (!servers||!servers->len)) {
                g_message("Nothing to do! Bye!");
                exit(EXIT_FAILURE);