1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/**
* Utilities for the quality of service module mod_qos.
*
* See http://opensource.adnovum.ch/mod_qos/ for further
* details.
*
* Copyright (C) 2007-2012 Pascal Buchbinder
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
static const char revision[] = "$Id: qs_util.c,v 1.11 2012/02/28 19:07:08 pbuchbinder Exp $";
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "qs_util.h"
/* ----------------------------------
* global stat counter
* ---------------------------------- */
static time_t m_qs_expiration = 60 * 10;
/* mutex for counter access */
static pthread_mutex_t m_qs_lock_cs;
/* online/offline mode */
static int m_qs_offline = 0;
/* internal clock for offline analysis
* stores time in seconds */
static time_t m_qs_virtualSystemTime = 0;
/* ----------------------------------
* functions
* ---------------------------------- */
/**
* man:
* - escape special chars, like "\" and "-"
* - wipe leading spaces
* - wipe tailing LF
*/
void qs_man_print(int man, const char *fmt, ...) {
char bufin[4096];
char bufout[4096];
va_list args;
int i = 0;
int j = 0;
memset(bufin, 0, 4096);
va_start(args, fmt);
vsprintf(bufin, fmt, args);
if(man) {
// wipe leading spaces
// while(bufin[i] == ' ' && bufin[i+1] == ' ') {
while(bufin[i] == ' ') {
i++;
}
}
while(bufin[i] && j < 4000) {
// escape "\\" and "-" for man page
if(man && (bufin[i] == '\\' || bufin[i] == '-')) {
bufout[j] = '\\';
j++;
}
if(bufin[i] == '\n') {
if(man) {
// skip LF for man page
i++;
} else {
// keep LF
bufout[j] = bufin[i];
i++;
j++;
}
} else {
// standard char
bufout[j] = bufin[i];
i++;
j++;
}
}
bufout[j] = '\0';
printf("%s", bufout);
if(man) {
printf(" ");
}
}
// escape only
void qs_man_println(int man, const char *fmt, ...) {
char bufin[4096];
char bufout[4096];
va_list args;
int i = 0;
int j = 0;
memset(bufin, 0, 4096);
va_start(args, fmt);
vsprintf(bufin, fmt, args);
while(bufin[i] && j < 4000) {
// escape "\\" and "-" for man page
if(man && (bufin[i] == '\\' || bufin[i] == '-')) {
bufout[j] = '\\';
j++;
}
// standard char
bufout[j] = bufin[i];
i++;
j++;
}
bufout[j] = '\0';
printf("%s", bufout);
}
char *qs_CMD(const char *cmd) {
char *buf = calloc(1024, 1);
int i = 0;
while(cmd[i] && i < 1023) {
buf[i] = toupper(cmd[i]);
i++;
}
buf[i] = '\0';
return buf;
}
/* io --------------------------------------------------------- */
/*
* reads a line from stdin
*
* @param s Buffer to write line to
* @param n Length of the buffer
* @return 0 on EOF, or 1 if there is more data to read
*/
int qs_getLine(char *s, int n) {
int i = 0;
while (1) {
s[i] = (char)getchar();
if(s[i] == EOF) return 0;
if (s[i] == CR) {
s[i] = getchar();
}
if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
s[i] = '\0';
return 1;
}
++i;
}
}
/*
* reads a line from file
*
* @param s Buffer to write line to
* @param n Length of the buffer
* @return 0 on EOF, or 1 if there is more data to read
*/
int qs_getLinef(char *s, int n, FILE *f) {
register int i = 0;
while (1) {
s[i] = (char) fgetc(f);
if (s[i] == CR) {
s[i] = fgetc(f);
}
if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
s[i] = '\0';
return (feof(f) ? 1 : 0);
}
++i;
}
}
/* time ------------------------------------------------------- */
/*
* We implement our own time which is either
* the system time (real time) or the time from
* the access log lines (offline) if m_qs_offline
* has been set (use qs_set2OfflineMode() to enable
* the offline mode).
*
* @param tme Set to the time since the Epoch in seconds.
*/
void qs_time(time_t *tme) {
if(m_qs_offline) {
/* use virtual time from the access log */
*tme = m_qs_virtualSystemTime;
} else {
time(tme);
}
}
/**
* Sets time measurement (qs_time()) to offline mode.
*/
void qs_set2OfflineMode() {
m_qs_offline = 1;
}
/*
* Updates the virtual time.
*/
void qs_setTime(time_t tme) {
m_qs_virtualSystemTime = tme;
}
/* synchronisation -------------------------------------------- */
/*
* locks all counter
*/
void qs_csLock() {
pthread_mutex_lock(&m_qs_lock_cs);
}
/*
* unlocks all counter
*/
void qs_csUnLock() {
pthread_mutex_unlock(&m_qs_lock_cs);
}
/*
* init locks
*/
void qs_csInitLock() {
pthread_mutex_init(&m_qs_lock_cs, NULL);
}
/* events ----------------------------------------------------- */
/*
* sets the expiration for events
*/
void qs_setExpiration(time_t sec) {
m_qs_expiration = sec;
}
/*
* creates a new event entry
*/
qs_event_t *qs_newEvent(char *id) {
qs_event_t *ev = calloc(sizeof(qs_event_t), 1);
ev->id = calloc(strlen(id) + 1, 1);
strcpy(ev->id, id);
qs_time(&ev->time);
ev->count = 1;
return ev;
}
/*
* deletes an event
*/
void qs_freeEvent(qs_event_t *ev) {
free(ev->id);
free(ev);
}
/**
* Inserts an event entry
*
* @param l_qs_event Pointer to the event list.
* @param id Identifer, e.g. IP address or user tracking cookie
*
* @return event counter (number of updates) for the provided id
*/
int qs_insertEvent(qs_event_t **l_qs_event, char *id) {
qs_event_t *lp = *l_qs_event; /** current entry to process */
qs_event_t *lpl = lp;
time_t gmt_time;
qs_time(&gmt_time);
if(*l_qs_event == NULL) {
*l_qs_event = qs_newEvent(id);
return 1;
}
while(lp) {
/* delete expired event */
if(lp->time < (gmt_time - m_qs_expiration)) {
qs_event_t *tmp = lp;
if(lpl == lp) {
/* first element */
lpl = lp->next;
lp = lp->next;
*l_qs_event = lpl;
} else {
lpl->next = lp->next;
lp = lp->next;
}
qs_freeEvent(tmp);
}
/* update time of existing event */
if((lp != NULL) && (strcmp(lp->id, id) == 0)) {
qs_time(&lp->time);
lp->count++;
return lp->count;
}
if(lp != NULL) {
lpl = lp;
lp = lp->next;
}
}
/* not found, insert new event */
if(lpl == NULL) {
/* list has become empty */
lpl = qs_newEvent(id);
*l_qs_event = lpl;
} else {
lpl->next = qs_newEvent(id);
}
return 1;
}
/**
* Deletes the specified event.
*
* @param l_qs_event Pointer to the event list.
* @param id Identifer, e.g. IP address or user tracking cookie
*/
void qs_deleteEvent(qs_event_t **l_qs_event, char *id) {
qs_event_t *lp = *l_qs_event;
qs_event_t *lpl = lp;
if(*l_qs_event == NULL) {
return;
}
while(lp) {
if(strcmp(lp->id, id) == 0) {
qs_event_t *tmp = lp;
if(lpl == lp) {
/* first element */
lpl = lp->next;
lp = lp->next;
*l_qs_event = lpl;
} else {
lpl->next = lp->next;
lp = lp->next;
}
qs_freeEvent(tmp);
return;
}
if(lp != NULL) {
lpl = lp;
lp = lp->next;
}
}
}
/**
* Runs garbage collection (deletes expired events)
*
* @param l_qs_event Pointer to the event list.
*/
void qs_GCEvent(qs_event_t **l_qs_event) {
qs_event_t *lp = *l_qs_event;
qs_event_t *lpl = lp;
time_t gmt_time;
qs_time(&gmt_time);
if(*l_qs_event == NULL) {
return;
}
while(lp) {
/* delete expired event */
if(lp->time < (gmt_time - m_qs_expiration)) {
qs_event_t *tmp = lp;
if(lpl == lp) {
/* first element */
lpl = lp->next;
lp = lp->next;
*l_qs_event = lpl;
} else {
lpl->next = lp->next;
lp = lp->next;
}
qs_freeEvent(tmp);
}
if(lp != NULL) {
lpl = lp;
lp = lp->next;
}
}
}
/**
* Returns the number of events in the list
*
* @param id Identifer, e.g. IP address or user tracking cookie
* @return Number of entries
*/
long qs_countEvent(qs_event_t **l_qs_event) {
qs_event_t *lp = *l_qs_event;
qs_event_t *lpl = lp;
long count = 0;
time_t gmt_time;
qs_time(&gmt_time);
while(lp) {
/* delete expired entries */
if(lp->time < (gmt_time - m_qs_expiration)) {
qs_event_t *tmp = lp;
if(lpl == lp) {
/* first element */
lpl = lp->next;
lp = lp->next;
*l_qs_event = lpl;
} else {
lpl->next = lp->next;
lp = lp->next;
}
qs_freeEvent(tmp);
}
if(lp != NULL) {
lpl = lp;
lp = lp->next;
count++;
}
}
return count;
}
|