Changeset 871

Show
Ignore:
Timestamp:
04/12/08 18:23:52 (7 months ago)
Author:
michux
Message:

clean up

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugin_fifo.c

    r869 r871  
    2323 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
    2424 * 
    25  */   
    26      
     25 */ 
     26 
    2727/*  
    2828 * Quick fifo hack for lcd4linux 
     
    3030 * most code is ripped ... 
    3131 * 
    32  */  
    33      
     32 */ 
    3433 
    35 /* define the include files you need */  
     34/* define the include files you need */ 
    3635#include "config.h" 
    37      
     36 
    3837#include <stdlib.h> 
    3938#include <string.h> 
     
    4342#include <fcntl.h> 
    4443#include <sys/stat.h> 
    45      
    46 /* these should always be included */  
     44 
     45/* these should always be included */ 
    4746#include "debug.h" 
    4847#include "plugin.h" 
    4948#include "cfg.h" 
    50      
     49 
    5150#ifdef WITH_DMALLOC 
    5251#include <dmalloc.h> 
    53 #endif  /*  
    54  */ 
    55      
     52#endif 
     53 
    5654#define FIFO_BUFFER_SIZE 80 
    57      
     55 
    5856typedef 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; 
    6760} FifoData; 
    6861 
    69  
    7062static char Section[] = "Plugin:FIFO"; 
    71  
    72  
    7363static FifoData fd; 
    74  
    7564static char msg[FIFO_BUFFER_SIZE]; 
    76  
    7765static char fifopath[1024]; 
    7866 
    7967 
    80 static void configure_fifo(void)  
     68static void configure_fifo(void) 
    8169{ 
    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); 
    10078} 
    10179 
    10280 
    103  
    104 static void removeFifo()  
     81static void removeFifo() 
    10582{ 
    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; 
    12189} 
    12290 
    12391 
    124 static void closeFifo()  
     92static void closeFifo() 
    12593{ 
    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} 
    129102 
    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  
     103static 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; 
    143111} 
    144112 
    145113 
    146 static int makeFifo()  
     114static int checkFifo() 
    147115{ 
    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) { 
    157119 
    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; 
    163131} 
    164132 
    165133 
    166  
    167 static int checkFifo()  
     134static int openFifo() 
    168135{ 
    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; 
    200145} 
    201146 
    202147 
    203 static int openFifo()  
     148static void fiforead(RESULT * result) 
    204149{ 
    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    } 
    210170 
    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); 
    226173} 
    227174 
    228175 
    229 static void fiforead(RESULT * result)  
     176/* plugin initialization */ 
     177int plugin_init_fifo(void) 
    230178{ 
    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; 
    280189} 
    281190 
    282191 
     192void plugin_exit_fifo(void) 
     193{ 
    283194 
    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(); 
    302197} 
    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 }