[Oisf-devel] [PATCH] pid file

Victor Julien victor at inliniac.net
Mon Mar 29 10:24:21 UTC 2010


Hey Steve, I see no one responded to this email. Sorry about that.

The patch looks clean and simple. I have a few smaller issues with it:

1. I'd like to code to use our naming conventions ThisIsAFunctionName
2. I'd like write_pidfile to live in a file like util-pidfile.[ch]
3. I'd like it to use a --pidfile long opt instead of -p. Snort,
Wireshark, tcpdump all use -p to not put an interface into promisc mode
so I want our -p remain available for that function later.

Cheers,
Victor

Steve Grubb wrote:
>> I will check in a bug for the pid file. 
> 
> This is a stab at it. There is now a -p commandline option so that distros can
> pass the pid file location to the daemon.
> 
> -Steve
> 
> 
> diff -urp oisf.orig/src/suricata.c oisf/src/suricata.c
> --- oisf.orig/src/suricata.c	2010-03-11 10:02:38.000000000 -0500
> +++ oisf/src/suricata.c	2010-03-11 11:15:26.000000000 -0500
> @@ -5,6 +5,7 @@
>  #include <getopt.h>
>  #include <signal.h>
>  #include <pthread.h>
> +#include <fcntl.h>
>  
>  #include "suricata.h"
>  #include "decode.h"
> @@ -352,6 +353,26 @@ static void SetBpfString(int optind, cha
>      }
>  }
>  
> +static int write_pidfile(const char *pidfile)
> +{
> +    int pidfd, len;
> +    char val[16];
> +
> +    len = snprintf(val, sizeof(val), "%u\n", getpid());
> +    if (len <= 0) {
> +        syslog(LOG_ERR, "Pid error (%s)", strerror(errno));
> +        return 1;
> +    }
> +    pidfd = open(pidfile, O_CREAT | O_TRUNC | O_NOFOLLOW | O_WRONLY, 0644);
> +    if (pidfd < 0) {
> +        syslog(LOG_ERR, "Unable to set pidfile (%s)", strerror(errno));
> +        return 1;
> +    }
> +    (void)write(pidfd, val, (unsigned int)len);
> +    close(pidfd);
> +    return 0;
> +}
> +
>  void usage(const char *progname)
>  {
>      printf("%s %s\n", PROG_NAME, PROG_VER);
> @@ -364,6 +385,7 @@ void usage(const char *progname)
>      printf("\t-s <path>                    : path to signature file (optional)\n");
>      printf("\t-l <dir>                     : default log directory\n");
>      printf("\t-D                           : run as daemon\n");
> +    printf("\t-p <path>                    : write pid to this file\n");
>  #ifdef UNITTESTS
>      printf("\t-u                           : run the unittests and exit\n");
>      printf("\t-U, --unittest-filter=REGEX  : filter unittests with a regex\n");
> @@ -390,6 +412,7 @@ int main(int argc, char **argv)
>      char *sig_file = NULL;
>      char *nfq_id = NULL;
>      char *conf_filename = NULL;
> +    char *pid_filename = NULL;
>  #ifdef UNITTESTS
>      char *regex_arg = NULL;
>  #endif
> @@ -428,7 +451,7 @@ int main(int argc, char **argv)
>      /* getopt_long stores the option index here. */
>      int option_index = 0;
>  
> -    char short_opts[] = "c:Dhi:l:q:d:r:us:U:V";
> +    char short_opts[] = "c:p:Dhi:l:q:d:r:us:U:V";
>  
>      while ((opt = getopt_long(argc, argv, short_opts, long_opts, &option_index)) != -1) {
>          switch (opt) {
> @@ -479,6 +502,9 @@ int main(int argc, char **argv)
>          case 'D':
>              daemon = 1;
>              break;
> +        case 'p':
> +            pid_filename = optarg;
> +            break;
>          case 'h':
>              usage(argv[0]);
>              exit(EXIT_SUCCESS);
> @@ -785,7 +811,12 @@ int main(int argc, char **argv)
>      }
>  #endif /* UNITTESTS */
>  
> -    if (daemon) Daemonize();
> +    if (daemon) {
> +        Daemonize();
> +        if (write_pidfile(pid_filename))
> +		pid_filename = NULL;
> +    } else
> +        pid_filename = NULL;
>  
>  #ifndef OS_WIN32
>      /* registering signals we use */
> @@ -999,6 +1030,8 @@ int main(int argc, char **argv)
>  
>      RunModeShutDown();
>      OutputDeregisterAll();
> +    if (pid_filename)
> +        unlink(pid_filename);
>  
>  #ifdef __SC_CUDA_SUPPORT__
>      /* all cuda contexts attached to any threads should be free by now.
> _______________________________________________
> Oisf-devel mailing list
> Oisf-devel at openinfosecfoundation.org
> http://lists.openinfosecfoundation.org/mailman/listinfo/oisf-devel


-- 
---------------------------------------------
Victor Julien
http://www.inliniac.net/
PGP: http://www.inliniac.net/victorjulien.asc
---------------------------------------------




More information about the Oisf-devel mailing list