pigpio/pigpiod.c

428 lines
11 KiB
C
Raw Normal View History

2013-12-12 11:27:22 +01:00
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/*
This version is for pigpio version 69+
2013-12-12 11:27:22 +01:00
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
2014-01-21 20:04:59 +01:00
#include <stdarg.h>
2013-12-12 11:27:22 +01:00
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
2016-09-22 15:12:26 +02:00
#include <sys/socket.h>
#include <netdb.h>
2013-12-12 11:27:22 +01:00
#include "pigpio.h"
/*
This program starts the pigpio library as a daemon.
*/
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;
2016-10-31 00:05:30 +01:00
static int foreground = PI_DEFAULT_FOREGROUND;
static unsigned DMAprimaryChannel = PI_DEFAULT_DMA_NOT_SET;
static unsigned DMAsecondaryChannel = PI_DEFAULT_DMA_NOT_SET;
2013-12-12 11:27:22 +01:00
static unsigned socketPort = PI_DEFAULT_SOCKET_PORT;
static char * socketPath = PI_DEFAULT_SOCKET_PATH;
2015-02-25 21:34:52 +01:00
static unsigned memAllocMode = PI_DEFAULT_MEM_ALLOC_MODE;
2013-12-12 11:32:49 +01:00
static uint64_t updateMask = -1;
2013-12-12 11:27:22 +01:00
2015-10-02 09:23:02 +02:00
static uint32_t cfgInternals = PI_DEFAULT_CFG_INTERNALS;
2014-04-19 13:19:29 +02:00
static int updateMaskSet = 0;
2013-12-12 11:27:22 +01:00
static FILE * errFifo;
2016-09-22 15:12:26 +02:00
static uint32_t sockNetAddr[MAX_CONNECT_ADDRESSES];
static int numSockNetAddr = 0;
2014-01-21 20:04:59 +01:00
void fatal(char *fmt, ...)
{
char buf[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
fflush(stderr);
exit(EXIT_FAILURE);
}
2013-12-12 11:27:22 +01:00
void usage()
{
fprintf(stderr, "\n" \
"pigpio V%d\n" \
2013-12-12 11:27:22 +01:00
"Usage: sudo pigpiod [OPTION] ...\n" \
2016-09-22 15:12:26 +02:00
" -a value, DMA mode, 0=AUTO, 1=PMAP, 2=MBOX, default AUTO\n" \
" -b value, sample buffer size in ms, default 120\n" \
" -c value, library internal settings, default 0\n" \
" -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" \
2016-10-31 00:05:30 +01:00
" -g, run in foreground (do not fork), default disabled\n" \
2016-09-22 15:12:26 +02:00
" -k, disable socket interface, default enabled\n" \
" -l, localhost socket only default local+remote\n" \
" -m, disable alerts default enabled\n" \
2018-02-08 22:30:08 +01:00
" -n IP addr, allow address, name or dotted, default allow all\n" \
2016-09-22 15:12:26 +02:00
" -p value, socket port, 1024-32000, default 8888\n" \
" -s value, sample rate, 1, 2, 4, 5, 8, or 10, default 5\n" \
" -S path, Unix socket file, default /var/run/pigpio.sock\n" \
2016-09-22 15:12:26 +02:00
" -t value, clock peripheral, 0=PWM 1=PCM, default PCM\n" \
" -v, -V, display pigpio version and exit\n" \
" -x mask, GPIO which may be updated, default board GPIO\n" \
2013-12-12 11:27:22 +01:00
"EXAMPLE\n" \
"sudo pigpiod -s 2 -b 200 -f\n" \
" Set a sample rate of 2 microseconds with a 200 millisecond\n" \
" buffer. Disable the fifo interface.\n" \
"\n", PIGPIO_VERSION);
2013-12-12 11:27:22 +01:00
}
2015-10-02 09:23:02 +02:00
static uint64_t getNum(char *str, int *err)
{
uint64_t val;
char *endptr;
*err = 0;
val = strtoll(str, &endptr, 0);
if (*endptr) {*err = 1; val = -1;}
return val;
}
2016-09-22 15:12:26 +02:00
static uint32_t checkAddr(char *addrStr)
{
int err;
struct addrinfo hints, *res;
struct sockaddr_in *sin;
const char *portStr;
uint32_t addr;
portStr = getenv(PI_ENVPORT);
if (!portStr) portStr = PI_DEFAULT_SOCKET_PORT_STR;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
err = getaddrinfo(addrStr, portStr, &hints, &res);
if (err) return 0;
sin = (struct sockaddr_in *)res->ai_addr;
addr = sin->sin_addr.s_addr;
freeaddrinfo(res);
return addr;
}
2013-12-12 11:27:22 +01:00
static void initOpts(int argc, char *argv[])
{
2015-10-02 09:23:02 +02:00
int opt, err, i;
2016-09-22 15:12:26 +02:00
uint32_t addr;
2015-10-02 09:23:02 +02:00
int64_t mask;
2013-12-12 11:27:22 +01:00
while ((opt = getopt(argc, argv, "a:b:c:d:e:fgkln:mp:s:S:t:x:vV")) != -1)
2013-12-12 11:27:22 +01:00
{
switch (opt)
{
2015-02-25 21:34:52 +01:00
case 'a':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2015-02-25 21:34:52 +01:00
if ((i >= PI_MEM_ALLOC_AUTO) && (i <= PI_MEM_ALLOC_MAILBOX))
memAllocMode = i;
else fatal("invalid -a option (%d)", i);
break;
2013-12-12 11:27:22 +01:00
case 'b':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2013-12-12 11:27:22 +01:00
if ((i >= PI_BUF_MILLIS_MIN) && (i <= PI_BUF_MILLIS_MAX))
bufferSizeMilliseconds = i;
2014-01-21 20:04:59 +01:00
else fatal("invalid -b option (%d)", i);
2013-12-12 11:27:22 +01:00
break;
2015-10-02 09:23:02 +02:00
case 'c':
i = getNum(optarg, &err);
if ((i >= 0) && (i < PI_CFG_ILLEGAL_VAL))
cfgInternals = i;
else fatal("invalid -c option (%x)", i);
break;
2013-12-12 11:27:22 +01:00
case 'd':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2016-03-08 15:28:15 +01:00
if ((i >= PI_MIN_DMA_CHANNEL) && (i <= PI_MAX_DMA_CHANNEL))
2013-12-12 11:32:49 +01:00
DMAprimaryChannel = i;
2014-01-21 20:04:59 +01:00
else fatal("invalid -d option (%d)", i);
2013-12-12 11:27:22 +01:00
break;
2013-12-12 11:32:49 +01:00
case 'e':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2016-03-08 15:28:15 +01:00
if ((i >= PI_MIN_DMA_CHANNEL) && (i <= PI_MAX_DMA_CHANNEL))
2013-12-12 11:32:49 +01:00
DMAsecondaryChannel = i;
2014-01-21 20:04:59 +01:00
else fatal("invalid -e option (%d)", i);
2013-12-12 11:32:49 +01:00
break;
2013-12-12 11:27:22 +01:00
case 'f':
ifFlags |= PI_DISABLE_FIFO_IF;
break;
2016-10-31 00:05:30 +01:00
case 'g':
foreground = 1;
break;
2013-12-12 11:27:22 +01:00
case 'k':
ifFlags |= PI_DISABLE_SOCK_IF;
break;
case 'l':
ifFlags |= PI_LOCALHOST_SOCK_IF;
break;
case 'm':
ifFlags |= PI_DISABLE_ALERT;
break;
2016-09-22 15:12:26 +02:00
case 'n':
addr = checkAddr(optarg);
if (addr && (numSockNetAddr<MAX_CONNECT_ADDRESSES))
sockNetAddr[numSockNetAddr++] = addr;
else fatal("invalid -n option (%s)", optarg);
break;
2013-12-12 11:27:22 +01:00
case 'p':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2013-12-12 11:27:22 +01:00
if ((i >= PI_MIN_SOCKET_PORT) && (i <= PI_MAX_SOCKET_PORT))
socketPort = i;
2014-01-21 20:04:59 +01:00
else fatal("invalid -p option (%d)", i);
2013-12-12 11:27:22 +01:00
break;
case 's':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2013-12-12 11:27:22 +01:00
switch(i)
{
case 1:
case 2:
case 4:
case 5:
case 8:
case 10:
clockMicros = i;
break;
default:
2014-01-21 20:04:59 +01:00
fatal("invalid -s option (%d)", i);
2013-12-12 11:27:22 +01:00
break;
}
break;
case 'S':
if (!*optarg)
fatal("invalid -S option (empty string)");
socketPath = optarg;
break;
2013-12-12 11:27:22 +01:00
case 't':
2015-10-02 09:23:02 +02:00
i = getNum(optarg, &err);
2013-12-12 11:27:22 +01:00
if ((i >= PI_CLOCK_PWM) && (i <= PI_CLOCK_PCM))
clockPeripheral = i;
2014-01-21 20:04:59 +01:00
else fatal("invalid -t option (%d)", i);
2013-12-12 11:27:22 +01:00
break;
2015-12-31 18:01:34 +01:00
case 'v':
case 'V':
printf("%d\n", PIGPIO_VERSION);
exit(EXIT_SUCCESS);
break;
2013-12-12 11:32:49 +01:00
case 'x':
2015-10-02 09:23:02 +02:00
mask = getNum(optarg, &err);
if (!err)
2014-04-19 13:19:29 +02:00
{
updateMask = mask;
updateMaskSet = 1;
}
2014-01-21 20:04:59 +01:00
else fatal("invalid -x option (%s)", optarg);
2013-12-12 11:32:49 +01:00
break;
2013-12-12 11:27:22 +01:00
default: /* '?' */
usage();
2015-12-31 18:01:34 +01:00
exit(EXIT_FAILURE);
2013-12-12 11:27:22 +01:00
}
}
}
void terminate(int signum)
{
/* only registered for SIGHUP/SIGTERM */
gpioTerminate();
fprintf(errFifo, "SIGHUP/SIGTERM received\n");
fflush(NULL);
fclose(errFifo);
unlink(PI_ERRFIFO);
exit(0);
}
int main(int argc, char **argv)
{
pid_t pid;
int flags;
2016-10-31 00:05:30 +01:00
/* check command line parameters */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
initOpts(argc, argv);
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
if (!foreground) {
/* Fork off the parent process */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
pid = fork();
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
if (pid < 0) { exit(EXIT_FAILURE); }
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
/* If we got a good PID, then we can exit the parent process. */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
if (pid > 0) { exit(EXIT_SUCCESS); }
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
/* Change the file mode mask */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
umask(0);
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
/* Open any logs here */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
/* NONE */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
/* Create a new SID for the child process */
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 */
2013-12-12 11:27:22 +01:00
2016-10-31 00:05:30 +01:00
fclose(stdin);
fclose(stdout);
}
2013-12-12 11:27:22 +01:00
/* configure library */
gpioCfgBufferSize(bufferSizeMilliseconds);
2014-12-17 23:31:17 +01:00
gpioCfgClock(clockMicros, clockPeripheral, 0);
2013-12-12 11:27:22 +01:00
gpioCfgInterfaces(ifFlags);
2013-12-12 11:32:49 +01:00
gpioCfgDMAchannels(DMAprimaryChannel, DMAsecondaryChannel);
2013-12-12 11:27:22 +01:00
gpioCfgSocketPort(socketPort);
gpioCfgSocketPath(socketPath);
2015-02-25 21:34:52 +01:00
gpioCfgMemAlloc(memAllocMode);
2014-04-19 13:19:29 +02:00
if (updateMaskSet) gpioCfgPermissions(updateMask);
2013-12-12 11:32:49 +01:00
2016-09-22 15:12:26 +02:00
gpioCfgNetAddr(numSockNetAddr, sockNetAddr);
2015-10-02 09:23:02 +02:00
gpioCfgSetInternals(cfgInternals);
2013-12-12 11:27:22 +01:00
/* start library */
2014-01-21 20:04:59 +01:00
if (gpioInitialise()< 0) fatal("Can't initialise pigpio library");
2013-12-12 11:27:22 +01:00
/* create pipe for error reporting */
unlink(PI_ERRFIFO);
mkfifo(PI_ERRFIFO, 0664);
if (chmod(PI_ERRFIFO, 0664) < 0)
2014-01-21 20:04:59 +01:00
fatal("chmod %s failed (%m)", PI_ERRFIFO);
2013-12-12 11:27:22 +01:00
errFifo = freopen(PI_ERRFIFO, "w+", stderr);
if (errFifo)
{
/* set stderr non-blocking */
flags = fcntl(fileno(errFifo), F_GETFL, 0);
fcntl(fileno(errFifo), F_SETFL, flags | O_NONBLOCK);
/* request SIGHUP/SIGTERM from libarary for termination */
gpioSetSignalFunc(SIGHUP, terminate);
gpioSetSignalFunc(SIGTERM, terminate);
/* sleep forever */
while (1)
{
/* cat /dev/pigerr to view daemon errors */
sleep(5);
fflush(errFifo);
}
}
else
{
fprintf(stderr, "freopen failed (%m)");
gpioTerminate();
}
return 0;
}
2014-12-17 23:31:17 +01:00
2015-12-31 18:01:34 +01:00