blob: 4ab811c4f5531052e078318af8b2011040194670 [file] [log] [blame]
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +05301/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053018#include <phosphor-logging/log.hpp>
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053019#include "argument.hpp"
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053020#include "timer.hpp"
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053021
22static void exitWithError(const char* err, char** argv)
23{
24 phosphor::watchdog::ArgumentParser::usage(argv);
25 std::cerr << "ERROR: " << err << "\n";
26 exit(EXIT_FAILURE);
27}
28
29int main(int argc, char** argv)
30{
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053031 using namespace phosphor::logging;
32
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053033 // Read arguments.
34 auto options = phosphor::watchdog::ArgumentParser(argc, argv);
35
36 // Parse out path argument.
37 auto path = (options)["path"];
38 if (path == phosphor::watchdog::ArgumentParser::emptyString)
39 {
40 exitWithError("Path not specified.", argv);
41 }
42
43 // Parse out service name argument
44 auto service = (options)["service"];
45 if (service == phosphor::watchdog::ArgumentParser::emptyString)
46 {
47 exitWithError("Service not specified.", argv);
48 }
49
50 // Parse out target argument. It is fine if the caller does not
51 // pass this if they are not interested in calling into any target
52 // on meeting a condition.
53 auto target = (options)["target"];
54
Vishwanatha Subbanna7e146552017-05-29 17:03:33 +053055 sd_event* event = nullptr;
56 auto r = sd_event_default(&event);
57 if (r < 0)
58 {
59 log<level::ERR>("Error creating a default sd_event handler");
60 return r;
61 }
62 phosphor::watchdog::EventPtr eventP{event};
63 event = nullptr;
64
65 // TODO: Creating the timer object would be inside watchdog implementation.
66 // Putting this here for completion of this piece
67 phosphor::watchdog::Timer timer(eventP);
68
69 while(!timer.expired())
70 {
71 // -1 denotes wait for ever
72 r = sd_event_run(eventP.get(), (uint64_t)-1);
73 if (r < 0)
74 {
75 log<level::ERR>("Error waiting for events");
76 return -1;
77 }
78 }
Vishwanatha Subbanna15b1dc12017-05-23 15:16:13 +053079 return 0;
80}