summaryrefslogtreecommitdiff
path: root/lib/configuration.c
blob: 1f89930ca0d1c25b9eac791741819985b2b1ce7d (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
/*
 * configuration.c
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "configuration.h"

time_t cSystemNotification::lastWatchdogAt = time(0);
const char* cSystemNotification::pidfile = "";

//***************************************************************************
// Class cSystemNotification
//***************************************************************************

cSystemNotification::cSystemNotification()
   : cThread("SystemNotification", yes)
{
   interval = 0;
   stop = no;
}

//***************************************************************************
// System Watchdog Check / notify
//***************************************************************************

void cSystemNotification::check(int force)
{
   if (interval && (force || lastWatchdogAt <= time(0) - interval))
   {
      // notify all 'n' seconds

      notify(evKeepalive);
      lastWatchdogAt = time(0);
   }
}

//***************************************************************************
// Start / Stop notification thread
//***************************************************************************

int cSystemNotification::startNotifyThread(int timeout)
{
   threadTimeout = timeout;
   Start(yes);
   return success;
}

int cSystemNotification::stopNotifyThread()
{
   stop = yes;
   waitCondition.Broadcast();
   Cancel(3);
   return success;
}

//***************************************************************************
// action
//***************************************************************************

void cSystemNotification::action()
{
   cMyMutex mutex;
   time_t timeoutAt = time(0) + threadTimeout;

   stop = no;
   mutex.Lock();

   while (time(0) < timeoutAt && Running() && !stop)
   {
      // tell(0, "loop ...");

      waitCondition.TimedWait(mutex, interval*1000);

      if (!stop)
         check();
   }

   if (time(0) >= timeoutAt)
      tell(0, "Warning: Ending notification thread, timeout reached!");
}

//***************************************************************************
// Notify System Deamon :o :(
//***************************************************************************

int cSystemNotification::notify(int event, const char* format, ...)
{
#ifdef USESYSD
   char* message;
   char* tmp;
   va_list ap;

   if (isEmpty(format))
      format = "";

   va_start(ap, format);
   vasprintf(&tmp, format, ap);

   switch (event)
   {
      case evStatus:    asprintf(&message, "%s", tmp);               break;
      case evStopping:  asprintf(&message, "STOPPING=1\n%s", tmp);   break;
      case evReady:     asprintf(&message, "READY=1\nSTATUS=Ready\nMAINPID=%d\n%s", getpid(), tmp);  break;
      case evKeepalive: asprintf(&message, "WATCHDOG=1\n%s", tmp);   break;
   }

   tell(event == evKeepalive ? 2 : 1, "Calling sd_notify(%s)", message);
   sd_notify(0, message);

   free(tmp);
   free(message);
#endif

   // handle pidfile at evReady / evStopping

   if (!isEmpty(pidfile))
   {
      if (event == evReady)
      {
         FILE* f = fopen(pidfile, "w");

         if (f)
         {
            pid_t pid = getpid();
            tell(1, "Creating pidfile '%s'; pid %d", pidfile, pid);
            fprintf(f, "%d\n", pid);
            fclose(f);
         }
         else
            tell(0, "Error: Can't create pid file '%s' error was (%d) '%s'",
                 pidfile, errno, strerror(errno));
      }
      else if (event == evStopping)
      {
         if (fileExists(pidfile))
         {
            tell(1, "Removing pidfile '%s'", pidfile);
            removeFile(pidfile);
         }
      }
   }

   return done;
}

//***************************************************************************
//
//***************************************************************************

int cSystemNotification::getWatchdogState(int minInterval)
{
   interval = 0;

#ifdef USESYSD

#  ifdef SYSDWDIFO
   uint64_t us = 0;
   int sec;

   if (sd_watchdog_enabled(0, &us) > 0 && us > 0)
   {
      sec = us / tmeUsecondsPerSecond;

      if (sec < minInterval*2)
      {
         tell(0, "Warning: Systemd watchdog configured to (%ds) but min %ds are required,"
              "  ignoring watchdog", sec, minInterval * 2);
         return no;
      }

      interval = sec / 2;

      tell(0, "Info: Systemd watchdog request interval"
           " of at least(%ds), using (%ds) now!", sec, interval);

      return yes;
   }

   tell(0, "Info: Systemd watchdog not configured, epgd won't be sending keep-alive messages!");

#  else
   interval = defaultInterval;
   return yes;
#  endif

#else
   tell(0, "Info: Systemd support not enabled, epgd won't be sending notifications!");
#endif

   return no;
}