pud: fix position file stat/use race
[olsrd.git] / lib / pud / src / posFile.c
1 #include "posFile.h"
2
3 /* Plugin includes */
4 #include "pud.h"
5 #include "configTools.h"
6
7 /* OLSR includes */
8
9 /* System includes */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <string.h>
14 #include <regex.h>
15 #include <sys/stat.h>
16
17 /** the maximal length of a line that is read from the file */
18 #define LINE_LENGTH 256
19
20 /** regular expression describing a comment */
21 static const char * regexCommentString = "^([[:space:]]*|[[:space:]#]+.*)$";
22
23 /** regular expression describing a key/value pair */
24 static const char * regexNameValueString =
25                 "^[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+)[[:space:]]*$";
26
27 /** the number of matches in regexNameValueString */
28 static const size_t regexNameValuematchCount = 3;
29
30 /** the compiled regular expression describing a comment */
31 static regex_t regexComment;
32
33 /** the compiled regular expression describing a key/value pair */
34 static regex_t regexNameValue;
35
36 /** true when the plugin has been started */
37 static bool started = false;
38
39 /** type to hold the cached stat result */
40 typedef struct _CachedStat {
41         time_t timeStamp; /* Time of last modification (second resolution) */
42 } CachedStat;
43
44 /** the cached stat result */
45 static CachedStat cachedStat;
46
47 /**
48  * Initialises the positionFile reader.
49  * @return true upon success, false otherwise
50  */
51 bool startPositionFile(void) {
52         if (started) {
53                 return true;
54         }
55
56         if (regcomp(&regexComment, regexCommentString, REG_EXTENDED | REG_ICASE)) {
57                 pudError(false, "Could not compile regex \"%s\"", regexCommentString);
58                 return false;
59         }
60
61         if (regcomp(&regexNameValue, regexNameValueString, REG_EXTENDED | REG_ICASE)) {
62                 pudError(false, "Could not compile regex \"%s\"", regexNameValueString);
63                 regfree(&regexComment);
64                 return false;
65         }
66
67         cachedStat.timeStamp = -1;
68
69         started = true;
70         return true;
71 }
72
73 /**
74  * Cleans up the positionFile reader.
75  */
76 void stopPositionFile(void) {
77         if (started) {
78                 regfree(&regexNameValue);
79                 regfree(&regexComment);
80                 started = false;
81         }
82 }
83
84 /**
85  * Performs a regex match
86  * @param regex the compiled regex to match against
87  * @param line the line to match
88  * @param nmatch the number of matches to produce
89  * @param pmatch the array with match information
90  * @return true upon success, false otherwise
91  */
92 static bool regexMatch(regex_t * regex, char * line, size_t nmatch, regmatch_t pmatch[]) {
93         int result = regexec(regex, line, nmatch, pmatch, 0);
94         if (!result) {
95                 return true;
96         }
97
98         if (result == REG_NOMATCH) {
99                 return false;
100         }
101
102         {
103                 char msgbuf[256];
104                 regerror(result, regex, msgbuf, sizeof(msgbuf));
105                 pudError(false, "Regex match failed: %s", msgbuf);
106         }
107
108         return false;
109 }
110
111 /** the buffer in which to store a line read from the file */
112 static char line[LINE_LENGTH];
113
114 /**
115  * Read the position file
116  * @param fileName the filename
117  * @param nmeaInfo the NMEA data
118  */
119 bool readPositionFile(char * fileName, nmeaINFO * nmeaInfo) {
120         bool retval = false;
121         int fd;
122         struct stat statBuf;
123         nmeaINFO result;
124         FILE * fp = NULL;
125         unsigned int lineNumber = 0;
126         char * name = NULL;
127         char * value = NULL;
128
129         fd = open(fileName, O_RDONLY);
130         if (fd < 0) {
131                 /* could not access the file */
132                 goto out;
133         }
134
135         if (fstat(fd, &statBuf)) {
136                 /* could not access the file */
137                 goto out;
138         }
139
140         if (!memcmp(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp))) {
141                 /* file did not change since last read */
142                 goto out;
143         }
144
145         fp = fdopen(fd, "r");
146         if (!fp) {
147                 goto out;
148         }
149
150         nmea_zero_INFO(&result);
151         result.sig = POSFILE_DEFAULT_SIG;
152         result.fix = POSFILE_DEFAULT_FIX;
153         result.HDOP = POSFILE_DEFAULT_HDOP;
154         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
155         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
156         result.lat = POSFILE_DEFAULT_LAT;
157         result.lon = POSFILE_DEFAULT_LON;
158         result.elv = POSFILE_DEFAULT_ELV;
159         result.speed = POSFILE_DEFAULT_SPEED;
160         result.track = POSFILE_DEFAULT_TRACK;
161         result.mtrack = POSFILE_DEFAULT_MTRACK;
162         result.magvar = POSFILE_DEFAULT_MAGVAR;
163
164         memcpy(&cachedStat.timeStamp, &statBuf.st_mtime, sizeof(cachedStat.timeStamp));
165
166         while (fgets(line, LINE_LENGTH, fp)) {
167                 regmatch_t pmatch[regexNameValuematchCount];
168
169                 lineNumber++;
170
171                 if (regexMatch(&regexComment, line, 0, NULL)) {
172                         continue;
173                 }
174
175                 if (!regexMatch(&regexNameValue, line, regexNameValuematchCount, pmatch)) {
176                         pudError(false, "Position file \"%s\", line %d uses invalid syntax: %s", fileName, lineNumber, line);
177                         goto out;
178                 }
179
180                 /* determine name/value */
181                 name = &line[pmatch[1].rm_so];
182                 line[pmatch[1].rm_eo] = '\0';
183                 value = &line[pmatch[2].rm_so];
184                 line[pmatch[2].rm_eo] = '\0';
185
186                 if (!strncasecmp(POSFILE_NAME_SIG, name, sizeof(line))) {
187                         if (!strncasecmp(POSFILE_VALUE_SIG_BAD, value, sizeof(line))) {
188                                 result.sig = NMEA_SIG_BAD;
189                         } else if (!strncasecmp(POSFILE_VALUE_SIG_LOW, value, sizeof(line))) {
190                                 result.sig = NMEA_SIG_LOW;
191                         } else if (!strncasecmp(POSFILE_VALUE_SIG_MID, value, sizeof(line))) {
192                                 result.sig = NMEA_SIG_MID;
193                         } else if (!strncasecmp(POSFILE_VALUE_SIG_HIGH, value, sizeof(line))) {
194                                 result.sig = NMEA_SIG_HIGH;
195                         } else {
196                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
197                                                 " valid values are [%s|%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_SIG,
198                                                 POSFILE_VALUE_SIG_BAD, POSFILE_VALUE_SIG_LOW, POSFILE_VALUE_SIG_MID, POSFILE_VALUE_SIG_HIGH);
199                                 goto out;
200                         }
201                         nmea_INFO_set_present(&result.present, SIG);
202                 } else if (!strncasecmp(POSFILE_NAME_FIX, name, sizeof(line))) {
203                         if (!strncasecmp(POSFILE_VALUE_FIX_BAD, value, sizeof(line))) {
204                                 result.fix = NMEA_FIX_BAD;
205                         } else if (!strncasecmp(POSFILE_VALUE_FIX_2D, value, sizeof(line))) {
206                                 result.fix = NMEA_FIX_2D;
207                         } else if (!strncasecmp(POSFILE_VALUE_FIX_3D, value, sizeof(line))) {
208                                 result.fix = NMEA_FIX_3D;
209                         } else {
210                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
211                                                 " valid values are [%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_FIX, POSFILE_VALUE_FIX_BAD,
212                                                 POSFILE_VALUE_FIX_2D, POSFILE_VALUE_FIX_3D);
213                                 goto out;
214                         }
215                         nmea_INFO_set_present(&result.present, FIX);
216                 } else if (!strncasecmp(POSFILE_NAME_HDOP, name, sizeof(line))) {
217                         double val;
218                         if (!readDouble(POSFILE_NAME_HDOP, value, &val)) {
219                                 goto out;
220                         }
221
222                         result.HDOP = val;
223                         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
224                         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
225                         nmea_INFO_set_present(&result.present, HDOP);
226                         nmea_INFO_set_present(&result.present, VDOP);
227                         nmea_INFO_set_present(&result.present, PDOP);
228                 } else if (!strncasecmp(POSFILE_NAME_LAT, name, sizeof(line))) {
229                         double val;
230                         if (!readDouble(POSFILE_NAME_LAT, value, &val)) {
231                                 goto out;
232                         }
233
234                         result.lat = val;
235                         nmea_INFO_set_present(&result.present, LAT);
236                 } else if (!strncasecmp(POSFILE_NAME_LON, name, sizeof(line))) {
237                         double val;
238                         if (!readDouble(POSFILE_NAME_LON, value, &val)) {
239                                 goto out;
240                         }
241
242                         result.lon = val;
243                         nmea_INFO_set_present(&result.present, LON);
244                 } else if (!strncasecmp(POSFILE_NAME_ELV, name, sizeof(line))) {
245                         double val;
246                         if (!readDouble(POSFILE_NAME_ELV, value, &val)) {
247                                 goto out;
248                         }
249
250                         result.elv = val;
251                         nmea_INFO_set_present(&result.present, ELV);
252                 } else if (!strncasecmp(POSFILE_NAME_SPEED, name, sizeof(line))) {
253                         double val;
254                         if (!readDouble(POSFILE_NAME_SPEED, value, &val)) {
255                                 goto out;
256                         }
257
258                         result.speed = val;
259                         nmea_INFO_set_present(&result.present, SPEED);
260                 } else if (!strncasecmp(POSFILE_NAME_TRACK, name, sizeof(line))) {
261                         double val;
262                         if (!readDouble(POSFILE_NAME_TRACK, value, &val)) {
263                                 goto out;
264                         }
265
266                         result.track = val;
267                         nmea_INFO_set_present(&result.present, TRACK);
268                 } else if (!strncasecmp(POSFILE_NAME_MTRACK, name, sizeof(line))) {
269                         double val;
270                         if (!readDouble(POSFILE_NAME_MTRACK, value, &val)) {
271                                 goto out;
272                         }
273
274                         result.mtrack = val;
275                         nmea_INFO_set_present(&result.present, MTRACK);
276                 } else if (!strncasecmp(POSFILE_NAME_MAGVAR, name, sizeof(line))) {
277                         double val;
278                         if (!readDouble(POSFILE_NAME_MAGVAR, value, &val)) {
279                                 goto out;
280                         }
281
282                         result.magvar = val;
283                         nmea_INFO_set_present(&result.present, MAGVAR);
284                 } else {
285                         pudError(false, "Position file \"%s\", line %d uses an invalid option \"%s\","
286                                         " valid options are [%s|%s|%s|%s|%s|%s|%s|%s|%s|%s]", fileName, lineNumber, name, POSFILE_NAME_SIG,
287                                         POSFILE_NAME_FIX, POSFILE_NAME_HDOP, POSFILE_NAME_LAT, POSFILE_NAME_LON, POSFILE_NAME_ELV,
288                                         POSFILE_NAME_SPEED, POSFILE_NAME_TRACK, POSFILE_NAME_MTRACK, POSFILE_NAME_MAGVAR);
289                         goto out;
290                 }
291         }
292
293         fclose(fp);
294         fp = 0;
295
296         result.smask = POSFILE_DEFAULT_SMASK;
297         nmea_INFO_set_present(&result.present, SMASK);
298
299         nmea_INFO_sanitise(&result);
300         nmea_INFO_unit_conversion(&result);
301
302         memcpy(nmeaInfo, &result, sizeof(result));
303         retval = true;
304
305         out: if (fp) {
306                 fclose(fp);
307         }
308         if (fd) {
309                 close(fd);
310         }
311         return retval;
312 }