This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.c
513 lines (419 loc) · 13.1 KB
/
file.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
507
508
509
510
511
512
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include "monitor.h"
/**
* Utilities for managing files used by monit.
*
* @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
* @author Christian Hopp, <chopp@iei.tu-clausthal.de>
* @author Martin Pala, <martinp@tildeslash.com>
*
* @file
*/
/* ------------------------------------------------------------------ Public */
/**
* Initialize the programs file variables
*/
void File_init() {
char pidfile[STRLEN];
char buf[STRLEN];
/* Check if the pidfile was already set during configfile parsing */
if(Run.pidfile == NULL) {
/* Set the location of this programs pidfile */
if(! getuid()) {
snprintf(pidfile, STRLEN, "%s/%s", MYPIDDIR, MYPIDFILE);
} else {
snprintf(pidfile, STRLEN, "%s/.%s", Run.Env.home, MYPIDFILE);
}
Run.pidfile= xstrdup(pidfile);
}
/* Set the location of monit's id file */
if(Run.idfile == NULL) {
snprintf(buf, STRLEN, "%s/.%s", Run.Env.home, MYIDFILE);
Run.idfile= xstrdup(buf);
}
Util_monitId(Run.idfile);
/* Set the location of monit's state file */
if(Run.statefile == NULL) {
snprintf(buf, STRLEN, "%s/.%s", Run.Env.home, MYSTATEFILE);
Run.statefile= xstrdup(buf);
}
}
/**
* Finalize and remove temporary files and make sure Monit id file exist
*/
void File_finalize() {
unlink(Run.pidfile);
// Make sure that Monit id file exist
if (! File_exist(Run.idfile)) {
FILE *f = fopen(Run.idfile,"w");
if (! f) {
LogError("%s: Error opening Monit id file '%s' for writing -- %s\n", prog, Run.idfile, STRERROR);
} else {
fprintf(f, "%s\n", Run.id);
fclose(f);
}
}
}
/**
* Get a object's last modified timestamp.
* @param object A object to stat
* @param type Requested object's type
* @return Max of either st_mtime or st_ctime or
* FALSE if not found or different type of object
*/
time_t File_getTimestamp(char *object, mode_t type) {
struct stat buf;
ASSERT(object);
if(! stat(object, &buf)) {
if(((type == S_IFREG) && S_ISREG(buf.st_mode)) ||
((type == S_IFDIR) && S_ISDIR(buf.st_mode)) ||
((type == (S_IFREG|S_IFDIR)) && (S_ISREG(buf.st_mode) ||
S_ISDIR(buf.st_mode)))
) {
return MAX(buf.st_mtime, buf.st_ctime);
} else {
LogError("%s: Invalid object type - %s\n", prog, object);
}
}
return FALSE;
}
/**
* Search the system for the monit control file. Try first ~/.monitrc,
* if that fails try /etc/monitrc, then SYSCONFDIR/monitrc (default:
* /usr/local/etc/monitrc) and finally ./monitrc.
* Exit the application if the control file was not found.
* @return The location of monits control file (monitrc)
*/
char *File_findControlFile() {
char *rcfile= xcalloc(sizeof(char), STRLEN + 1);
snprintf(rcfile, STRLEN, "%s/.%s", Run.Env.home, MONITRC);
if(File_exist(rcfile)) {
return (rcfile);
}
memset(rcfile, 0, STRLEN);
snprintf(rcfile, STRLEN, "/etc/%s", MONITRC);
if(File_exist(rcfile)) {
return (rcfile);
}
memset(rcfile, 0, STRLEN);
snprintf(rcfile, STRLEN, "%s/%s", SYSCONFDIR, MONITRC);
if(File_exist(rcfile)) {
return (rcfile);
}
memset(rcfile, 0, STRLEN);
snprintf(rcfile, STRLEN, "/usr/local/etc/%s", MONITRC);
if(File_exist(rcfile)) {
return (rcfile);
}
if(File_exist(MONITRC)) {
memset(rcfile, 0, STRLEN);
snprintf(rcfile, STRLEN, "%s/%s", Run.Env.cwd, MONITRC);
return (rcfile);
}
LogError("%s: Cannot find the control file at "
"~/.%s, /etc/%s, %s/%s, /usr/local/etc/%s or at ./%s \n",
prog, MONITRC, MONITRC, SYSCONFDIR, MONITRC, MONITRC, MONITRC);
exit(1);
}
/**
* Create a program's pidfile - Such a file is created when in daemon
* mode. The file is created with mask = MYPIDMASK (usually 644).
* @param pidfile The name of the pidfile to create
* @return TRUE if the file was created, otherwise FALSE.
*/
int File_createPidFile(char *pidfile) {
FILE *F= NULL;
ASSERT(pidfile);
umask(MYPIDMASK);
unlink(pidfile);
if ((F= fopen(pidfile,"w")) == (FILE *)NULL) {
LogError("%s: Error opening pidfile '%s' for writing -- %s\n", prog, pidfile, STRERROR);
return(FALSE);
}
fprintf(F, "%d\n", (int)getpid());
fclose(F);
return TRUE;
}
/**
* Check if the file is a regular file
* @param file A path to the file to check
* @return TRUE if file exist and is a regular file, otherwise FALSE
*/
int File_isFile(char *file) {
struct stat buf;
ASSERT(file);
return (stat(file, &buf) == 0 && S_ISREG(buf.st_mode));
}
/**
* Check if this is a directory.
* @param dir An absolute directory path
* @return TRUE if dir exist and is a regular directory, otherwise
* FALSE
*/
int File_isDirectory(char *dir) {
struct stat buf;
ASSERT(dir);
return (stat(dir, &buf) == 0 && S_ISDIR(buf.st_mode));
}
/**
* Check if this is a fifo
* @param fifo A path to the fifo to check
* @return TRUE if fifo exist, otherwise FALSE
*/
int File_isFifo(char *fifo) {
struct stat buf;
ASSERT(fifo);
return (stat(fifo, &buf) == 0 && S_ISFIFO(buf.st_mode));
}
/**
* Check if the file exist on the system
* @file A path to the file to check
* @return TRUE if file exist otherwise FALSE
*/
int File_exist(char *file) {
struct stat buf;
ASSERT(file);
return (stat(file, &buf) == 0);
}
/**
* Security check for files. The files must have the same uid as the
* REAL uid of this process, it must have permissions no greater than
* "maxpermission".
* @param filename The filename of the checked file
* @param description The description of the checked file
* @param permmask The permission mask for the file
* @return TRUE if the test succeeded otherwise FALSE
*/
int File_checkStat(char *filename, char *description, int permmask) {
struct stat buf;
errno= 0;
ASSERT(filename);
ASSERT(description);
if(stat(filename, &buf) < 0) {
LogError("%s: Cannot stat the %s '%s' -- %s\n", prog, description, filename, STRERROR);
return FALSE;
}
if(!S_ISREG(buf.st_mode)) {
LogError("%s: The %s '%s' is not a regular file.\n", prog, description, filename);
return FALSE;
}
if(buf.st_uid != geteuid()) {
LogError("%s: The %s '%s' must be owned by you.\n", prog, description, filename);
return FALSE;
}
if((buf.st_mode & 0777 ) & ~permmask) {
/*
Explanation:
buf.st_mode & 0777 -> We just want to check the
permissions not the file type...
we did it already!
() & ~permmask -> We check if there are any other
permissions set than in permmask
*/
LogError("%s: The %s '%s' must have permissions no more "
"than -%c%c%c%c%c%c%c%c%c (0%o); "
"right now permissions are -%c%c%c%c%c%c%c%c%c (0%o).\n",
prog, description, filename,
permmask&S_IRUSR?'r':'-',
permmask&S_IWUSR?'w':'-',
permmask&S_IXUSR?'x':'-',
permmask&S_IRGRP?'r':'-',
permmask&S_IWGRP?'w':'-',
permmask&S_IXGRP?'x':'-',
permmask&S_IROTH?'r':'-',
permmask&S_IWOTH?'w':'-',
permmask&S_IXOTH?'x':'-',
permmask&0777,
buf.st_mode&S_IRUSR?'r':'-',
buf.st_mode&S_IWUSR?'w':'-',
buf.st_mode&S_IXUSR?'x':'-',
buf.st_mode&S_IRGRP?'r':'-',
buf.st_mode&S_IWGRP?'w':'-',
buf.st_mode&S_IXGRP?'x':'-',
buf.st_mode&S_IROTH?'r':'-',
buf.st_mode&S_IWOTH?'w':'-',
buf.st_mode&S_IXOTH?'x':'-',
buf.st_mode& 0777);
return FALSE;
}
return TRUE;
}
/**
* Check whether the specified directory exist or create it using
* specified mode.
* @param path The fully qualified path to the directory
* @param mode The permission for the directory
* @return TRUE if the succeeded otherwise FALSE
*/
int File_checkQueueDirectory(char *path, mode_t mode) {
struct stat st;
if(stat(path, &st)) {
if(errno == ENOENT) {
int rv;
mode_t mask = umask(QUEUEMASK);
rv = mkdir(path, mode);
umask(mask);
if(rv) {
LogError("%s: cannot create the event queue directory %s -- %s\n",
prog, path, STRERROR);
return FALSE;
}
} else {
LogError("%s: cannot read the event queue directory %s -- %s\n",
prog, path, STRERROR);
return FALSE;
}
} else if(! S_ISDIR(st.st_mode)) {
LogError("%s: event queue: the %s is not the directory\n", prog, path);
return FALSE;
}
return TRUE;
}
/**
* Check the queue size limit.
* @param path The fully qualified path to the directory
* @param limit The queue limit
* @return TRUE if the succeeded otherwise FALSE
*/
int File_checkQueueLimit(char *path, int limit) {
int used = 0;
DIR *dir = NULL;
struct dirent *de = NULL;
if(limit < 0)
return TRUE;
if(! (dir = opendir(path)) ) {
LogError("%s: cannot open the event queue directory %s -- %s\n", prog, path, STRERROR);
return FALSE;
}
while( (de = readdir(dir)) ) {
struct stat st;
if(!stat(de->d_name, &st) && S_ISREG(st.st_mode) && ++used > limit) {
LogError("%s: event queue full\n", prog);
closedir(dir);
return FALSE;
}
}
closedir(dir);
return TRUE;
}
/**
* Write data to the queue file
* @param file Filedescriptor to write to
* @param data Data to be written
* @param size Size of the data to be written
* @return TRUE if the succeeded otherwise FALSE
*/
int File_writeQueue(FILE *file, void *data, int size) {
int rv;
ASSERT(file);
/* write size */
if((rv = fwrite(&size, 1, sizeof(int), file)) != sizeof(int)) {
if (feof(file) || ferror(file))
LogError("%s: queued event file: unable to write event size -- %s\n", prog, feof(file) ? "end of file" : "stream error");
else
LogError("%s: queued event file: unable to write event size -- read returned %d bytes\n", prog, rv);
return FALSE;
}
/* write data if any */
if(size > 0) {
if((rv = fwrite(data, 1, size, file)) != size) {
if (feof(file) || ferror(file))
LogError("%s: queued event file: unable to write event size -- %s\n", prog, feof(file) ? "end of file" : "stream error");
else
LogError("%s: queued event file: unable to write event size -- read returned %d bytes\n", prog, rv);
return FALSE;
}
}
return TRUE;
}
/**
* Read the data from the queue file's actual position
* @param file Filedescriptor to read from
* @param size Size of the data read
* @return The data read if any or NULL. The size parameter is set
* appropriately.
*/
void *File_readQueue(FILE *file, int *size) {
int rv;
void *data = NULL;
ASSERT(file);
/* read size */
if((rv = fread(size, 1, sizeof(int), file)) != sizeof(int)) {
if (feof(file) || ferror(file))
LogError("%s: queued event file: unable to read event size -- %s\n", prog, feof(file) ? "end of file" : "stream error");
else
LogError("%s: queued event file: unable to read event size -- read returned %d bytes\n", prog, rv);
return NULL;
}
/* read data if any (allow 1MB at maximum to prevent enormous memory allocation) */
if(*size > 0 && *size < 1048576) {
data = xcalloc(1, *size);
if((rv = fread(data, 1, *size, file)) != *size) {
FREE(data);
if (feof(file) || ferror(file))
LogError("%s: queued event file: unable to read event data -- %s\n", prog, feof(file) ? "end of file" : "stream error");
else
LogError("%s: queued event file: unable to read event data -- read returned %d bytes\n", prog, rv);
return NULL;
}
}
return data;
}