2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
34 #include <glib-object.h>
38 #include <QCoreApplication>
46 #include "pluginloader.h"
51 IMainLoop* mainloop = nullptr;
53 void interrupt(int sign)
55 signal(sign, SIG_IGN);
56 cout<<"Signal caught. Exiting gracefully.\n"<<endl;
58 /// this will cause the application to terminate and clean up:
64 void printhelp(const char *argv0);
66 static const char shortopts[] = "hvDc:d:l:we";
68 static const struct option longopts[] = {
69 { "help", no_argument, NULL, 'h' }, ///< Print the help text
70 { "version", no_argument, NULL, 'v' }, ///< Print the version text
71 { "daemonise", no_argument, NULL, 'D' }, ///< Daemonise
72 { "config", required_argument, NULL, 'c' },
73 { "debug", required_argument, NULL, 'd' },
74 { "log", required_argument, NULL, 'l' },
75 { "warn", no_argument, NULL, 'w' },
76 { "err", no_argument, NULL, 'e' },
77 { NULL, 0, NULL, 0 } ///< End
82 DebugOut(0)<<PROJECT_NAME<<endl;
83 DebugOut(0)<<"Version: "<<PROJECT_VERSION<<" ( "<<PROJECT_CODENAME<<" "<<PROJECT_QUALITY<<" )"<<endl;
86 static void glibLogHandler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
88 if(log_level == G_LOG_LEVEL_CRITICAL)
89 DebugOut(DebugOut::Error) << message << endl;
90 else if(log_level == G_LOG_LEVEL_WARNING)
91 DebugOut(DebugOut::Warning) << message << endl;
93 DebugOut() << message << endl;
96 int main(int argc, char **argv)
99 bool isdeamonize=false;
102 string config="/etc/ambd/config";
106 g_log_set_handler(G_LOG_DOMAIN, GLogLevelFlags(G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL), glibLogHandler, nullptr);
108 while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
121 DebugOut(0)<<"Config: "<<optarg<<endl;
126 DebugOut::setDebugThreshhold(th);
132 DebugOut::setThrowWarn(true);
135 DebugOut::setThrowErr(true);
138 DebugOut(0)<<"Unknown option "<<optc<<endl;
150 logfile.open(logfn, ios::out | ios::trunc);
151 DebugOut::setOutput(logfile);
158 DebugOut(DebugOut::Warning)<<"Running as root. This is dangerous."<<endl;
161 #ifndef GLIB_VERSION_2_36
165 VehicleProperty::factory();
167 PluginLoader loader(config, argc, argv);
169 if(!loader.sources().size())
171 throw std::runtime_error("No sources present. aborting");
174 mainloop = loader.mainloop();
176 /* Register signal handler */
177 signal(SIGINT, interrupt);
178 signal(SIGTERM, interrupt);
182 if(logfile.is_open())
193 return; // already a daemon
197 fprintf(stderr, "%s:%s(%d) - fork error: %s", __FILE__, __FUNCTION__, __LINE__, strerror(errno));
202 exit(0); // parent exits
203 } // child (daemon) continues
204 setsid(); // obtain a new process group
205 for(i = getdtablesize(); i >= 0; --i)
207 close(i); // close all descriptors
209 { // handle standard I/O
210 i = open("/dev/null", O_RDWR);
214 // first instance continues
217 void printhelp(const char *argv0)
219 printf("Usage: %s [args]\n"
220 " [-D|--daemonise] run ambd in daemon mode\n"
221 " [-v|--version] spit out the version then exit\n"
222 " [-c|--config </path/to/config> ] specify which config to use\n"
223 " [-d|--debug <level (0-5)> ] set the debug level\n"
224 " [-l]--log </path/to/logfile> ] specify an debug output log file\n"
225 " [-w]--warn] throw on warnings\n"
226 " [-e]--err] throw on errors\n"
227 " [-h|--help] print this menu and exit\n"