[Initng-svn] r2326 - in initng: plugins/history plugins/interactive plugins/ngc2 plugins/pidfile plugins/reload plugins/renice tools

svn at initng.thinktux.net svn at initng.thinktux.net
Sun Dec 4 22:08:57 CET 2005


Author: jimmy
Date: Sun Dec  4 22:08:56 2005
New Revision: 2326

Modified:
   initng/plugins/history/initng_history.c
   initng/plugins/interactive/initng_interactive.c
   initng/plugins/ngc2/initng_ngc2.c
   initng/plugins/ngc2/ngc2.c
   initng/plugins/pidfile/initng_pidfile.c
   initng/plugins/reload/initng_reload.c
   initng/plugins/renice/initng_renice.c
   initng/tools/install_service.c
   initng/tools/killall5.c
Log:
Fixed warnings detected by -D_FORTIFY_SOURCE=2. Basically, this adds
errorhandling for functions like fwrite() or fgets().

write(2) and read(2) IO primitives are wrapped into a TEMP_FAILURE_RETRY
to handle EINTR/EAGAIN properly.

The error-handling in plugins/ngc2/initng_ngc2.c is solved very
simple by this patch; a better way might be a wrapper function
around 'fwrite(3)'.

Bug #315

Tnx Enrico


Modified: initng/plugins/history/initng_history.c
==============================================================================
--- initng/plugins/history/initng_history.c	(original)
+++ initng/plugins/history/initng_history.c	Sun Dec  4 22:08:56 2005
@@ -26,7 +26,7 @@
 #include <time.h>
 #include <stdio.h>                          /* printf() */
 #include <assert.h>
-
+#include <errno.h>
 
 #include "initng_history.h"
 #include "../../src/initng_global.h"
@@ -319,7 +319,8 @@
         else
             row.n[0] = '\0';
 
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("Failed to write all data\n");
     }
 }
 

Modified: initng/plugins/interactive/initng_interactive.c
==============================================================================
--- initng/plugins/interactive/initng_interactive.c	(original)
+++ initng/plugins/interactive/initng_interactive.c	Sun Dec  4 22:08:56 2005
@@ -46,7 +46,10 @@
     {
 
         fprintf(stderr, "Start service %s, (Y/n/a):", service->name);
-        fgets(asw, 9, stdin);
+        /* HACK: ignore read errors by assuming 'n' */
+        if (fgets(asw, 9, stdin) == NULL)
+            asw[0] = 'n';
+
         /* if it is true, then its ok to launch service */
         if (asw[0] == 'y' || asw[0] == 'Y')
             return (TRUE);
@@ -66,7 +69,10 @@
     {
 
         fprintf(stderr, "Stop service %s, (Y/n/a):", service->name);
-        fgets(asw, 9, stdin);
+        /* HACK: ignore read errors by assuming 'n' */
+        if (fgets(asw, 9, stdin) == NULL)
+            asw[0] = 'n';
+
         if (asw[0] == 'y' || asw[0] == 'Y')
             return (TRUE);
 

Modified: initng/plugins/ngc2/initng_ngc2.c
==============================================================================
--- initng/plugins/ngc2/initng_ngc2.c	(original)
+++ initng/plugins/ngc2/initng_ngc2.c	Sun Dec  4 22:08:56 2005
@@ -179,7 +179,8 @@
         result.t = 0;
         result.s = S_TRUE;
         D_("Ping received, sending pong\n");
-        fwrite(&result, sizeof(result), 1, file_sock);
+        if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -198,7 +199,8 @@
         result.c = header.c;
         result.t = 0;
         result.s = S_COMMAND_NOT_FOUND;
-        fwrite(&result, sizeof(result), 1, file_sock);
+        if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -211,7 +213,8 @@
         result.c = header.c;
         result.t = 0;
         result.s = S_REQUIRES_OPT;
-        fwrite(&result, sizeof(result), 1, file_sock);
+        if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -222,7 +225,8 @@
         result.c = header.c;
         result.t = 0;
         result.s = S_NOT_REQUIRES_OPT;
-        fwrite(&result, sizeof(result), 1, file_sock);
+        if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -243,7 +247,9 @@
 
                 /* Write a header respond */
                 result.s = S_TRUE;
-                fwrite(&result, sizeof(result), 1, file_sock);
+                if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                    F_("failed to send all data\n");
+                /* TODO: really continue?? */
 
                 /* execute command */
                 ret = (int) (*tmp_cmd->u.int_command_call) (header.o);
@@ -261,7 +267,9 @@
 
                 /* write an header respond */
                 result.s = S_TRUE;
-                fwrite(&result, sizeof(result), 1, file_sock);
+                if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                    F_("failed to send all data\n");
+                /* TODO: really continue?? */
 
                 /* execute command */
                 send_buf = (*tmp_cmd->u.string_command_call) (header.o);
@@ -282,7 +290,9 @@
 
                 /* write an header respond */
                 result.s = S_TRUE;
-                fwrite(&result, sizeof(result), 1, file_sock);
+                if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                    F_("failed to send all data\n");
+                /* TODO: really continue?? */
 
                 /* execute command */
                 (*tmp_cmd->u.print_command_call) (header.o, file_sock);
@@ -296,7 +306,9 @@
 
                 /* write an header respond */
                 result.s = S_TRUE;
-                fwrite(&result, sizeof(result), 1, file_sock);
+                if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                    F_("failed to send all data\n");
+                /* TODO: really continue?? */
 
                 (*tmp_cmd->u.void_command_call) (header.o);
                 break;
@@ -308,7 +320,9 @@
 
                 /* write an header respond */
                 result.s = S_TRUE;
-                fwrite(&result, sizeof(result), 1, file_sock);
+                if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                    F_("failed to send all data\n");
+                /* TODO: really continue?? */
 
                 /* execute command */
                 (*tmp_cmd->u.print_command_call) (header.o, file_sock);
@@ -318,7 +332,9 @@
 
             /* return FAIL header respond */
             result.s = S_INVALID_TYPE;
-            fwrite(&result, sizeof(result), 1, file_sock);
+            if (fwrite(&result, sizeof(result), 1, file_sock) != 1)
+                F_("failed to send all data\n");
+            /* TODO: really continue?? */
 
             F_("Invalid command type '%c', ling '%s', option : \"%s\"",
                header.c, header.l, header.o);
@@ -626,7 +642,11 @@
         row.o = current->opt_type;
         strncpy(row.d, current->description, 100);
         strncpy(row.l, current->long_id, 100);
-        fwrite(&row, sizeof(help_row), 1, fd);
+        if (fwrite(&row, sizeof(help_row), 1, fd) != 1)
+        {
+            F_("failed to send all data\n");
+            return;
+        }
     }
 }
 
@@ -642,7 +662,8 @@
         strcpy(row.s, "NOT_FOUND");
         row.i = IS_FAILED;
         row.t = 0;
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -653,7 +674,8 @@
         strcpy(row.s, "NOT_FOUND");
         row.i = IS_FAILED;
         row.t = 0;
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -671,7 +693,8 @@
         row.s[0] = '\0';
     }
 
-    fwrite(&row, sizeof(active_row), 1, fd);
+    if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+        F_("failed to send all data\n");
 }
 
 static void cmd_stop(char *arg, FILE * fd)
@@ -686,7 +709,8 @@
         strcpy(row.s, "NOT_FOUND");
         row.i = IS_FAILED;
         row.t = 0;
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -697,7 +721,8 @@
         strcpy(row.s, "NOT_FOUND");
         row.i = IS_FAILED;
         row.t = 0;
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -717,7 +742,8 @@
         row.s[0] = '\0';
     }
 
-    fwrite(&row, sizeof(active_row), 1, fd);
+    if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+        F_("failed to send all data\n");
 }
 
 
@@ -748,7 +774,8 @@
             strncpy(row.o, current->ot->name, 100);
         }
 
-        fwrite(&row, sizeof(option_row), 1, fd);
+        if (fwrite(&row, sizeof(option_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -771,8 +798,8 @@
             strcpy(row.o, "all");
 
         /* D_("sending option: %s\n", row.n); */
-        fwrite(&row, sizeof(option_row), 1, fd);
-
+        if (fwrite(&row, sizeof(option_row), 1, fd) != 1)
+            F_("failed to send all data\n");
     }
 
 }
@@ -808,7 +835,8 @@
             row.t = current->time_got_status;
             strncpy(row.n, current->name, 100);
         }
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
         return;
     }
 
@@ -828,7 +856,8 @@
         }
         row.t = current->time_got_status;
         strncpy(row.n, current->name, 100);
-        fwrite(&row, sizeof(active_row), 1, fd);
+        if (fwrite(&row, sizeof(active_row), 1, fd) != 1)
+            F_("failed to send all data\n");
     }
 }
 

Modified: initng/plugins/ngc2/ngc2.c
==============================================================================
--- initng/plugins/ngc2/ngc2.c	(original)
+++ initng/plugins/ngc2/ngc2.c	Sun Dec  4 22:08:56 2005
@@ -32,7 +32,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <assert.h>
-
+#include <errno.h>
 
 /* #include "ngc.h" */
 #include "../../src/initng.h"
@@ -191,7 +191,12 @@
     result = calloc(1, sizeof(result_desc));
 
     /* read header data */
-    read(sock, result, sizeof(result_desc));
+    if (TEMP_FAILURE_RETRY(read(sock, result, sizeof(result_desc))) !=
+        sizeof(*result))
+    {
+        printf("failed to fetch the result\n");
+        exit(1);
+    }
 
     /* check that protocol matches */
     if (result->p_ver != PROTOCOL_VERSION)
@@ -287,7 +292,14 @@
 
     while (counter < 30)
     {
-        read(sock, &row, sizeof(active_row));
+        if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
+            sizeof(row))
+        {
+            printf("\tFailed to fetch result\n");
+            close_socket();
+            return FALSE;
+        }
+
         close_socket();
         /*printf("row.s = %s, row.n = %s, row.t = %i, row.i = %i\n", row.s, row.n, (int) row.t, row.i); */
         if (row.i == IS_UP || row.i == IS_WAITING || row.i == IS_FAILED)
@@ -341,7 +353,14 @@
 
     printf("Stopping service: \n");
 
-    read(sock, &row, sizeof(active_row));
+    if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
+        sizeof(row))
+    {
+        printf("failed to read response\n");
+        close_socket();
+        return FALSE;
+    }
+
     close_socket();
 
 
@@ -370,7 +389,14 @@
             close_socket();
             return (FALSE);
         }
-        read(sock, &row, sizeof(active_row));
+        if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
+            sizeof(row))
+        {
+            printf("failed to read response\n");
+            close_socket();
+            return FALSE;
+        }
+
         close_socket();
 
         counter++;

Modified: initng/plugins/pidfile/initng_pidfile.c
==============================================================================
--- initng/plugins/pidfile/initng_pidfile.c	(original)
+++ initng/plugins/pidfile/initng_pidfile.c	Sun Dec  4 22:08:56 2005
@@ -94,8 +94,9 @@
             continue;
         }
 
-        buf[0] = '\0';
-        fgets(buf, BUFF_SIZE, fp);
+        /* HACK: ignore errors */
+        if (fgets(buf, BUFF_SIZE, fp) == NULL)
+            buf[0] = '\0';
 
         s = buf;
 

Modified: initng/plugins/reload/initng_reload.c
==============================================================================
--- initng/plugins/reload/initng_reload.c	(original)
+++ initng/plugins/reload/initng_reload.c	Sun Dec  4 22:08:56 2005
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "../../src/initng_handler.h"
 #include "../../src/initng_global.h"
@@ -203,9 +204,10 @@
     s_data *c_d = NULL;
 
     fil = fopen(filename, "w+");
-    if (fil==0) {
-      F_("Could not open '%s' for writing\n", filename);
-      return;
+    if (fil == 0)
+    {
+        F_("Could not open '%s' for writing\n", filename);
+        return;
     }
 
     /* walk the active_db */
@@ -293,7 +295,12 @@
         }
 
         D_("Saving : %s\n", entry.name);
-        fwrite(&entry, sizeof(entry), 1, fil);
+        if (fwrite(&entry, sizeof(entry), 1, fil) != 1)
+        {
+            F_("failed to write entry '%s': %m\n", entry.name);
+            /* TODO: database recovery?? */
+            break;
+        }
     }
 
     fclose(fil);

Modified: initng/plugins/renice/initng_renice.c
==============================================================================
--- initng/plugins/renice/initng_renice.c	(original)
+++ initng/plugins/renice/initng_renice.c	Sun Dec  4 22:08:56 2005
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 #include "../../src/initng_handler.h"
 #include "../../src/initng_global.h"
 #include "../../src/initng_plugin_hook.h"
@@ -43,7 +44,12 @@
     if (active_db_is(&NICE, s))
     {
         D_("Will renice %s to %i !\n", s->name, active_db_get_int(&NICE, s));
-        nice(active_db_get_int(&NICE, s));
+        errno = 0;
+        if (nice(active_db_get_int(&NICE, s)) == -1 && errno != 0)
+        {
+            F_("Failed to set the nice value: %s", strerror(errno));
+            return FALSE;
+        }
     }
     return (TRUE);
 }

Modified: initng/tools/install_service.c
==============================================================================
--- initng/tools/install_service.c	(original)
+++ initng/tools/install_service.c	Sun Dec  4 22:08:56 2005
@@ -121,11 +121,8 @@
         char *tmp;
 
         /* get the line */
-        fgets(line, LINE_LEN, f);
-
-
         /* make sure this is not end of file */
-        if (feof(f))
+        if (fgets(line, LINE_LEN, f) == 0 || feof(f))
             break;
 
         tmp = line;
@@ -606,8 +603,11 @@
 {
     int pipe_fork[2], inpipe_fork[2];
 
-    pipe(pipe_fork);
-    pipe(inpipe_fork);
+    if (pipe(pipe_fork) == -1 || pipe(inpipe_fork) == -1)
+    {
+        perror("pipe()");
+        return 0;
+    }
 
     int status;
     int datalen = 0;
@@ -640,8 +640,12 @@
            printf("\n"); */
 
         D_("Data sent to fork (%i): \"%s\"\n", length, data);
-        write(pipe_fork[1], data, length);
-        write(pipe_fork[1], "\n\nexit\n", 7);
+        if (TEMP_FAILURE_RETRY(write(pipe_fork[1], data, length)) != length ||
+            TEMP_FAILURE_RETRY(write(pipe_fork[1], "\n\nexit\n", 7)) != 7)
+        {
+            fprintf(stderr, "%s:%u: failed to write all data\n",
+                    __FILE__, __LINE__);
+        }
         close(pipe_fork[1]);
 
         wait(&status);

Modified: initng/tools/killall5.c
==============================================================================
--- initng/tools/killall5.c	(original)
+++ initng/tools/killall5.c	Sun Dec  4 22:08:56 2005
@@ -236,11 +236,9 @@
         snprintf(path, sizeof(path), "/proc/%s/stat", d->d_name);
 
         /* Read SID & statname from it. */
-        if ((fp = fopen(path, "r")) != NULL)
+        if ((fp = fopen(path, "r")) != NULL &&
+            fgets(buf, sizeof buf, fp) != NULL)
         {
-            buf[0] = 0;
-            fgets(buf, sizeof(buf), fp);
-
             /* See if name starts with '(' */
             s = buf;
             while (*s != ' ')
@@ -290,6 +288,8 @@
         }
         else
         {
+            if (fp != NULL)
+                fclose(fp);
             /* Process disappeared.. */
             free(p);
             continue;


More information about the Initng-svn mailing list