From: Jason Diamond Date: Sun, 30 Oct 2016 23:05:30 +0000 (+0000) Subject: add option for running in foreground X-Git-Tag: archive/raspbian/1.68-2+rpi1~73^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=1ec947281300da123a927c6007e513ed9d64e019;p=pigpio.git add option for running in foreground --- diff --git a/pigpio.h b/pigpio.h index c9bb83b..978e6b5 100644 --- 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 diff --git a/pigpiod.c b/pigpiod.c index d7caee5..1789dac 100644 --- 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 */