summaryrefslogtreecommitdiffstats
path: root/tools/src/qs_util.c
blob: ddf1e895ce937712d380d8fa23c585763fd80837 (plain)
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
/**
 * Utilities for the quality of service module mod_qos.
 *
 * See http://mod-qos.sourceforge.net/ for further
 * details.
 *
 * Copyright (C) 2023 Pascal Buchbinder
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

static const char revision[] = "$Id: qs_util.c 2654 2022-05-13 09:12:42Z pbuchbinder $";

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>

#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
typedef pcre2_match_data* match_data_pt;
typedef size_t*           match_vector_pt;

#include "qs_util.h"

/* 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);
}

/* logs ------------------------------------------------------- */

/**
 * Keeps only the specified number of files
 *
 * @param file_name Absolute file name
 * @param generations Number of files to keep
 */
void qs_deleteOldFiles(const char *file_name, int generations) {
  DIR *dir;
  char dirname[QS_HUGE_STR];
  char *p;
  memset(dirname, 0, QS_HUGE_STR);
  if(strlen(file_name) > (QS_HUGE_STR - 12)) {
    // invalid file length
    return;
  }
  if(strrchr(file_name, '/') == NULL) {
    sprintf(dirname, "./%s", file_name);
  } else {
    strcpy(dirname, file_name);
  }
  p = strrchr(dirname, '/');
  p[0] = '\0'; p++;
  dir = opendir(dirname);
  if(dir) {
    int num = 0;
    struct dirent *de;
    char filename[QS_HUGE_STR];
    snprintf(filename, sizeof(filename), "%s.20", p);
    /* determine how many files to delete */
    while((de = readdir(dir)) != 0) {
      if(de->d_name && (strncmp(de->d_name, filename, strlen(filename)) == 0)) {
	num++;
      }
    }
    /* delete the oldest files (assumes they are ordered by their creation date) */
    while(num > generations) {
      char old[QS_HUGE_STR];
      old[0] = '\0';
      rewinddir(dir);
      while((de = readdir(dir)) != 0) {
	if(de->d_name && (strncmp(de->d_name, filename, strlen(filename)) == 0)) {
	  if(strcmp(old, de->d_name) > 0) {
	    snprintf(old, sizeof(old), "%s", de->d_name);
	  } else {
	    if(old[0] == '\0') {
	      snprintf(old, sizeof(old), "%s", de->d_name);
	    }
	  }
	}
      }
      {
	/* build abs path and delete it */
	char unl[QS_HUGE_STR];
	snprintf(unl, sizeof(unl), "%s/%s", dirname, old);
	unlink(unl);
      }
      num--;
    }
    closedir(dir);
  }
}

/* user ------------------------------------------------------- */
void qs_setuid(const char *username, const char *cmd) {
  if(username && getuid() == 0) {
    struct passwd *pwd = getpwnam(username);
    uid_t uid, gid;
    if(pwd == NULL) {
      fprintf(stderr, "[%s] failed to switch user: unknown user id '%s'\n", cmd, username);
      exit(1);
    }
    uid = pwd->pw_uid;
    gid = pwd->pw_gid;
    setgid(gid);
    setuid(uid);
    if(getuid() != uid) {
      fprintf(stderr, "[%s] setuid failed (%s,%d)\n", cmd, username, uid);
      exit(1);
    }
    if(getgid() != gid) {
      fprintf(stderr, "[%s] setgid failed (%d)\n", cmd, gid);
      exit(1);
    }
  }
}
/* pcre ------------------------------------------------------- */
int qs_pregfree(void *p) {
  qs_regfree((qs_regex_t *)p);
  return 0;
}

void qs_regfree(qs_regex_t *preg) {
  if(preg->state == 1) {
    pcre2_code_free(preg->re_pcre);
  }
}

int qs_regcomp(qs_regex_t *preg, const char *pattern, int cflags) {
  unsigned int capcount;
  size_t erroffset;
  int errcode = 0;
  int options = cflags;
  preg->state = 0;

  preg->re_pcre = pcre2_compile((const unsigned char *)pattern,
				PCRE2_ZERO_TERMINATED, options, &errcode,
				&erroffset, NULL);

  if (preg->re_pcre == NULL) {
    return 1;
  }

  pcre2_pattern_info((const pcre2_code *)preg->re_pcre,
		     PCRE2_INFO_CAPTURECOUNT, &capcount);
  preg->re_nsub = capcount;

  preg->state = 1;
  
  return 0;
}

int qs_regexec_len(const qs_regex_t *preg, const char *buff,
		   unsigned int len, unsigned int nmatch,
		   qs_regmatch_t *pmatch, int eflags) {
  int rc;
  int options = 0;
  match_vector_pt ovector = NULL;
  unsigned int ncaps = (unsigned int)preg->re_nsub + 1;
  match_data_pt data = pcre2_match_data_create(ncaps, NULL);

  if (!data) {
    return -1;
  }

  options = eflags;

  rc = pcre2_match((const pcre2_code *)preg->re_pcre,
		   (const unsigned char *)buff, len,
		   0, options, data, NULL);
  ovector = pcre2_get_ovector_pointer(data);
  
  if (rc >= 0) {
    unsigned int n = rc, i;
    if (n == 0 || n > nmatch)
      rc = n = nmatch; /* All capture slots were filled in */
    for (i = 0; i < n; i++) {
      pmatch[i].rm_so = ovector[i * 2];
      pmatch[i].rm_eo = ovector[i * 2 + 1];
    }
    for (; i < nmatch; i++) {
      pmatch[i].rm_so = pmatch[i].rm_eo = -1;
    }
    pcre2_match_data_free(data);
    return rc;
  }
  else {
    pcre2_match_data_free(data);
    
    return -1;
  }
}