Project

General

Profile

Submit #3063 » final_path_i_think.patch

htse, 09/25/2017 01:54 PM

View differences:

usr.bin/kcollect/kcollect.c
#define DISPLAY_TIME_ONLY "%H:%M:%S"
#define DISPLAY_FULL_DATE "%F %H:%M:%S"
#define HDR_FMT "HEADER0"
#define HDR_TITLE "HEADER1"
static void format_output(uintmax_t value,char fmt,uintmax_t scale, char* ret);
static void dump_text(kcollect_t *ary, size_t count,
......
kcollect_t **ret_ary, size_t *counter);
static void dump_fields(kcollect_t *ary);
static void adjust_fields(kcollect_t *ent, const char *fields);
static void restore_headers(kcollect_t *ary, const char *datafile);
FILE *OutFP;
int UseGMT;
int OutputWidth = 1024;
int OutputHeight = 1024;
int SmoothOpt;
int LoadedFromDB = 0;
int
main(int ac, char **av)
......
exit(1);
}
while ((ch = getopt(ac, av, "o:b:d:flsgt:xw:GW:H:")) != -1) {
while ((ch = getopt(ac, av, "o:b:d:r:flsgt:xw:GW:H:")) != -1) {
char *suffix;
switch(ch) {
......
dbmFile = optarg;
fromFile = 1;
break;
case 'r':
datafile = optarg;
cmd = 'r';
break;
case 'f':
keepalive = 1;
break;
......
if (count > 2)
dump_dbm(ary, count, datafile);
break;
case 'r':
if (count > 2)
restore_headers(ary, datafile);
break;
case 'l':
dump_fields(ary);
exit(0);
......
* Timestamp
*/
t = ary[i].realtime.tv_sec;
if (UseGMT)
if (UseGMT && !LoadedFromDB) {
tmv = gmtime(&t);
else
}
else {
tmv = localtime(&t);
}
strftime(sbuf, sizeof(sbuf), display_fmt, tmv);
printf("%8s", sbuf);
......
}
}
/* restores the DBM database header records to current machine */
static
void
restore_headers(kcollect_t *ary, const char *datafile)
{
DBM *db;
char hdr_fmt[] = HDR_FMT;
char hdr_title[] = HDR_TITLE;
datum key, value;
db = dbm_open(datafile, (O_RDWR),
(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP));
if (db == NULL) {
switch (errno) {
case EACCES:
fprintf(stderr,
"[ERR] database file \"%s\" is read-only, "
"check permissions. (%i)\n",
datafile, errno);
break;
default:
fprintf(stderr,
"[ERR] opening our database file \"%s\" "
"produced an error. (%i)\n",
datafile, errno);
}
exit(EXIT_FAILURE);
} else {
key.dptr = hdr_fmt;
key.dsize = sizeof(HDR_FMT);
value.dptr = &ary[0].data;
value.dsize = sizeof(uint64_t) * KCOLLECT_ENTRIES;
if (dbm_store(db,key,value,DBM_REPLACE) == -1) {
fprintf(stderr,
"[ERR] error storing the value in "
"the database file \"%s\" (%i)\n",
datafile, errno);
dbm_close(db);
exit(EXIT_FAILURE);
}
key.dptr = hdr_title;
key.dsize = sizeof(HDR_FMT);
value.dptr = &ary[1].data;
value.dsize = sizeof(uint64_t) * KCOLLECT_ENTRIES;
if (dbm_store(db,key,value,DBM_REPLACE) == -1) {
fprintf(stderr,
"[ERR] error storing the value in "
"the database file \"%s\" (%i)\n",
datafile, errno);
dbm_close(db);
exit(EXIT_FAILURE);
}
}
dbm_close(db);
}
/*
* Store the array of kcollect_t records in a dbm db database,
* path passed in datafile
......
datum value;
time_t t;
uint i;
char h0[] = "HEADER 0";
char h1[] = "HEADER 1";
char hdr_fmt[] = HDR_FMT;
char hdr_title[] = HDR_TITLE;
for (i = 0; i < (count); ++i) {
/* first 2 INFO records are special and get 0|1 */
t = (i < 2)? i : ary[i].realtime.tv_sec;
tmv = gmtime(&t);
strftime(buf, sizeof(buf), DISPLAY_FULL_DATE, tmv);
/* store the first 2 records with their own key */
key.dptr = (t < 2)?((t == 0)? h0:h1): buf;
key.dsize = (t < 2)?sizeof("HEADER 0"):sizeof(buf);
if( i < 2) {
if(i == 0)
key.dptr = hdr_fmt;
else
key.dptr = hdr_title;
key.dsize = sizeof(HDR_FMT);
}
else {
t = ary[i].realtime.tv_sec;
if(LoadedFromDB)
tmv = localtime(&t);
else
tmv = gmtime(&t);
strftime(buf, sizeof(buf), DISPLAY_FULL_DATE, tmv);
key.dptr = buf;
key.dsize = sizeof(buf);
}
value.dptr = ary[i].data;
value.dsize = sizeof(uint64_t) * KCOLLECT_ENTRIES;
if (dbm_store(db,key,value,DBM_INSERT) == -1) {
......
*/
memset(&tm, 0, sizeof(struct tm));
strptime(str, fmt, &tm);
ts = mktime(&tm);
ts = timegm(&tm);
return (int)ts;
}
......
if (*ret_ary)
free(*ret_ary);
*ret_ary = malloc(sizeof(kcollect_t) * (recCounter));
bzero(*ret_ary, sizeof(kcollect_t) * recCounter);
if (*ret_ary == NULL) {
fprintf(stderr,
"[ERR] failed to allocate enough memory to "
......
while (key.dptr && c < recCounter) {
value = dbm_fetch(db, key);
if (value.dptr != NULL) {
if(!strcmp(key.dptr,"HEADER 0")) {
if(!strcmp(key.dptr,HDR_FMT)) {
sc = 0;
headersFound |= 1;
}
else if(!strcmp(key.dptr,"HEADER 1")) {
else if(!strcmp(key.dptr,HDR_TITLE)) {
sc = 1;
headersFound |= 2;
}
......
fprintf(stderr, "We could not retrieve all necessary headers, might be the database file is corrupted? (%i)\n", headersFound);
exit(EXIT_FAILURE);
}
LoadedFromDB = 1;
}
static void
(5-5/5)