Changeset 871
- Timestamp:
- 04/12/08 18:23:52 (7 months ago)
- Files:
-
- 1 modified
-
trunk/plugin_fifo.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugin_fifo.c
r869 r871 23 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 24 * 25 */ 26 25 */ 26 27 27 /* 28 28 * Quick fifo hack for lcd4linux … … 30 30 * most code is ripped ... 31 31 * 32 */ 33 32 */ 34 33 35 /* define the include files you need */ 34 /* define the include files you need */ 36 35 #include "config.h" 37 36 38 37 #include <stdlib.h> 39 38 #include <string.h> … … 43 42 #include <fcntl.h> 44 43 #include <sys/stat.h> 45 46 /* these should always be included */ 44 45 /* these should always be included */ 47 46 #include "debug.h" 48 47 #include "plugin.h" 49 48 #include "cfg.h" 50 49 51 50 #ifdef WITH_DMALLOC 52 51 #include <dmalloc.h> 53 #endif /* 54 */ 55 52 #endif 53 56 54 #define FIFO_BUFFER_SIZE 80 57 55 58 56 typedef struct _FifoData { 59 60 char *path; 61 62 int input; 63 64 int created; 65 66 57 char *path; 58 int input; 59 int created; 67 60 } FifoData; 68 61 69 70 62 static char Section[] = "Plugin:FIFO"; 71 72 73 63 static FifoData fd; 74 75 64 static char msg[FIFO_BUFFER_SIZE]; 76 77 65 static char fifopath[1024]; 78 66 79 67 80 static void configure_fifo(void) 68 static void configure_fifo(void) 81 69 { 82 83 char *s; 84 85 s = cfg_get(Section, "fifopath", "/tmp/lcd4linux.fifo"); 86 87 if (*s == '\0') { 88 89 info("[FIFO] empty '%s.fifopath' entry from %s, assuming '/tmp/lcd4linux.fifo'", Section, cfg_source()); 90 91 strcpy(fifopath, "/tmp/lcd4linux.fifo"); 92 93 } else 94 95 strcpy(fifopath, s); 96 97 98 free(s); 99 70 char *s; 71 s = cfg_get(Section, "fifopath", "/tmp/lcd4linux.fifo"); 72 if (*s == '\0') { 73 info("[FIFO] empty '%s.fifopath' entry from %s, assuming '/tmp/lcd4linux.fifo'", Section, cfg_source()); 74 strcpy(fifopath, "/tmp/lcd4linux.fifo"); 75 } else 76 strcpy(fifopath, s); 77 free(s); 100 78 } 101 79 102 80 103 104 static void removeFifo() 81 static void removeFifo() 105 82 { 106 107 debug("Removing FIFO \"%s\"\n", fd.path); 108 109 110 if (unlink(fd.path) < 0) { 111 112 error("Could not remove FIFO \"%s\": %s\n", fd.path, strerror(errno)); 113 114 return; 115 116 } 117 118 119 fd.created = 0; 120 83 debug("Removing FIFO \"%s\"\n", fd.path); 84 if (unlink(fd.path) < 0) { 85 error("Could not remove FIFO \"%s\": %s\n", fd.path, strerror(errno)); 86 return; 87 } 88 fd.created = 0; 121 89 } 122 90 123 91 124 static void closeFifo() 92 static void closeFifo() 125 93 { 126 127 struct stat st; 128 94 struct stat st; 95 if (fd.input >= 0) { 96 close(fd.input); 97 fd.input = -1; 98 } 99 if (fd.created && (stat(fd.path, &st) == 0)) 100 removeFifo(fd); 101 } 129 102 130 if (fd.input >= 0) { 131 132 close(fd.input); 133 134 fd.input = -1; 135 136 } 137 138 139 if (fd.created && (stat(fd.path, &st) == 0)) 140 141 removeFifo(fd); 142 103 static int makeFifo() 104 { 105 if (mkfifo(fd.path, 0666) < 0) { 106 error("Couldn't create FIFO \"%s\": %s\n", fd.path, strerror(errno)); 107 return -1; 108 } 109 fd.created = 1; 110 return 0; 143 111 } 144 112 145 113 146 static int makeFifo()114 static int checkFifo() 147 115 { 148 149 if (mkfifo(fd.path, 0666) < 0) { 150 151 error("Couldn't create FIFO \"%s\": %s\n", fd.path, strerror(errno)); 152 153 return -1; 154 155 } 156 116 struct stat st; 117 if (stat(fd.path, &st) < 0) { 118 if (errno == ENOENT) { 157 119 158 fd.created = 1; 159 160 161 return 0; 162 120 /* Path doesn't exist */ 121 return makeFifo(fd); 122 } 123 error("Failed to stat FIFO \"%s\": %s\n", fd.path, strerror(errno)); 124 return -1; 125 } 126 if (!S_ISFIFO(st.st_mode)) { 127 error("\"%s\" already exists, but is not a FIFO\n", fd.path); 128 return -1; 129 } 130 return 0; 163 131 } 164 132 165 133 166 167 static int checkFifo() 134 static int openFifo() 168 135 { 169 170 struct stat st; 171 172 if (stat(fd.path, &st) < 0) { 173 174 if (errno == ENOENT) { 175 176 /* Path doesn't exist */ 177 return makeFifo(fd); 178 179 } 180 181 182 error("Failed to stat FIFO \"%s\": %s\n", fd.path, strerror(errno)); 183 184 return -1; 185 186 } 187 188 189 if (!S_ISFIFO(st.st_mode)) { 190 191 error("\"%s\" already exists, but is not a FIFO\n", fd.path); 192 193 return -1; 194 195 } 196 197 198 return 0; 199 136 if (checkFifo() < 0) 137 return -1; 138 fd.input = open(fd.path, O_RDONLY | O_NONBLOCK); 139 if (fd.input < 0) { 140 error("Could not open FIFO \"%s\" for reading: %s\n", fd.path, strerror(errno)); 141 closeFifo(); 142 return -1; 143 } 144 return 0; 200 145 } 201 146 202 147 203 static int openFifo()148 static void fiforead(RESULT * result) 204 149 { 205 206 if (checkFifo() < 0) 207 208 return -1; 209 150 char buf[FIFO_BUFFER_SIZE]; 151 int i, bytes = 1; 152 memset(buf, 0, FIFO_BUFFER_SIZE); 153 strcat(buf, "ERROR"); 154 if (checkFifo() == 0) { 155 memset(buf, 0, FIFO_BUFFER_SIZE); 156 while (bytes > 0 && errno != EINTR) 157 bytes = read(fd.input, buf, FIFO_BUFFER_SIZE); 158 if (bytes < 0) { 159 error("[FIFO] Error %i: %s\n", errno, strerror(errno)); 160 } else { 161 if (strlen(buf) > 0) { 162 strcpy(msg, buf); 163 } 164 for (i = 0; i < strlen(buf); i++) { 165 if (msg[i] < 0x20) 166 msg[i] = ' '; 167 } 168 } 169 } 210 170 211 fd.input = open(fd.path, O_RDONLY | O_NONBLOCK); 212 213 if (fd.input < 0) { 214 215 error("Could not open FIFO \"%s\" for reading: %s\n", fd.path, strerror(errno)); 216 217 closeFifo(); 218 219 return -1; 220 221 } 222 223 224 return 0; 225 171 /* store result */ 172 SetResult(&result, R_STRING, msg); 226 173 } 227 174 228 175 229 static void fiforead(RESULT * result) 176 /* plugin initialization */ 177 int plugin_init_fifo(void) 230 178 { 231 232 char buf[FIFO_BUFFER_SIZE]; 233 234 int i, bytes = 1; 235 236 memset(buf, 0, FIFO_BUFFER_SIZE); 237 238 239 strcat(buf, "ERROR"); 240 241 if (checkFifo() == 0) { 242 243 memset(buf, 0, FIFO_BUFFER_SIZE); 244 245 while (bytes > 0 && errno != EINTR) 246 247 bytes = read(fd.input, buf, FIFO_BUFFER_SIZE); 248 249 250 if (bytes < 0) { 251 252 error("[FIFO] Error %i: %s\n", errno, strerror(errno)); 253 254 } else { 255 256 257 if (strlen(buf) > 0) { 258 259 strcpy(msg, buf); 260 261 } 262 263 264 for (i = 0; i < strlen(buf); i++) { 265 266 if (msg[i] < 0x20) 267 268 msg[i] = ' '; 269 270 } 271 272 } 273 274 } 275 276 277 /* store result */ 278 SetResult(&result, R_STRING, msg); 279 179 configure_fifo(); 180 fd.path = fifopath; 181 fd.input = -1; 182 fd.created = 0; 183 if (openFifo() < 0) { 184 return -1; 185 } 186 memset(msg, 0, FIFO_BUFFER_SIZE); 187 AddFunction("fifo::read", 0, fiforead); 188 return 0; 280 189 } 281 190 282 191 192 void plugin_exit_fifo(void) 193 { 283 194 284 /* plugin initialization */ 285 int plugin_init_fifo(void) 286 { 287 288 configure_fifo(); 289 290 291 fd.path = fifopath; 292 293 fd.input = -1; 294 295 fd.created = 0; 296 297 298 if (openFifo() < 0) { 299 300 return -1; 301 195 /* close filedescriptors */ 196 closeFifo(); 302 197 } 303 304 305 memset(msg, 0, FIFO_BUFFER_SIZE);306 307 AddFunction("fifo::read", 0, fiforead);308 309 310 return 0;311 312 }313 314 315 void plugin_exit_fifo(void)316 {317 318 /* close filedescriptors */319 closeFifo();320 321 }
