aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_fifo.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--plugin_fifo.c343
1 files changed, 171 insertions, 172 deletions
diff --git a/plugin_fifo.c b/plugin_fifo.c
index d181b97..9a49285 100644
--- a/plugin_fifo.c
+++ b/plugin_fifo.c
@@ -65,255 +65,254 @@
#define string(s) str(s)
struct FifoData {
- char *path;
- char *msg;
- int msglen;
- int input;
- int created;
+ char *path;
+ char *msg;
+ int msglen;
+ int input;
+ int created;
};
static struct FifoData fd = {
- .path = NULL,
- .msg = NULL,
- .msglen = -1,
- .input = -1,
- .created = -1,
+ .path = NULL,
+ .msg = NULL,
+ .msglen = -1,
+ .input = -1,
+ .created = -1,
};
static int confFifo(struct FifoData *p)
{
- char *path, *disp, *sect, *fifosect = "Plugin:FIFO";
- unsigned int pathlen;
+ char *path, *disp, *sect, *fifosect = "Plugin:FIFO";
+ unsigned int pathlen;
- info("[FIFO] Reading config file '%s'", cfg_source());
+ info("[FIFO] Reading config file '%s'", cfg_source());
- path = cfg_get(fifosect, "FifoPath", string(FIFO_DEFAULT_PATH));
+ path = cfg_get(fifosect, "FifoPath", string(FIFO_DEFAULT_PATH));
+ pathlen = strlen(path);
+ if (pathlen == 0) {
+ info("[FIFO] Invalid '%s.FifoPath' entry from '%s'. "
+ "Assuming " string(FIFO_DEFAULT_PATH), fifosect, cfg_source());
+ free(path);
+ path = strdup(string(FIFO_DEFAULT_PATH));
pathlen = strlen(path);
- if (pathlen == 0) {
- info("[FIFO] Invalid '%s.FifoPath' entry from '%s'. "
- "Assuming "string(FIFO_DEFAULT_PATH), fifosect, cfg_source());
- free(path);
- path = strdup(string(FIFO_DEFAULT_PATH));
- pathlen = strlen(path);
- }
- if (pathlen > FIFO_MAXPATH) {
- error("[FIFO] Error: Too long '%s.FifoPath' entry from '%s'. "
- "(MAX "string(FIFO_MAXPATH)" chars)", fifosect, cfg_source());
- free(path);
- return (-1);
- }
- info("[FIFO] Read '%s.FifoPath' value is '%s'", fifosect, path);
+ }
+ if (pathlen > FIFO_MAXPATH) {
+ error("[FIFO] Error: Too long '%s.FifoPath' entry from '%s'. "
+ "(MAX " string(FIFO_MAXPATH) " chars)", fifosect, cfg_source());
+ free(path);
+ return (-1);
+ }
+ info("[FIFO] Read '%s.FifoPath' value is '%s'", fifosect, path);
- disp = cfg_get(NULL, "Display", NULL);
- if (disp == NULL) {
- error("[FIFO] Error: Could not get the Display name from '%s'", cfg_source());
- free(path);
- return (-1);
- }
- if ((sect = malloc(1+strlen("Display:")+strlen(disp))) == NULL) {
- error("[FIFO] Error: Memory allocation failed");
- free(disp);
- free(path);
- return (-1);
+ disp = cfg_get(NULL, "Display", NULL);
+ if (disp == NULL) {
+ error("[FIFO] Error: Could not get the Display name from '%s'", cfg_source());
+ free(path);
+ return (-1);
+ }
+ if ((sect = malloc(1 + strlen("Display:") + strlen(disp))) == NULL) {
+ error("[FIFO] Error: Memory allocation failed");
+ free(disp);
+ free(path);
+ return (-1);
+ }
+ strcpy(sect, "Display:");
+ strcat(sect, disp);
+ info("[FIFO] Using display '%s'.", disp);
+ free(disp);
+
+ disp = cfg_get(sect, "Size", NULL);
+ if (disp != NULL) {
+ info("[FIFO] Getting the buffer size from '%s.Size'", sect);
+ if (sscanf(disp, "%dx%*d", &p->msglen) != 1) {
+ info("[FIFO] Could not determine the display size. " "Assuming " string(FIFO_DEFAULT_BUFSIZE));
+ p->msglen = FIFO_DEFAULT_BUFSIZE;
}
- strcpy(sect, "Display:");
- strcat(sect, disp);
- info("[FIFO] Using display '%s'.", disp);
free(disp);
-
- disp = cfg_get(sect, "Size", NULL);
- if (disp != NULL) {
- info("[FIFO] Getting the buffer size from '%s.Size'", sect);
- if (sscanf(disp, "%dx%*d", &p->msglen) != 1) {
- info("[FIFO] Could not determine the display size. "
- "Assuming "string(FIFO_DEFAULT_BUFSIZE));
- p->msglen = FIFO_DEFAULT_BUFSIZE;
- }
- free(disp);
+ } else {
+ info("[FIFO] Could not find a '%s.Size' entry.", sect);
+ if (cfg_number(fifosect, "FifoBufSize", FIFO_DEFAULT_BUFSIZE, 0, -1, &p->msglen) > 0) {
+ info("[FIFO] Getting the buffer size from '%s.FifoBufSize'", fifosect);
} else {
- info("[FIFO] Could not find a '%s.Size' entry.", sect);
- if (cfg_number(fifosect, "FifoBufSize", FIFO_DEFAULT_BUFSIZE, 0, -1, &p->msglen) > 0) {
- info("[FIFO] Getting the buffer size from '%s.FifoBufSize'", fifosect);
- } else {
- info("[FIFO] Could not find a valid '%s.FifoBufSize' entry. "
- "Assuming "string(FIFO_DEFAULT_BUFSIZE), fifosect);
- p->msglen = FIFO_DEFAULT_BUFSIZE;
- }
+ info("[FIFO] Could not find a valid '%s.FifoBufSize' entry. "
+ "Assuming " string(FIFO_DEFAULT_BUFSIZE), fifosect);
+ p->msglen = FIFO_DEFAULT_BUFSIZE;
}
- info("[FIFO] Read buffer size is '%d'", p->msglen);
- free(sect);
+ }
+ info("[FIFO] Read buffer size is '%d'", p->msglen);
+ free(sect);
- if ((p->msg = malloc(2+pathlen+p->msglen)) == NULL) {
- error("[FIFO] Error: Memory allocation failed");
- free(path);
- return (-1);
- }
- p->msg[0] = 0;
- p->path = p->msg+p->msglen+1;
- strcpy(p->path, path);
+ if ((p->msg = malloc(2 + pathlen + p->msglen)) == NULL) {
+ error("[FIFO] Error: Memory allocation failed");
free(path);
-
- return (0);
+ return (-1);
+ }
+ p->msg[0] = 0;
+ p->path = p->msg + p->msglen + 1;
+ strcpy(p->path, path);
+ free(path);
+
+ return (0);
}
static int makeFifo(struct FifoData *p)
{
- struct stat st;
-
- if (stat(p->path, &st) < 0) {
- if (errno == ENOENT) {
- if (mkfifo(p->path, 0666) == 0) {
- p->created = 1;
+ struct stat st;
- return (0);
- }
- error("Couldn't create FIFO \"%s\": %s\n", p->path, strerror(errno));
+ if (stat(p->path, &st) < 0) {
+ if (errno == ENOENT) {
+ if (mkfifo(p->path, 0666) == 0) {
+ p->created = 1;
- return (-1);
- }
- error("Failed to stat FIFO \"%s\": %s\n", p->path, strerror(errno));
+ return (0);
+ }
+ error("Couldn't create FIFO \"%s\": %s\n", p->path, strerror(errno));
- return (-1);
+ return (-1);
}
+ error("Failed to stat FIFO \"%s\": %s\n", p->path, strerror(errno));
- if (! S_ISFIFO(st.st_mode)) {
- error("\"%s\" already exists, but is not a FIFO", p->path);
+ return (-1);
+ }
- return (-1);
- }
+ if (!S_ISFIFO(st.st_mode)) {
+ error("\"%s\" already exists, but is not a FIFO", p->path);
+
+ return (-1);
+ }
- return (0);
+ return (0);
}
static void closeFifo(struct FifoData *p)
{
- struct stat st;
+ struct stat st;
- if (p->input >= 0) {
- close(p->input);
- p->input = -1;
- }
+ if (p->input >= 0) {
+ close(p->input);
+ p->input = -1;
+ }
- if ((p->created >= 0) && (stat(p->path, &st) == 0)) {
- debug("Removing FIFO \"%s\"\n", p->path);
- if (unlink(p->path) < 0) {
- error("Could not remove FIFO \"%s\": %s\n", p->path, strerror(errno));
+ if ((p->created >= 0) && (stat(p->path, &st) == 0)) {
+ debug("Removing FIFO \"%s\"\n", p->path);
+ if (unlink(p->path) < 0) {
+ error("Could not remove FIFO \"%s\": %s\n", p->path, strerror(errno));
- return;
- }
- p->created = -1;
- }
-
- if (p->msg) {
- free(p->msg);
- p->msg = p->path = NULL;
- p->msglen = -1;
+ return;
}
+ p->created = -1;
+ }
+
+ if (p->msg) {
+ free(p->msg);
+ p->msg = p->path = NULL;
+ p->msglen = -1;
+ }
}
static int openFifo(struct FifoData *p)
{
- if (p->created < 0) {
- error("Error: FIFO \"%s\" does not exist: %s\n", p->path, strerror(errno));
+ if (p->created < 0) {
+ error("Error: FIFO \"%s\" does not exist: %s\n", p->path, strerror(errno));
- return (-1);
- }
+ return (-1);
+ }
- if ((p->input = open(p->path, O_RDONLY | O_NONBLOCK)) < 0) {
- error("Could not open FIFO \"%s\" for reading: %s\n", p->path, strerror(errno));
- closeFifo(p);
+ if ((p->input = open(p->path, O_RDONLY | O_NONBLOCK)) < 0) {
+ error("Could not open FIFO \"%s\" for reading: %s\n", p->path, strerror(errno));
+ closeFifo(p);
- return (-1);
- }
+ return (-1);
+ }
- return (0);
+ return (0);
}
static int startFifo(struct FifoData *p)
{
- int res;
+ int res;
- if ((res = confFifo(p)))
- return (res);
+ if ((res = confFifo(p)))
+ return (res);
- if ((res = makeFifo(p)))
- return (res);
+ if ((res = makeFifo(p)))
+ return (res);
- if ((res = openFifo(p)))
- return (res);
+ if ((res = openFifo(p)))
+ return (res);
- /* ignore broken pipe */
- signal(SIGPIPE, SIG_IGN);
+ /* ignore broken pipe */
+ signal(SIGPIPE, SIG_IGN);
- return (res);
+ return (res);
}
static void readFifo(struct FifoData *p)
{
- int bytes;
-
- bytes = read(p->input, p->msg, p->msglen);
- if (bytes == 0)
- return;
-
- if (bytes > 0) {
- p->msg[bytes] = 0;
- while (bytes--)
- if (p->msg[bytes] < 0x20)
- p->msg[bytes] = ' ';
- } else {
- error("[FIFO] Error %i: %s", errno, strerror(errno));
- strcpy(p->msg, "ERROR");
- }
+ int bytes;
+
+ bytes = read(p->input, p->msg, p->msglen);
+ if (bytes == 0)
+ return;
+
+ if (bytes > 0) {
+ p->msg[bytes] = 0;
+ while (bytes--)
+ if (p->msg[bytes] < 0x20)
+ p->msg[bytes] = ' ';
+ } else {
+ error("[FIFO] Error %i: %s", errno, strerror(errno));
+ strcpy(p->msg, "ERROR");
+ }
}
-static void runFifo(RESULT *result)
+static void runFifo(RESULT * result)
{
- static int state = 1;
- struct FifoData *p = &fd;
- char *s;
-
- switch (state) {
- case 1:
- /* Called for the first time. Set up everything. */
- state = startFifo(p);
- s = "";
- break;
-
- case 0:
- /* Init went fine. Now run in normal operation mode. */
- readFifo(p);
- s = p->msg;
- break;
-
- default:
- /* There was an error somewhere in init. Do nothing. */
- s = "ERROR";
- break;
- }
-
- /* Store the result */
- SetResult(&result, R_STRING, s);
+ static int state = 1;
+ struct FifoData *p = &fd;
+ char *s;
+
+ switch (state) {
+ case 1:
+ /* Called for the first time. Set up everything. */
+ state = startFifo(p);
+ s = "";
+ break;
+
+ case 0:
+ /* Init went fine. Now run in normal operation mode. */
+ readFifo(p);
+ s = p->msg;
+ break;
+
+ default:
+ /* There was an error somewhere in init. Do nothing. */
+ s = "ERROR";
+ break;
+ }
+
+ /* Store the result */
+ SetResult(&result, R_STRING, s);
}
/* plugin initialization */
int plugin_init_fifo(void)
{
- AddFunction("fifo::read", 0, runFifo);
+ AddFunction("fifo::read", 0, runFifo);
- return (0);
+ return (0);
}
void plugin_exit_fifo(void)
{
- closeFifo(&fd);
+ closeFifo(&fd);
}