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
|
/*
* configuration.h
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __CONFIGURATION_H
#define __CONFIGURATION_H
#include "thread.h"
#include "common.h"
extern const char* confDir;
//***************************************************************************
// Configuration
//***************************************************************************
class Configuration
{
public:
Configuration() {};
virtual ~Configuration() {};
virtual int atConfigItem(const char* Name, const char* Value) = 0;
virtual int readConfig(const char* file = 0)
{
int count = 0;
FILE* f;
char* line = 0;
size_t size = 0;
char* value;
char* name;
char* fileName;
if (!isEmpty(file))
asprintf(&fileName, "%s", file);
else
asprintf(&fileName, "%s/epgd.conf", confDir);
if (access(fileName, F_OK) != 0)
{
fprintf(stderr, "Cannot access configuration file '%s'\n", fileName);
free(fileName);
return fail;
}
f = fopen(fileName, "r");
while (getline(&line, &size, f) > 0)
{
char* p = strchr(line, '#');
if (p) *p = 0;
allTrim(line);
if (isEmpty(line))
continue;
if (!(value = strchr(line, '=')))
continue;
*value = 0;
value++;
lTrim(value);
name = line;
allTrim(name);
if (atConfigItem(name, value) != success)
{
fprintf(stderr, "Found unexpected parameter '%s', aborting\n", name);
free(fileName);
return fail;
}
count++;
}
free(line);
fclose(f);
tell(0, "Read %d option from %s", count , fileName);
free(fileName);
return success;
}
};
//***************************************************************************
// System Notification Interface (systemd, watchdog, pidfile, ...)
//***************************************************************************
class cSystemNotification : public cThread
{
public:
enum SystemEvent
{
evReady,
evStatus,
evKeepalive,
evStopping
};
enum Misc
{
defaultInterval = 60
};
cSystemNotification();
int __attribute__ ((format(printf, 3, 4))) notify(int event, const char* format = 0, ...);
int getWatchdogState(int minInterval);
void check(int force = no);
int startNotifyThread(int timeout);
int stopNotifyThread();
static void setPidFile(const char* file) { pidfile = file; }
protected:
virtual void action();
int interval;
int threadTimeout;
cCondVar waitCondition;
int stop;
static time_t lastWatchdogAt;
static const char* pidfile;
};
//***************************************************************************
#endif // __CONFIGURATION_H
|