add option for running in foreground
authorJason Diamond <jason@diamond.name>
Sun, 30 Oct 2016 23:05:30 +0000 (23:05 +0000)
committerJason Diamond <jason@diamond.name>
Sun, 30 Oct 2016 23:05:30 +0000 (23:05 +0000)
pigpio.h
pigpiod.c

index c9bb83badf40f12c09e3e5ff74116cf2889bfff0..978e6b5fd5f41c39e4ee18fe565d28250f5c6a1b 100644 (file)
--- a/pigpio.h
+++ b/pigpio.h
@@ -6277,6 +6277,7 @@ after this command is issued.
 #define PI_DEFAULT_CLK_MICROS              5
 #define PI_DEFAULT_CLK_PERIPHERAL          PI_CLOCK_PCM
 #define PI_DEFAULT_IF_FLAGS                0
+#define PI_DEFAULT_FOREGROUND              0
 #define PI_DEFAULT_DMA_CHANNEL             14
 #define PI_DEFAULT_DMA_PRIMARY_CHANNEL     14
 #define PI_DEFAULT_DMA_SECONDARY_CHANNEL   6
index d7caee529adaeea44ed7dcf2905fc74c32db3739..1789dac451275952cc977333987f8a3360a72e46 100644 (file)
--- a/pigpiod.c
+++ b/pigpiod.c
@@ -55,6 +55,7 @@ static unsigned bufferSizeMilliseconds = PI_DEFAULT_BUFFER_MILLIS;
 static unsigned clockMicros            = PI_DEFAULT_CLK_MICROS;
 static unsigned clockPeripheral        = PI_DEFAULT_CLK_PERIPHERAL;
 static unsigned ifFlags                = PI_DEFAULT_IF_FLAGS;
+static int      foreground             = PI_DEFAULT_FOREGROUND;
 static unsigned DMAprimaryChannel      = PI_DEFAULT_DMA_PRIMARY_CHANNEL;
 static unsigned DMAsecondaryChannel    = PI_DEFAULT_DMA_SECONDARY_CHANNEL;
 static unsigned socketPort             = PI_DEFAULT_SOCKET_PORT;
@@ -98,6 +99,7 @@ void usage()
       "   -d value,   primary DMA channel, 0-14,         default 14\n" \
       "   -e value,   secondary DMA channel, 0-14,       default 6\n" \
       "   -f,         disable fifo interface,            default enabled\n" \
+      "   -g,         run in foreground (do not fork),   default disabled\n" \
       "   -k,         disable socket interface,          default enabled\n" \
       "   -l,         localhost socket only              default local+remote\n" \
       "   -n IP addr, allow address, name or dotted,     default allow all\n" \
@@ -160,7 +162,7 @@ static void initOpts(int argc, char *argv[])
    uint32_t addr;
    int64_t mask;
 
-   while ((opt = getopt(argc, argv, "a:b:c:d:e:fkln:p:s:t:x:vV")) != -1)
+   while ((opt = getopt(argc, argv, "a:b:c:d:e:fgkln:p:s:t:x:vV")) != -1)
    {
       switch (opt)
       {
@@ -203,6 +205,10 @@ static void initOpts(int argc, char *argv[])
             ifFlags |= PI_DISABLE_FIFO_IF;
             break; 
 
+         case 'g':
+            foreground = 1;
+            break;
+
          case 'k':
             ifFlags |= PI_DISABLE_SOCK_IF;
             break; 
@@ -298,40 +304,42 @@ int main(int argc, char **argv)
    pid_t pid;
    int flags;
 
-   /* Fork off the parent process */
+   /* check command line parameters */
 
-   pid = fork();
+   initOpts(argc, argv);
 
-   if (pid < 0) { exit(EXIT_FAILURE); }
+   if (!foreground) {
+      /* Fork off the parent process */
 
-   /* If we got a good PID, then we can exit the parent process. */
+      pid = fork();
 
-   if (pid > 0) { exit(EXIT_SUCCESS); }
+      if (pid < 0) { exit(EXIT_FAILURE); }
 
-   /* Change the file mode mask */
+      /* If we got a good PID, then we can exit the parent process. */
 
-   umask(0);       
-   
-   /* Open any logs here */
+      if (pid > 0) { exit(EXIT_SUCCESS); }
 
-   /* NONE */
-   
-   /* Create a new SID for the child process */
+      /* Change the file mode mask */
 
-   if (setsid() < 0) fatal("setsid failed (%m)");
+      umask(0);
 
-   /* Change the current working directory */
+      /* Open any logs here */
 
-   if ((chdir("/")) < 0) fatal("chdir failed (%m)");
-   
-   /* check command line parameters */
+      /* NONE */
 
-   initOpts(argc, argv);
-   
-   /* Close out the standard file descriptors */
+      /* Create a new SID for the child process */
 
-   fclose(stdin);
-   fclose(stdout);
+      if (setsid() < 0) fatal("setsid failed (%m)");
+
+      /* Change the current working directory */
+
+      if ((chdir("/")) < 0) fatal("chdir failed (%m)");
+
+      /* Close out the standard file descriptors */
+
+      fclose(stdin);
+      fclose(stdout);
+   }
 
    /* configure library */