| 1 |
/* file_iterate: Reliable, fast monitoring of files |
| 2 |
* Author: Dave Taht |
| 3 |
* Date: 22 Nov 2015 |
| 4 |
* Copyright (C) 2015-6 Michael David Taht |
| 5 |
* Copyright (C) 2015-6 Toke Høiland-Jørgensen |
| 6 |
|
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <locale.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <iconv.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <math.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/wait.h> |
| 35 |
#include <sys/timerfd.h> |
| 36 |
|
| 37 |
/* |
| 38 |
openwrt's ash shell does not have usleep, |
| 39 |
nor nanosecond time in the date command. |
| 40 |
|
| 41 |
for i in $(seq $count); do |
| 42 |
cat $filename |
| 43 |
date '+Time: %s.%N' |
| 44 |
echo "---" |
| 45 |
sleep $interval |
| 46 |
done |
| 47 |
|
| 48 |
So this C program (which is more accurate and lighter weight |
| 49 |
than the shell script) needs to be used on things like openwrt, |
| 50 |
when high resolution data is needed. |
| 51 |
*/ |
| 52 |
|
| 53 |
#define BUFFERSIZE (1024*1024) |
| 54 |
#define NSEC_PER_SEC (1000000000.0) |
| 55 |
|
| 56 |
struct arg { |
| 57 |
int count; |
| 58 |
struct timespec interval; |
| 59 |
double finterval; |
| 60 |
char *filename; |
| 61 |
char *command; |
| 62 |
char buffer; |
| 63 |
}; |
| 64 |
|
| 65 |
typedef struct arg args; |
| 66 |
|
| 67 |
static const struct option long_options[] = { |
| 68 |
{ "count" , required_argument , NULL , 'c' } , |
| 69 |
{ "interval" , required_argument , NULL , 'I' } , |
| 70 |
{ "help" , no_argument , NULL , 'h' } , |
| 71 |
{ "buffer" , no_argument , NULL , 'b' } , |
| 72 |
}; |
| 73 |
|
| 74 |
void usage (char *err) { |
| 75 |
if(err) fprintf(stderr,"%s\n",err); |
| 76 |
printf("file_iterate [options]\n"); |
| 77 |
printf( |
| 78 |
"\t-h --help \n" |
| 79 |
"\t-b --buffer \n" |
| 80 |
"\t-c --count [number of iterations]\n" |
| 81 |
"\t-I --interval [fractional number of seconds]\n" |
| 82 |
"\t-f --filename [filename]\n"); |
| 83 |
exit(-1); |
| 84 |
} |
| 85 |
|
| 86 |
static void defaults(args *a) { |
| 87 |
a->filename = NULL; |
| 88 |
a->finterval=.2; |
| 89 |
a->count=10; |
| 90 |
a->interval.tv_nsec = 0; |
| 91 |
a->interval.tv_sec = 0; |
| 92 |
a->buffer = 0; |
| 93 |
} |
| 94 |
|
| 95 |
#define QSTRING "c:I:f:hb" |
| 96 |
|
| 97 |
int process_options(int argc, char **argv, args *o) |
| 98 |
{ |
| 99 |
int option_index = 0; |
| 100 |
int opt = 0; |
| 101 |
optind = 1; |
| 102 |
|
| 103 |
while(1) |
| 104 |
{ |
| 105 |
opt = getopt_long(argc, argv, |
| 106 |
QSTRING, |
| 107 |
long_options, &option_index); |
| 108 |
if(opt == -1) break; |
| 109 |
|
| 110 |
switch (opt) |
| 111 |
{ |
| 112 |
case 'c': o->count = strtoul(optarg,NULL,10); break; |
| 113 |
case 'I': o->finterval = strtod(optarg,NULL); break; |
| 114 |
case 'f': o->filename = optarg; break; |
| 115 |
case 'b': o->buffer = 1; break; |
| 116 |
case '?': |
| 117 |
case 'h': usage(NULL); break; |
| 118 |
default: usage(NULL); |
| 119 |
} |
| 120 |
} |
| 121 |
o->interval.tv_sec = floor(o->finterval); |
| 122 |
o->interval.tv_nsec = (long long) ((o->finterval - o->interval.tv_sec) * NSEC_PER_SEC); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static int read_once(char *filename, char *buffer, int bufsize) { |
| 127 |
int fp = open(filename, O_RDONLY); |
| 128 |
ssize_t len = 0, r = 0; |
| 129 |
if(!fp) { |
| 130 |
perror("Unable to open file"); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
while((r = read(fp, buffer, bufsize-len)) > 0) |
| 135 |
len += r; |
| 136 |
|
| 137 |
return len; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
static void result(int out, int size, int bufsize, char *buffer) { |
| 142 |
struct timespec cur_time; |
| 143 |
if(bufsize - size > 40) { |
| 144 |
clock_gettime(CLOCK_REALTIME, &cur_time); |
| 145 |
int added = sprintf(&buffer[size],"Time: %ld.%09ld\n---\n", |
| 146 |
cur_time.tv_sec,cur_time.tv_nsec); |
| 147 |
write(out,buffer,size+added); |
| 148 |
} else { |
| 149 |
write(2,"Buffer Overrun\n",sizeof("Buffer Overrun\n")); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Since this is linux only we can use timerfd for an isochronous clock |
| 154 |
|
| 155 |
int run(args *a) |
| 156 |
{ |
| 157 |
char tmpfile[] = "/tmp/file_iterateXXXXXX"; |
| 158 |
int out = a->buffer ? mkstemp(tmpfile) : STDOUT_FILENO; |
| 159 |
|
| 160 |
if(a->buffer && !out) { |
| 161 |
perror("Unable to create tmpfile"); |
| 162 |
exit(-1); |
| 163 |
} |
| 164 |
|
| 165 |
if (!a->filename) |
| 166 |
usage("Must specify filename"); |
| 167 |
|
| 168 |
struct itimerspec new_value = {0}; |
| 169 |
|
| 170 |
int timer = timerfd_create(CLOCK_REALTIME, 0); |
| 171 |
//new_value.it_interval.tv_nsec = 100000000; // 10ms interval |
| 172 |
//new_value.it_interval.tv_sec = 0; |
| 173 |
new_value.it_interval = a->interval; |
| 174 |
new_value.it_value = a->interval; |
| 175 |
|
| 176 |
/* better method would be to poll for input (since writes from the |
| 177 |
tool could block or return no output for some reason), timestamp the input, |
| 178 |
and if the difference is less than half, skip this round. |
| 179 |
this would absorb non-completing stuff */ |
| 180 |
|
| 181 |
char buffer[BUFFERSIZE]; |
| 182 |
if (read_once(a->filename, buffer, sizeof(buffer)) <= 0) { |
| 183 |
perror("Unable to read file"); |
| 184 |
exit(-1); |
| 185 |
} |
| 186 |
int size = 0; |
| 187 |
|
| 188 |
int ctr = 0; |
| 189 |
timerfd_settime(timer,0,&new_value,NULL); // relative timer |
| 190 |
|
| 191 |
do { |
| 192 |
long long fired; |
| 193 |
if(read(timer,&fired,sizeof(fired))!=8) perror("reading timer"); |
| 194 |
ctr+=fired; |
| 195 |
if((size = read_once(a->filename,buffer,sizeof(buffer))) > 0) { |
| 196 |
result(out,size,BUFFERSIZE,buffer); |
| 197 |
} else { |
| 198 |
result(out,0,BUFFERSIZE,buffer); |
| 199 |
perror("reading file"); |
| 200 |
} |
| 201 |
} while (ctr < a->count); |
| 202 |
|
| 203 |
if(a->buffer) { |
| 204 |
lseek(out, 0, SEEK_SET); |
| 205 |
while((size = read(out, buffer, sizeof(buffer))) > 0) |
| 206 |
write(STDOUT_FILENO,buffer,size); |
| 207 |
unlink(tmpfile); |
| 208 |
} |
| 209 |
close(out); |
| 210 |
wait(NULL); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
int main(int argc,char **argv) { |
| 215 |
args a; |
| 216 |
int status = 0; |
| 217 |
defaults(&a); |
| 218 |
process_options(argc, argv, &a); |
| 219 |
run(&a); |
| 220 |
return status; |
| 221 |
} |