File: sinfod.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (325 lines) | stat: -rw-r--r-- 9,420 bytes parent folder | download | duplicates (4)
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
#include <boost/asio.hpp>
#include <getopt.h>
#include <sys/wait.h>
#include <boost/bind.hpp>
#include <iostream>
#include "basicmessages.idlx.parser.h"
#include "sinfoworker.h"
#include "sinfo.idlx.parser.h"
#include "udpmessageserver.h"
#include "tcpmessageserver.h"
#include "cleanuplist.h"
#include "measureserver.h"
#include "udpmessagetransmitter.h"
#include "broadcastreceiver.h"
#include "udpmessagetransmitter.h"
#include "demoninit.h"
#include "sinfo.h"
#include "lastgasp.h"
#include "rpcserverconnectorfactory.h"
#include "parser.h"
using namespace std;


class MainClass
{
private:
  bool broadcastonlymode;
  bool cmdlinemode;
  long listtop;
  std::list < std::string > ignoreList;
  bool watchdogmode;
  string bcast_address;
  string networkcard;
  string marker;

  void mainLoopNoWatchdog();
  void mainLoopWithWatchdog();

  void printHelpAndExit(const char * plaint = "Usage:-");

public:
  MainClass(int argc, char * argv[]);

  int mainLoop();
};


MainClass::MainClass(int argc, char * argv[])
{
  bool demon = true;
  bool quiet = false;

  broadcastonlymode = false;
  cmdlinemode = false;
  listtop=5;
  watchdogmode = false;
  bcast_address=string("255.255.255.255");

  int nicevalue = 10; // start with nice 10 to ensure niceability in accidental cases.


#ifdef HAVE_GETOPT_LONG
  static const struct option long_options[] =
  {
    { "bcastaddress", required_argument, 0, 'b'},
    { "cmdline", no_argument, 0, 'c'},
    { "networkcard", required_argument, 0, 'n'},
    { "marker", required_argument, 0, 'm'},
    { "ignore", required_argument, 0, 'i' },
    { "top", required_argument, 0, 't' },
    { "nice", required_argument, 0, 'N' },
    { "foreground", no_argument, 0, 'F' },
    { "version", no_argument, 0, 'V' },
    { "quiet", no_argument, 0, 'q' },

    { "spymode", no_argument, 0, 's' },
    { "watchdog", no_argument, 0, 'W' },

    { "help", no_argument, 0, '?' },

    { 0, 0, 0, 0 }
  };
#endif


  int c;
#ifdef HAVE_GETOPT_LONG
  while ((c = getopt_long(argc, argv, "b:cB:VFi:t:n:N:qa:A:lsWh?", long_options, NULL)) != -1)
#else
  while ((c = getopt (argc, argv, "b:cB:VFi:t:n:N:qa:A:lsWh?")) != -1)
#endif
  {
    switch (c)
    {
    case 'b':
      bcast_address = string(optarg);
      break;
    case 'c':
      cmdlinemode = true;
      break;
    case 'n':
      networkcard = string(optarg);
      break;
    case 'm':
      marker = string(optarg);
      break;
    case 'i':
      ignoreList.push_back(string(optarg));
      break;
    case 't':
      listtop = atoi(optarg);
      break;
    case 'N':
      nicevalue = atoi(optarg);
      break;
    case 'F':
      demon = false;
      break;
    case 'q':
      quiet = true;
      break;
    case 'V':
      cout << argv[0] << " " << VERSION
           << "  (compiled " __DATE__ " " __TIME__ ")"
           << endl;
      exit(0);
      break;
    case 's':
      broadcastonlymode = true;
      break;
    case 'W':
      watchdogmode = true;
      break;
    case '?':
    case 'h':
      printHelpAndExit();
      break;

    default:
      printHelpAndExit("No such option");
    }
  }

  if (false == quiet)
    cout << argv[0] << " " VERSION "  (compiled " __DATE__ " " __TIME__ ")" << endl;


  nice(nicevalue);

  if (demon)
    demoninit();
}


void MainClass::printHelpAndExit(const char * plaint)
{
  cout << plaint << endl
       << endl
       << "sinfod [-F][-q][-V] [-b #.#.#.#] [-c] [-n card] [-N nicevalue ] [ -m marker ] [-i process] [-s] [-W]" << endl
       << endl
       << "OPTIONS (for full descriptions see \"man sinfod\")" << endl
       << "-F/--foreground" << endl
       << "  Do not detach from the terminal." << endl
       << "-q/--quiet" << endl
       << "  be quiet - don't display startup informations." << endl
       << "-V/--version" << endl
       << "  Print the version number and exit." << endl
       << "-b #.#.#.#/--bcastaddress=#.#.#.#" << endl
       << "  Set  broadcast address of sinfod." << endl
       << "-c/--cmdline" << endl
       << "  broadcast the full command line of a process instead of the command only." << endl
       << "-n card/--networkcard=card" << endl
       << "  Determine network load fron card." << endl
       << "-N nicevalue/--nice=nicevalue" << endl
       << "  Set the priority of this demon to nicevalue." << endl
       << "-m marker/--marker=marker" << endl
       << "  Mark the machine with a name." << endl
       << "-i/--ignore <process>" << endl
       << "  Don't broadcast any information on <process> ; --ignore may be set multiple times." << endl
       << "-t/--top <count>" << endl
       << "  Broadcast information on the top <count> processes; default: 5" << endl
       << "-s/--spymode" << endl
       << "  Deactivate TCP interface, broadcast only mode." << endl
       << "-W/--watchdog" << endl
       << "  start with simple watchdog." << endl
       << endl;
  exit(1);
}


void MainClass::mainLoopNoWatchdog()
{
  try
  {
    boost::asio::io_service ioservice;
    list < Wsinfo > wsinfoList;

    if (true==broadcastonlymode)
    {
      UDPMessageTransmitter udpMessageTransmitter(ioservice, boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(bcast_address), SINFO_BROADCAST_PORT));

      MeasureServer measureServer(ioservice, networkcard, marker, cmdlinemode, ignoreList, listtop);
      measureServer.sendMeasurementSignal.connect(boost::bind(&UDPMessageTransmitter::send, &udpMessageTransmitter, _1));

      LastGasp lastGasp;
      lastGasp.sendMessageSignal.connect(boost::bind(&UDPMessageTransmitter::send, &udpMessageTransmitter, _1));

      ioservice.run();
    }
    else
    {
      bool restartCounterEventFlag=true;

      // create worker
      boost::shared_ptr<BasicmessagesWorker> basicmessagesWorkerPtr=boost::shared_ptr<BasicmessagesWorker>(new BasicmessagesWorker);
      boost::shared_ptr<SinfoWorker> sinfoWorkerPtr=boost::shared_ptr<SinfoWorker>(new SinfoWorker(wsinfoList));

      // create parser tree
      CompositeParser topLevelParser;
      topLevelParser.addParser(1,1,Parser::SPtr(new BasicmessagesParser(basicmessagesWorkerPtr)));
      topLevelParser.addParser(2,2,Parser::SPtr(new SinfoParser(sinfoWorkerPtr)));


      UDPMessageServer udpMessageServerIPv4(ioservice,boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), SINFO_REQUEST_PORT));
      udpMessageServerIPv4.receiveMessageSignal.connect(boost::bind(&CompositeParser::parse, &topLevelParser, _1, _2));
#ifdef ENABLEIPv6
      UDPMessageServer udpMessageServerIPv6(ioservice,boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(), SINFO_REQUEST_PORT));
      udpMessageServerIPv6.receiveMessageSignal.connect(boost::bind(&CompositeParser::parse, &topLevelParser, _1, _2));
#endif

      RPCServerConnectorFactory rpcServerConnectorFactory;
      rpcServerConnectorFactory.receiveMessageSignal.connect(boost::bind(&CompositeParser::parse, &topLevelParser, _1, _2));
      TCPMessageServer tcpMessageServerIPv4(ioservice, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), SINFO_REQUEST_PORT), rpcServerConnectorFactory);
#ifdef ENABLEIPv6
      TCPMessageServer tcpMessageServerIPv6(ioservice, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), SINFO_REQUEST_PORT), rpcServerConnectorFactory);
#endif
      UDPMessageTransmitter udpMessageTransmitter(ioservice, boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(bcast_address), SINFO_BROADCAST_PORT));

      MeasureServer measureServer(ioservice, networkcard, marker, cmdlinemode, ignoreList, listtop);
      measureServer.sendMeasurementSignal.connect(boost::bind(&UDPMessageTransmitter::send, &udpMessageTransmitter, _1));

      LastGasp lastGasp;
      lastGasp.sendMessageSignal.connect(boost::bind(&UDPMessageTransmitter::send, &udpMessageTransmitter, _1));

      CleanupList cleanupList(ioservice, wsinfoList);

      BroadcastReceiver broadcastReceiver(ioservice, restartCounterEventFlag, wsinfoList);
      while (ioservice.run_one())
      {
        // while (messagepassingEventQueue.size()>0)
        // {
        //   deliverEvent(messagepassingEventQueue.pop());
        // }
        // FIXME : restartCounterEventFlag is much better than a global variable,
        // but the Event passing should be more generic and the classes shoud register a callback by themselve
        // e.g. by using an observer design pattern
        if (restartCounterEventFlag)
        {
          measureServer.restartCounterEvent();
          restartCounterEventFlag=false;
        }
      }
    }

    cout << "ioservice run exit" << endl;
  }
  catch (exception& e)
  {
    cerr << "Exception: " << e.what() << "\n";
  }
}


void MainClass::mainLoopWithWatchdog()
{
  while (1)
  {

    pid_t childpid = fork();
    cout << childpid << endl;
    if ( -1 == childpid)
    {
      cerr << "error forking child process" << endl;
      exit(1);
    }

    if (0 == childpid)
    {
      mainLoopNoWatchdog();
    }

    if (childpid > 0)
    {
      int status;
      waitpid(childpid, &status, 0);
    }

    sleep(1);
  }
}


int MainClass::mainLoop()
{
  // writing to a bad socket will abort the porgram if we dont ignore SIGPIPE
  signal(SIGPIPE, SIG_IGN);

  if (true==watchdogmode)
  {
    mainLoopWithWatchdog();
  }
  else
  {
    mainLoopNoWatchdog();
  }
  return 0;
}


int main(int argc, char * argv[])
{
  MainClass mainClass(argc,argv);

  return mainClass.mainLoop();
}