Imported Upstream version 2.5.1p1
[debian/amanda] / server-src / amindexd.c
index 96e2483a575360b7c0c5e32e8aabfdfde10e0311..34587d46eda991f76be13c0259de05df1cdf3f19 100644 (file)
@@ -24,7 +24,7 @@
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: amindexd.c,v 1.106 2006/07/25 18:27:57 martinea Exp $
+ * $Id: amindexd.c,v 1.106.2.2 2006/09/27 12:04:09 martinea Exp $
  *
  * This is the server daemon part of the index client/server system.
  * It is assumed that this is launched from inetd instead of being
@@ -92,6 +92,8 @@ static int process_ls_dump(char *, DUMP_ITEM *, int, char **);
 static size_t reply_buffer_size = 1;
 static char *reply_buffer = NULL;
 static char *amandad_auth = NULL;
+static FILE *cmdin;
+static FILE *cmdout;
 
 static void reply(int, char *, ...)
     __attribute__ ((format (printf, 2, 3)));
@@ -295,7 +297,7 @@ printf_arglist_function1(static void reply, int, n, char *, fmt)
        len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
        arglist_end(args);
 
-       if (len > -1 && (size_t)len < reply_buffer_size)
+       if (len > -1 && (size_t)len < reply_buffer_size-1)
            break;
 
        reply_buffer_size *= 2;
@@ -303,14 +305,14 @@ printf_arglist_function1(static void reply, int, n, char *, fmt)
        reply_buffer = alloc(reply_buffer_size);
     }
 
-    if (printf("%03d %s\r\n", n, reply_buffer) < 0)
+    if (fprintf(cmdout,"%03d %s\r\n", n, reply_buffer) < 0)
     {
        dbprintf(("%s: ! error %d (%s) in printf\n",
                  debug_prefix_time(NULL), errno, strerror(errno)));
        uncompress_remove = remove_files(uncompress_remove);
        exit(1);
     }
-    if (fflush(stdout) != 0)
+    if (fflush(cmdout) != 0)
     {
        dbprintf(("%s: ! error %d (%s) in fflush\n",
                  debug_prefix_time(NULL), errno, strerror(errno)));
@@ -334,7 +336,7 @@ printf_arglist_function1(static void lreply, int, n, char *, fmt)
        len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
        arglist_end(args);
 
-       if (len > -1 && (size_t)len < reply_buffer_size)
+       if (len > -1 && (size_t)len < reply_buffer_size-1)
            break;
 
        reply_buffer_size *= 2;
@@ -342,14 +344,14 @@ printf_arglist_function1(static void lreply, int, n, char *, fmt)
        reply_buffer = alloc(reply_buffer_size);
     }
 
-    if (printf("%03d-%s\r\n", n, reply_buffer) < 0)
+    if (fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
     {
        dbprintf(("%s: ! error %d (%s) in printf\n",
                  debug_prefix_time(NULL), errno, strerror(errno)));
        uncompress_remove = remove_files(uncompress_remove);
        exit(1);
     }
-    if (fflush(stdout) != 0)
+    if (fflush(cmdout) != 0)
     {
        dbprintf(("%s: ! error %d (%s) in fflush\n",
                  debug_prefix_time(NULL), errno, strerror(errno)));
@@ -375,7 +377,7 @@ printf_arglist_function1(static void fast_lreply, int, n, char *, fmt)
        len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
        arglist_end(args);
 
-       if (len > -1 && (size_t)len < reply_buffer_size)
+       if (len > -1 && (size_t)len < reply_buffer_size-1)
            break;
 
        reply_buffer_size *= 2;
@@ -383,7 +385,7 @@ printf_arglist_function1(static void fast_lreply, int, n, char *, fmt)
        reply_buffer = alloc(reply_buffer_size);
     }
 
-    if (printf("%03d-%s\r\n", n, reply_buffer) < 0)
+    if (fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
     {
        dbprintf(("%s: ! error %d (%s) in printf\n",
                  debug_prefix_time(NULL), errno, strerror(errno)));
@@ -1177,6 +1179,8 @@ main(
        remote_hostname = newstralloc(remote_hostname, fp);
        s[-1] = (char)ch;
        amfree(fp);
+       cmdout = stdout;
+       cmdin = stdin;
     }
     else {
        cmdfdout  = DATA_FD_OFFSET + 0;
@@ -1211,9 +1215,18 @@ main(
        printf("\n");
        fflush(stdin);
        fflush(stdout);
-       if ((dup2(cmdfdout, fileno(stdout)) < 0)
-                || (dup2(cmdfdin, fileno(stdin)) < 0)) {
-           error("amandad: Failed to setup stdin or stdout");
+       fclose(stdin);
+       fclose(stdout);
+       
+       cmdout = fdopen(cmdfdout, "a");
+       if (!cmdout) {
+           error("amindexd: Can't fdopen(cmdfdout): %s", strerror(errno));
+           /*NOTREACHED*/
+       }
+
+       cmdin = fdopen(cmdfdin, "r");
+       if (!cmdin) {
+           error("amindexd: Can't fdopen(cmdfdin): %s", strerror(errno));
            /*NOTREACHED*/
        }
     }
@@ -1241,7 +1254,7 @@ main(
     {
        /* get a line from the client */
        while(1) {
-           if((part = agets(stdin)) == NULL) {
+           if((part = agets(cmdin)) == NULL) {
                if(errno != 0) {
                    dbprintf(("%s: ? read error: %s\n",
                              debug_prefix_time(NULL), strerror(errno)));