aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_fifo.c
blob: 4166b05452f0f02de993f32f5d87b8fc9da93725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* $Id: plugin_fifo.c 1112 2010-02-14 02:47:49Z michael $
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_fifo.c $
 *
 * Fifo plugin
 *
 * Copyright (C) 2008 Michael Vogt <michu@neophob.com>
 * Copyright (C) 2010 Mattia Jona-Lasinio <mjona@users.sourceforge.net>
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Configuration parameters:
 *
 * - FifoPath 'string'	: use <string> as the fifo complete file path
 *			  If absent use  /tmp/lcd4linux.fifo)
 *
 * - FifoBufSize num	: if the plugin is unable to determine the display size then
 *			  set the size of the internal buffer to <num> characters
 *			  otherwise use the display size (number of columns).
 *			  If no display size is available and no FifoBufSize parameter
  *			  is specified then arbitrarily set the internal buffer size
 *			  to 80 characters.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>

#include "debug.h"
#include "plugin.h"
#include "cfg.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

#define FIFO_MAXPATH		256
#define FIFO_DEFAULT_PATH	/tmp/lcd4linux.fifo
#define FIFO_DEFAULT_BUFSIZE	80
#define str(s) #s
#define string(s) str(s)

struct FifoData {
    char *path;
    char *msg;
    int msglen;
    int input;
    int created;
};

static struct FifoData fd = {
    .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;

    info("[FIFO] Reading config file '%s'", cfg_source());

    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 > 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);
    }
    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 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);

    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);
    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;

		return (0);
	    }
	    error("Couldn't create FIFO \"%s\": %s\n", p->path, strerror(errno));

	    return (-1);
	}
	error("Failed to stat FIFO \"%s\": %s\n", p->path, strerror(errno));

	return (-1);
    }

    if (!S_ISFIFO(st.st_mode)) {
	error("\"%s\" already exists, but is not a FIFO", p->path);

	return (-1);
    }

    return (0);
}


static void closeFifo(struct FifoData *p)
{
    struct stat st;

    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));

	    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));

	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);

	return (-1);
    }

    return (0);
}


static int startFifo(struct FifoData *p)
{
    int res;

    if ((res = confFifo(p)))
	return (res);

    if ((res = makeFifo(p)))
	return (res);

    if ((res = openFifo(p)))
	return (res);

    /* ignore broken pipe */
    signal(SIGPIPE, SIG_IGN);

    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");
    }
}


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);
}


/* plugin initialization */
int plugin_init_fifo(void)
{
    AddFunction("fifo::read", 0, runFifo);

    return (0);
}


void plugin_exit_fifo(void)
{
    closeFifo(&fd);
}