r74: * Added checks for GLib (not used yet, but will be in this release)
[nbd.git] / nbd-server.c
index 7c8b2c0..58050bc 100644 (file)
@@ -54,6 +54,7 @@
 /* Includes LFS defines, which defines behaviours of some of the following
  * headers, so must come before those */
 #include "config.h"
+#include "lfs.h"
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -80,6 +81,8 @@
 #include <strings.h>
 #include <dirent.h>
 
+#include <glib.h>
+
 /* used in cliserv.h, so must come first */
 #define MY_NAME "nbd_server"
 #include "cliserv.h"
 
 /** Logging macros, now nothing goes to syslog unless you say ISSERVER */
 #ifdef ISSERVER
-#define msg2(a,b) syslog(a,b)
-#define msg3(a,b,c) syslog(a,b,c)
-#define msg4(a,b,c,d) syslog(a,b,c,d)
+#define msg2(a,b) syslog(a,"%s", b)
+#define msg3(a,b,c) syslog(a,"%s %s", b,c)
+#define msg4(a,b,c,d) syslog(a,"%s %s %s", b,c,d)
 #else
-#define msg2(a,b) do { fprintf(stderr,b) ; fputs("\n",stderr) ; } while(0) 
-#define msg3(a,b,c) do { fprintf(stderr,b,c); fputs("\n",stderr) ; } while(0) 
-#define msg4(a,b,c,d) do { fprintf(stderr,b,c,d); fputs("\n",stderr) ; } while(0)
+#define msg2(a,b) do { fprintf(stderr,"%s\n", b) ; } while(0) 
+#define msg3(a,b,c) do { fprintf(stderr,"%s %s\n", b,c); } while(0) 
+#define msg4(a,b,c,d) do { fprintf(stderr,"%s %s %s\n", b,c,d); } while(0)
 #endif
 
 /* Debugging macros */
 #define F_READONLY 1      /**< flag to tell us a file is readonly */
 #define F_MULTIFILE 2    /**< flag to tell us a file is exported using -m */
 #define F_COPYONWRITE 4          /**< flag to tell us a file is exported using copyonwrite */
-char difffilename[256]; /**< filename of the copy-on-write file. Doesn't belong here! */
+char difffilename[1024]; /**< filename of the copy-on-write file. Doesn't belong here! */
 unsigned int timeout = 0; /**< disconnect timeout */
 int autoreadonly = 0; /**< 1 = switch to readonly if opening readwrite isn't
                        possible */
@@ -354,15 +357,20 @@ void sigchld_handler(int s)
 {
         int* status=NULL;
        int i;
+       char buf[80];
        pid_t pid;
 
        while((pid=wait(status)) > 0) {
                if(WIFEXITED(status)) {
-                       msg3(LOG_INFO, "Child exited with %d", WEXITSTATUS(status));
+                       memset(buf,'\0', 80);
+                       snprintf(buf, 79, "%d", WEXITSTATUS(status));
+                       msg3(LOG_INFO, "Child exited with ", buf);
                }
                for(i=0;children[i]!=pid&&i<child_arraysize;i++);
                if(i>=child_arraysize) {
-                       msg3(LOG_INFO, "SIGCHLD received for an unknown child with PID %ld",(long) pid);
+                       memset(buf, '\0', 80);
+                       snprintf(buf, 79, "%ld", (long)pid);
+                       msg3(LOG_INFO, "SIGCHLD received for an unknown child with PID ", buf);
                } else {
                        children[i]=(pid_t)0;
                        DEBUG2("Removing %d from the list of children", pid);
@@ -735,11 +743,16 @@ int mainloop(int net)
  **/
 int splitexport(void) {
        off_t i ;
-       
+
        for (i=0; i<exportsize; i+=hunksize) {
                char exportname3[1024];
-               
-               snprintf(exportname3, 1024, "%s.%d", exportname2, (int)i/hunksize);
+
+               if(flags & F_MULTIFILE) {
+                       snprintf(exportname3, 1024, "%s.%d", exportname2, (int)(i/hunksize));
+               } else {
+                       strncpy(exportname3, exportname2, 1024);
+               }
+               exportname3[1023]='\0';
                printf( "Opening %s\n", exportname3 );
                if ((export[i/hunksize] = open(exportname3, (flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1) {
                        /* Read WRITE ACCESS was requested by media is only read only */
@@ -751,8 +764,9 @@ int splitexport(void) {
        }
 
        if (flags & F_COPYONWRITE) {
-               snprintf(difffilename, 256, "%s-%s-%d.diff",exportname2,clientname,
+               snprintf(difffilename, 1024, "%s-%s-%d.diff",exportname2,clientname,
                        (int)getpid()) ;
+               difffilename[1023]='\0';
                msg3(LOG_INFO,"About to create map and diff file %s",difffilename) ;
                difffile=open(difffilename,O_RDWR | O_CREAT | O_TRUNC,0600) ;
                if (difffile<0) err("Could not create diff file (%m)") ;
@@ -771,7 +785,8 @@ int splitexport(void) {
  *
  * @param net A network socket connected to an nbd client
  **/
-void serveconnection(int net) {   
+void serveconnection(int net) {
+       char buf[80];
        splitexport();
        if (exportsize == OFFT_MAX) {
                exportsize = size_autodetect(export[0]);
@@ -779,9 +794,11 @@ void serveconnection(int net) {
        if (exportsize > OFFT_MAX) {
                err("Size of exported file is too big\n");
        }
-       else
-               msg3(LOG_INFO, "size of exported file/device is %Lu",
-                    (unsigned long long)exportsize);
+       else {
+               memset(buf, '\0', 80);
+               snprintf(buf, 79, "%Lu", (unsigned long long)exportsize);
+               msg3(LOG_INFO, "size of exported file/device is ", buf);
+       }
 
        setmysockopt(net);
 
@@ -807,6 +824,7 @@ void set_peername(int net,char *clientname)
                err("getsockname failed: %m");
        peername = inet_ntoa(addrin.sin_addr);
        snprintf(exportname2, 1024, exportname, peername);
+       exportname2[1023]='\0';
 
        msg4(LOG_INFO, "connect from %s, assigned file is %s", 
             peername, exportname2);