forked from inteos/pgsql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
506 lines (430 loc) · 11 KB
/
utils.c
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (c) 2013 by Inteos sp. z o.o.
* All rights reserved. See LICENSE.Inteos for details.
*
* Common definitions and utility functions for Inteos utils.
* Functions defines a common framework used in all utilities and plugins.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#ifndef __WIN32__
#include <grp.h>
#else
#include <windef.h>
#include <winbase.h>
#endif
#include "utils.h"
#if 0
/*
* perform a file copy from src into dst
* copy is performed by reading a file into memory buffer and wrining it into newly
* created file
*
* in:
* src - source file
* dst - destination file
* out:
* 0 - success
* 1 - error
*/
int _copy_file ( char * src, char * dst ){
int fdsrc, fddst;
int err;
struct stat file_stat;
void * filebuf;
fddst = open ( dst, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR );
if ( fddst < 0 ){
logprg ( LOGERROR, "Destination WAL access problem:" );
logprg ( LOGERROR, strerror ( errno ) );
return 1;
}
err = stat ( src, &file_stat );
if ( err ) {
close ( fddst );
return 1;
}
fdsrc = open ( src, O_RDONLY );
if ( fdsrc < 0 ){
close ( fddst );
return 1;
}
/* TODO: check if mmaping a file instead of read/write will be more efective */
filebuf = malloc ( file_stat.st_size );
if ( ! filebuf ){
close ( fddst );
close ( fdsrc );
return 1;
}
err = read ( fdsrc, filebuf, file_stat.st_size );
if ( err != file_stat.st_size ){
close ( fddst );
close ( fdsrc );
FREE ( filebuf );
return 1;
}
err = write ( fddst, filebuf, file_stat.st_size );
if ( err != file_stat.st_size ){
FREE ( filebuf );
close ( fddst );
close ( fdsrc );
return 1;
}
FREE ( filebuf );
close ( fddst );
close ( fdsrc );
err = chown ( dst, file_stat.st_uid, file_stat.st_gid );
return 0;
}
#endif
#ifndef __WIN32__
/*
* checks if program instance is running
*
* in:
* pidfile - program pid file
* out:
* 1 - is running
* 0 - is not running
* -1 - error
*/
int check_program_is_running ( char * pidfile ){
int pidfd;
int err;
int pid;
int out;
char buf[32];
int rc = 0;
struct stat st;
ASSERT_NVAL_RET_NONE ( pidfile );
err = stat ( pidfile, &st );
ASSERT_VAL_RET_NONE ( err );
pidfd = open ( pidfile, O_RDONLY );
if ( pidfd > 0 ){
/* file exist, check if program is running on that pid, if so
* read contents (pid number encoded as ascii number) */
err = read ( pidfd, buf, sizeof ( buf ) );
if ( err > 2 ){
/* minimal valid file found, fd no longer needed */
close ( pidfd );
/* check for pid number in the first line of the file */
out = sscanf ( buf, "%i\n", &pid );
if ( out == 1 ){
/* valid pid number found, check process existence */
//printf ( "PID %i\n", pid );
snprintf ( buf, sizeof ( buf ), "/proc/%i/status", pid );
pidfd = open ( buf, O_RDONLY );
if ( pidfd > 0 ){
close ( pidfd );
rc = 1;
}
}
} else {
close ( pidfd );
rc = 0;
}
}
return rc;
}
#endif
/*
* standard readline
*/
int readline ( int fd, char * buf, int size ){
char c;
int n;
for ( n = 0; n < size; n++ ){
if ( read ( fd, &c, 1 ) == 1 ){
/* read a char */
if ( c == '\n' ){
buf [ n ] = 0;
break;
} else {
buf [ n ] = c;
}
} else {
buf [ n ] = 0;
break;
}
}
return n;
}
/*
* standard readline for file stream
*/
int freadline ( FILE * stream, char * buf, int size ){
char c;
int n;
for ( n = 0; n < size; n++ ){
if ( fread ( &c, 1, 1, stream ) == 1 ){
/* read a char */
if ( c == '\n' ){
buf [ n ] = 0;
break;
} else {
buf [ n ] = c;
}
} else {
buf [ n ] = 0;
break;
}
}
return n;
}
#if 0
/*
* time format correction
*/
void _correct_time ( udtime_t * t ){
if ( t->y < 70 ){
t->y += 2000;
} else
if ( t->y < 100 ){
t->y += 1900;
}
if ( t->m < 1 )
t->m = 1;
if ( t->m > 12 )
t->m = 12;
if ( t->d < 1 )
t->d = 1;
if ( t->d > 31 )
t->d = 31;
if ( t->h < 0 )
t->h = 0;
if ( t->h > 23 )
t->h = 23;
if ( t->mi < 0 )
t->mi = 0;
if ( t->mi > 59 )
t->mi = 59;
if ( t->s < 0 )
t->s = 0;
if ( t->s > 59 )
t->s = 59;
}
/*
* scans a supplied string for date&time definition, corrects it and prepare
* a string for bacula restore
*
* input:
* str - user supplied date/time string
* output:
* computer interpreted date/time
*/
char * format_btime ( const char * str ){
char * out;
char buf[64];
udtime_t t;
memset ( &t, 0, sizeof ( t ) );
sscanf(str, "%d-%d-%d %d:%d:%d", &t.y, &t.m, &t.d, &t.h, &t.mi, &t.s );
_correct_time ( &t );
snprintf ( buf, sizeof ( buf ), "%d-%02d-%02d %02d:%02d:%02d", t.y, t.m, t.d, t.h, t.mi, t.s );
out = strndup ( buf, sizeof ( buf ) );
return out;
}
#endif
#ifdef __sun__
/*
* missing routine in Solaris
*/
int getgrouplist (const char *uname, gid_t agroup, gid_t *groups, int *grpcnt)
{
const struct group *grp;
int i, maxgroups, ngroups, ret;
ret = 0;
ngroups = 0;
maxgroups = *grpcnt;
/*
* When installing primary group, duplicate it;
* the first element of groups is the effective gid
* and will be overwritten when a setgid file is executed.
*/
groups ? groups[ngroups++] = agroup : ngroups++;
if (maxgroups > 1)
groups ? groups[ngroups++] = agroup : ngroups++;
/*
* Scan the group file to find additional groups.
*/
setgrent();
while ((grp = getgrent()) != NULL) {
if (groups) {
for (i = 0; i < ngroups; i++) {
if (grp->gr_gid == groups[i])
goto skip;
}
}
for (i = 0; grp->gr_mem[i]; i++) {
if (!strcmp(grp->gr_mem[i], uname)) {
if (ngroups >= maxgroups) {
ret = -1;
break;
}
groups ? groups[ngroups++] = grp->gr_gid : ngroups++;
break;
}
}
skip:
;
}
endgrent();
*grpcnt = ngroups;
return (ret);
}
#endif
#ifdef __WIN32__
/* Win32 missing functions */
static const char *errorString ( void ){
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
/* Strip any \r or \n */
char *rval = (char *) lpMsgBuf;
char *cp = strchr(rval, '\r');
if (cp != NULL) {
*cp = 0;
} else {
cp = strchr(rval, '\n');
if (cp != NULL)
*cp = 0;
}
return rval;
}
void *dlopen ( const char *file, int mode ){
void *handle;
handle = LoadLibrary ( file );
return handle;
}
void *dlsym ( void *handle, const char *name ){
void *symaddr;
symaddr = (void*) GetProcAddress ((HMODULE)handle, name);
return symaddr;
}
int dlclose ( void *handle ){
if (handle && !FreeLibrary ((HMODULE) handle )){
return 1;
}
return 0;
}
char *dlerror ( void ){
static char buf[200];
const char *err = errorString();
strncpy ( buf, (char *) err, sizeof(buf) );
LocalFree ( (void*) err);
return buf;
}
/* realpath implementation grabbed from TC as public domain */
char *realpath ( const char *path, char resolved_path[PATH_MAX] ){
char *return_path = NULL;
if ( path ){
if ( resolved_path ){
return_path = resolved_path;
} else {
//Non standard extension that glibc uses
return_path = (char*) malloc (PATH_MAX);
}
if ( return_path ){
//This is a Win32 API function similar to what realpath() is supposed to do
size_t size = GetFullPathNameA(path, PATH_MAX, return_path, 0);
//GetFullPathNameA() returns a size larger than buffer if buffer is too small
if ( size > PATH_MAX ){
if ( return_path != resolved_path ){
//Malloc'd buffer - Unstandard extension retry
size_t new_size;
free(return_path);
return_path = (char*) malloc(size);
if ( return_path ){
new_size = GetFullPathNameA(path, size, return_path, 0); //Try again
if ( new_size > size ){ //If it's still too large, we have a problem, don't try again
free(return_path);
return_path = 0;
errno = ENAMETOOLONG;
} else {
size = new_size;
}
} else {
//I wasn't sure what to return here, but the standard does say to return EINVAL
//if resolved_path is null, and in this case we couldn't malloc large enough buffer
errno = EINVAL;
}
} else { //resolved_path buffer isn't big enough
return_path = 0;
errno = ENAMETOOLONG;
}
}
//GetFullPathNameA() returns 0 if some path resolve problem occured
if ( !size ){
if ( return_path != resolved_path ){
//Malloc'd buffer
free ( return_path );
}
return_path = 0;
//Convert MS errors into standard errors
switch ( GetLastError() ){
case ERROR_FILE_NOT_FOUND:
errno = ENOENT;
break;
case ERROR_PATH_NOT_FOUND: case ERROR_INVALID_DRIVE:
errno = ENOTDIR;
break;
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
default: //Unknown Error
errno = EIO;
break;
}
}
//If we get to here with a valid return_path, we're still doing good
if ( return_path ){
struct stat stat_buffer;
//Make sure path exists, stat() returns 0 on success
if ( stat ( return_path, &stat_buffer ) ){
if ( return_path != resolved_path ){
free(return_path);
}
return_path = 0;
//stat() will set the correct errno for us
}
//else we succeeded!
}
} else {
errno = EINVAL;
}
} else {
errno = EINVAL;
}
return return_path;
}
#endif
/*
* checks if supplied string could be printed
*/
int strisprintable ( char * str, int len ){
int a;
for ( a = 0; a < len; a++ ){
if ( ! isprint( str[a] ) ){
return 0;
}
}
return 1;
}
#ifdef __cplusplus
}
#endif