[Snort-devel] Re: Snort PID file rename patch

Phil Wood cpw at ...86...
Fri Aug 9 16:00:03 EDT 2002


The attached might work on 1.8.7.  I had some other stuff but it was site
specific.  So, the line numbers will be off.  Let me know how it goes.

On Fri, Aug 09, 2002 at 05:45:21PM -0500, Larabee, Rick (TIFS) wrote:
> Hi Phil,
> 
> I saw one of your messages on the snort-devel list and was wondering if you
> could help me with something.  I saw your patch for 1.8.3 and 1.9. to be
> able to rename the pid file but I wasnt sure if it would work with 1.8.6/7.
> Can you point me in the right direction?
> 
> http://marc.theaimsgroup.com/?l=snort-devel&m=101811241103382&w=2
> http://marc.theaimsgroup.com/?l=snort-devel&m=101070038407291&w=2
> 
> Thank you,
> Rick

-- 
Phil Wood, cpw at ...86...

-------------- next part --------------
diff -Naur -b snort-1.8.7/snort.c snort+/snort.c
--- snort-1.8.7/snort.c	Thu Jul 18 17:49:19 2002
+++ snort+/snort.c	Thu Jul 18 17:45:08 2002
@@ -251,7 +247,7 @@
      * all of that is done in CreatePidFile
      *
      */
-    if(pv.use_rules || pv.log_flag || pv.daemon_flag)
+    if(pv.use_rules || pv.log_flag || pv.daemon_flag || *pv.pidfile_suffix)
     {
         if(!pv.nolog_flag && 
 	   (pv.alert_mode == ALERT_FAST ||
@@ -259,13 +255,13 @@
                  pv.alert_mode == 0))   
         {
             /* perform some sanity checks on the output directory
-	       TODO: rename this function -- it only checks logoutput dir.
+	         * TODO: rename this function -- it only checks logoutput dir.
              */
             SanityChecks();
         }
 
         /* ... then create a PID file if not reading from a file */
-        if (!pv.readmode_flag && pv.daemon_flag)
+        if (!pv.readmode_flag && (pv.daemon_flag || *pv.pidfile_suffix))
 	{
 #ifndef WIN32
             CreatePidFile(pv.interfaces[0]);
@@ -683,12 +679,13 @@
     username = NULL;
     groupname = NULL;
     chrootdir = NULL;
+    pv.pidfile_suffix[0] = 0;
 
 #ifndef WIN32
-    valid_options = "B:fk:TXL:IOCqS:pNA:m:F:DM:br:xeh:l:dc:n:P:"
+    valid_options = "R:B:fk:TXL:IOCqS:pNA:m:F:DM:br:xeh:l:dc:n:P:"
         "i:G:vV?aso6u:g:t:Uyz";
 #else
-    valid_options = "B:fk:TXL:IOCWqS:pNA:m:F:DM:br:xeh:l:dc:n:P:"
+    valid_options = "R:B:fk:TXL:IOCWqS:pNA:m:F:DM:br:xeh:l:dc:n:P:"
         "i:G:vV?aEo6u:g:s:t:Uyzw:";
 #endif
 
@@ -1053,6 +1050,28 @@
 
                 break;
 
+            case 'R': /* augment pid file name CPW*/
+                if (strlen(optarg) < MAX_PIDFILE_SUFFIX && strlen(optarg) > 0)
+                {
+                    if (!strstr(optarg, "..") && !(strstr(optarg, "/")))
+                    {
+                        snprintf(pv.pidfile_suffix, MAX_PIDFILE_SUFFIX, "%s",
+                                optarg);
+                    }
+                    else
+                    {
+                        FatalError("ERROR: illegal pidfile suffix: %s\n",
+                                optarg);
+                    }
+                }
+                else
+                {
+                    FatalError("ERROR: pidfile suffix length problem: %d\n",
+                            strlen(optarg) );
+                }
+
+                break;
+
             case 's':  /* log alerts to syslog */
                 pv.syslog_flag = 1;
                 DebugMessage(DEBUG_INIT, "Logging alerts to syslog\n");
@@ -2997,20 +3017,21 @@
         FatalError("ERROR: CreatePidFile() failed to lookup interface or pid_path is unknown!\n");
     }
     
-    LogMessage("Writing PID file to \"%s\"\n", pv.pid_path);
 
-    snprintf(pv.pid_filename, STD_BUF,  "%s/snort_%s.pid", pv.pid_path, intf);
+    snprintf(pv.pid_filename, STD_BUF,  "%s/snort_%s%s.pid", pv.pid_path, intf,
+            pv.pidfile_suffix);
 
     pid_file = fopen(pv.pid_filename, "w");
 
     if(pid_file)
     {
+        LogMessage("Writing PID file to \"%s\"\n", pv.pid_filename);
         fprintf(pid_file, "%d\n", (int) getpid());
         fclose(pid_file);
     }
     else
     {
-        ErrorMessage("Failed to create pid file %s", pv.pid_filename);
+        ErrorMessage("Failed to create PID file %s", pv.pid_filename);
         pv.pid_filename[0] = 0;
     }
 }
diff -Naur -b snort-1.8.7/snort.h snort+/snort.h
--- snort-1.8.7/snort.h	Fri Jun 28 14:26:07 2002
+++ snort+/snort.h	Thu Jul 18 17:39:39 2002
@@ -264,6 +264,8 @@
 #define RF_ANY_DP     0x10
 #define RF_ANY_FLAGS  0x20
 
+#define MAX_PIDFILE_SUFFIX 11 /* uniqueness extension to PID file, see '-R' */
+
 /*
  * you may need to ajust this on the systems which don't have standard
  * paths defined
@@ -414,6 +416,7 @@
     int include_year;
     int ghetto_msg_flag;
     ClassTypes *ct;  /* rule classification types */
+    char pidfile_suffix[MAX_PIDFILE_SUFFIX+1]; /* room for a null */
 } PV;
 
 /* struct to collect packet statistics */


More information about the Snort-devel mailing list